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

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.62! root      166: BOOL is_winxp_or_later;
1.1.1.54  root      167: BOOL is_xp_64_or_later;
1.1.1.14  root      168: BOOL is_vista_or_later;
                    169: 
1.1.1.35  root      170: #define UPDATE_OPS 16384
                    171: #define REQUEST_HARDWRE_UPDATE() { \
                    172:        update_ops = UPDATE_OPS - 1; \
                    173: }
                    174: UINT32 update_ops = 0;
                    175: UINT32 idle_ops = 0;
                    176: 
1.1.1.54  root      177: inline BOOL is_sse2_ready()
                    178: {
                    179:        static int result = -1;
                    180:        int cpu_info[4];
                    181:        
                    182:        if(result == -1) {
                    183:                result = 0;
                    184:                __cpuid(cpu_info, 0);
                    185:                if(cpu_info[0] >= 1){
                    186:                        __cpuid(cpu_info, 1);
                    187:                        if(cpu_info[3] & (1 << 26)) {
                    188:                                result = 1;
                    189:                        }
                    190:                }
                    191:        }
                    192:        return(result == 1);
                    193: }
                    194: 
1.1.1.14  root      195: inline void maybe_idle()
                    196: {
                    197:        // if it appears to be in a tight loop, assume waiting for input
                    198:        // allow for one updated video character, for a spinning cursor
1.1.1.35  root      199:        if(!stay_busy && idle_ops < 1024 && vram_length_char <= 1 && vram_length_attr <= 1) {
1.1.1.54  root      200:                if(is_sse2_ready()) {
                    201:                        _mm_pause();    // SSE2 pause
                    202:                } else if(is_xp_64_or_later) {
                    203:                        Sleep(0);       // switch to other thread that is ready to run, without checking priority
                    204:                } else {
                    205:                        Sleep(1);
                    206:                        REQUEST_HARDWRE_UPDATE();
                    207:                }
1.1.1.14  root      208:        }
1.1.1.35  root      209:        idle_ops = 0;
1.1.1.14  root      210: }
1.1.1.12  root      211: 
1.1       root      212: /* ----------------------------------------------------------------------------
1.1.1.3   root      213:        MAME i86/i386
1.1       root      214: ---------------------------------------------------------------------------- */
                    215: 
1.1.1.10  root      216: #ifndef __BIG_ENDIAN__
1.1       root      217: #define LSB_FIRST
1.1.1.10  root      218: #endif
1.1       root      219: 
                    220: #ifndef INLINE
                    221: #define INLINE inline
                    222: #endif
                    223: #define U64(v) UINT64(v)
                    224: 
                    225: //#define logerror(...) fprintf(stderr, __VA_ARGS__)
                    226: #define logerror(...)
                    227: //#define popmessage(...) fprintf(stderr, __VA_ARGS__)
                    228: #define popmessage(...)
                    229: 
                    230: /*****************************************************************************/
1.1.1.10  root      231: /* src/emu/devcpu.h */
                    232: 
                    233: // CPU interface functions
                    234: #define CPU_INIT_NAME(name)                    cpu_init_##name
                    235: #define CPU_INIT(name)                         void CPU_INIT_NAME(name)()
                    236: #define CPU_INIT_CALL(name)                    CPU_INIT_NAME(name)()
                    237: 
                    238: #define CPU_RESET_NAME(name)                   cpu_reset_##name
                    239: #define CPU_RESET(name)                                void CPU_RESET_NAME(name)()
                    240: #define CPU_RESET_CALL(name)                   CPU_RESET_NAME(name)()
                    241: 
                    242: #define CPU_EXECUTE_NAME(name)                 cpu_execute_##name
                    243: #define CPU_EXECUTE(name)                      void CPU_EXECUTE_NAME(name)()
                    244: #define CPU_EXECUTE_CALL(name)                 CPU_EXECUTE_NAME(name)()
                    245: 
                    246: #define CPU_TRANSLATE_NAME(name)               cpu_translate_##name
                    247: #define CPU_TRANSLATE(name)                    int CPU_TRANSLATE_NAME(name)(address_spacenum space, int intention, offs_t *address)
                    248: #define CPU_TRANSLATE_CALL(name)               CPU_TRANSLATE_NAME(name)(space, intention, address)
                    249: 
                    250: #define CPU_DISASSEMBLE_NAME(name)             cpu_disassemble_##name
                    251: #define CPU_DISASSEMBLE(name)                  int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
                    252: #define CPU_DISASSEMBLE_CALL(name)             CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
                    253: 
1.1.1.14  root      254: #define CPU_MODEL_STR(name)                    #name
                    255: #define CPU_MODEL_NAME(name)                   CPU_MODEL_STR(name)
                    256: 
1.1.1.10  root      257: /*****************************************************************************/
                    258: /* src/emu/didisasm.h */
                    259: 
                    260: // Disassembler constants
                    261: const UINT32 DASMFLAG_SUPPORTED     = 0x80000000;   // are disassembly flags supported?
                    262: const UINT32 DASMFLAG_STEP_OUT      = 0x40000000;   // this instruction should be the end of a step out sequence
                    263: const UINT32 DASMFLAG_STEP_OVER     = 0x20000000;   // this instruction should be stepped over by setting a breakpoint afterwards
                    264: const UINT32 DASMFLAG_OVERINSTMASK  = 0x18000000;   // number of extra instructions to skip when stepping over
                    265: const UINT32 DASMFLAG_OVERINSTSHIFT = 27;           // bits to shift after masking to get the value
                    266: const UINT32 DASMFLAG_LENGTHMASK    = 0x0000ffff;   // the low 16-bits contain the actual length
                    267: 
                    268: /*****************************************************************************/
1.1       root      269: /* src/emu/diexec.h */
                    270: 
                    271: // I/O line states
                    272: enum line_state
                    273: {
                    274:        CLEAR_LINE = 0,                         // clear (a fired or held) line
                    275:        ASSERT_LINE,                            // assert an interrupt immediately
                    276:        HOLD_LINE,                              // hold interrupt line until acknowledged
                    277:        PULSE_LINE                              // pulse interrupt line instantaneously (only for NMI, RESET)
                    278: };
                    279: 
                    280: // I/O line definitions
                    281: enum
                    282: {
                    283:        INPUT_LINE_IRQ = 0,
                    284:        INPUT_LINE_NMI
                    285: };
                    286: 
                    287: /*****************************************************************************/
1.1.1.10  root      288: /* src/emu/dimemory.h */
1.1       root      289: 
1.1.1.10  root      290: // Translation intentions
                    291: const int TRANSLATE_TYPE_MASK       = 0x03;     // read write or fetch
                    292: const int TRANSLATE_USER_MASK       = 0x04;     // user mode or fully privileged
                    293: const int TRANSLATE_DEBUG_MASK      = 0x08;     // debug mode (no side effects)
                    294: 
                    295: const int TRANSLATE_READ            = 0;        // translate for read
                    296: const int TRANSLATE_WRITE           = 1;        // translate for write
                    297: const int TRANSLATE_FETCH           = 2;        // translate for instruction fetch
                    298: const int TRANSLATE_READ_USER       = (TRANSLATE_READ | TRANSLATE_USER_MASK);
                    299: const int TRANSLATE_WRITE_USER      = (TRANSLATE_WRITE | TRANSLATE_USER_MASK);
                    300: const int TRANSLATE_FETCH_USER      = (TRANSLATE_FETCH | TRANSLATE_USER_MASK);
                    301: const int TRANSLATE_READ_DEBUG      = (TRANSLATE_READ | TRANSLATE_DEBUG_MASK);
                    302: const int TRANSLATE_WRITE_DEBUG     = (TRANSLATE_WRITE | TRANSLATE_DEBUG_MASK);
                    303: const int TRANSLATE_FETCH_DEBUG     = (TRANSLATE_FETCH | TRANSLATE_DEBUG_MASK);
1.1       root      304: 
1.1.1.10  root      305: /*****************************************************************************/
                    306: /* src/emu/emucore.h */
1.1       root      307: 
1.1.1.10  root      308: // constants for expression endianness
                    309: enum endianness_t
                    310: {
                    311:        ENDIANNESS_LITTLE,
                    312:        ENDIANNESS_BIG
                    313: };
1.1       root      314: 
1.1.1.10  root      315: // declare native endianness to be one or the other
                    316: #ifdef LSB_FIRST
                    317: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
                    318: #else
                    319: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
                    320: #endif
                    321: 
                    322: // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
                    323: #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
                    324: 
                    325: // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
                    326: #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
                    327: 
                    328: // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
                    329: #define ENDIAN_VALUE_NE_NNE(endian,leval,beval)        (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
1.1       root      330: 
                    331: /*****************************************************************************/
                    332: /* src/emu/memory.h */
                    333: 
1.1.1.10  root      334: // address spaces
                    335: enum address_spacenum
                    336: {
                    337:        AS_0,                           // first address space
                    338:        AS_1,                           // second address space
                    339:        AS_2,                           // third address space
                    340:        AS_3,                           // fourth address space
                    341:        ADDRESS_SPACES,                 // maximum number of address spaces
                    342: 
                    343:        // alternate address space names for common use
                    344:        AS_PROGRAM = AS_0,              // program address space
                    345:        AS_DATA = AS_1,                 // data address space
                    346:        AS_IO = AS_2                    // I/O address space
                    347: };
                    348: 
1.1       root      349: // offsets and addresses are 32-bit (for now...)
1.1.1.30  root      350: //typedef UINT32       offs_t;
1.1       root      351: 
                    352: // read accessors
                    353: UINT8 read_byte(offs_t byteaddress)
1.1.1.33  root      354: #ifdef USE_DEBUGGER
                    355: {
                    356:        if(now_debugging) {
                    357:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    358:                        if(rd_break_point.table[i].status == 1) {
                    359:                                if(byteaddress == rd_break_point.table[i].addr) {
                    360:                                        rd_break_point.hit = i + 1;
                    361:                                        now_suspended = true;
                    362:                                        break;
                    363:                                }
                    364:                        }
                    365:                }
                    366:        }
                    367:        return(debugger_read_byte(byteaddress));
                    368: }
                    369: UINT8 debugger_read_byte(offs_t byteaddress)
                    370: #endif
1.1       root      371: {
1.1.1.4   root      372: #if defined(HAS_I386)
1.1       root      373:        if(byteaddress < MAX_MEM) {
                    374:                return mem[byteaddress];
1.1.1.3   root      375: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    376: //             return read_byte(byteaddress & 0xfffff);
1.1       root      377:        }
                    378:        return 0;
1.1.1.4   root      379: #else
                    380:        return mem[byteaddress];
                    381: #endif
1.1       root      382: }
                    383: 
                    384: UINT16 read_word(offs_t byteaddress)
1.1.1.33  root      385: #ifdef USE_DEBUGGER
                    386: {
                    387:        if(now_debugging) {
                    388:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    389:                        if(rd_break_point.table[i].status == 1) {
                    390:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 2) {
                    391:                                        rd_break_point.hit = i + 1;
                    392:                                        now_suspended = true;
                    393:                                        break;
                    394:                                }
                    395:                        }
                    396:                }
                    397:        }
                    398:        return(debugger_read_word(byteaddress));
                    399: }
                    400: UINT16 debugger_read_word(offs_t byteaddress)
                    401: #endif
1.1       root      402: {
1.1.1.14  root      403:        if(byteaddress == 0x41c) {
                    404:                // pointer to first free slot in keyboard buffer
1.1.1.35  root      405:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                    406: #ifdef USE_SERVICE_THREAD
                    407:                        EnterCriticalSection(&key_buf_crit_sect);
                    408: #endif
1.1.1.55  root      409:                        bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root      410: #ifdef USE_SERVICE_THREAD
                    411:                        LeaveCriticalSection(&key_buf_crit_sect);
                    412: #endif
1.1.1.51  root      413:                        if(empty) maybe_idle();
1.1.1.14  root      414:                }
                    415:        }
1.1.1.4   root      416: #if defined(HAS_I386)
1.1       root      417:        if(byteaddress < MAX_MEM - 1) {
                    418:                return *(UINT16 *)(mem + byteaddress);
1.1.1.3   root      419: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    420: //             return read_word(byteaddress & 0xfffff);
1.1       root      421:        }
                    422:        return 0;
1.1.1.4   root      423: #else
                    424:        return *(UINT16 *)(mem + byteaddress);
                    425: #endif
1.1       root      426: }
                    427: 
                    428: UINT32 read_dword(offs_t byteaddress)
1.1.1.33  root      429: #ifdef USE_DEBUGGER
                    430: {
                    431:        if(now_debugging) {
                    432:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    433:                        if(rd_break_point.table[i].status == 1) {
                    434:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 4) {
                    435:                                        rd_break_point.hit = i + 1;
                    436:                                        now_suspended = true;
                    437:                                        break;
                    438:                                }
                    439:                        }
                    440:                }
                    441:        }
                    442:        return(debugger_read_dword(byteaddress));
                    443: }
                    444: UINT32 debugger_read_dword(offs_t byteaddress)
                    445: #endif
1.1       root      446: {
1.1.1.4   root      447: #if defined(HAS_I386)
1.1       root      448:        if(byteaddress < MAX_MEM - 3) {
                    449:                return *(UINT32 *)(mem + byteaddress);
1.1.1.3   root      450: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    451: //             return read_dword(byteaddress & 0xfffff);
1.1       root      452:        }
                    453:        return 0;
1.1.1.4   root      454: #else
                    455:        return *(UINT32 *)(mem + byteaddress);
                    456: #endif
1.1       root      457: }
                    458: 
                    459: // write accessors
1.1.1.35  root      460: #ifdef USE_VRAM_THREAD
1.1.1.14  root      461: void vram_flush_char()
                    462: {
                    463:        if(vram_length_char != 0) {
                    464:                DWORD num;
1.1.1.60  root      465:                WriteConsoleOutputCharacterA(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, vram_length_char, vram_coord_char, &num);
1.1.1.14  root      466:                vram_length_char = vram_last_length_char = 0;
                    467:        }
                    468: }
                    469: 
                    470: void vram_flush_attr()
                    471: {
                    472:        if(vram_length_attr != 0) {
                    473:                DWORD num;
1.1.1.23  root      474:                WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, vram_length_attr, vram_coord_attr, &num);
1.1.1.14  root      475:                vram_length_attr = vram_last_length_attr = 0;
                    476:        }
                    477: }
                    478: 
                    479: void vram_flush()
                    480: {
                    481:        if(vram_length_char != 0 || vram_length_attr != 0) {
                    482:                EnterCriticalSection(&vram_crit_sect);
                    483:                vram_flush_char();
                    484:                vram_flush_attr();
                    485:                LeaveCriticalSection(&vram_crit_sect);
                    486:        }
                    487: }
                    488: #endif
                    489: 
                    490: void write_text_vram_char(offs_t offset, UINT8 data)
1.1.1.8   root      491: {
1.1.1.35  root      492: #ifdef USE_VRAM_THREAD
1.1.1.14  root      493:        static offs_t first_offset_char, last_offset_char;
                    494:        
                    495:        if(vram_length_char != 0) {
                    496:                if(offset <= last_offset_char && offset >= first_offset_char) {
                    497:                        scr_char[(offset - first_offset_char) >> 1] = data;
                    498:                        return;
                    499:                }
                    500:                if(offset != last_offset_char + 2) {
                    501:                        vram_flush_char();
                    502:                }
                    503:        }
                    504:        if(vram_length_char == 0) {
                    505:                first_offset_char = offset;
                    506:                vram_coord_char.X = (offset >> 1) % scr_width;
                    507:                vram_coord_char.Y = (offset >> 1) / scr_width + scr_top;
                    508:        }
                    509:        scr_char[vram_length_char++] = data;
                    510:        last_offset_char = offset;
                    511: #else
1.1.1.8   root      512:        COORD co;
                    513:        DWORD num;
                    514:        
1.1.1.14  root      515:        co.X = (offset >> 1) % scr_width;
                    516:        co.Y = (offset >> 1) / scr_width;
                    517:        scr_char[0] = data;
1.1.1.60  root      518:        WriteConsoleOutputCharacterA(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, 1, co, &num);
1.1.1.14  root      519: #endif
                    520: }
                    521: 
                    522: void write_text_vram_attr(offs_t offset, UINT8 data)
                    523: {
1.1.1.35  root      524: #ifdef USE_VRAM_THREAD
1.1.1.14  root      525:        static offs_t first_offset_attr, last_offset_attr;
                    526:        
                    527:        if(vram_length_attr != 0) {
                    528:                if(offset <= last_offset_attr && offset >= first_offset_attr) {
                    529:                        scr_attr[(offset - first_offset_attr) >> 1] = data;
                    530:                        return;
                    531:                }
                    532:                if(offset != last_offset_attr + 2) {
                    533:                        vram_flush_attr();
                    534:                }
                    535:        }
                    536:        if(vram_length_attr == 0) {
                    537:                first_offset_attr = offset;
                    538:                vram_coord_attr.X = (offset >> 1) % scr_width;
                    539:                vram_coord_attr.Y = (offset >> 1) / scr_width + scr_top;
                    540:        }
                    541:        scr_attr[vram_length_attr++] = data;
                    542:        last_offset_attr = offset;
                    543: #else
                    544:        COORD co;
                    545:        DWORD num;
1.1.1.8   root      546:        
1.1.1.14  root      547:        co.X = (offset >> 1) % scr_width;
                    548:        co.Y = (offset >> 1) / scr_width;
                    549:        scr_attr[0] = data;
1.1.1.23  root      550:        WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, 1, co, &num);
1.1.1.14  root      551: #endif
                    552: }
                    553: 
                    554: void write_text_vram_byte(offs_t offset, UINT8 data)
                    555: {
1.1.1.35  root      556: #ifdef USE_VRAM_THREAD
1.1.1.14  root      557:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      558: #endif
1.1.1.8   root      559:        if(offset & 1) {
1.1.1.14  root      560:                write_text_vram_attr(offset, data);
1.1.1.8   root      561:        } else {
1.1.1.14  root      562:                write_text_vram_char(offset, data);
1.1.1.8   root      563:        }
1.1.1.35  root      564: #ifdef USE_VRAM_THREAD
1.1.1.14  root      565:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      566: #endif
1.1.1.8   root      567: }
                    568: 
                    569: void write_text_vram_word(offs_t offset, UINT16 data)
                    570: {
1.1.1.35  root      571: #ifdef USE_VRAM_THREAD
1.1.1.14  root      572:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      573: #endif
1.1.1.8   root      574:        if(offset & 1) {
1.1.1.14  root      575:                write_text_vram_attr(offset    , (data     ) & 0xff);
                    576:                write_text_vram_char(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      577:        } else {
1.1.1.14  root      578:                write_text_vram_char(offset    , (data     ) & 0xff);
                    579:                write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      580:        }
1.1.1.35  root      581: #ifdef USE_VRAM_THREAD
1.1.1.14  root      582:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      583: #endif
1.1.1.8   root      584: }
                    585: 
                    586: void write_text_vram_dword(offs_t offset, UINT32 data)
                    587: {
1.1.1.35  root      588: #ifdef USE_VRAM_THREAD
1.1.1.14  root      589:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      590: #endif
1.1.1.8   root      591:        if(offset & 1) {
1.1.1.14  root      592:                write_text_vram_attr(offset    , (data      ) & 0xff);
                    593:                write_text_vram_char(offset + 1, (data >>  8) & 0xff);
                    594:                write_text_vram_attr(offset + 2, (data >> 16) & 0xff);
                    595:                write_text_vram_char(offset + 3, (data >> 24) & 0xff);
                    596:        } else {
                    597:                write_text_vram_char(offset    , (data      ) & 0xff);
                    598:                write_text_vram_attr(offset + 1, (data >>  8) & 0xff);
                    599:                write_text_vram_char(offset + 2, (data >> 16) & 0xff);
                    600:                write_text_vram_attr(offset + 3, (data >> 24) & 0xff);
1.1.1.8   root      601:        }
1.1.1.35  root      602: #ifdef USE_VRAM_THREAD
1.1.1.14  root      603:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      604: #endif
1.1.1.8   root      605: }
                    606: 
1.1       root      607: void write_byte(offs_t byteaddress, UINT8 data)
1.1.1.33  root      608: #ifdef USE_DEBUGGER
                    609: {
                    610:        if(now_debugging) {
                    611:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    612:                        if(wr_break_point.table[i].status == 1) {
                    613:                                if(byteaddress == wr_break_point.table[i].addr) {
                    614:                                        wr_break_point.hit = i + 1;
                    615:                                        now_suspended = true;
                    616:                                        break;
                    617:                                }
                    618:                        }
                    619:                }
                    620:        }
                    621:        debugger_write_byte(byteaddress, data);
                    622: }
                    623: void debugger_write_byte(offs_t byteaddress, UINT8 data)
                    624: #endif
1.1       root      625: {
1.1.1.8   root      626:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      627:                mem[byteaddress] = data;
1.1.1.8   root      628:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      629:                if(!restore_console_on_exit) {
                    630:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      631:                }
1.1.1.8   root      632:                write_text_vram_byte(byteaddress - text_vram_top_address, data);
                    633:                mem[byteaddress] = data;
                    634:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    635:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    636:                        write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1       root      637:                }
                    638:                mem[byteaddress] = data;
1.1.1.4   root      639: #if defined(HAS_I386)
1.1.1.3   root      640:        } else if(byteaddress < MAX_MEM) {
1.1.1.4   root      641: #else
                    642:        } else {
                    643: #endif
1.1.1.3   root      644:                mem[byteaddress] = data;
1.1       root      645:        }
                    646: }
                    647: 
                    648: void write_word(offs_t byteaddress, UINT16 data)
1.1.1.33  root      649: #ifdef USE_DEBUGGER
                    650: {
                    651:        if(now_debugging) {
                    652:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    653:                        if(wr_break_point.table[i].status == 1) {
                    654:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 2) {
                    655:                                        wr_break_point.hit = i + 1;
                    656:                                        now_suspended = true;
                    657:                                        break;
                    658:                                }
                    659:                        }
                    660:                }
                    661:        }
                    662:        debugger_write_word(byteaddress, data);
                    663: }
                    664: void debugger_write_word(offs_t byteaddress, UINT16 data)
                    665: #endif
1.1       root      666: {
1.1.1.8   root      667:        if(byteaddress < MEMORY_END) {
1.1.1.51  root      668:                if(byteaddress == cursor_position_address) {
                    669:                        if(*(UINT16 *)(mem + byteaddress) != data) {
                    670:                                COORD co;
                    671:                                co.X = data & 0xff;
                    672:                                co.Y = (data >> 8) + scr_top;
                    673:                                SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), co);
1.1.1.59  root      674:                                cursor_moved = false;
                    675:                                cursor_moved_by_crtc = false;
1.1.1.51  root      676:                        }
1.1.1.14  root      677:                }
1.1.1.3   root      678:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8   root      679:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      680:                if(!restore_console_on_exit) {
                    681:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      682:                }
1.1.1.8   root      683:                write_text_vram_word(byteaddress - text_vram_top_address, data);
                    684:                *(UINT16 *)(mem + byteaddress) = data;
                    685:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    686:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    687:                        write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1       root      688:                }
                    689:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4   root      690: #if defined(HAS_I386)
1.1.1.3   root      691:        } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4   root      692: #else
                    693:        } else {
                    694: #endif
1.1.1.3   root      695:                *(UINT16 *)(mem + byteaddress) = data;
1.1       root      696:        }
                    697: }
                    698: 
                    699: void write_dword(offs_t byteaddress, UINT32 data)
1.1.1.33  root      700: #ifdef USE_DEBUGGER
                    701: {
                    702:        if(now_debugging) {
                    703:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    704:                        if(wr_break_point.table[i].status == 1) {
                    705:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 4) {
                    706:                                        wr_break_point.hit = i + 1;
                    707:                                        now_suspended = true;
                    708:                                        break;
                    709:                                }
                    710:                        }
                    711:                }
                    712:        }
                    713:        debugger_write_dword(byteaddress, data);
                    714: }
                    715: void debugger_write_dword(offs_t byteaddress, UINT32 data)
                    716: #endif
1.1       root      717: {
1.1.1.8   root      718:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      719:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8   root      720:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      721:                if(!restore_console_on_exit) {
                    722:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      723:                }
1.1.1.8   root      724:                write_text_vram_dword(byteaddress - text_vram_top_address, data);
                    725:                *(UINT32 *)(mem + byteaddress) = data;
                    726:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    727:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    728:                        write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1       root      729:                }
                    730:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4   root      731: #if defined(HAS_I386)
1.1.1.3   root      732:        } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4   root      733: #else
                    734:        } else {
                    735: #endif
1.1.1.3   root      736:                *(UINT32 *)(mem + byteaddress) = data;
1.1       root      737:        }
                    738: }
                    739: 
                    740: #define read_decrypted_byte read_byte
                    741: #define read_decrypted_word read_word
                    742: #define read_decrypted_dword read_dword
                    743: 
1.1.1.3   root      744: #define read_raw_byte read_byte
                    745: #define write_raw_byte write_byte
                    746: 
                    747: #define read_word_unaligned read_word
                    748: #define write_word_unaligned write_word
                    749: 
                    750: #define read_io_word_unaligned read_io_word
                    751: #define write_io_word_unaligned write_io_word
                    752: 
1.1       root      753: UINT8 read_io_byte(offs_t byteaddress);
                    754: UINT16 read_io_word(offs_t byteaddress);
                    755: UINT32 read_io_dword(offs_t byteaddress);
                    756: 
                    757: void write_io_byte(offs_t byteaddress, UINT8 data);
                    758: void write_io_word(offs_t byteaddress, UINT16 data);
                    759: void write_io_dword(offs_t byteaddress, UINT32 data);
                    760: 
                    761: /*****************************************************************************/
                    762: /* src/osd/osdcomm.h */
                    763: 
                    764: /* Highly useful macro for compile-time knowledge of an array size */
                    765: #define ARRAY_LENGTH(x)     (sizeof(x) / sizeof(x[0]))
                    766: 
1.1.1.54  root      767: // flag to exit MS-DOS Player
                    768: // this is set when the first process is terminated and jump to FFFF:0000 HALT
                    769: int m_exit = 0;
                    770: 
1.1.1.3   root      771: #if defined(HAS_I386)
1.1.1.10  root      772:        static CPU_TRANSLATE(i386);
                    773:        #include "mame/lib/softfloat/softfloat.c"
                    774:        #include "mame/emu/cpu/i386/i386.c"
1.1.1.12  root      775:        #include "mame/emu/cpu/vtlb.c"
1.1.1.3   root      776: #elif defined(HAS_I286)
1.1.1.10  root      777:        #include "mame/emu/cpu/i86/i286.c"
1.1.1.3   root      778: #else
1.1.1.10  root      779:        #include "mame/emu/cpu/i86/i86.c"
1.1.1.3   root      780: #endif
1.1.1.33  root      781: #ifdef USE_DEBUGGER
1.1.1.10  root      782:        #include "mame/emu/cpu/i386/i386dasm.c"
1.1       root      783: #endif
                    784: 
1.1.1.3   root      785: #if defined(HAS_I386)
                    786:        #define SREG(x)                         m_sreg[x].selector
                    787:        #define SREG_BASE(x)                    m_sreg[x].base
                    788:        int cpu_type, cpu_step;
                    789: #else
                    790:        #define REG8(x)                         m_regs.b[x]
                    791:        #define REG16(x)                        m_regs.w[x]
                    792:        #define SREG(x)                         m_sregs[x]
                    793:        #define SREG_BASE(x)                    m_base[x]
1.1.1.33  root      794:        #define m_eip                           (m_pc - m_base[CS])
1.1.1.3   root      795:        #define m_CF                            m_CarryVal
                    796:        #define m_a20_mask                      AMASK
                    797:        #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
                    798:        #if defined(HAS_I286)
                    799:                #define i386_set_a20_line(x)    i80286_set_a20_line(x)
                    800:        #else
                    801:                #define i386_set_a20_line(x)
                    802:        #endif
                    803:        #define i386_set_irq_line(x, y)         set_irq_line(x, y)
                    804: #endif
1.1       root      805: 
                    806: void i386_jmp_far(UINT16 selector, UINT32 address)
                    807: {
1.1.1.3   root      808: #if defined(HAS_I386)
1.1       root      809:        if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3   root      810:                i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1       root      811:        } else {
1.1.1.3   root      812:                SREG(CS) = selector;
                    813:                m_performed_intersegment_jump = 1;
                    814:                i386_load_segment_descriptor(CS);
                    815:                m_eip = address;
                    816:                CHANGE_PC(m_eip);
1.1       root      817:        }
1.1.1.3   root      818: #elif defined(HAS_I286)
                    819:        i80286_code_descriptor(selector, address, 1);
                    820: #else
                    821:        SREG(CS) = selector;
                    822:        i386_load_segment_descriptor(CS);
                    823:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    824: #endif
1.1       root      825: }
                    826: 
1.1.1.24  root      827: void i386_call_far(UINT16 selector, UINT32 address)
                    828: {
                    829: #if defined(HAS_I386)
                    830:        if(PROTECTED_MODE && !V8086_MODE) {
                    831:                i386_protected_mode_call(selector, address, 1, m_operand_size);
                    832:        } else {
                    833:                PUSH16(SREG(CS));
                    834:                PUSH16(m_eip);
                    835:                SREG(CS) = selector;
                    836:                m_performed_intersegment_jump = 1;
                    837:                i386_load_segment_descriptor(CS);
                    838:                m_eip = address;
                    839:                CHANGE_PC(m_eip);
                    840:        }
                    841: #else
                    842:        UINT16 ip = m_pc - SREG_BASE(CS);
                    843:        UINT16 cs = SREG(CS);
                    844: #if defined(HAS_I286)
                    845:        i80286_code_descriptor(selector, address, 2);
                    846: #else
                    847:        SREG(CS) = selector;
                    848:        i386_load_segment_descriptor(CS);
                    849:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    850: #endif
                    851:        PUSH(cs);
                    852:        PUSH(ip);
                    853:        CHANGE_PC(m_pc);
                    854: #endif
                    855: }
1.1.1.49  root      856: 
                    857: void i386_push16(UINT16 value)
                    858: {
                    859: #if defined(HAS_I386)
                    860:        PUSH16(value);
                    861: #else
                    862:        PUSH(value);
1.1.1.35  root      863: #endif
1.1.1.49  root      864: }
                    865: 
                    866: UINT16 i386_pop16()
                    867: {
                    868: #if defined(HAS_I386)
                    869:        return POP16();
                    870: #else
                    871:        UINT16 value;
                    872:        POP(value);
                    873:        return value;
                    874: #endif
                    875: }
1.1.1.24  root      876: 
1.1.1.29  root      877: UINT16 i386_read_stack()
                    878: {
                    879: #if defined(HAS_I386)
                    880:        UINT32 ea, new_esp;
                    881:        if( STACK_32BIT ) {
                    882:                new_esp = REG32(ESP) + 2;
                    883:                ea = i386_translate(SS, new_esp - 2, 0);
                    884:        } else {
                    885:                new_esp = REG16(SP) + 2;
                    886:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    887:        }
                    888:        return READ16(ea);
                    889: #else
                    890:        UINT16 sp = m_regs.w[SP] + 2;
                    891:        return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
                    892: #endif
                    893: }
                    894: 
1.1.1.53  root      895: void i386_write_stack(UINT16 value)
                    896: {
                    897: #if defined(HAS_I386)
                    898:        UINT32 ea, new_esp;
                    899:        if( STACK_32BIT ) {
                    900:                new_esp = REG32(ESP) + 2;
                    901:                ea = i386_translate(SS, new_esp - 2, 0);
                    902:        } else {
                    903:                new_esp = REG16(SP) + 2;
                    904:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    905:        }
                    906:        WRITE16(ea, value);
                    907: #else
                    908:        UINT16 sp = m_regs.w[SP] + 2;
                    909:        WriteWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK), value);
                    910: #endif
                    911: }
                    912: 
1.1       root      913: /* ----------------------------------------------------------------------------
1.1.1.33  root      914:        debugger
                    915: ---------------------------------------------------------------------------- */
                    916: 
                    917: #ifdef USE_DEBUGGER
                    918: #define TELNET_BLUE      0x0004 // text color contains blue.
                    919: #define TELNET_GREEN     0x0002 // text color contains green.
                    920: #define TELNET_RED       0x0001 // text color contains red.
                    921: #define TELNET_INTENSITY 0x0008 // text color is intensified.
                    922: 
                    923: int svr_socket = 0;
                    924: int cli_socket = 0;
                    925: 
1.1.1.55  root      926: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error);
1.1.1.33  root      927: 
                    928: void debugger_init()
                    929: {
                    930:        now_debugging = false;
                    931:        now_going = false;
                    932:        now_suspended = false;
                    933:        force_suspend = false;
                    934:        
                    935:        memset(&break_point, 0, sizeof(break_point_t));
                    936:        memset(&rd_break_point, 0, sizeof(break_point_t));
                    937:        memset(&wr_break_point, 0, sizeof(break_point_t));
                    938:        memset(&in_break_point, 0, sizeof(break_point_t));
                    939:        memset(&out_break_point, 0, sizeof(break_point_t));
                    940:        memset(&int_break_point, 0, sizeof(int_break_point_t));
                    941: }
                    942: 
1.1.1.45  root      943: void telnet_send(const char *string)
1.1.1.33  root      944: {
                    945:        char buffer[8192], *ptr;
                    946:        strcpy(buffer, string);
                    947:        while((ptr = strstr(buffer, "\n")) != NULL) {
                    948:                char tmp[8192];
                    949:                *ptr = '\0';
                    950:                sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
                    951:                strcpy(buffer, tmp);
                    952:        }
                    953:        
                    954:        int len = strlen(buffer), res;
                    955:        ptr = buffer;
                    956:        while(len > 0) {
                    957:                if((res = send(cli_socket, ptr, len, 0)) > 0) {
                    958:                        len -= res;
                    959:                        ptr += res;
                    960:                }
                    961:        }
                    962: }
                    963: 
                    964: void telnet_command(const char *format, ...)
                    965: {
                    966:        char buffer[1024];
                    967:        va_list ap;
                    968:        va_start(ap, format);
                    969:        vsprintf(buffer, format, ap);
                    970:        va_end(ap);
                    971:        
                    972:        telnet_send(buffer);
                    973: }
                    974: 
                    975: void telnet_printf(const char *format, ...)
                    976: {
                    977:        char buffer[1024];
                    978:        va_list ap;
                    979:        va_start(ap, format);
                    980:        vsprintf(buffer, format, ap);
                    981:        va_end(ap);
                    982:        
                    983:        if(fp_debugger != NULL) {
                    984:                fprintf(fp_debugger, "%s", buffer);
                    985:        }
                    986:        telnet_send(buffer);
                    987: }
                    988: 
                    989: bool telnet_gets(char *str, int n)
                    990: {
                    991:        char buffer[1024];
                    992:        int ptr = 0;
                    993:        
                    994:        telnet_command("\033[12l"); // local echo on
                    995:        telnet_command("\033[2l");  // key unlock
                    996:        
1.1.1.54  root      997:        while(!m_exit) {
1.1.1.33  root      998:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    999:                
                   1000:                if(len > 0 && buffer[0] != 0xff) {
                   1001:                        for(int i = 0; i < len; i++) {
                   1002:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                   1003:                                        str[ptr] = 0;
                   1004:                                        telnet_command("\033[2h");  // key lock
                   1005:                                        telnet_command("\033[12h"); // local echo off
1.1.1.54  root     1006:                                        return(!m_exit);
1.1.1.33  root     1007:                                } else if(buffer[i] == 0x08) {
                   1008:                                        if(ptr > 0) {
                   1009:                                                telnet_command("\033[0K"); // erase from cursor position
                   1010:                                                ptr--;
                   1011:                                        } else {
                   1012:                                                telnet_command("\033[1C"); // move cursor forward
                   1013:                                        }
                   1014:                                } else if(ptr < n - 1) {
1.1.1.37  root     1015:                                        if(buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
1.1.1.33  root     1016:                                                str[ptr++] = buffer[i];
                   1017:                                        }
                   1018:                                } else {
                   1019:                                        telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
                   1020:                                }
                   1021:                        }
                   1022:                } else if(len == -1) {
                   1023:                        if(WSAGetLastError() != WSAEWOULDBLOCK) {
                   1024:                                return(false);
                   1025:                        }
                   1026:                } else if(len == 0) {
                   1027:                        return(false);
                   1028:                }
                   1029:                Sleep(10);
                   1030:        }
1.1.1.54  root     1031:        return(!m_exit);
1.1.1.33  root     1032: }
                   1033: 
                   1034: bool telnet_kbhit()
                   1035: {
                   1036:        char buffer[1024];
                   1037:        
1.1.1.54  root     1038:        if(!m_exit) {
1.1.1.33  root     1039:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1040:                
                   1041:                if(len > 0) {
                   1042:                        for(int i = 0; i < len; i++) {
                   1043:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                   1044:                                        return(true);
                   1045:                                }
                   1046:                        }
                   1047:                } else if(len == 0) {
                   1048:                        return(true); // disconnected
                   1049:                }
                   1050:        }
                   1051:        return(false);
                   1052: }
                   1053: 
                   1054: bool telnet_disconnected()
                   1055: {
                   1056:        char buffer[1024];
                   1057:        int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1058:        
                   1059:        if(len == 0) {
                   1060:                return(true);
                   1061:        } else if(len == -1) {
                   1062:                if(WSAGetLastError() != WSAEWOULDBLOCK) {
                   1063:                        return(true);
                   1064:                }
                   1065:        }
                   1066:        return(false);
                   1067: }
                   1068: 
                   1069: void telnet_set_color(int color)
                   1070: {
                   1071:        telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
                   1072: }
                   1073: 
                   1074: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
                   1075: {
                   1076: //     UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
                   1077:        UINT8 ops[16];
                   1078:        for(int i = 0; i < 16; i++) {
                   1079:                ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
                   1080:        }
                   1081:        UINT8 *oprom = ops;
                   1082:        
                   1083: #if defined(HAS_I386)
                   1084:        if(m_operand_size) {
                   1085:                return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
                   1086:        } else
                   1087: #endif
                   1088:        return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
                   1089: }
                   1090: 
                   1091: void debugger_regs_info(char *buffer)
                   1092: {
                   1093: #if defined(HAS_I386)
                   1094:        UINT32 flags = get_flags();
                   1095: #else
                   1096:        UINT32 flags = CompressFlags();
                   1097: #endif
                   1098: #if defined(HAS_I386)
                   1099:        if(m_operand_size) {
                   1100:                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",
                   1101:                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),
                   1102:                PROTECTED_MODE ? "PE" : "--",
                   1103:                (flags & 0x40000) ? 'A' : '-',
                   1104:                (flags & 0x20000) ? 'V' : '-',
                   1105:                (flags & 0x10000) ? 'R' : '-',
                   1106:                (flags & 0x04000) ? 'N' : '-',
                   1107:                (flags & 0x02000) ? '1' : '0',
                   1108:                (flags & 0x01000) ? '1' : '0',
                   1109:                (flags & 0x00800) ? 'O' : '-',
                   1110:                (flags & 0x00400) ? 'D' : '-',
                   1111:                (flags & 0x00200) ? 'I' : '-',
                   1112:                (flags & 0x00100) ? 'T' : '-',
                   1113:                (flags & 0x00080) ? 'S' : '-',
                   1114:                (flags & 0x00040) ? 'Z' : '-',
                   1115:                (flags & 0x00010) ? 'A' : '-',
                   1116:                (flags & 0x00004) ? 'P' : '-',
                   1117:                (flags & 0x00001) ? 'C' : '-');
                   1118:        } else {
                   1119: #endif
                   1120:                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",
                   1121:                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),
                   1122: #if defined(HAS_I386)
                   1123:                PROTECTED_MODE ? "PE" : "--",
                   1124: #else
                   1125:                "--",
                   1126: #endif
                   1127:                (flags & 0x40000) ? 'A' : '-',
                   1128:                (flags & 0x20000) ? 'V' : '-',
                   1129:                (flags & 0x10000) ? 'R' : '-',
                   1130:                (flags & 0x04000) ? 'N' : '-',
                   1131:                (flags & 0x02000) ? '1' : '0',
                   1132:                (flags & 0x01000) ? '1' : '0',
                   1133:                (flags & 0x00800) ? 'O' : '-',
                   1134:                (flags & 0x00400) ? 'D' : '-',
                   1135:                (flags & 0x00200) ? 'I' : '-',
                   1136:                (flags & 0x00100) ? 'T' : '-',
                   1137:                (flags & 0x00080) ? 'S' : '-',
                   1138:                (flags & 0x00040) ? 'Z' : '-',
                   1139:                (flags & 0x00010) ? 'A' : '-',
                   1140:                (flags & 0x00004) ? 'P' : '-',
                   1141:                (flags & 0x00001) ? 'C' : '-');
                   1142: #if defined(HAS_I386)
                   1143:        }
                   1144: #endif
                   1145: }
                   1146: 
                   1147: void debugger_process_info(char *buffer)
                   1148: {
                   1149:        UINT16 psp_seg = current_psp;
                   1150:        process_t *process;
                   1151:        bool check[0x10000] = {0};
                   1152:        
                   1153:        buffer[0] = '\0';
                   1154:        
                   1155:        while(!check[psp_seg] && (process  = msdos_process_info_get(psp_seg, false)) != NULL) {
                   1156:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1157:                char *file = process->module_path, *s;
                   1158:                char tmp[8192];
                   1159:                
                   1160:                while((s = strstr(file, "\\")) != NULL) {
                   1161:                        file = s + 1;
                   1162:                }
                   1163:                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));
                   1164:                strcat(tmp, buffer);
                   1165:                strcpy(buffer, tmp);
                   1166:                
                   1167:                check[psp_seg] = true;
                   1168:                psp_seg = psp->parent_psp;
                   1169:        }
                   1170: }
                   1171: 
                   1172: UINT32 debugger_get_val(const char *str)
                   1173: {
                   1174:        char tmp[1024];
                   1175:        
                   1176:        if(str == NULL || strlen(str) == 0) {
                   1177:                return(0);
                   1178:        }
                   1179:        strcpy(tmp, str);
                   1180:        
                   1181:        if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
                   1182:                // ank
                   1183:                return(tmp[1] & 0xff);
                   1184:        } else if(tmp[0] == '%') {
                   1185:                // decimal
                   1186:                return(strtoul(tmp + 1, NULL, 10));
                   1187:        }
                   1188:        return(strtoul(tmp, NULL, 16));
                   1189: }
                   1190: 
                   1191: UINT32 debugger_get_seg(const char *str, UINT32 val)
                   1192: {
                   1193:        char tmp[1024], *s;
                   1194:        
                   1195:        if(str == NULL || strlen(str) == 0) {
                   1196:                return(val);
                   1197:        }
                   1198:        strcpy(tmp, str);
                   1199:        
                   1200:        if((s = strstr(tmp, ":")) != NULL) {
                   1201:                // 0000:0000
                   1202:                *s = '\0';
                   1203:                return(debugger_get_val(tmp));
                   1204:        }
                   1205:        return(val);
                   1206: }
                   1207: 
                   1208: UINT32 debugger_get_ofs(const char *str)
                   1209: {
                   1210:        char tmp[1024], *s;
                   1211:        
                   1212:        if(str == NULL || strlen(str) == 0) {
                   1213:                return(0);
                   1214:        }
                   1215:        strcpy(tmp, str);
                   1216:        
                   1217:        if((s = strstr(tmp, ":")) != NULL) {
                   1218:                // 0000:0000
                   1219:                return(debugger_get_val(s + 1));
                   1220:        }
                   1221:        return(debugger_get_val(tmp));
                   1222: }
                   1223: 
                   1224: void debugger_main()
                   1225: {
                   1226:        telnet_command("\033[20h"); // cr-lf
                   1227:        
                   1228:        force_suspend = true;
                   1229:        now_going = false;
                   1230:        now_debugging = true;
                   1231:        Sleep(100);
                   1232:        
1.1.1.54  root     1233:        if(!m_exit && !now_suspended) {
1.1.1.33  root     1234:                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1235:                telnet_printf("waiting until cpu is suspended...\n");
                   1236:        }
1.1.1.54  root     1237:        while(!m_exit && !now_suspended) {
1.1.1.33  root     1238:                if(telnet_disconnected()) {
                   1239:                        break;
                   1240:                }
                   1241:                Sleep(10);
                   1242:        }
                   1243:        
                   1244:        char buffer[8192];
                   1245:        
                   1246:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1247:        debugger_process_info(buffer);
                   1248:        telnet_printf("%s", buffer);
                   1249:        debugger_regs_info(buffer);
                   1250:        telnet_printf("%s", buffer);
                   1251:        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1252:        telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
                   1253:        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1254:        debugger_dasm(buffer, SREG(CS), m_eip);
                   1255:        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1256:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1257:        
                   1258:        #define MAX_COMMAND_LEN 64
                   1259:        
                   1260:        char command[MAX_COMMAND_LEN + 1];
                   1261:        char prev_command[MAX_COMMAND_LEN + 1] = {0};
                   1262:        
                   1263:        UINT32 data_seg = SREG(DS);
                   1264:        UINT32 data_ofs = 0;
                   1265:        UINT32 dasm_seg = SREG(CS);
                   1266:        UINT32 dasm_ofs = m_eip;
                   1267:        
1.1.1.54  root     1268:        while(!m_exit) {
1.1.1.33  root     1269:                telnet_printf("- ");
                   1270:                command[0] = '\0';
                   1271:                
                   1272:                if(fi_debugger != NULL) {
                   1273:                        while(command[0] == '\0') {
                   1274:                                if(fgets(command, sizeof(command), fi_debugger) == NULL) {
                   1275:                                        break;
                   1276:                                }
                   1277:                                while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
                   1278:                                        command[strlen(command) - 1] = '\0';
                   1279:                                }
                   1280:                        }
                   1281:                        if(command[0] != '\0') {
                   1282:                                telnet_command("%s\n", command);
                   1283:                        }
                   1284:                }
                   1285:                if(command[0] == '\0') {
                   1286:                        if(!telnet_gets(command, sizeof(command))) {
                   1287:                                break;
                   1288:                        }
                   1289:                }
                   1290:                if(command[0] == '\0') {
                   1291:                        strcpy(command, prev_command);
                   1292:                } else {
                   1293:                        strcpy(prev_command, command);
                   1294:                }
                   1295:                if(fp_debugger != NULL) {
                   1296:                        fprintf(fp_debugger, "%s\n", command);
                   1297:                }
                   1298:                
1.1.1.54  root     1299:                if(!m_exit && command[0] != 0) {
1.1.1.33  root     1300:                        char *params[32], *token = NULL;
                   1301:                        int num = 0;
                   1302:                        
                   1303:                        if((token = strtok(command, " ")) != NULL) {
                   1304:                                params[num++] = token;
                   1305:                                while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
                   1306:                                        params[num++] = token;
                   1307:                                }
                   1308:                        }
                   1309:                        if(stricmp(params[0], "D") == 0) {
                   1310:                                if(num <= 3) {
                   1311:                                        if(num >= 2) {
                   1312:                                                data_seg = debugger_get_seg(params[1], data_seg);
                   1313:                                                data_ofs = debugger_get_ofs(params[1]);
                   1314:                                        }
                   1315:                                        UINT32 end_seg = data_seg;
                   1316:                                        UINT32 end_ofs = data_ofs + 8 * 16 - 1;
                   1317:                                        if(num == 3) {
                   1318:                                                end_seg = debugger_get_seg(params[2], data_seg);
                   1319:                                                end_ofs = debugger_get_ofs(params[2]);
                   1320:                                        }
                   1321:                                        UINT64 start_addr = (data_seg << 4) + data_ofs;
                   1322:                                        UINT64 end_addr = (end_seg << 4) + end_ofs;
1.1.1.37  root     1323: //                                     bool is_sjis = false;
1.1.1.33  root     1324:                                        
                   1325:                                        for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
                   1326:                                                if((addr & 0x0f) == 0) {
                   1327:                                                        if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
                   1328:                                                                data_seg += 0x1000;
                   1329:                                                                data_ofs -= 0x10000;
                   1330:                                                        }
                   1331:                                                        telnet_printf("%06X:%04X ", data_seg, data_ofs);
                   1332:                                                        memset(buffer, 0, sizeof(buffer));
                   1333:                                                }
                   1334:                                                if(addr < start_addr || addr > end_addr) {
                   1335:                                                        telnet_printf("   ");
                   1336:                                                        buffer[addr & 0x0f] = ' ';
                   1337:                                                } else {
                   1338:                                                        UINT8 data = debugger_read_byte(addr & ADDR_MASK);
                   1339:                                                        telnet_printf(" %02X", data);
1.1.1.37  root     1340: //                                                     if(is_sjis) {
1.1.1.33  root     1341: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1342: //                                                             is_sjis = false;
1.1.1.33  root     1343: //                                                     } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
                   1344: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1345: //                                                             is_sjis = true;
1.1.1.33  root     1346: //                                                     } else
                   1347:                                                        if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
                   1348:                                                                buffer[addr & 0x0f] = data;
                   1349:                                                        } else {
                   1350:                                                                buffer[addr & 0x0f] = '.';
                   1351:                                                        }
                   1352:                                                }
                   1353:                                                if((addr & 0x0f) == 0x0f) {
                   1354:                                                        telnet_printf("  %s\n", buffer);
                   1355:                                                }
                   1356:                                        }
                   1357:                                        if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
                   1358:                                                data_seg += 0x1000;
                   1359:                                                data_ofs -= 0x10000;
                   1360:                                        }
                   1361:                                        prev_command[1] = '\0'; // remove parameters to dump continuously
                   1362:                                } else {
                   1363:                                        telnet_printf("invalid parameter number\n");
                   1364:                                }
                   1365:                        } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 0) {
                   1366:                                if(num >= 3) {
                   1367:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1368:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1369:                                        for(int i = 2, j = 0; i < num; i++, j++) {
                   1370:                                                debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
                   1371:                                        }
                   1372:                                } else {
                   1373:                                        telnet_printf("invalid parameter number\n");
                   1374:                                }
                   1375:                        } else if(stricmp(params[0], "EW") == 0) {
                   1376:                                if(num >= 3) {
                   1377:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1378:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1379:                                        for(int i = 2, j = 0; i < num; i++, j += 2) {
                   1380:                                                debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
                   1381:                                        }
                   1382:                                } else {
                   1383:                                        telnet_printf("invalid parameter number\n");
                   1384:                                }
                   1385:                        } else if(stricmp(params[0], "ED") == 0) {
                   1386:                                if(num >= 3) {
                   1387:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1388:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1389:                                        for(int i = 2, j = 0; i < num; i++, j += 4) {
                   1390:                                                debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
                   1391:                                        }
                   1392:                                } else {
                   1393:                                        telnet_printf("invalid parameter number\n");
                   1394:                                }
                   1395:                        } else if(stricmp(params[0], "EA") == 0) {
                   1396:                                if(num >= 3) {
                   1397:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1398:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1399:                                        strcpy(buffer, prev_command);
                   1400:                                        if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
                   1401:                                                int len = strlen(token);
                   1402:                                                for(int i = 0; i < len; i++) {
                   1403:                                                        debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
                   1404:                                                }
                   1405:                                        } else {
                   1406:                                                telnet_printf("invalid parameter\n");
                   1407:                                        }
                   1408:                                } else {
                   1409:                                        telnet_printf("invalid parameter number\n");
                   1410:                                }
                   1411:                        } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
                   1412:                                if(num == 2) {
                   1413:                                        telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
                   1414:                                } else {
                   1415:                                        telnet_printf("invalid parameter number\n");
                   1416:                                }
                   1417:                        } else if(stricmp(params[0], "IW") == 0) {
                   1418:                                if(num == 2) {
                   1419:                                        telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
                   1420:                                } else {
                   1421:                                        telnet_printf("invalid parameter number\n");
                   1422:                                }
                   1423:                        } else if(stricmp(params[0], "ID") == 0) {
                   1424:                                if(num == 2) {
                   1425:                                        telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
                   1426:                                } else {
                   1427:                                        telnet_printf("invalid parameter number\n");
                   1428:                                }
                   1429:                        } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
                   1430:                                if(num == 3) {
                   1431:                                        debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
                   1432:                                } else {
                   1433:                                        telnet_printf("invalid parameter number\n");
                   1434:                                }
                   1435:                        } else if(stricmp(params[0], "OW") == 0) {
                   1436:                                if(num == 3) {
                   1437:                                        debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
                   1438:                                } else {
                   1439:                                        telnet_printf("invalid parameter number\n");
                   1440:                                }
                   1441:                        } else if(stricmp(params[0], "OD") == 0) {
                   1442:                                if(num == 3) {
                   1443:                                        debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
                   1444:                                } else {
                   1445:                                        telnet_printf("invalid parameter number\n");
                   1446:                                }
                   1447:                        } else if(stricmp(params[0], "R") == 0) {
                   1448:                                if(num == 1) {
                   1449:                                        debugger_regs_info(buffer);
                   1450:                                        telnet_printf("%s", buffer);
                   1451:                                } else if(num == 3) {
                   1452: #if defined(HAS_I386)
                   1453:                                        if(stricmp(params[1], "EAX") == 0) {
                   1454:                                                REG32(EAX) = debugger_get_val(params[2]);
                   1455:                                        } else if(stricmp(params[1], "EBX") == 0) {
                   1456:                                                REG32(EBX) = debugger_get_val(params[2]);
                   1457:                                        } else if(stricmp(params[1], "ECX") == 0) {
                   1458:                                                REG32(ECX) = debugger_get_val(params[2]);
                   1459:                                        } else if(stricmp(params[1], "EDX") == 0) {
                   1460:                                                REG32(EDX) = debugger_get_val(params[2]);
                   1461:                                        } else if(stricmp(params[1], "ESP") == 0) {
                   1462:                                                REG32(ESP) = debugger_get_val(params[2]);
                   1463:                                        } else if(stricmp(params[1], "EBP") == 0) {
                   1464:                                                REG32(EBP) = debugger_get_val(params[2]);
                   1465:                                        } else if(stricmp(params[1], "ESI") == 0) {
                   1466:                                                REG32(ESI) = debugger_get_val(params[2]);
                   1467:                                        } else if(stricmp(params[1], "EDI") == 0) {
                   1468:                                                REG32(EDI) = debugger_get_val(params[2]);
                   1469:                                        } else
                   1470: #endif
                   1471:                                        if(stricmp(params[1], "AX") == 0) {
                   1472:                                                REG16(AX) = debugger_get_val(params[2]);
                   1473:                                        } else if(stricmp(params[1], "BX") == 0) {
                   1474:                                                REG16(BX) = debugger_get_val(params[2]);
                   1475:                                        } else if(stricmp(params[1], "CX") == 0) {
                   1476:                                                REG16(CX) = debugger_get_val(params[2]);
                   1477:                                        } else if(stricmp(params[1], "DX") == 0) {
                   1478:                                                REG16(DX) = debugger_get_val(params[2]);
                   1479:                                        } else if(stricmp(params[1], "SP") == 0) {
                   1480:                                                REG16(SP) = debugger_get_val(params[2]);
                   1481:                                        } else if(stricmp(params[1], "BP") == 0) {
                   1482:                                                REG16(BP) = debugger_get_val(params[2]);
                   1483:                                        } else if(stricmp(params[1], "SI") == 0) {
                   1484:                                                REG16(SI) = debugger_get_val(params[2]);
                   1485:                                        } else if(stricmp(params[1], "DI") == 0) {
                   1486:                                                REG16(DI) = debugger_get_val(params[2]);
                   1487:                                        } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
                   1488: #if defined(HAS_I386)
                   1489:                                                if(m_operand_size) {
                   1490:                                                        m_eip = debugger_get_val(params[2]);
                   1491:                                                } else {
                   1492:                                                        m_eip = debugger_get_val(params[2]) & 0xffff;
                   1493:                                                }
                   1494:                                                CHANGE_PC(m_eip);
                   1495: #else
                   1496:                                                m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
                   1497:                                                CHANGE_PC(m_pc);
                   1498: #endif
                   1499:                                        } else if(stricmp(params[1], "AL") == 0) {
                   1500:                                                REG8(AL) = debugger_get_val(params[2]);
                   1501:                                        } else if(stricmp(params[1], "AH") == 0) {
                   1502:                                                REG8(AH) = debugger_get_val(params[2]);
                   1503:                                        } else if(stricmp(params[1], "BL") == 0) {
                   1504:                                                REG8(BL) = debugger_get_val(params[2]);
                   1505:                                        } else if(stricmp(params[1], "BH") == 0) {
                   1506:                                                REG8(BH) = debugger_get_val(params[2]);
                   1507:                                        } else if(stricmp(params[1], "CL") == 0) {
                   1508:                                                REG8(CL) = debugger_get_val(params[2]);
                   1509:                                        } else if(stricmp(params[1], "CH") == 0) {
                   1510:                                                REG8(CH) = debugger_get_val(params[2]);
                   1511:                                        } else if(stricmp(params[1], "DL") == 0) {
                   1512:                                                REG8(DL) = debugger_get_val(params[2]);
                   1513:                                        } else if(stricmp(params[1], "DH") == 0) {
                   1514:                                                REG8(DH) = debugger_get_val(params[2]);
                   1515:                                        } else {
                   1516:                                                telnet_printf("unknown register %s\n", params[1]);
                   1517:                                        }
                   1518:                                } else {
                   1519:                                        telnet_printf("invalid parameter number\n");
                   1520:                                }
1.1.1.60  root     1521:                        } else if(stricmp(params[0], "S") == 0) {
1.1.1.33  root     1522:                                if(num >= 4) {
                   1523:                                        UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
                   1524:                                        UINT32 cur_ofs = debugger_get_ofs(params[1]);
                   1525:                                        UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
                   1526:                                        UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1527:                                        UINT8 list[32];
                   1528:                                        
                   1529:                                        for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1530:                                                list[j] = debugger_get_val(params[i]);
                   1531:                                        }
                   1532:                                        while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
                   1533:                                                bool found = true;
                   1534:                                                for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1535:                                                        if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
                   1536:                                                                found = false;
                   1537:                                                                break;
                   1538:                                                        }
                   1539:                                                }
                   1540:                                                if(found) {
                   1541:                                                        telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
                   1542:                                                }
                   1543:                                                if((cur_ofs += 1) > 0xffff) {
                   1544:                                                        cur_seg += 0x1000;
                   1545:                                                        cur_ofs -= 0x10000;
                   1546:                                                }
                   1547:                                        }
                   1548:                                } else {
                   1549:                                        telnet_printf("invalid parameter number\n");
                   1550:                                }
                   1551:                        } else if(stricmp(params[0], "U") == 0) {
                   1552:                                if(num <= 3) {
                   1553:                                        if(num >= 2) {
                   1554:                                                dasm_seg = debugger_get_seg(params[1], dasm_seg);
                   1555:                                                dasm_ofs = debugger_get_ofs(params[1]);
                   1556:                                        }
                   1557:                                        if(num == 3) {
                   1558:                                                UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
                   1559:                                                UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1560:                                                
                   1561:                                                while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
                   1562:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1563:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1564:                                                        for(int i = 0; i < len; i++) {
                   1565:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1566:                                                        }
                   1567:                                                        for(int i = len; i < 8; i++) {
                   1568:                                                                telnet_printf("  ");
                   1569:                                                        }
                   1570:                                                        telnet_printf("  %s\n", buffer);
                   1571:                                                        if((dasm_ofs += len) > 0xffff) {
                   1572:                                                                dasm_seg += 0x1000;
                   1573:                                                                dasm_ofs -= 0x10000;
                   1574:                                                        }
                   1575:                                                }
                   1576:                                        } else {
                   1577:                                                for(int i = 0; i < 16; i++) {
                   1578:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1579:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1580:                                                        for(int i = 0; i < len; i++) {
                   1581:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1582:                                                        }
                   1583:                                                        for(int i = len; i < 8; i++) {
                   1584:                                                                telnet_printf("  ");
                   1585:                                                        }
                   1586:                                                        telnet_printf("  %s\n", buffer);
                   1587:                                                        if((dasm_ofs += len) > 0xffff) {
                   1588:                                                                dasm_seg += 0x1000;
                   1589:                                                                dasm_ofs -= 0x10000;
                   1590:                                                        }
                   1591:                                                }
                   1592:                                        }
                   1593:                                        prev_command[1] = '\0'; // remove parameters to disassemble continuously
                   1594:                                } else {
                   1595:                                        telnet_printf("invalid parameter number\n");
                   1596:                                }
                   1597:                        } else if(stricmp(params[0], "H") == 0) {
                   1598:                                if(num == 3) {
                   1599:                                        UINT32 l = debugger_get_val(params[1]);
                   1600:                                        UINT32 r = debugger_get_val(params[2]);
                   1601:                                        telnet_printf("%08X  %08X\n", l + r, l - r);
                   1602:                                } else {
                   1603:                                        telnet_printf("invalid parameter number\n");
                   1604:                                }
                   1605:                        } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
                   1606:                                break_point_t *break_point_ptr;
                   1607:                                #define GET_BREAK_POINT_PTR() { \
1.1.1.58  root     1608:                                        if(params[0][0] == 'R' || params[0][0] == 'r') { \
1.1.1.33  root     1609:                                                break_point_ptr = &rd_break_point; \
1.1.1.58  root     1610:                                        } else if(params[0][0] == 'W' || params[0][0] == 'w') { \
1.1.1.33  root     1611:                                                break_point_ptr = &wr_break_point; \
1.1.1.58  root     1612:                                        } else if(params[0][0] == 'I' || params[0][0] == 'i') { \
1.1.1.33  root     1613:                                                break_point_ptr = &in_break_point; \
1.1.1.58  root     1614:                                        } else if(params[0][0] == 'O' || params[0][0] == 'o') { \
1.1.1.33  root     1615:                                                break_point_ptr = &out_break_point; \
                   1616:                                        } else { \
                   1617:                                                break_point_ptr = &break_point; \
                   1618:                                        } \
                   1619:                                }
                   1620:                                GET_BREAK_POINT_PTR();
                   1621:                                if(num == 2) {
1.1.1.58  root     1622:                                        UINT32 seg = 0;
                   1623:                                        if(params[0][0] == 'R' || params[0][0] == 'r' || params[0][0] == 'W' || params[0][0] == 'w') {
                   1624:                                                seg = debugger_get_seg(params[1], data_seg);
                   1625:                                        } else {
                   1626:                                                seg = debugger_get_seg(params[1], SREG(CS));
                   1627:                                        }
1.1.1.33  root     1628:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1629:                                        bool found = false;
                   1630:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1631:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
                   1632:                                                        break_point_ptr->table[i].addr = (seg << 4) + ofs;
                   1633:                                                        break_point_ptr->table[i].seg = seg;
                   1634:                                                        break_point_ptr->table[i].ofs = ofs;
                   1635:                                                        break_point_ptr->table[i].status = 1;
                   1636:                                                        found = true;
                   1637:                                                }
                   1638:                                        }
                   1639:                                        if(!found) {
                   1640:                                                telnet_printf("too many break points\n");
                   1641:                                        }
                   1642:                                } else {
                   1643:                                        telnet_printf("invalid parameter number\n");
                   1644:                                }
                   1645:                        } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
                   1646:                                break_point_t *break_point_ptr;
                   1647:                                GET_BREAK_POINT_PTR();
                   1648:                                if(num == 2) {
                   1649:                                        UINT32 addr = debugger_get_val(params[1]);
                   1650:                                        bool found = false;
                   1651:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1652:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
                   1653:                                                        break_point_ptr->table[i].addr = addr;
                   1654:                                                        break_point_ptr->table[i].status = 1;
                   1655:                                                        found = true;
                   1656:                                                }
                   1657:                                        }
                   1658:                                        if(!found) {
                   1659:                                                telnet_printf("too many break points\n");
                   1660:                                        }
                   1661:                                } else {
                   1662:                                        telnet_printf("invalid parameter number\n");
                   1663:                                }
                   1664:                        } 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) {
                   1665:                                break_point_t *break_point_ptr;
                   1666:                                GET_BREAK_POINT_PTR();
                   1667:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1668:                                        memset(break_point_ptr, 0, sizeof(break_point_t));
                   1669:                                } else if(num >= 2) {
                   1670:                                        for(int i = 1; i < num; i++) {
                   1671:                                                int index = debugger_get_val(params[i]);
                   1672:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1673:                                                        telnet_printf("invalid index %x\n", index);
                   1674:                                                } else {
                   1675:                                                        break_point_ptr->table[index - 1].addr = 0;
                   1676:                                                        break_point_ptr->table[index - 1].seg = 0;
                   1677:                                                        break_point_ptr->table[index - 1].ofs = 0;
                   1678:                                                        break_point_ptr->table[index - 1].status = 0;
                   1679:                                                }
                   1680:                                        }
                   1681:                                } else {
                   1682:                                        telnet_printf("invalid parameter number\n");
                   1683:                                }
                   1684:                        } 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 ||
                   1685:                                  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) {
                   1686:                                break_point_t *break_point_ptr;
                   1687:                                GET_BREAK_POINT_PTR();
                   1688:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1689:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1690:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1691:                                                if(break_point_ptr->table[i].status != 0) {
                   1692:                                                        break_point_ptr->table[i].status = enabled ? 1 : -1;
                   1693:                                                }
                   1694:                                        }
                   1695:                                } else if(num >= 2) {
                   1696:                                        for(int i = 1; i < num; i++) {
                   1697:                                                int index = debugger_get_val(params[i]);
                   1698:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1699:                                                        telnet_printf("invalid index %x\n", index);
                   1700:                                                } else if(break_point_ptr->table[index - 1].status == 0) {
                   1701:                                                        telnet_printf("break point %x is null\n", index);
                   1702:                                                } else {
                   1703:                                                        break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
                   1704:                                                }
                   1705:                                        }
                   1706:                                } else {
                   1707:                                        telnet_printf("invalid parameter number\n");
                   1708:                                }
                   1709:                        } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
                   1710:                                break_point_t *break_point_ptr;
                   1711:                                GET_BREAK_POINT_PTR();
                   1712:                                if(num == 1) {
                   1713:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1714:                                                if(break_point_ptr->table[i].status) {
                   1715:                                                        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);
                   1716:                                                }
                   1717:                                        }
                   1718:                                } else {
                   1719:                                        telnet_printf("invalid parameter number\n");
                   1720:                                }
                   1721:                        } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
                   1722:                                break_point_t *break_point_ptr;
                   1723:                                GET_BREAK_POINT_PTR();
                   1724:                                if(num == 1) {
                   1725:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1726:                                                if(break_point_ptr->table[i].status) {
                   1727:                                                        telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
                   1728:                                                }
                   1729:                                        }
                   1730:                                } else {
                   1731:                                        telnet_printf("invalid parameter number\n");
                   1732:                                }
                   1733:                        } else if(stricmp(params[0], "INTBP") == 0) {
                   1734:                                if(num >= 2 && num <= 4) {
                   1735:                                        int int_num = debugger_get_val(params[1]);
                   1736:                                        UINT8 ah = 0, ah_registered = 0;
                   1737:                                        UINT8 al = 0, al_registered = 0;
                   1738:                                        if(num >= 3) {
                   1739:                                                ah = debugger_get_val(params[2]);
                   1740:                                                ah_registered = 1;
                   1741:                                        }
                   1742:                                        if(num == 4) {
                   1743:                                                al = debugger_get_val(params[3]);
                   1744:                                                al_registered = 1;
                   1745:                                        }
                   1746:                                        bool found = false;
                   1747:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1748:                                                if(int_break_point.table[i].status == 0 || (
                   1749:                                                   int_break_point.table[i].int_num == int_num &&
                   1750:                                                   int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
                   1751:                                                   int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
                   1752:                                                        int_break_point.table[i].int_num = int_num;
                   1753:                                                        int_break_point.table[i].ah = ah;
                   1754:                                                        int_break_point.table[i].ah_registered = ah_registered;
                   1755:                                                        int_break_point.table[i].al = al;
                   1756:                                                        int_break_point.table[i].al_registered = al_registered;
                   1757:                                                        int_break_point.table[i].status = 1;
                   1758:                                                        found = true;
                   1759:                                                }
                   1760:                                        }
                   1761:                                        if(!found) {
                   1762:                                                telnet_printf("too many break points\n");
                   1763:                                        }
                   1764:                                } else {
                   1765:                                        telnet_printf("invalid parameter number\n");
                   1766:                                }
                   1767:                        } else if(stricmp(params[0], "INTBC") == 0) {
                   1768:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1769:                                        memset(&int_break_point, 0, sizeof(int_break_point_t));
                   1770:                                } else if(num >= 2) {
                   1771:                                        for(int i = 1; i < num; i++) {
                   1772:                                                int index = debugger_get_val(params[i]);
                   1773:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1774:                                                        telnet_printf("invalid index %x\n", index);
                   1775:                                                } else {
                   1776:                                                        int_break_point.table[index - 1].int_num = 0;
                   1777:                                                        int_break_point.table[index - 1].ah = 0;
                   1778:                                                        int_break_point.table[index - 1].ah_registered = 0;
                   1779:                                                        int_break_point.table[index - 1].al = 0;
                   1780:                                                        int_break_point.table[index - 1].al_registered = 0;
                   1781:                                                        int_break_point.table[index - 1].status = 0;
                   1782:                                                }
                   1783:                                        }
                   1784:                                } else {
                   1785:                                        telnet_printf("invalid parameter number\n");
                   1786:                                }
                   1787:                        } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
                   1788:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1789:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1790:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1791:                                                if(int_break_point.table[i].status != 0) {
                   1792:                                                        int_break_point.table[i].status = enabled ? 1 : -1;
                   1793:                                                }
                   1794:                                        }
                   1795:                                } else if(num >= 2) {
                   1796:                                        for(int i = 1; i < num; i++) {
                   1797:                                                int index = debugger_get_val(params[i]);
                   1798:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1799:                                                        telnet_printf("invalid index %x\n", index);
                   1800:                                                } else if(int_break_point.table[index - 1].status == 0) {
                   1801:                                                        telnet_printf("break point %x is null\n", index);
                   1802:                                                } else {
                   1803:                                                        int_break_point.table[index - 1].status = enabled ? 1 : -1;
                   1804:                                                }
                   1805:                                        }
                   1806:                                } else {
                   1807:                                        telnet_printf("invalid parameter number\n");
                   1808:                                }
                   1809:                        } else if(stricmp(params[0], "INTBL") == 0) {
                   1810:                                if(num == 1) {
                   1811:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1812:                                                if(int_break_point.table[i].status) {
                   1813:                                                        telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
                   1814:                                                        if(int_break_point.table[i].ah_registered) {
                   1815:                                                                telnet_printf(" %02X", int_break_point.table[i].ah);
                   1816:                                                        }
                   1817:                                                        if(int_break_point.table[i].al_registered) {
                   1818:                                                                telnet_printf(" %02X", int_break_point.table[i].al);
                   1819:                                                        }
                   1820:                                                        telnet_printf("\n");
                   1821:                                                }
                   1822:                                        }
                   1823:                                } else {
                   1824:                                        telnet_printf("invalid parameter number\n");
                   1825:                                }
                   1826:                        } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
                   1827:                                if(num == 1 || num == 2) {
                   1828:                                        break_point_t break_point_stored;
                   1829:                                        bool break_points_stored = false;
                   1830:                                        
                   1831:                                        if(stricmp(params[0], "P") == 0) {
                   1832:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1833:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1834:                                                break_points_stored = true;
                   1835:                                                
                   1836:                                                break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
                   1837:                                                break_point.table[0].status = 1;
                   1838:                                        } else if(num >= 2) {
                   1839:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1840:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1841:                                                break_points_stored = true;
                   1842:                                                
                   1843:                                                UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1844:                                                UINT32 ofs = debugger_get_ofs(params[1]);
                   1845:                                                break_point.table[0].addr = (seg << 4) + ofs;
                   1846:                                                break_point.table[0].seg = seg;
                   1847:                                                break_point.table[0].ofs = ofs;
                   1848:                                                break_point.table[0].status = 1;
                   1849:                                        }
                   1850:                                        break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1851:                                        now_going = true;
                   1852:                                        now_suspended = false;
                   1853:                                        
                   1854:                                        telnet_command("\033[2l"); // key unlock
1.1.1.54  root     1855:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1856:                                                if(telnet_kbhit()) {
                   1857:                                                        break;
                   1858:                                                }
                   1859:                                                Sleep(10);
                   1860:                                        }
                   1861:                                        now_going = false;
                   1862:                                        telnet_command("\033[2h"); // key lock
                   1863:                                        
                   1864:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1865:                                                Sleep(100);
1.1.1.54  root     1866:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1867:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1868:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1869:                                                }
                   1870:                                        }
1.1.1.54  root     1871:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1872:                                                if(telnet_disconnected()) {
                   1873:                                                        break;
                   1874:                                                }
                   1875:                                                Sleep(10);
                   1876:                                        }
                   1877:                                        dasm_seg = SREG(CS);
                   1878:                                        dasm_ofs = m_eip;
                   1879:                                        
                   1880:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1881:                                        debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1882:                                        telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1883:                                        
                   1884:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1885:                                        debugger_regs_info(buffer);
                   1886:                                        telnet_printf("%s", buffer);
                   1887:                                        
                   1888:                                        if(break_point.hit) {
                   1889:                                                if(stricmp(params[0], "G") == 0 && num == 1) {
                   1890:                                                        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1891:                                                        telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1892:                                                }
                   1893:                                        } else if(rd_break_point.hit) {
                   1894:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1895:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1896:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1897:                                                m_prev_cs, m_prev_eip);
                   1898:                                        } else if(wr_break_point.hit) {
                   1899:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1900:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1901:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1902:                                                m_prev_cs, m_prev_eip);
                   1903:                                        } else if(in_break_point.hit) {
                   1904:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1905:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1906:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1907:                                                m_prev_cs, m_prev_eip);
                   1908:                                        } else if(out_break_point.hit) {
                   1909:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1910:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1911:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1912:                                                m_prev_cs, m_prev_eip);
                   1913:                                        } else if(int_break_point.hit) {
                   1914:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1915:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1916:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1917:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1918:                                                }
                   1919:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1920:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1921:                                                }
                   1922:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1923:                                        } else {
                   1924:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1925:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1926:                                        }
                   1927:                                        if(break_points_stored) {
                   1928:                                                memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
                   1929:                                        }
                   1930:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1931:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1932:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1933:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1934:                                } else {
                   1935:                                        telnet_printf("invalid parameter number\n");
                   1936:                                }
                   1937:                        } else if(stricmp(params[0], "T") == 0) {
                   1938:                                if(num == 1 || num == 2) {
                   1939:                                        int steps = 1;
                   1940:                                        if(num >= 2) {
                   1941:                                                steps = debugger_get_val(params[1]);
                   1942:                                        }
                   1943:                                        
                   1944:                                        telnet_command("\033[2l"); // key unlock
                   1945:                                        while(steps-- > 0) {
                   1946:                                                break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1947:                                                now_going = false;
                   1948:                                                now_suspended = false;
                   1949:                                                
1.1.1.54  root     1950:                                                while(!m_exit && !now_suspended) {
1.1.1.33  root     1951:                                                        if(telnet_disconnected()) {
                   1952:                                                                break;
                   1953:                                                        }
                   1954:                                                        Sleep(10);
                   1955:                                                }
                   1956:                                                dasm_seg = SREG(CS);
                   1957:                                                dasm_ofs = m_eip;
                   1958:                                                
                   1959:                                                telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1960:                                                debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1961:                                                telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1962:                                                
                   1963:                                                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1964:                                                debugger_regs_info(buffer);
                   1965:                                                telnet_printf("%s", buffer);
                   1966:                                                
                   1967:                                                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()) {
                   1968:                                                        break;
                   1969:                                                }
                   1970:                                        }
                   1971:                                        telnet_command("\033[2h"); // key lock
                   1972:                                        
                   1973:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1974:                                                Sleep(100);
1.1.1.54  root     1975:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1976:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1977:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1978:                                                }
                   1979:                                        }
1.1.1.54  root     1980:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1981:                                                if(telnet_disconnected()) {
                   1982:                                                        break;
                   1983:                                                }
                   1984:                                                Sleep(10);
                   1985:                                        }
                   1986:                                        if(break_point.hit) {
                   1987:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1988:                                                telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1989:                                        } else if(rd_break_point.hit) {
                   1990:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1991:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1992:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1993:                                                m_prev_cs, m_prev_eip);
                   1994:                                        } else if(wr_break_point.hit) {
                   1995:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1996:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1997:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1998:                                                m_prev_cs, m_prev_eip);
                   1999:                                        } else if(in_break_point.hit) {
                   2000:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2001:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   2002:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   2003:                                                m_prev_cs, m_prev_eip);
                   2004:                                        } else if(out_break_point.hit) {
                   2005:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2006:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   2007:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   2008:                                                m_prev_cs, m_prev_eip);
                   2009:                                        } else if(int_break_point.hit) {
                   2010:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2011:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   2012:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   2013:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   2014:                                                }
                   2015:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   2016:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   2017:                                                }
                   2018:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   2019:                                        } else if(steps > 0) {
                   2020:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2021:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   2022:                                        }
                   2023:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   2024:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   2025:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   2026:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   2027:                                } else {
                   2028:                                        telnet_printf("invalid parameter number\n");
                   2029:                                }
                   2030:                        } else if(stricmp(params[0], "Q") == 0) {
                   2031:                                break;
                   2032:                        } else if(stricmp(params[0], "X") == 0) {
                   2033:                                debugger_process_info(buffer);
                   2034:                                telnet_printf("%s", buffer);
                   2035:                        } else if(stricmp(params[0], ">") == 0) {
                   2036:                                if(num == 2) {
                   2037:                                        if(fp_debugger != NULL) {
                   2038:                                                fclose(fp_debugger);
                   2039:                                                fp_debugger = NULL;
                   2040:                                        }
                   2041:                                        fp_debugger = fopen(params[1], "w");
                   2042:                                } else {
                   2043:                                        telnet_printf("invalid parameter number\n");
                   2044:                                }
                   2045:                        } else if(stricmp(params[0], "<") == 0) {
                   2046:                                if(num == 2) {
                   2047:                                        if(fi_debugger != NULL) {
                   2048:                                                fclose(fi_debugger);
                   2049:                                                fi_debugger = NULL;
                   2050:                                        }
                   2051:                                        fi_debugger = fopen(params[1], "r");
                   2052:                                } else {
                   2053:                                        telnet_printf("invalid parameter number\n");
                   2054:                                }
                   2055:                        } else if(stricmp(params[0], "?") == 0) {
                   2056:                                telnet_printf("D [<start> [<end>]] - dump memory\n");
                   2057:                                telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
                   2058:                                telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
                   2059:                                telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
                   2060:                                telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
                   2061:                                
                   2062:                                telnet_printf("R - show registers\n");
                   2063:                                telnet_printf("R <reg> <value> - edit register\n");
                   2064:                                telnet_printf("S <start> <end> <list> - search\n");
                   2065:                                telnet_printf("U [<start> [<end>]] - unassemble\n");
                   2066:                                
                   2067:                                telnet_printf("H <value> <value> - hexadd\n");
                   2068:                                
                   2069:                                telnet_printf("BP <address> - set breakpoint\n");
                   2070:                                telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
                   2071:                                telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
                   2072:                                telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
                   2073:                                telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
                   2074:                                telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
                   2075:                                
                   2076:                                telnet_printf("G - go (press enter key to break)\n");
                   2077:                                telnet_printf("G <address> - go and break at address\n");
                   2078:                                telnet_printf("P - trace one opcode (step over)\n");
                   2079:                                telnet_printf("T [<count>] - trace (step in)\n");
                   2080:                                telnet_printf("Q - quit\n");
                   2081:                                telnet_printf("X - show dos process info\n");
                   2082:                                
                   2083:                                telnet_printf("> <filename> - output logfile\n");
                   2084:                                telnet_printf("< <filename> - input commands from file\n");
                   2085:                                
                   2086:                                telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
                   2087:                                telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
                   2088:                        } else {
                   2089:                                telnet_printf("unknown command %s\n", params[0]);
                   2090:                        }
                   2091:                }
                   2092:        }
                   2093:        if(fp_debugger != NULL) {
                   2094:                fclose(fp_debugger);
                   2095:                fp_debugger = NULL;
                   2096:        }
                   2097:        if(fi_debugger != NULL) {
                   2098:                fclose(fi_debugger);
                   2099:                fi_debugger = NULL;
                   2100:        }
                   2101:        now_debugging = now_going = now_suspended = force_suspend = false;
                   2102:        closesocket(cli_socket);
                   2103: }
                   2104: 
                   2105: const char *debugger_get_ttermpro_path()
                   2106: {
                   2107:        static char path[MAX_PATH] = {0};
                   2108:        
                   2109:        if(getenv("ProgramFiles")) {
                   2110:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
                   2111:        }
                   2112:        return(path);
                   2113: }
                   2114: 
                   2115: const char *debugger_get_ttermpro_x86_path()
                   2116: {
                   2117:        static char path[MAX_PATH] = {0};
                   2118:        
                   2119:        if(getenv("ProgramFiles(x86)")) {
                   2120:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
                   2121:        }
                   2122:        return(path);
                   2123: }
                   2124: 
                   2125: const char *debugger_get_putty_path()
                   2126: {
                   2127:        static char path[MAX_PATH] = {0};
                   2128:        
                   2129:        if(getenv("ProgramFiles")) {
                   2130:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
                   2131:        }
                   2132:        return(path);
                   2133: }
                   2134: 
                   2135: const char *debugger_get_putty_x86_path()
                   2136: {
                   2137:        static char path[MAX_PATH] = {0};
                   2138:        
                   2139:        if(getenv("ProgramFiles(x86)")) {
                   2140:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
                   2141:        }
                   2142:        return(path);
                   2143: }
                   2144: 
                   2145: const char *debugger_get_telnet_path()
                   2146: {
                   2147:        // NOTE: When you run 32bit version of msdos.exe on Windows x64,
                   2148:        // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
                   2149:        // But 32bit version of telnet.exe will not be installed in SysWOW64
                   2150:        // and 64bit version of telnet.exe will be installed in System32.
                   2151:        static char path[MAX_PATH] = {0};
                   2152:        
                   2153:        if(getenv("windir") != NULL) {
                   2154:                sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
                   2155:        }
                   2156:        return(path);
                   2157: }
                   2158: 
                   2159: DWORD WINAPI debugger_thread(LPVOID)
                   2160: {
                   2161:        WSADATA was_data;
                   2162:        struct sockaddr_in svr_addr;
                   2163:        struct sockaddr_in cli_addr;
                   2164:        int cli_addr_len = sizeof(cli_addr);
                   2165:        int port = 23;
                   2166:        int bind_stat = SOCKET_ERROR;
                   2167:        struct timeval timeout;
                   2168:        
                   2169:        WSAStartup(MAKEWORD(2,0), &was_data);
                   2170:        
                   2171:        if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
                   2172:                memset(&svr_addr, 0, sizeof(svr_addr));
                   2173:                svr_addr.sin_family = AF_INET;
                   2174:                svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
                   2175:                
1.1.1.54  root     2176:                while(!m_exit && port < 10000) {
1.1.1.33  root     2177:                        svr_addr.sin_port = htons(port);
                   2178:                        if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
                   2179:                                break;
                   2180:                        } else {
                   2181:                                port = (port == 23) ? 9000 : (port + 1);
                   2182:                        }
                   2183:                }
                   2184:                if(bind_stat == 0) {
                   2185:                        timeout.tv_sec = 1;
                   2186:                        timeout.tv_usec = 0;
1.1.1.45  root     2187:                        setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
1.1.1.33  root     2188:                        
                   2189:                        listen(svr_socket, 1);
                   2190:                        
                   2191:                        char command[MAX_PATH] = {0};
1.1.1.60  root     2192:                        STARTUPINFOA si;
1.1.1.33  root     2193:                        PROCESS_INFORMATION pi;
                   2194:                        
                   2195:                        if(_access(debugger_get_ttermpro_path(), 0) == 0) {
                   2196:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
                   2197:                        } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
                   2198:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
                   2199:                        } else if(_access(debugger_get_putty_path(), 0) == 0) {
                   2200:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
                   2201:                        } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
                   2202:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
                   2203:                        } else if(_access(debugger_get_telnet_path(), 0) == 0) {
                   2204:                                sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
                   2205:                        }
                   2206:                        if(command[0] != '\0') {
1.1.1.60  root     2207:                                memset(&si, 0, sizeof(STARTUPINFOA));
1.1.1.33  root     2208:                                memset(&pi, 0, sizeof(PROCESS_INFORMATION));
1.1.1.60  root     2209:                                CreateProcessA(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
1.1.1.33  root     2210:                        }
                   2211:                        
1.1.1.54  root     2212:                        while(!m_exit) {
1.1.1.33  root     2213:                                if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
                   2214:                                        u_long val = 1;
                   2215:                                        ioctlsocket(cli_socket, FIONBIO, &val);
                   2216:                                        debugger_main();
                   2217:                                }
                   2218:                        }
                   2219:                }
                   2220:        }
                   2221:        WSACleanup();
                   2222:        return(0);
                   2223: }
                   2224: #endif
                   2225: 
                   2226: /* ----------------------------------------------------------------------------
1.1       root     2227:        main
                   2228: ---------------------------------------------------------------------------- */
                   2229: 
1.1.1.28  root     2230: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                   2231: {
                   2232:        if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33  root     2233:                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     2234: #ifdef USE_SERVICE_THREAD
                   2235:                        EnterCriticalSection(&key_buf_crit_sect);
                   2236: #endif
1.1.1.51  root     2237:                        pcbios_clear_key_buffer();
1.1.1.35  root     2238: #ifdef USE_SERVICE_THREAD
                   2239:                        LeaveCriticalSection(&key_buf_crit_sect);
                   2240: #endif
1.1.1.33  root     2241:                }
                   2242: //             key_code = key_recv = 0;
1.1.1.28  root     2243:                return TRUE;
                   2244:        } else if(dwCtrlType == CTRL_C_EVENT) {
                   2245:                return TRUE;
                   2246:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
                   2247:                // this program will be terminated abnormally, do minimum end process
                   2248:                exit_handler();
                   2249:                exit(1);
                   2250:        }
                   2251:        return FALSE;
                   2252: }
                   2253: 
                   2254: void exit_handler()
                   2255: {
                   2256:        if(temp_file_created) {
1.1.1.60  root     2257:                DeleteFileA(temp_file_path);
1.1.1.28  root     2258:                temp_file_created = false;
                   2259:        }
                   2260:        if(key_buf_char != NULL) {
                   2261:                key_buf_char->release();
                   2262:                delete key_buf_char;
                   2263:                key_buf_char = NULL;
                   2264:        }
                   2265:        if(key_buf_scan != NULL) {
                   2266:                key_buf_scan->release();
                   2267:                delete key_buf_scan;
                   2268:                key_buf_scan = NULL;
                   2269:        }
1.1.1.57  root     2270:        if(key_buf_data != NULL) {
                   2271:                key_buf_data->release();
                   2272:                delete key_buf_data;
                   2273:                key_buf_data = NULL;
                   2274:        }
1.1.1.32  root     2275: #ifdef SUPPORT_XMS
                   2276:        msdos_xms_release();
                   2277: #endif
1.1.1.28  root     2278:        hardware_release();
                   2279: }
                   2280: 
1.1.1.35  root     2281: #ifdef USE_VRAM_THREAD
1.1.1.28  root     2282: DWORD WINAPI vram_thread(LPVOID)
                   2283: {
1.1.1.54  root     2284:        while(!m_exit) {
1.1.1.28  root     2285:                EnterCriticalSection(&vram_crit_sect);
                   2286:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                   2287:                        vram_flush_char();
                   2288:                }
                   2289:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                   2290:                        vram_flush_attr();
                   2291:                }
                   2292:                vram_last_length_char = vram_length_char;
                   2293:                vram_last_length_attr = vram_length_attr;
                   2294:                LeaveCriticalSection(&vram_crit_sect);
                   2295:                // this is about half the maximum keyboard repeat rate - any
                   2296:                // lower tends to be jerky, any higher misses updates
                   2297:                Sleep(15);
                   2298:        }
                   2299:        return 0;
                   2300: }
                   2301: #endif
                   2302: 
1.1.1.45  root     2303: long get_section_in_exec_file(FILE *fp, const char *name)
1.1.1.28  root     2304: {
                   2305:        UINT8 header[0x400];
                   2306:        
                   2307:        long position = ftell(fp);
                   2308:        fseek(fp, 0, SEEK_SET);
                   2309:        fread(header, sizeof(header), 1, fp);
                   2310:        fseek(fp, position, SEEK_SET);
                   2311:        
                   2312:        try {
                   2313:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2314:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2315:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2316:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2317:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2318:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2319:                
                   2320:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                   2321:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                   2322:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                   2323:                                return(sectionHeader->PointerToRawData);
                   2324:                        }
                   2325:                }
                   2326:        } catch(...) {
                   2327:        }
                   2328:        return(0);
                   2329: }
                   2330: 
1.1.1.10  root     2331: bool is_started_from_command_prompt()
                   2332: {
1.1.1.58  root     2333:        bool result = false;
1.1.1.60  root     2334:        HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.58  root     2335:        
1.1.1.18  root     2336:        if(hLibrary) {
                   2337:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                   2338:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                   2339:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
1.1.1.58  root     2340:                if(lpfnGetConsoleProcessList) { // Windows XP or later
1.1.1.18  root     2341:                        DWORD pl;
1.1.1.58  root     2342:                        result = (lpfnGetConsoleProcessList(&pl, 1) > 1);
1.1.1.18  root     2343:                        FreeLibrary(hLibrary);
1.1.1.58  root     2344:                        return(result);
1.1.1.18  root     2345:                }
                   2346:                FreeLibrary(hLibrary);
                   2347:        }
                   2348:        
                   2349:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   2350:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                   2351:                DWORD dwParentProcessID = 0;
                   2352:                PROCESSENTRY32 pe32;
                   2353:                pe32.dwSize = sizeof(PROCESSENTRY32);
                   2354:                if(Process32First(hSnapshot, &pe32)) {
                   2355:                        do {
                   2356:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                   2357:                                        dwParentProcessID = pe32.th32ParentProcessID;
                   2358:                                        break;
                   2359:                                }
                   2360:                        } while(Process32Next(hSnapshot, &pe32));
                   2361:                }
                   2362:                CloseHandle(hSnapshot);
                   2363:                if(dwParentProcessID != 0) {
                   2364:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                   2365:                        if(hProcess != NULL) {
                   2366:                                HMODULE hMod;
                   2367:                                DWORD cbNeeded;
                   2368:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                   2369:                                        char module_name[MAX_PATH];
1.1.1.60  root     2370:                                        if(GetModuleBaseNameA(hProcess, hMod, module_name, sizeof(module_name))) {
1.1.1.58  root     2371:                                                result = (_strnicmp(module_name, "cmd.exe", 7) == 0);
1.1.1.18  root     2372:                                        }
                   2373:                                }
                   2374:                                CloseHandle(hProcess);
                   2375:                        }
                   2376:                }
                   2377:        }
1.1.1.58  root     2378:        return(result);
1.1.1.14  root     2379: }
                   2380: 
                   2381: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                   2382: {
1.1.1.60  root     2383:        HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.14  root     2384:        
1.1.1.60  root     2385:        if(hLibrary) {
                   2386:                typedef ULONGLONG (WINAPI* VerSetConditionMaskFunction)(ULONGLONG, DWORD, BYTE);
                   2387:                typedef BOOL(WINAPI* VerifyVersionInfoFunction)(LPOSVERSIONINFOEXA, DWORD, DWORDLONG);
                   2388:                
                   2389:                VerSetConditionMaskFunction lpfnVerSetConditionMask = reinterpret_cast<VerSetConditionMaskFunction>(::GetProcAddress(hLibrary, "VerSetConditionMask"));
                   2390:                VerifyVersionInfoFunction lpfnVerifyVersionInfo = reinterpret_cast<VerifyVersionInfoFunction>(::GetProcAddress(hLibrary, "VerifyVersionInfoA"));
                   2391:                
                   2392:                if(lpfnVerSetConditionMask && lpfnVerifyVersionInfo) { // Windows 2000 or later
                   2393:                        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
                   2394:                        OSVERSIONINFOEXA osvi;
                   2395:                        DWORDLONG dwlConditionMask = 0;
                   2396:                        int op = VER_GREATER_EQUAL;
                   2397:                        
                   2398:                        // Initialize the OSVERSIONINFOEXA structure.
                   2399:                        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEXA));
                   2400:                        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXA);
                   2401:                        osvi.dwMajorVersion = dwMajorVersion;
                   2402:                        osvi.dwMinorVersion = dwMinorVersion;
                   2403:                        osvi.wServicePackMajor = wServicePackMajor;
                   2404:                        osvi.wServicePackMinor = wServicePackMinor;
                   2405:                        
                   2406:                         // Initialize the condition mask.
                   2407:                        #define MY_VER_SET_CONDITION(_m_,_t_,_c_) ((_m_)=lpfnVerSetConditionMask((_m_),(_t_),(_c_)))
                   2408:                        
                   2409:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                   2410:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                   2411:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                   2412:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                   2413:                        
                   2414:                        // Perform the test.
                   2415:                        BOOL result = lpfnVerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                   2416:                        FreeLibrary(hLibrary);
                   2417:                        return(result);
                   2418:                }
                   2419:                FreeLibrary(hLibrary);
                   2420:        }
                   2421:        
                   2422:        OSVERSIONINFOA osvi;
                   2423:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
                   2424:        
                   2425:        if(GetVersionExA((LPOSVERSIONINFOA)&osvi)) {
                   2426:                if(osvi.dwPlatformId != VER_PLATFORM_WIN32_NT) {
                   2427:                        return(false);
                   2428:                } else if(osvi.dwMajorVersion > dwMajorVersion) {
                   2429:                        return(true);
                   2430:                } else if(osvi.dwMajorVersion < dwMajorVersion) {
                   2431:                        return(false);
                   2432:                } else if(osvi.dwMinorVersion > dwMinorVersion) {
                   2433:                        return(true);
                   2434:                } else if(osvi.dwMinorVersion < dwMinorVersion) {
                   2435:                        return(false);
                   2436:                }
                   2437:                // FIXME: check wServicePackMajor and wServicePackMinor :-(
                   2438:                return(true);
                   2439:        }
                   2440:        return(false);
1.1.1.14  root     2441: }
                   2442: 
1.1.1.61  root     2443: HWND get_console_window_handle()
1.1.1.58  root     2444: {
1.1.1.61  root     2445:        static HWND hwndFound = 0;
                   2446:        
                   2447:        if(hwndFound == 0) {
                   2448:                // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
                   2449:                char pszNewWindowTitle[1024];
                   2450:                char pszOldWindowTitle[1024];
                   2451:                
                   2452:                GetConsoleTitleA(pszOldWindowTitle, 1024);
                   2453:                wsprintfA(pszNewWindowTitle, "%d/%d", GetTickCount(), GetCurrentProcessId());
                   2454:                SetConsoleTitleA(pszNewWindowTitle);
                   2455:                Sleep(100);
                   2456:                hwndFound = FindWindowA(NULL, pszNewWindowTitle);
                   2457:                SetConsoleTitleA(pszOldWindowTitle);
                   2458:        }
                   2459:        return hwndFound;
                   2460: }
                   2461: 
                   2462: HDC get_console_window_device_context()
                   2463: {
                   2464:        return GetDC(get_console_window_handle());
                   2465: }
                   2466: 
                   2467: bool get_console_font_size(int *width, int *height)
                   2468: {
                   2469:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.58  root     2470:        bool result = false;
                   2471:        
1.1.1.62! root     2472:        if(is_winxp_or_later) {
        !          2473:                HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
        !          2474:                if(hLibrary) {
        !          2475:                        typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
        !          2476:                        GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
        !          2477:                        if(lpfnGetCurrentConsoleFont) { // Windows XP or later
        !          2478:                                CONSOLE_FONT_INFO fi;
        !          2479:                                if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi) != 0) {
        !          2480:                                        *width  = fi.dwFontSize.X;
        !          2481:                                        *height = fi.dwFontSize.Y;
        !          2482:                                        result = true;
        !          2483:                                }
1.1.1.58  root     2484:                        }
1.1.1.62! root     2485:                        FreeLibrary(hLibrary);
        !          2486:                }
        !          2487:        } else {
        !          2488:                CONSOLE_SCREEN_BUFFER_INFO csbi;
        !          2489:                RECT rect;
        !          2490:                if(GetConsoleScreenBufferInfo(hStdout, &csbi) && GetClientRect(get_console_window_handle(), &rect)) {
        !          2491:                        int cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
        !          2492:                        int rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
        !          2493:                        *width  = rect.right / cols;
        !          2494:                        *height = rect.bottom / rows;
        !          2495:                        result = true;
1.1.1.58  root     2496:                }
                   2497:        }
                   2498:        return(result);
                   2499: }
                   2500: 
1.1.1.61  root     2501: bool set_console_font_size(int width, int height)
1.1.1.56  root     2502: {
                   2503:        // http://d.hatena.ne.jp/aharisu/20090427/1240852598
1.1.1.61  root     2504:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.56  root     2505:        bool result = false;
1.1.1.60  root     2506:        HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.56  root     2507:        
                   2508:        if(hLibrary) {
1.1.1.62! root     2509:                CONSOLE_SCREEN_BUFFER_INFO csbi;
        !          2510:                RECT rect;
        !          2511:                GetConsoleScreenBufferInfo(hStdout, &csbi);
        !          2512:                int cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
        !          2513:                int rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
        !          2514:                
1.1.1.56  root     2515:                typedef BOOL (WINAPI* GetConsoleFontInfoFunction)(HANDLE, BOOL, DWORD, PCONSOLE_FONT_INFO);
                   2516:                typedef DWORD (WINAPI* GetNumberOfConsoleFontsFunction)(VOID);
1.1.1.60  root     2517:                typedef COORD (WINAPI* GetConsoleFontSizeFunction)(HANDLE, DWORD);
1.1.1.56  root     2518:                typedef BOOL (WINAPI* SetConsoleFontFunction)(HANDLE, DWORD);
                   2519:                typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
1.1.1.61  root     2520:                typedef BOOL (WINAPI* GetCurrentConsoleFontExFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFOEX);
                   2521:                typedef BOOL (WINAPI* SetCurrentConsoleFontExFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFOEX);
1.1.1.56  root     2522:                
                   2523:                GetConsoleFontInfoFunction lpfnGetConsoleFontInfo = reinterpret_cast<GetConsoleFontInfoFunction>(::GetProcAddress(hLibrary, "GetConsoleFontInfo"));
                   2524:                GetNumberOfConsoleFontsFunction lpfnGetNumberOfConsoleFonts = reinterpret_cast<GetNumberOfConsoleFontsFunction>(::GetProcAddress(hLibrary, "GetNumberOfConsoleFonts"));
1.1.1.60  root     2525:                GetConsoleFontSizeFunction lpfnGetConsoleFontSize = reinterpret_cast<GetConsoleFontSizeFunction>(::GetProcAddress(hLibrary, "GetConsoleFontSize"));
1.1.1.56  root     2526:                SetConsoleFontFunction lpfnSetConsoleFont = reinterpret_cast<SetConsoleFontFunction>(::GetProcAddress(hLibrary, "SetConsoleFont"));
                   2527:                GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
1.1.1.61  root     2528:                GetCurrentConsoleFontExFunction lpfnGetCurrentConsoleFontEx = reinterpret_cast<GetCurrentConsoleFontExFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFontEx"));
                   2529:                SetCurrentConsoleFontExFunction lpfnSetCurrentConsoleFontEx = reinterpret_cast<SetCurrentConsoleFontExFunction>(::GetProcAddress(hLibrary, "SetCurrentConsoleFontEx"));
1.1.1.56  root     2530:                
1.1.1.62! root     2531:                if(lpfnGetConsoleFontInfo && lpfnGetNumberOfConsoleFonts && lpfnSetConsoleFont) { // Windows 2000 or later
1.1.1.56  root     2532:                        DWORD dwFontNum = lpfnGetNumberOfConsoleFonts();
1.1.1.61  root     2533:                        if(dwFontNum) {
                   2534:                                CONSOLE_FONT_INFO* fonts = (CONSOLE_FONT_INFO*)malloc(sizeof(CONSOLE_FONT_INFO) * dwFontNum);
                   2535:                                lpfnGetConsoleFontInfo(hStdout, FALSE, dwFontNum, fonts);
                   2536:                                for(int i = 0; i < dwFontNum; i++) {
                   2537:                                        fonts[i].dwFontSize = lpfnGetConsoleFontSize(hStdout, fonts[i].nFont);
                   2538:                                        if(fonts[i].dwFontSize.X == width && fonts[i].dwFontSize.Y == height) {
1.1.1.62! root     2539:                                                if(lpfnSetConsoleFont(hStdout, fonts[i].nFont)) {
        !          2540:                                                        if(is_winxp_or_later && lpfnGetCurrentConsoleFont) { // Windows XP or later
        !          2541:                                                                CONSOLE_FONT_INFO fi;
        !          2542:                                                                if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi)) {
        !          2543:                                                                        if(fonts[i].dwFontSize.X == fi.dwFontSize.X && fonts[i].dwFontSize.Y == fi.dwFontSize.Y) {
        !          2544:                                                                                result = true;
        !          2545:                                                                                break;
        !          2546:                                                                        }
        !          2547:                                                                }
        !          2548:                                                        } else {
        !          2549:                                                                Sleep(10);
        !          2550:                                                                if(GetClientRect(get_console_window_handle(), &rect)) {
        !          2551:                                                                        if(fonts[i].dwFontSize.X * cols == rect.right && fonts[i].dwFontSize.Y * rows == rect.bottom) {
        !          2552:                                                                                result = true;
        !          2553:                                                                                break;
        !          2554:                                                                        }
        !          2555:                                                                }
1.1.1.58  root     2556:                                                        }
                   2557:                                                }
1.1.1.61  root     2558:                                        }
                   2559:                                }
                   2560:                                free(fonts);
                   2561:                        } else if(lpfnGetCurrentConsoleFontEx && lpfnSetCurrentConsoleFontEx) {
                   2562:                                // for Windows10 enhanced command prompt
                   2563:                                CONSOLE_FONT_INFOEX fi_old, fi_new;
                   2564:                                fi_old.cbSize = sizeof(CONSOLE_FONT_INFOEX);
                   2565:                                if(lpfnGetCurrentConsoleFontEx(hStdout, FALSE, &fi_old)) {
                   2566:                                        fi_new = fi_old;
                   2567:                                        fi_new.dwFontSize.X = width;
                   2568:                                        fi_new.dwFontSize.Y = height;
                   2569:                                        if(lpfnSetCurrentConsoleFontEx(hStdout, FALSE, &fi_new)) {
                   2570:                                                lpfnGetCurrentConsoleFontEx(hStdout, FALSE, &fi_new);
                   2571:                                                if(fi_new.dwFontSize.X == width && fi_new.dwFontSize.Y == height) {
                   2572:                                                        result = true;
                   2573:                                                } else {
                   2574:                                                        lpfnSetCurrentConsoleFontEx(hStdout, FALSE, &fi_old);
1.1.1.58  root     2575:                                                }
                   2576:                                        }
1.1.1.57  root     2577:                                }
1.1.1.56  root     2578:                        }
                   2579:                }
                   2580:                FreeLibrary(hLibrary);
                   2581:        }
                   2582:        return(result);
                   2583: }
                   2584: 
1.1.1.59  root     2585: bool is_cursor_blink_off()
                   2586: {
                   2587:        static int result = -1;
                   2588:        HKEY hKey;
                   2589:        char chData[64];
                   2590:        DWORD dwSize = sizeof(chData);
                   2591:        
                   2592:        if(result == -1) {
                   2593:                result = 0;
1.1.1.60  root     2594:                if(RegOpenKeyExA(HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   2595:                        if(RegQueryValueExA(hKey, "CursorBlinkRate", NULL, NULL, (LPBYTE)chData, &dwSize) == ERROR_SUCCESS) {
1.1.1.59  root     2596:                                if(strncmp(chData, "-1", 2) == 0) {
                   2597:                                        result = 1;
                   2598:                                }
                   2599:                        }
                   2600:                        RegCloseKey(hKey);
                   2601:                }
                   2602:        }
                   2603:        return(result != 0);
                   2604: }
                   2605: 
1.1.1.27  root     2606: void get_sio_port_numbers()
                   2607: {
                   2608:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                   2609:        HDEVINFO hDevInfo = 0;
                   2610:        HKEY hKey = 0;
1.1.1.60  root     2611:        if((hDevInfo = SetupDiGetClassDevsA(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
1.1.1.27  root     2612:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                   2613:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                   2614:                                char chData[256];
                   2615:                                DWORD dwType = 0;
                   2616:                                DWORD dwSize = sizeof(chData);
                   2617:                                int port_number = 0;
                   2618:                                
1.1.1.60  root     2619:                                if(RegQueryValueExA(hKey, "PortName", NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
1.1.1.27  root     2620:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                   2621:                                                port_number = atoi(chData + 3);
                   2622:                                        }
                   2623:                                }
                   2624:                                RegCloseKey(hKey);
                   2625:                                
1.1.1.29  root     2626:                                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     2627:                                        continue;
                   2628:                                }
                   2629:                                if(sio_port_number[0] == 0) {
                   2630:                                        sio_port_number[0] = port_number;
                   2631:                                } else if(sio_port_number[1] == 0) {
                   2632:                                        sio_port_number[1] = port_number;
1.1.1.29  root     2633:                                } else if(sio_port_number[2] == 0) {
                   2634:                                        sio_port_number[2] = port_number;
                   2635:                                } else if(sio_port_number[3] == 0) {
                   2636:                                        sio_port_number[3] = port_number;
1.1.1.27  root     2637:                                }
1.1.1.29  root     2638:                                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     2639:                                        break;
                   2640:                                }
                   2641:                        }
                   2642:                }
                   2643:        }
                   2644: }
                   2645: 
1.1.1.28  root     2646: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                   2647: 
1.1       root     2648: int main(int argc, char *argv[], char *envp[])
                   2649: {
1.1.1.9   root     2650:        int arg_offset = 0;
                   2651:        int standard_env = 0;
1.1.1.14  root     2652:        int buf_width = 0, buf_height = 0;
1.1.1.28  root     2653:        bool get_console_info_success = false;
1.1.1.56  root     2654:        bool get_console_font_success = false;
1.1.1.28  root     2655:        bool screen_size_changed = false;
                   2656:        
1.1.1.60  root     2657:        char path[MAX_PATH], full[MAX_PATH], *name = NULL;
                   2658:        GetModuleFileNameA(NULL, path, MAX_PATH);
                   2659:        GetFullPathNameA(path, MAX_PATH, full, &name);
1.1       root     2660:        
1.1.1.27  root     2661:        char dummy_argv_0[] = "msdos.exe";
                   2662:        char dummy_argv_1[MAX_PATH];
                   2663:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                   2664:        char new_exec_file[MAX_PATH];
                   2665:        bool convert_cmd_file = false;
1.1.1.28  root     2666:        unsigned int code_page = 0;
1.1.1.27  root     2667:        
                   2668:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root     2669:                // check if command file is embedded to this execution file
                   2670:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root     2671:                FILE* fp = fopen(full, "rb");
1.1.1.28  root     2672:                long offset = get_section_in_exec_file(fp, ".msdos");
                   2673:                if(offset != 0) {
1.1.1.30  root     2674:                        UINT8 buffer[16];
1.1.1.28  root     2675:                        fseek(fp, offset, SEEK_SET);
                   2676:                        fread(buffer, sizeof(buffer), 1, fp);
                   2677:                        
                   2678:                        // restore flags
                   2679:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                   2680:                        no_windows          = ((buffer[0] & 0x02) != 0);
                   2681:                        standard_env        = ((buffer[0] & 0x04) != 0);
                   2682:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                   2683:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                   2684:                        if((buffer[0] & 0x20) != 0) {
                   2685:                                get_sio_port_numbers();
                   2686:                        }
                   2687:                        if((buffer[0] & 0x40) != 0) {
                   2688:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                   2689:                                support_ems = true;
1.1.1.30  root     2690:                        }
1.1.1.27  root     2691: #ifdef SUPPORT_XMS
1.1.1.30  root     2692:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root     2693:                                support_xms = true;
                   2694:                        }
1.1.1.30  root     2695: #endif
1.1.1.28  root     2696:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                   2697:                                buf_width  = buffer[1] | (buffer[2] << 8);
                   2698:                                buf_height = buffer[3] | (buffer[4] << 8);
                   2699:                        }
                   2700:                        if(buffer[5] != 0) {
1.1.1.30  root     2701:                                dos_major_version = buffer[5];
                   2702:                                dos_minor_version = buffer[6];
                   2703:                        }
                   2704:                        if(buffer[7] != 0) {
                   2705:                                win_major_version = buffer[7];
                   2706:                                win_minor_version = buffer[8];
1.1.1.28  root     2707:                        }
1.1.1.30  root     2708:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root     2709:                                SetConsoleCP(code_page);
                   2710:                                SetConsoleOutputCP(code_page);
                   2711:                        }
1.1.1.30  root     2712:                        int name_len = buffer[11];
                   2713:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root     2714:                        
                   2715:                        // restore command file name
                   2716:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                   2717:                        fread(dummy_argv_1, name_len, 1, fp);
                   2718:                        
                   2719:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                   2720:                                // if original command file exists, create a temporary file name
1.1.1.60  root     2721:                                if(GetTempFileNameA(".", "DOS", 0, dummy_argv_1) != 0) {
1.1.1.28  root     2722:                                        // create a temporary command file in the current director
1.1.1.60  root     2723:                                        DeleteFileA(dummy_argv_1);
1.1.1.27  root     2724:                                } else {
1.1.1.28  root     2725:                                        // create a temporary command file in the temporary folder
1.1.1.60  root     2726:                                        GetTempPathA(MAX_PATH, path);
                   2727:                                        if(GetTempFileNameA(path, "DOS", 0, dummy_argv_1) != 0) {
                   2728:                                                DeleteFileA(dummy_argv_1);
1.1.1.28  root     2729:                                        } else {
                   2730:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                   2731:                                        }
1.1.1.27  root     2732:                                }
1.1.1.28  root     2733:                                // check the command file type
                   2734:                                fread(buffer, 2, 1, fp);
                   2735:                                fseek(fp, -2, SEEK_CUR);
                   2736:                                if(memcmp(buffer, "MZ", 2) != 0) {
                   2737:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                   2738:                                } else {
                   2739:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root     2740:                                }
                   2741:                        }
1.1.1.28  root     2742:                        
                   2743:                        // restore command file
                   2744:                        FILE* fo = fopen(dummy_argv_1, "wb");
                   2745:                        for(int i = 0; i < file_len; i++) {
                   2746:                                fputc(fgetc(fp), fo);
                   2747:                        }
                   2748:                        fclose(fo);
                   2749:                        
1.1.1.60  root     2750:                        GetFullPathNameA(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
1.1.1.28  root     2751:                        temp_file_created = true;
1.1.1.60  root     2752:                        SetFileAttributesA(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
1.1.1.28  root     2753:                        
                   2754:                        // adjust argc/argv
                   2755:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                   2756:                                dummy_argv[i + 1] = argv[i];
                   2757:                        }
                   2758:                        argc++;
                   2759:                        argv = dummy_argv;
1.1.1.27  root     2760:                }
                   2761:                fclose(fp);
                   2762:        }
1.1.1.9   root     2763:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     2764:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   2765:                        stay_busy = true;
                   2766:                        arg_offset++;
1.1.1.27  root     2767:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   2768:                        if(argv[i][2] != '\0') {
                   2769:                                strcpy(new_exec_file, &argv[i][2]);
                   2770:                        } else {
                   2771:                                strcpy(new_exec_file, "new_exec_file.exe");
                   2772:                        }
                   2773:                        convert_cmd_file = true;
                   2774:                        arg_offset++;
1.1.1.28  root     2775:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   2776:                        if(IS_NUMERIC(argv[i][2])) {
                   2777:                                code_page = atoi(&argv[i][2]);
                   2778:                        } else {
                   2779:                                code_page = GetConsoleCP();
                   2780:                        }
                   2781:                        arg_offset++;
1.1.1.25  root     2782:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   2783:                        no_windows = true;
                   2784:                        arg_offset++;
                   2785:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     2786:                        standard_env = 1;
                   2787:                        arg_offset++;
1.1.1.14  root     2788:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   2789:                        ignore_illegal_insn = true;
                   2790:                        arg_offset++;
                   2791:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   2792:                        limit_max_memory = true;
                   2793:                        arg_offset++;
                   2794:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.51  root     2795:                        int result = sscanf(argv[i] + 2, "%d,%d", &buf_height, &buf_width);
                   2796:                        if(result == 1) {
                   2797:                                buf_width = 0;
                   2798:                        } else if(result != 2) {
1.1.1.17  root     2799:                                buf_width = buf_height = 0;
                   2800:                        }
                   2801:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   2802:                                buf_width = 80;
                   2803:                        }
                   2804:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   2805:                                buf_height = 25;
                   2806:                        }
1.1.1.14  root     2807:                        arg_offset++;
1.1.1.25  root     2808:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     2809:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     2810:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   2811:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   2812:                                        sio_port_number[1] = atoi(p1 + 1);
                   2813:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   2814:                                                sio_port_number[2] = atoi(p2 + 1);
                   2815:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   2816:                                                        sio_port_number[3] = atoi(p3 + 1);
                   2817:                                                }
                   2818:                                        }
1.1.1.25  root     2819:                                }
1.1.1.29  root     2820:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     2821:                        }
1.1.1.29  root     2822:                        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     2823:                                get_sio_port_numbers();
1.1.1.25  root     2824:                        }
                   2825:                        arg_offset++;
1.1.1.9   root     2826:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     2827:                        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     2828:                                dos_major_version = argv[i][2] - '0';
                   2829:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   2830:                        }
                   2831:                        arg_offset++;
                   2832:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   2833:                        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]))) {
                   2834:                                win_major_version = argv[i][2] - '0';
                   2835:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     2836:                        }
                   2837:                        arg_offset++;
1.1.1.25  root     2838:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   2839:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   2840:                        support_ems = true;
                   2841: #ifdef SUPPORT_XMS
                   2842:                        support_xms = true;
                   2843: #endif
                   2844:                        arg_offset++;
1.1.1.9   root     2845:                } else {
                   2846:                        break;
                   2847:                }
                   2848:        }
                   2849:        if(argc < 2 + arg_offset) {
1.1       root     2850: #ifdef _WIN64
1.1.1.14  root     2851:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     2852: #else
1.1.1.14  root     2853:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     2854: #endif
1.1.1.25  root     2855:                fprintf(stderr,
1.1.1.28  root     2856:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     2857:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     2858:                        "\n"
                   2859:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     2860: #ifdef _WIN64
1.1.1.27  root     2861:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     2862: #else
1.1.1.27  root     2863:                        "\t-c\tconvert command file to 32bit execution file\n"
                   2864: #endif
1.1.1.28  root     2865:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     2866:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   2867:                        "\t-e\tuse a reduced environment block\n"
                   2868:                        "\t-i\tignore invalid instructions\n"
                   2869:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   2870:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   2871:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   2872:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     2873:                        "\t-w\tset the Windows version\n"
1.1.1.19  root     2874: #ifdef SUPPORT_XMS
1.1.1.28  root     2875:                        "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19  root     2876: #else
1.1.1.28  root     2877:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2878: #endif
                   2879:                );
1.1.1.10  root     2880:                
                   2881:                if(!is_started_from_command_prompt()) {
                   2882:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2883:                        while(!_kbhit()) {
                   2884:                                Sleep(10);
                   2885:                        }
                   2886:                }
1.1.1.20  root     2887: #ifdef _DEBUG
                   2888:                _CrtDumpMemoryLeaks();
                   2889: #endif
1.1       root     2890:                return(EXIT_FAILURE);
                   2891:        }
1.1.1.27  root     2892:        if(convert_cmd_file) {
                   2893:                retval = EXIT_FAILURE;
1.1.1.28  root     2894:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2895:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2896:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2897:                        
1.1.1.28  root     2898:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2899:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2900:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2901:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2902:                        } else {
1.1.1.28  root     2903:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2904:                                if(offset != 0) {
                   2905:                                        UINT8 buffer[14];
                   2906:                                        fseek(fp, offset, SEEK_SET);
                   2907:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2908:                                        memset(path, 0, sizeof(path));
                   2909:                                        fread(path, buffer[9], 1, fp);
                   2910:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2911:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2912:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2913:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2914:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2915:                                } else {
                   2916:                                        // read pe header of msdos.exe
                   2917:                                        UINT8 header[0x400];
                   2918:                                        fseek(fp, 0, SEEK_SET);
                   2919:                                        fread(header, sizeof(header), 1, fp);
                   2920:                                        
                   2921:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2922:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2923:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2924:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2925:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2926:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2927:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2928:                                        
                   2929:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2930:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2931:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2932:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2933:                                        if(dwExtraLastSectionBytes != 0) {
                   2934:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2935:                                                dwLastSectionSize += dwRemain;
                   2936:                                        }
                   2937:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2938:                                        
                   2939:                                        // store msdos.exe
                   2940:                                        fseek(fp, 0, SEEK_SET);
                   2941:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2942:                                                if((data = fgetc(fp)) != EOF) {
                   2943:                                                        fputc(data, fo);
                   2944:                                                } else {
                   2945:                                                        // we should not reach here :-(
                   2946:                                                        fputc(0, fo);
                   2947:                                                }
                   2948:                                        }
                   2949:                                        
                   2950:                                        // store options
                   2951:                                        UINT8 flags = 0;
                   2952:                                        if(stay_busy) {
                   2953:                                                flags |= 0x01;
                   2954:                                        }
                   2955:                                        if(no_windows) {
                   2956:                                                flags |= 0x02;
                   2957:                                        }
                   2958:                                        if(standard_env) {
                   2959:                                                flags |= 0x04;
                   2960:                                        }
                   2961:                                        if(ignore_illegal_insn) {
                   2962:                                                flags |= 0x08;
                   2963:                                        }
                   2964:                                        if(limit_max_memory) {
                   2965:                                                flags |= 0x10;
                   2966:                                        }
1.1.1.29  root     2967:                                        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     2968:                                                flags |= 0x20;
                   2969:                                        }
                   2970:                                        if(support_ems) {
                   2971:                                                flags |= 0x40;
                   2972:                                        }
1.1.1.30  root     2973: #ifdef SUPPORT_XMS
                   2974:                                        if(support_xms) {
                   2975:                                                flags |= 0x80;
                   2976:                                        }
                   2977: #endif
1.1.1.28  root     2978:                                        
                   2979:                                        fputc(flags, fo);
                   2980:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2981:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2982:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2983:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2984:                                        fputc(dos_major_version, fo);
                   2985:                                        fputc(dos_minor_version, fo);
                   2986:                                        fputc(win_major_version, fo);
                   2987:                                        fputc(win_minor_version, fo);
1.1.1.28  root     2988:                                        fputc((code_page >> 0) & 0xff, fo);
                   2989:                                        fputc((code_page >> 8) & 0xff, fo);
                   2990:                                        
                   2991:                                        // store command file info
1.1.1.60  root     2992:                                        GetFullPathNameA(argv[arg_offset + 1], MAX_PATH, full, &name);
1.1.1.28  root     2993:                                        int name_len = strlen(name);
                   2994:                                        fseek(fs, 0, SEEK_END);
                   2995:                                        long file_size = ftell(fs);
                   2996:                                        
                   2997:                                        fputc(name_len, fo);
                   2998:                                        fputc((file_size >>  0) & 0xff, fo);
                   2999:                                        fputc((file_size >>  8) & 0xff, fo);
                   3000:                                        fputc((file_size >> 16) & 0xff, fo);
                   3001:                                        fputc((file_size >> 24) & 0xff, fo);
                   3002:                                        fwrite(name, name_len, 1, fo);
                   3003:                                        
                   3004:                                        // store command file
                   3005:                                        fseek(fs, 0, SEEK_SET);
                   3006:                                        for(int i = 0; i < file_size; i++) {
                   3007:                                                if((data = fgetc(fs)) != EOF) {
                   3008:                                                        fputc(data, fo);
                   3009:                                                } else {
                   3010:                                                        // we should not reach here :-(
                   3011:                                                        fputc(0, fo);
                   3012:                                                }
                   3013:                                        }
                   3014:                                        
                   3015:                                        // store padding data and update pe header
1.1.1.29  root     3016:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   3017:                                        coffHeader->NumberOfSections++;
                   3018:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   3019:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   3020:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   3021:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   3022:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     3023:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   3024:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   3025:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     3026:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     3027:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   3028:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     3029:                                                        if(i < 2) {
                   3030:                                                                fputc(padding[i & 15], fo);
                   3031:                                                        } else {
                   3032:                                                                fputc(padding[(i - 2) & 15], fo);
                   3033:                                                        }
1.1.1.28  root     3034:                                                }
                   3035:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   3036:                                        }
                   3037:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   3038:                                        
                   3039:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   3040:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   3041:                                        if(dwExtraNewSectionBytes != 0) {
                   3042:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   3043:                                                dwNewSectionSize += dwRemain;
                   3044:                                        }
                   3045:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   3046:                                        
                   3047:                                        fseek(fo, 0, SEEK_SET);
                   3048:                                        fwrite(header, sizeof(header), 1, fo);
                   3049:                                        
                   3050:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   3051:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     3052:                                }
                   3053:                        }
                   3054:                        if(fp != NULL) {
                   3055:                                fclose(fp);
                   3056:                        }
                   3057:                        if(fs != NULL) {
                   3058:                                fclose(fs);
                   3059:                        }
                   3060:                        if(fo != NULL) {
                   3061:                                fclose(fo);
                   3062:                        }
                   3063:                }
                   3064: #ifdef _DEBUG
                   3065:                _CrtDumpMemoryLeaks();
                   3066: #endif
                   3067:                return(retval);
                   3068:        }
1.1       root     3069:        
1.1.1.62! root     3070:        is_winxp_or_later = is_greater_windows_version(5, 1, 0, 0);
1.1.1.54  root     3071:        is_xp_64_or_later = is_greater_windows_version(5, 2, 0, 0);
1.1.1.14  root     3072:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   3073:        
1.1.1.23  root     3074:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     3075:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     3076:        CONSOLE_CURSOR_INFO ci;
1.1.1.61  root     3077:        UINT input_cp = GetConsoleCP();
                   3078:        UINT output_cp = GetConsoleOutputCP();
                   3079:        int multibyte_cp = _getmbcp();
1.1.1.23  root     3080:        
1.1.1.28  root     3081:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     3082:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.59  root     3083:        ci_old = ci_new = ci;
1.1.1.24  root     3084:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1.1.61  root     3085:        get_console_font_success = get_console_font_size(&font_width, &font_height);
1.1       root     3086:        
1.1.1.14  root     3087:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   3088:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   3089:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3090:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     3091:                }
                   3092:        }
1.1.1.28  root     3093:        if(get_console_info_success) {
1.1.1.12  root     3094:                scr_width = csbi.dwSize.X;
1.1.1.14  root     3095:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   3096:                
1.1.1.28  root     3097:                // v-text shadow buffer size must be lesser than 0x7fd0
                   3098:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     3099:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   3100:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   3101:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     3102:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     3103:                                scr_width = 80;
                   3104:                                scr_height = 25;
                   3105:                        }
1.1.1.28  root     3106:                        screen_size_changed = true;
1.1.1.14  root     3107:                }
1.1.1.12  root     3108:        } else {
                   3109:                // for a proof (not a console)
                   3110:                scr_width = 80;
                   3111:                scr_height = 25;
                   3112:        }
1.1.1.14  root     3113:        scr_buf_size.X = scr_width;
                   3114:        scr_buf_size.Y = scr_height;
                   3115:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   3116:        scr_top = csbi.srWindow.Top;
1.1       root     3117:        cursor_moved = false;
1.1.1.59  root     3118:        cursor_moved_by_crtc = false;
1.1       root     3119:        
1.1.1.54  root     3120:        SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
                   3121:        
1.1.1.35  root     3122: #ifdef USE_SERVICE_THREAD
                   3123:        InitializeCriticalSection(&input_crit_sect);
                   3124:        InitializeCriticalSection(&key_buf_crit_sect);
                   3125:        InitializeCriticalSection(&putch_crit_sect);
1.1.1.50  root     3126:        main_thread_id = GetCurrentThreadId();
1.1.1.35  root     3127: #endif
1.1.1.50  root     3128:        
1.1.1.25  root     3129:        key_buf_char = new FIFO(256);
                   3130:        key_buf_scan = new FIFO(256);
1.1.1.57  root     3131:        key_buf_data = new FIFO(256);
1.1       root     3132:        
                   3133:        hardware_init();
                   3134:        
1.1.1.33  root     3135: #ifdef USE_DEBUGGER
                   3136:        debugger_init();
                   3137: #endif
                   3138:        
1.1.1.9   root     3139:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     3140:                retval = EXIT_FAILURE;
                   3141:        } else {
1.1.1.27  root     3142: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     3143:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   3144: #endif
                   3145:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   3146:                
1.1.1.28  root     3147:                if(screen_size_changed) {
1.1.1.24  root     3148:                        change_console_size(scr_width, scr_height);
                   3149:                }
1.1.1.8   root     3150:                TIMECAPS caps;
                   3151:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   3152:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.35  root     3153: #ifdef USE_VRAM_THREAD
1.1.1.14  root     3154:                InitializeCriticalSection(&vram_crit_sect);
                   3155:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   3156: #endif
1.1.1.33  root     3157: #ifdef USE_DEBUGGER
                   3158:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   3159:                // wait until telnet client starts and connects to me
                   3160:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   3161:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   3162:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   3163:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   3164:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   3165:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   3166:                                Sleep(100);
                   3167:                        }
                   3168:                }
                   3169: #endif
1.1       root     3170:                hardware_run();
1.1.1.35  root     3171: #ifdef USE_VRAM_THREAD
1.1.1.14  root     3172:                vram_flush();
                   3173:                DeleteCriticalSection(&vram_crit_sect);
                   3174: #endif
1.1.1.24  root     3175:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     3176:                
1.1.1.24  root     3177:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.61  root     3178:                hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   3179:                
                   3180:                // restore console settings
                   3181:                _setmbcp(multibyte_cp);
                   3182:                SetConsoleCP(input_cp);
                   3183:                SetConsoleOutputCP(multibyte_cp);
                   3184:                
1.1.1.28  root     3185:                if(get_console_info_success) {
1.1.1.12  root     3186:                        if(restore_console_on_exit) {
1.1.1.14  root     3187:                                // window can't be bigger than buffer,
                   3188:                                // buffer can't be smaller than window,
                   3189:                                // so make a tiny window,
                   3190:                                // set the required buffer,
                   3191:                                // then set the required window
1.1.1.61  root     3192:                                CONSOLE_SCREEN_BUFFER_INFO cur_csbi;
1.1.1.14  root     3193:                                SMALL_RECT rect;
1.1.1.61  root     3194:                                GetConsoleScreenBufferInfo(hStdout, &cur_csbi);
                   3195:                                int min_width  = min(cur_csbi.srWindow.Right - cur_csbi.srWindow.Left + 1, csbi.srWindow.Right - csbi.srWindow.Left + 1);
                   3196:                                int min_height = min(cur_csbi.srWindow.Bottom - cur_csbi.srWindow.Top + 1, csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
                   3197:                                
                   3198:                                SET_RECT(rect, 0, cur_csbi.srWindow.Top, min_width - 1, cur_csbi.srWindow.Top + min_height - 1);
1.1.1.14  root     3199:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     3200:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     3201:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.61  root     3202:                                if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
                   3203:                                        SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
                   3204:                                        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3205:                                }
                   3206:                        }
                   3207:                }
                   3208:                if(get_console_font_success) {
                   3209:                        set_console_font_size(font_width, font_height);
                   3210:                }
                   3211:                if(get_console_info_success) {
                   3212:                        if(restore_console_on_exit) {
                   3213:                                SMALL_RECT rect;
                   3214:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
                   3215:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
                   3216:                                if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
                   3217:                                        SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
                   3218:                                        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3219:                                }
1.1.1.12  root     3220:                        }
1.1.1.14  root     3221:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   3222:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     3223:                }
1.1.1.24  root     3224:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   3225:                
1.1       root     3226:                msdos_finish();
1.1.1.14  root     3227:                
                   3228:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     3229:        }
1.1.1.35  root     3230:        if(temp_file_created) {
1.1.1.60  root     3231:                DeleteFileA(temp_file_path);
1.1.1.35  root     3232:                temp_file_created = false;
                   3233:        }
1.1.1.10  root     3234:        hardware_finish();
                   3235:        
1.1.1.28  root     3236:        if(key_buf_char != NULL) {
                   3237:                key_buf_char->release();
                   3238:                delete key_buf_char;
                   3239:                key_buf_char = NULL;
                   3240:        }
                   3241:        if(key_buf_scan != NULL) {
                   3242:                key_buf_scan->release();
                   3243:                delete key_buf_scan;
                   3244:                key_buf_scan = NULL;
                   3245:        }
1.1.1.57  root     3246:        if(key_buf_data != NULL) {
                   3247:                key_buf_data->release();
                   3248:                delete key_buf_data;
                   3249:                key_buf_data = NULL;
                   3250:        }
1.1.1.35  root     3251: #ifdef USE_SERVICE_THREAD
                   3252:        DeleteCriticalSection(&input_crit_sect);
                   3253:        DeleteCriticalSection(&key_buf_crit_sect);
                   3254:        DeleteCriticalSection(&putch_crit_sect);
                   3255: #endif
1.1.1.20  root     3256: #ifdef _DEBUG
                   3257:        _CrtDumpMemoryLeaks();
                   3258: #endif
1.1       root     3259:        return(retval);
                   3260: }
                   3261: 
1.1.1.20  root     3262: /* ----------------------------------------------------------------------------
                   3263:        console
                   3264: ---------------------------------------------------------------------------- */
                   3265: 
1.1.1.14  root     3266: void change_console_size(int width, int height)
1.1.1.12  root     3267: {
1.1.1.23  root     3268:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     3269:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   3270:        SMALL_RECT rect;
                   3271:        COORD co;
                   3272:        
                   3273:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     3274:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   3275:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
1.1.1.60  root     3276:                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
1.1.1.14  root     3277:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.60  root     3278:                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     3279:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   3280:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
1.1.1.60  root     3281:                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     3282:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.60  root     3283:                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     3284:                }
                   3285:        }
1.1.1.14  root     3286:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     3287:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     3288:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     3289:                SetConsoleCursorPosition(hStdout, co);
                   3290:                cursor_moved = true;
1.1.1.59  root     3291:                cursor_moved_by_crtc = false;
1.1.1.12  root     3292:        }
1.1.1.14  root     3293:        
                   3294:        // window can't be bigger than buffer,
                   3295:        // buffer can't be smaller than window,
                   3296:        // so make a tiny window,
                   3297:        // set the required buffer,
                   3298:        // then set the required window
1.1.1.61  root     3299:        int min_width  = min(csbi.srWindow.Right - csbi.srWindow.Left + 1, width);
                   3300:        int min_height = min(csbi.srWindow.Bottom - csbi.srWindow.Top + 1, height);
                   3301:        
                   3302:        SET_RECT(rect, 0, csbi.srWindow.Top, min_width - 1, csbi.srWindow.Top + min_height - 1);
1.1.1.12  root     3303:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     3304:        co.X = width;
                   3305:        co.Y = height;
1.1.1.12  root     3306:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     3307:        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.61  root     3308:        if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
                   3309:                SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
                   3310:                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3311:        }
1.1.1.14  root     3312:        
                   3313:        scr_width = scr_buf_size.X = width;
                   3314:        scr_height = scr_buf_size.Y = height;
                   3315:        scr_top = 0;
                   3316:        
                   3317:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   3318:        
                   3319:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     3320:        text_vram_end_address = text_vram_top_address + regen;
                   3321:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   3322:        
1.1.1.14  root     3323:        if(regen > 0x4000) {
                   3324:                regen = 0x8000;
                   3325:                vram_pages = 1;
                   3326:        } else if(regen > 0x2000) {
                   3327:                regen = 0x4000;
                   3328:                vram_pages = 2;
                   3329:        } else if(regen > 0x1000) {
                   3330:                regen = 0x2000;
                   3331:                vram_pages = 4;
                   3332:        } else {
                   3333:                regen = 0x1000;
                   3334:                vram_pages = 8;
                   3335:        }
1.1.1.15  root     3336:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   3337:        *(UINT16 *)(mem + 0x44c) = regen;
                   3338:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   3339:        
1.1.1.24  root     3340:        mouse.min_position.x = 0;
                   3341:        mouse.min_position.y = 0;
1.1.1.34  root     3342:        mouse.max_position.x = 8 * (scr_width  - 1);
                   3343:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     3344:        
1.1.1.15  root     3345:        restore_console_on_exit = true;
1.1.1.14  root     3346: }
                   3347: 
                   3348: void clear_scr_buffer(WORD attr)
                   3349: {
                   3350:        for(int y = 0; y < scr_height; y++) {
                   3351:                for(int x = 0; x < scr_width; x++) {
                   3352:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3353:                        SCR_BUF(y,x).Attributes = attr;
                   3354:                }
                   3355:        }
1.1.1.12  root     3356: }
                   3357: 
1.1.1.24  root     3358: bool update_console_input()
1.1       root     3359: {
1.1.1.35  root     3360: #ifdef USE_SERVICE_THREAD
                   3361:        EnterCriticalSection(&input_crit_sect);
                   3362: #endif
1.1.1.23  root     3363:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     3364:        DWORD dwNumberOfEvents = 0;
1.1       root     3365:        DWORD dwRead;
                   3366:        INPUT_RECORD ir[16];
1.1.1.24  root     3367:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   3368:        bool result = false;
1.1       root     3369:        
1.1.1.8   root     3370:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   3371:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   3372:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     3373:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.59  root     3374:                                        if(ir[i].Event.MouseEvent.dwEventFlags & MOUSE_MOVED) {
                   3375:                                                if(mouse.hidden == 0 || (mouse.call_addr_ps2.dw && mouse.enabled_ps2)) {
                   3376:                                                        // NOTE: if restore_console_on_exit, console is not scrolled
                   3377:                                                        if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   3378:                                                                GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   3379:                                                        }
                   3380:                                                        // FIXME: character size is always 8x8 ???
                   3381:                                                        int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   3382:                                                        int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
                   3383:                                                        
                   3384:                                                        if(mouse.position.x != x || mouse.position.y != y) {
                   3385:                                                                mouse.position.x = x;
                   3386:                                                                mouse.position.y = y;
                   3387:                                                                mouse.status |= 1;
                   3388:                                                                mouse.status_alt |= 1;
                   3389:                                                        }
1.1.1.34  root     3390:                                                }
1.1.1.59  root     3391:                                        } else if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   3392:                                                for(int j = 0; j < MAX_MOUSE_BUTTONS; j++) {
1.1.1.34  root     3393:                                                        static const DWORD bits[] = {
                   3394:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   3395:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
                   3396:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
                   3397:                                                        };
1.1.1.59  root     3398:                                                        bool prev_status = mouse.buttons[j].status;
                   3399:                                                        mouse.buttons[j].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[j]) != 0);
1.1.1.34  root     3400:                                                        
1.1.1.59  root     3401:                                                        if(!prev_status && mouse.buttons[j].status) {
                   3402:                                                                mouse.buttons[j].pressed_times++;
                   3403:                                                                mouse.buttons[j].pressed_position.x = mouse.position.x;
                   3404:                                                                mouse.buttons[j].pressed_position.y = mouse.position.x;
                   3405:                                                                if(j < 2) {
                   3406:                                                                        mouse.status_alt |= 2 << (j * 2);
1.1.1.43  root     3407:                                                                }
1.1.1.59  root     3408:                                                                mouse.status |= 2 << (j * 2);
                   3409:                                                        } else if(prev_status && !mouse.buttons[j].status) {
                   3410:                                                                mouse.buttons[j].released_times++;
                   3411:                                                                mouse.buttons[j].released_position.x = mouse.position.x;
                   3412:                                                                mouse.buttons[j].released_position.y = mouse.position.x;
                   3413:                                                                if(j < 2) {
                   3414:                                                                        mouse.status_alt |= 4 << (j * 2);
1.1.1.43  root     3415:                                                                }
1.1.1.59  root     3416:                                                                mouse.status |= 4 << (j * 2);
1.1.1.14  root     3417:                                                        }
                   3418:                                                }
                   3419:                                        }
1.1.1.24  root     3420:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3421:                                        // update keyboard flags in bios data area
1.1.1.35  root     3422:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
                   3423:                                                mem[0x417] |= 0x40;
1.1.1.33  root     3424:                                        } else {
1.1.1.35  root     3425:                                                mem[0x417] &= ~0x40;
1.1.1.33  root     3426:                                        }
1.1.1.35  root     3427:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
                   3428:                                                mem[0x417] |= 0x20;
1.1.1.33  root     3429:                                        } else {
1.1.1.35  root     3430:                                                mem[0x417] &= ~0x20;
                   3431:                                        }
                   3432:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
                   3433:                                                mem[0x417] |= 0x10;
                   3434:                                        } else {
                   3435:                                                mem[0x417] &= ~0x10;
1.1.1.33  root     3436:                                        }
                   3437:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43  root     3438:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3439:                                                        mouse.status_alt |= 0x80;
                   3440:                                                }
1.1.1.33  root     3441:                                                mem[0x417] |= 0x08;
                   3442:                                        } else {
                   3443:                                                mem[0x417] &= ~0x08;
                   3444:                                        }
1.1.1.35  root     3445:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43  root     3446:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3447:                                                        mouse.status_alt |= 0x40;
                   3448:                                                }
1.1.1.35  root     3449:                                                mem[0x417] |= 0x04;
1.1.1.33  root     3450:                                        } else {
1.1.1.35  root     3451:                                                mem[0x417] &= ~0x04;
1.1.1.33  root     3452:                                        }
                   3453:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43  root     3454:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3455:                                                        mouse.status_alt |= 0x20;
                   3456:                                                }
1.1.1.33  root     3457:                                                if(!(mem[0x417] & 0x03)) {
                   3458:                                                        mem[0x417] |= 0x02; // left shift
                   3459:                                                }
                   3460:                                        } else {
                   3461:                                                mem[0x417] &= ~0x03;
                   3462:                                        }
1.1.1.35  root     3463:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3464:                                                mem[0x418] |= 0x02;
                   3465:                                        } else {
                   3466:                                                mem[0x418] &= ~0x02;
                   3467:                                        }
                   3468:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3469:                                                mem[0x418] |= 0x01;
                   3470:                                        } else {
                   3471:                                                mem[0x418] &= ~0x01;
                   3472:                                        }
1.1.1.33  root     3473:                                        
1.1.1.28  root     3474:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.57  root     3475: //                                     kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
                   3476: //                                     kbd_status |= 1;
                   3477:                                        UINT8 tmp_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3478:                                        
                   3479:                                        // update dos key buffer
                   3480:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3481:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
1.1.1.51  root     3482:                                        UINT8 scn_old = scn;
1.1.1.33  root     3483:                                        
                   3484:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3485:                                                // make
1.1.1.57  root     3486:                                                tmp_data &= 0x7f;
1.1.1.24  root     3487:                                                
1.1.1.33  root     3488:                                                if(chr == 0x00) {
1.1.1.24  root     3489:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3490:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3491:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3492:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3493:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3494:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3495:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3496:                                                                } else if(scn == 0x35) {
                   3497:                                                                        scn = 0xa4;             // keypad /
                   3498:                                                                }
                   3499:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3500:                                                                if(scn == 0x07) {
                   3501:                                                                        chr = 0x1e;     // Ctrl+^
                   3502:                                                                } else if(scn == 0x0c) {
                   3503:                                                                        chr = 0x1f;     // Ctrl+_
                   3504:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3505:                                                                        static const UINT8 ctrl_map[] = {
                   3506:                                                                                0x95,   // keypad /
                   3507:                                                                                0,
                   3508:                                                                                0x96,   // keypad *
                   3509:                                                                                0, 0, 0,
                   3510:                                                                                0x5e,   // F1
                   3511:                                                                                0x5f,   // F2
                   3512:                                                                                0x60,   // F3
                   3513:                                                                                0x61,   // F4
                   3514:                                                                                0x62,   // F5
                   3515:                                                                                0x63,   // F6
                   3516:                                                                                0x64,   // F7
                   3517:                                                                                0x65,   // F8
                   3518:                                                                                0x66,   // F9
                   3519:                                                                                0x67,   // F10
                   3520:                                                                                0,
                   3521:                                                                                0,
                   3522:                                                                                0x77,   // Home
                   3523:                                                                                0x8d,   // Up
                   3524:                                                                                0x84,   // PgUp
                   3525:                                                                                0x8e,   // keypad -
                   3526:                                                                                0x73,   // Left
                   3527:                                                                                0x8f,   // keypad center
                   3528:                                                                                0x74,   // Right
                   3529:                                                                                0x90,   // keyapd +
                   3530:                                                                                0x75,   // End
                   3531:                                                                                0x91,   // Down
                   3532:                                                                                0x76,   // PgDn
                   3533:                                                                                0x92,   // Insert
                   3534:                                                                                0x93,   // Delete
                   3535:                                                                                0, 0, 0,
                   3536:                                                                                0x89,   // F11
                   3537:                                                                                0x8a,   // F12
                   3538:                                                                        };
                   3539:                                                                        scn = ctrl_map[scn - 0x35];
                   3540:                                                                }
                   3541:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3542:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3543:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3544:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3545:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3546:                                                                }
                   3547:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3548:                                                                scn += 0x85 - 0x57;
                   3549:                                                        }
                   3550:                                                        // ignore shift, ctrl, alt, win and menu keys
1.1.1.51  root     3551:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && !(scn >= 0x5b && scn <= 0x5d && scn == scn_old)) {
1.1.1.32  root     3552:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3553: #ifdef USE_SERVICE_THREAD
                   3554:                                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3555: #endif
1.1.1.32  root     3556:                                                                        if(chr == 0) {
1.1.1.51  root     3557:                                                                                pcbios_set_key_buffer(0x00, ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
1.1.1.32  root     3558:                                                                        }
1.1.1.51  root     3559:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3560: #ifdef USE_SERVICE_THREAD
                   3561:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3562: #endif
1.1.1.24  root     3563:                                                                }
                   3564:                                                        }
                   3565:                                                } else {
                   3566:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3567:                                                                chr = 0;
                   3568:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3569:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3570:                                                                }
                   3571:                                                        }
1.1.1.32  root     3572:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3573: #ifdef USE_SERVICE_THREAD
                   3574:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3575: #endif
1.1.1.51  root     3576:                                                                pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3577: #ifdef USE_SERVICE_THREAD
                   3578:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3579: #endif
1.1.1.32  root     3580:                                                        }
1.1.1.24  root     3581:                                                }
1.1.1.57  root     3582:                                        } else {
                   3583:                                                if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
                   3584:                                                        // ctrl-break, ctrl-c
                   3585:                                                        if(scn == 0x46) {
                   3586:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3587: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3588:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3589: #endif
1.1.1.57  root     3590:                                                                        pcbios_set_key_buffer(0x00, 0x00);
1.1.1.35  root     3591: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3592:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3593: #endif
1.1.1.57  root     3594:                                                                }
                   3595:                                                                ctrl_break_pressed = true;
                   3596:                                                                mem[0x471] = 0x80;
                   3597:                                                                raise_int_1bh = true;
                   3598:                                                        } else {
                   3599:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3600: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3601:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3602: #endif
1.1.1.57  root     3603:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3604: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3605:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3606: #endif
1.1.1.57  root     3607:                                                                }
                   3608:                                                                ctrl_c_pressed = (scn == 0x2e);
1.1.1.33  root     3609:                                                        }
                   3610:                                                }
                   3611:                                                // break
1.1.1.57  root     3612:                                                tmp_data |= 0x80;
                   3613:                                        }
                   3614:                                        if(!(kbd_status & 1)) {
                   3615:                                                kbd_data = tmp_data;
                   3616:                                                kbd_status |= 1;
                   3617:                                        } else {
                   3618:                                                if(key_buf_data != NULL) {
                   3619: #ifdef USE_SERVICE_THREAD
                   3620:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3621: #endif
                   3622:                                                        key_buf_data->write(tmp_data);
                   3623: #ifdef USE_SERVICE_THREAD
                   3624:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3625: #endif
                   3626:                                                }
1.1       root     3627:                                        }
1.1.1.24  root     3628:                                        result = key_changed = true;
1.1.1.36  root     3629:                                        // IME may be on and it may causes screen scroll up and cursor position change
                   3630:                                        cursor_moved = true;
1.1       root     3631:                                }
                   3632:                        }
                   3633:                }
                   3634:        }
1.1.1.35  root     3635: #ifdef USE_SERVICE_THREAD
                   3636:        LeaveCriticalSection(&input_crit_sect);
                   3637: #endif
1.1.1.24  root     3638:        return(result);
1.1.1.8   root     3639: }
                   3640: 
1.1.1.14  root     3641: bool update_key_buffer()
1.1.1.8   root     3642: {
1.1.1.35  root     3643:        if(update_console_input()) {
                   3644:                return(true);
                   3645:        }
                   3646:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3647: #ifdef USE_SERVICE_THREAD
                   3648:                EnterCriticalSection(&key_buf_crit_sect);
                   3649: #endif
1.1.1.55  root     3650:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     3651: #ifdef USE_SERVICE_THREAD
                   3652:                LeaveCriticalSection(&key_buf_crit_sect);
                   3653: #endif
                   3654:                if(!empty) return(true);
                   3655:        }
                   3656:        return(false);
1.1.1.8   root     3657: }
                   3658: 
1.1.1.20  root     3659: /* ----------------------------------------------------------------------------
                   3660:        MS-DOS virtual machine
                   3661: ---------------------------------------------------------------------------- */
                   3662: 
1.1.1.32  root     3663: static const struct {
1.1.1.33  root     3664:        char *name;
                   3665:        DWORD lcid;
                   3666:        char *std;
                   3667:        char *dlt;
                   3668: } tz_table[] = {
                   3669:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3670: //     0       GMT             Greenwich Mean Time             GMT0
                   3671:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3672:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3673:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3674:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3675: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3676:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3677:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3678: //     3       BST             Brazil Standard Time            BST3
                   3679:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3680:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3681:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3682: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3683:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3684: //     3       GST             Greenland Standard Time         GST3
                   3685:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3686: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3687:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3688: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3689:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3690: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3691:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3692:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3693: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3694:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3695:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3696:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3697: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3698:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3699: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3700:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3701: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3702:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3703: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3704:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3705:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3706:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3707: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3708:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3709: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3710:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3711:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3712:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3713: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3714:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3715:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3716: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3717: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3718:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3719: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3720:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3721:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3722: //     11      SST             Samoa Standard Time             SST11
                   3723:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3724: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3725:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3726: //     -10     GST             Guam Standard Time              GST-10
                   3727:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3728: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3729:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3730:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3731:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3732: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3733:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3734:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3735: //     -9      JST             Japan Standard Time             JST-9
                   3736:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3737: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3738:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3739:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3740: //     -8      HKT             Hong Kong Time                  HKT-8
                   3741:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3742: //     -8      CCT             China Coast Time                CCT-8
                   3743:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3744:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3745: //     -8      SST             Singapore Standard Time         SST-8
                   3746:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3747: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3748:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3749:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3750: //     -7:30   JT              Java Standard Time              JST-7:30
                   3751: //     -7      NST             North Sumatra Time              NST-7
                   3752:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3753: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3754:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3755: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3756:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3757: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3758:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3759:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3760: //     -2      EET             Eastern Europe Time             EET-2
                   3761:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3762:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3763:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3764:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3765: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3766:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3767: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3768: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3769: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3770: //     -1      CET     CES     Central European Time           CET-1CES
                   3771:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3772:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3773:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3774:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3775: //     -1      WAT             West African Time               WAT-1
                   3776:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3777:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3778: //     0       UTC             Universal Coordinated Time      UTC0
                   3779:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3780:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3781:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3782:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3783:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3784:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3785: };
                   3786: 
1.1.1.53  root     3787: // FIXME: consider to build on non-Japanese environment :-(
                   3788: // message_japanese string must be in shift-jis
                   3789: 
1.1.1.33  root     3790: static const struct {
1.1.1.32  root     3791:        UINT16 code;
                   3792:        char *message_english;
                   3793:        char *message_japanese;
                   3794: } standard_error_table[] = {
                   3795:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3796:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3797:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3798:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3799:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3800:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3801:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3802:        {0x08,  "Insufficient memory", "������������܂���."},
                   3803:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3804:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3805:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3806:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3807:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3808:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3809:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3810:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3811:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3812:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3813:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3814:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3815:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3816:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3817:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3818:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3819:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3820:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3821:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3822:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3823:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3824:        {0x1F,  "General failure", "�G���[�ł�."},
                   3825:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3826:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3827:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3828:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3829:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3830:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3831:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3832:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3833: /*
                   3834:        {0x32,  "Network request not supported", NULL},
                   3835:        {0x33,  "Remote computer not listening", NULL},
                   3836:        {0x34,  "Duplicate name on network", NULL},
                   3837:        {0x35,  "Network name not found", NULL},
                   3838:        {0x36,  "Network busy", NULL},
                   3839:        {0x37,  "Network device no longer exists", NULL},
                   3840:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3841:        {0x39,  "Network adapter hardware error", NULL},
                   3842:        {0x3A,  "Incorrect response from network", NULL},
                   3843:        {0x3B,  "Unexpected network error", NULL},
                   3844:        {0x3C,  "Incompatible remote adapter", NULL},
                   3845:        {0x3D,  "Print queue full", NULL},
                   3846:        {0x3E,  "Queue not full", NULL},
                   3847:        {0x3F,  "Not enough space to print file", NULL},
                   3848:        {0x40,  "Network name was deleted", NULL},
                   3849:        {0x41,  "Network: Access denied", NULL},
                   3850:        {0x42,  "Network device type incorrect", NULL},
                   3851:        {0x43,  "Network name not found", NULL},
                   3852:        {0x44,  "Network name limit exceeded", NULL},
                   3853:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3854:        {0x46,  "Temporarily paused", NULL},
                   3855:        {0x47,  "Network request not accepted", NULL},
                   3856:        {0x48,  "Network print/disk redirection paused", NULL},
                   3857:        {0x49,  "Network software not installed", NULL},
                   3858:        {0x4A,  "Unexpected adapter close", NULL},
                   3859: */
                   3860:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3861:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3862:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3863:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3864:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3865:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3866:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3867:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3868:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3869:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
1.1.1.53  root     3870: #ifdef SUPPORT_MSCDEX
1.1.1.32  root     3871:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3872:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3873:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3874:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3875:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
1.1.1.53  root     3876: #endif
1.1.1.32  root     3877:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3878:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3879:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3880:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3881:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3882:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3883: };
                   3884: 
                   3885: static const struct {
                   3886:        UINT16 code;
                   3887:        char *message_english;
                   3888:        char *message_japanese;
                   3889: } param_error_table[] = {
                   3890:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3891:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3892:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3893:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3894:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3895:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3896:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3897:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3898:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3899:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3900:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3901: };
                   3902: 
                   3903: static const struct {
                   3904:        UINT16 code;
                   3905:        char *message_english;
                   3906:        char *message_japanese;
                   3907: } critical_error_table[] = {
                   3908:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3909:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3910:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3911:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3912:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3913:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3914:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3915:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3916:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3917:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3918:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3919:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3920:        {0x0C,  "General failure", "�G���[�ł�."},
                   3921:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3922:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3923:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3924:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3925:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3926:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3927:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3928:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3929:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3930: };
                   3931: 
1.1.1.20  root     3932: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3933: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3934: void msdos_putch(UINT8 data);
1.1.1.50  root     3935: void msdos_putch_fast(UINT8 data);
1.1.1.35  root     3936: #ifdef USE_SERVICE_THREAD
                   3937: void msdos_putch_tmp(UINT8 data);
                   3938: #endif
1.1.1.45  root     3939: const char *msdos_short_path(const char *path);
1.1.1.44  root     3940: bool msdos_is_valid_drive(int drv);
                   3941: bool msdos_is_removable_drive(int drv);
                   3942: bool msdos_is_cdrom_drive(int drv);
                   3943: bool msdos_is_remote_drive(int drv);
                   3944: bool msdos_is_subst_drive(int drv);
1.1.1.20  root     3945: 
1.1       root     3946: // process info
                   3947: 
                   3948: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3949: {
                   3950:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3951:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3952:                        memset(&process[i], 0, sizeof(process_t));
                   3953:                        process[i].psp = psp_seg;
                   3954:                        return(&process[i]);
                   3955:                }
                   3956:        }
                   3957:        fatalerror("too many processes\n");
                   3958:        return(NULL);
                   3959: }
                   3960: 
1.1.1.52  root     3961: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error = true)
1.1       root     3962: {
                   3963:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3964:                if(process[i].psp == psp_seg) {
                   3965:                        return(&process[i]);
                   3966:                }
                   3967:        }
1.1.1.33  root     3968:        if(show_error) {
                   3969:                fatalerror("invalid psp address\n");
                   3970:        }
1.1       root     3971:        return(NULL);
                   3972: }
                   3973: 
1.1.1.23  root     3974: void msdos_sda_update(int psp_seg)
                   3975: {
                   3976:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3977:        
                   3978:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3979:                if(process[i].psp == psp_seg) {
                   3980:                        sda->switchar = process[i].switchar;
                   3981:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3982:                        sda->current_dta.w.h = process[i].dta.w.h;
                   3983:                        sda->current_psp = process[i].psp;
                   3984:                        break;
                   3985:                }
                   3986:        }
                   3987:        sda->malloc_strategy = malloc_strategy;
                   3988:        sda->return_code = retval;
                   3989:        sda->current_drive = _getdrive();
                   3990: }
                   3991: 
1.1.1.13  root     3992: // dta info
                   3993: 
                   3994: void msdos_dta_info_init()
                   3995: {
1.1.1.14  root     3996:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     3997:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3998:        }
                   3999: }
                   4000: 
                   4001: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   4002: {
                   4003:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     4004:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   4005:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   4006:                        if(free_dta == NULL) {
1.1.1.13  root     4007:                                free_dta = &dtalist[i];
                   4008:                        }
1.1.1.14  root     4009:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     4010:                        return(&dtalist[i]);
                   4011:                }
                   4012:        }
1.1.1.14  root     4013:        if(free_dta) {
1.1.1.13  root     4014:                free_dta->psp = psp_seg;
                   4015:                free_dta->dta = dta_laddr;
                   4016:                return(free_dta);
                   4017:        }
                   4018:        fatalerror("too many dta\n");
                   4019:        return(NULL);
                   4020: }
                   4021: 
                   4022: void msdos_dta_info_free(UINT16 psp_seg)
                   4023: {
1.1.1.14  root     4024:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   4025:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     4026:                        FindClose(dtalist[i].find_handle);
                   4027:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   4028:                }
                   4029:        }
                   4030: }
                   4031: 
1.1       root     4032: void msdos_cds_update(int drv)
                   4033: {
1.1.1.44  root     4034:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1       root     4035:        
1.1.1.44  root     4036:        memset(cds, 0, 88);
                   4037:        
                   4038:        if(msdos_is_valid_drive(drv)) {
                   4039:                char path[MAX_PATH];
                   4040:                if(msdos_is_remote_drive(drv)) {
                   4041:                        cds->drive_attrib = 0xc000;     // network drive
                   4042:                } else if(msdos_is_subst_drive(drv)) {
                   4043:                        cds->drive_attrib = 0x5000;     // subst drive
                   4044:                } else {
                   4045:                        cds->drive_attrib = 0x4000;     // physical drive
                   4046:                }
                   4047:                if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
                   4048:                        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
                   4049:                }
                   4050:        }
                   4051:        if(cds->path_name[0] == '\0') {
                   4052:                sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   4053:        }
                   4054:        cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   4055:        cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
                   4056:        cds->word_1 = cds->word_2 = 0xffff;
                   4057:        cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
                   4058:        cds->bs_offset = 2;
                   4059: }
                   4060: 
1.1.1.45  root     4061: void msdos_cds_update(int drv, const char *path)
1.1.1.44  root     4062: {
                   4063:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
                   4064:        
                   4065:        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1       root     4066: }
                   4067: 
1.1.1.17  root     4068: // nls information tables
                   4069: 
                   4070: // uppercase table (func 6502h)
                   4071: void msdos_upper_table_update()
                   4072: {
                   4073:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     4074:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     4075:                UINT8 c[4];
1.1.1.33  root     4076:                *(UINT32 *)c = 0;               // reset internal conversion state
                   4077:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     4078:                c[0] = 0x80 + i;
                   4079:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   4080:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   4081:        }
                   4082: }
                   4083: 
1.1.1.23  root     4084: // lowercase table (func 6503h)
                   4085: void msdos_lower_table_update()
                   4086: {
                   4087:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   4088:        for(unsigned i = 0; i < 0x80; ++i) {
                   4089:                UINT8 c[4];
1.1.1.33  root     4090:                *(UINT32 *)c = 0;               // reset internal conversion state
                   4091:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     4092:                c[0] = 0x80 + i;
                   4093:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   4094:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   4095:        }
                   4096: }
                   4097: 
1.1.1.17  root     4098: // filename uppercase table (func 6504h)
                   4099: void msdos_filename_upper_table_init()
                   4100: {
                   4101:        // depended on (file)system, not on active codepage
                   4102:        // temporary solution: just filling data
                   4103:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     4104:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     4105:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   4106:        }
                   4107: }
                   4108: 
                   4109: // filaname terminator table (func 6505h)
                   4110: void msdos_filename_terminator_table_init()
                   4111: {
                   4112:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   4113:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   4114:        
                   4115:        data[2] = 1;            // marker? (permissible character value)
                   4116:        data[3] = 0x00;         // 00h...FFh
                   4117:        data[4] = 0xff;
                   4118:        data[5] = 0;            // marker? (excluded character)
                   4119:        data[6] = 0x00;         // 00h...20h
                   4120:        data[7] = 0x20;
                   4121:        data[8] = 2;            // marker? (illegal characters for filename)
                   4122:        data[9] = (UINT8)strlen(illegal_chars);
                   4123:        memcpy(data + 10, illegal_chars, data[9]);
                   4124:        
                   4125:        // total length
                   4126:        *(UINT16 *)data = (10 - 2) + data[9];
                   4127: }
                   4128: 
                   4129: // collating table (func 6506h)
                   4130: void msdos_collating_table_update()
                   4131: {
                   4132:        // temporary solution: just filling data
                   4133:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     4134:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     4135:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   4136:        }
                   4137: }
                   4138: 
1.1       root     4139: // dbcs
                   4140: 
                   4141: void msdos_dbcs_table_update()
                   4142: {
                   4143:        UINT8 dbcs_data[DBCS_SIZE];
                   4144:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   4145:        
                   4146:        CPINFO info;
                   4147:        GetCPInfo(active_code_page, &info);
                   4148:        
                   4149:        if(info.MaxCharSize != 1) {
                   4150:                for(int i = 0;; i += 2) {
                   4151:                        UINT8 lo = info.LeadByte[i + 0];
                   4152:                        UINT8 hi = info.LeadByte[i + 1];
                   4153:                        dbcs_data[2 + i + 0] = lo;
                   4154:                        dbcs_data[2 + i + 1] = hi;
                   4155:                        if(lo == 0 && hi == 0) {
                   4156:                                dbcs_data[0] = i + 2;
                   4157:                                break;
                   4158:                        }
                   4159:                }
                   4160:        } else {
                   4161:                dbcs_data[0] = 2;       // ???
                   4162:        }
                   4163:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   4164: }
                   4165: 
1.1.1.17  root     4166: void msdos_dbcs_table_finish()
                   4167: {
1.1.1.32  root     4168:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     4169:                _setmbcp(system_code_page);
                   4170:        }
1.1.1.32  root     4171:        if(console_code_page != GetConsoleCP()) {
                   4172:                SetConsoleCP(console_code_page);
                   4173:                SetConsoleOutputCP(console_code_page);
                   4174:        }
1.1.1.17  root     4175: }
                   4176: 
                   4177: void msdos_nls_tables_init()
1.1       root     4178: {
1.1.1.32  root     4179:        active_code_page = console_code_page = GetConsoleCP();
                   4180:        system_code_page = _getmbcp();
                   4181:        
                   4182:        if(active_code_page != system_code_page) {
                   4183:                if(_setmbcp(active_code_page) != 0) {
                   4184:                        active_code_page = system_code_page;
                   4185:                }
                   4186:        }
                   4187:        
1.1.1.17  root     4188:        msdos_upper_table_update();
1.1.1.23  root     4189:        msdos_lower_table_update();
1.1.1.17  root     4190:        msdos_filename_terminator_table_init();
                   4191:        msdos_filename_upper_table_init();
                   4192:        msdos_collating_table_update();
1.1       root     4193:        msdos_dbcs_table_update();
                   4194: }
                   4195: 
1.1.1.17  root     4196: void msdos_nls_tables_update()
1.1       root     4197: {
1.1.1.17  root     4198:        msdos_dbcs_table_update();
                   4199:        msdos_upper_table_update();
1.1.1.23  root     4200:        msdos_lower_table_update();
                   4201: //     msdos_collating_table_update();
1.1       root     4202: }
                   4203: 
                   4204: int msdos_lead_byte_check(UINT8 code)
                   4205: {
                   4206:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   4207:        
                   4208:        for(int i = 0;; i += 2) {
                   4209:                UINT8 lo = dbcs_table[i + 0];
                   4210:                UINT8 hi = dbcs_table[i + 1];
                   4211:                if(lo == 0 && hi == 0) {
                   4212:                        break;
                   4213:                }
                   4214:                if(lo <= code && code <= hi) {
                   4215:                        return(1);
                   4216:                }
                   4217:        }
                   4218:        return(0);
                   4219: }
                   4220: 
1.1.1.20  root     4221: int msdos_ctrl_code_check(UINT8 code)
                   4222: {
1.1.1.22  root     4223:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     4224: }
                   4225: 
1.1.1.36  root     4226: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
                   4227: {
                   4228:        int is_kanji_1st = 0;
                   4229:        int is_kanji_2nd = 0;
                   4230:        
                   4231:        for(int p = 0;; p++) {
                   4232:                if(is_kanji_1st) {
                   4233:                        is_kanji_1st = 0;
                   4234:                        is_kanji_2nd = 1;
                   4235:                } else if(msdos_lead_byte_check(buf[p])) {
                   4236:                        is_kanji_1st = 1;
                   4237:                }
                   4238:                if(p == n) {
                   4239:                        return(is_kanji_2nd);
                   4240:                }
                   4241:                is_kanji_2nd = 0;
                   4242:        }
                   4243: }
                   4244: 
1.1       root     4245: // file control
                   4246: 
1.1.1.45  root     4247: const char *msdos_remove_double_quote(const char *path)
1.1.1.14  root     4248: {
                   4249:        static char tmp[MAX_PATH];
                   4250:        
                   4251:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45  root     4252:                memset(tmp, 0, sizeof(tmp));
1.1.1.14  root     4253:                memcpy(tmp, path + 1, strlen(path) - 2);
                   4254:        } else {
                   4255:                strcpy(tmp, path);
                   4256:        }
                   4257:        return(tmp);
                   4258: }
                   4259: 
1.1.1.45  root     4260: const char *msdos_remove_end_separator(const char *path)
1.1.1.32  root     4261: {
                   4262:        static char tmp[MAX_PATH];
                   4263:        
                   4264:        strcpy(tmp, path);
1.1.1.45  root     4265:        
                   4266:        // for example "C:\" case, the end separator should not be removed
                   4267:        if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
                   4268:                tmp[strlen(tmp) - 1] = '\0';
1.1.1.32  root     4269:        }
                   4270:        return(tmp);
                   4271: }
                   4272: 
1.1.1.45  root     4273: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14  root     4274: {
                   4275:        static char tmp[MAX_PATH];
1.1.1.45  root     4276:        const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14  root     4277:        
                   4278:        if(strlen(tmp_dir) == 0) {
                   4279:                strcpy(tmp, file);
                   4280:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   4281:                sprintf(tmp, "%s%s", tmp_dir, file);
                   4282:        } else {
                   4283:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   4284:        }
                   4285:        return(tmp);
                   4286: }
                   4287: 
1.1.1.45  root     4288: const char *msdos_trimmed_path(const char *path, int lfn)
1.1       root     4289: {
                   4290:        static char tmp[MAX_PATH];
                   4291:        
                   4292:        if(lfn) {
                   4293:                strcpy(tmp, path);
                   4294:        } else {
                   4295:                // remove space in the path
1.1.1.45  root     4296:                const char *src = path;
                   4297:                char *dst = tmp;
1.1       root     4298:                
                   4299:                while(*src != '\0') {
                   4300:                        if(msdos_lead_byte_check(*src)) {
                   4301:                                *dst++ = *src++;
                   4302:                                *dst++ = *src++;
                   4303:                        } else if(*src != ' ') {
                   4304:                                *dst++ = *src++;
                   4305:                        } else {
                   4306:                                src++;  // skip space
                   4307:                        }
                   4308:                }
                   4309:                *dst = '\0';
                   4310:        }
1.1.1.14  root     4311:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   4312:                // redirect C:\COMMAND.COM to comspec_path
                   4313:                strcpy(tmp, comspec_path);
                   4314:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   4315:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   4316:                static int root_drive_protected = -1;
                   4317:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   4318:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   4319:                
1.1.1.60  root     4320:                if(GetFullPathNameA(tmp, MAX_PATH, temp, &name_temp) != 0 &&
1.1.1.14  root     4321:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   4322:                        strcpy(name, name_temp);
                   4323:                        name_temp[0] = '\0';
                   4324:                        
                   4325:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   4326:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   4327:                                if(root_drive_protected == -1) {
                   4328:                                        FILE *fp = NULL;
                   4329:                                        
                   4330:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   4331:                                        root_drive_protected = 1;
                   4332:                                        try {
                   4333:                                                if((fp = fopen(temp, "w")) != NULL) {
                   4334:                                                        if(fprintf(fp, "TEST") == 4) {
                   4335:                                                                root_drive_protected = 0;
                   4336:                                                        }
                   4337:                                                }
                   4338:                                        } catch(...) {
                   4339:                                        }
                   4340:                                        if(fp != NULL) {
                   4341:                                                fclose(fp);
                   4342:                                        }
                   4343:                                        if(_access(temp, 0) == 0) {
                   4344:                                                remove(temp);
                   4345:                                        }
                   4346:                                }
                   4347:                                if(root_drive_protected == 1) {
1.1.1.60  root     4348:                                        if(GetEnvironmentVariableA("TEMP", temp, MAX_PATH) != 0 ||
                   4349:                                           GetEnvironmentVariableA("TMP",  temp, MAX_PATH) != 0) {
1.1.1.14  root     4350:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   4351:                                        }
                   4352:                                }
                   4353:                        }
                   4354:                }
                   4355:        }
1.1       root     4356:        return(tmp);
                   4357: }
                   4358: 
1.1.1.45  root     4359: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28  root     4360: {
1.1.1.32  root     4361:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     4362:        static char env_path[ENV_SIZE];
                   4363:        char tmp[ENV_SIZE], *token;
                   4364:        
                   4365:        memset(env_path, 0, sizeof(env_path));
                   4366:        strcpy(tmp, src);
                   4367:        token = my_strtok(tmp, ";");
                   4368:        
                   4369:        while(token != NULL) {
                   4370:                if(token[0] != '\0') {
1.1.1.45  root     4371:                        const char *path = msdos_remove_double_quote(token);
                   4372:                        char short_path[MAX_PATH];
1.1.1.32  root     4373:                        if(path != NULL && strlen(path) != 0) {
                   4374:                                if(env_path[0] != '\0') {
                   4375:                                        strcat(env_path, ";");
                   4376:                                }
1.1.1.60  root     4377:                                if(GetShortPathNameA(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     4378:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     4379:                                } else {
                   4380:                                        my_strupr(short_path);
1.1.1.32  root     4381:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     4382:                                }
                   4383:                        }
                   4384:                }
                   4385:                token = my_strtok(NULL, ";");
                   4386:        }
                   4387:        return(env_path);
                   4388: }
                   4389: 
1.1.1.45  root     4390: bool match(const char *text, const char *pattern)
1.1       root     4391: {
1.1.1.24  root     4392:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     4393:        switch(*pattern) {
1.1       root     4394:        case '\0':
                   4395:                return !*text;
                   4396:        case '*':
1.1.1.14  root     4397:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     4398:        case '?':
                   4399:                return *text && match(text + 1, pattern + 1);
                   4400:        default:
                   4401:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   4402:        }
                   4403: }
                   4404: 
1.1.1.45  root     4405: bool msdos_match_volume_label(const char *path, const char *volume)
1.1       root     4406: {
1.1.1.45  root     4407:        const char *p = NULL;
1.1       root     4408:        
1.1.1.14  root     4409:        if(!*volume) {
                   4410:                return false;
                   4411:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     4412:                return msdos_match_volume_label(p + 1, volume);
                   4413:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   4414:                return msdos_match_volume_label(p + 1, volume);
                   4415:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     4416:                char tmp[MAX_PATH];
                   4417:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   4418:                return match(volume, tmp);
1.1       root     4419:        } else {
                   4420:                return match(volume, path);
                   4421:        }
                   4422: }
                   4423: 
1.1.1.45  root     4424: const char *msdos_fcb_path(fcb_t *fcb)
1.1       root     4425: {
                   4426:        static char tmp[MAX_PATH];
                   4427:        char name[9], ext[4];
                   4428:        
                   4429:        memset(name, 0, sizeof(name));
                   4430:        memcpy(name, fcb->file_name, 8);
                   4431:        strcpy(name, msdos_trimmed_path(name, 0));
                   4432:        
                   4433:        memset(ext, 0, sizeof(ext));
                   4434:        memcpy(ext, fcb->file_name + 8, 3);
                   4435:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4436:        
                   4437:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4438:                strcpy(name, "*");
                   4439:        }
                   4440:        if(ext[0] == '\0') {
                   4441:                strcpy(tmp, name);
                   4442:        } else {
                   4443:                if(strcmp(ext, "???") == 0) {
                   4444:                        strcpy(ext, "*");
                   4445:                }
                   4446:                sprintf(tmp, "%s.%s", name, ext);
                   4447:        }
                   4448:        return(tmp);
                   4449: }
                   4450: 
1.1.1.45  root     4451: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1       root     4452: {
1.1.1.60  root     4453:        char tmp[MAX_PATH];
                   4454:        strcpy(tmp, path);
                   4455:        char *ext = my_strchr(tmp, '.');
1.1       root     4456:        
                   4457:        memset(fcb->file_name, 0x20, 8 + 3);
1.1.1.60  root     4458:        if(ext != NULL && tmp[0] != '.') {
1.1       root     4459:                *ext = '\0';
                   4460:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4461:        }
1.1.1.60  root     4462:        memcpy(fcb->file_name, tmp, strlen(tmp));
1.1       root     4463: }
                   4464: 
1.1.1.45  root     4465: const char *msdos_short_path(const char *path)
1.1       root     4466: {
                   4467:        static char tmp[MAX_PATH];
                   4468:        
1.1.1.60  root     4469:        if(GetShortPathNameA(path, tmp, MAX_PATH) == 0) {
1.1.1.24  root     4470:                strcpy(tmp, path);
                   4471:        }
1.1       root     4472:        my_strupr(tmp);
                   4473:        return(tmp);
                   4474: }
                   4475: 
1.1.1.60  root     4476: const char *msdos_short_name(WIN32_FIND_DATAA *fd)
1.1.1.13  root     4477: {
                   4478:        static char tmp[MAX_PATH];
1.1.1.45  root     4479:        
1.1.1.14  root     4480:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4481:                strcpy(tmp, fd->cAlternateFileName);
                   4482:        } else {
                   4483:                strcpy(tmp, fd->cFileName);
                   4484:        }
                   4485:        my_strupr(tmp);
                   4486:        return(tmp);
                   4487: }
                   4488: 
1.1.1.45  root     4489: const char *msdos_short_full_path(const char *path)
1.1       root     4490: {
                   4491:        static char tmp[MAX_PATH];
                   4492:        char full[MAX_PATH], *name;
                   4493:        
1.1.1.14  root     4494:        // Full works with non-existent files, but Short does not
1.1.1.60  root     4495:        GetFullPathNameA(path, MAX_PATH, full, &name);
1.1.1.14  root     4496:        *tmp = '\0';
1.1.1.60  root     4497:        if(GetShortPathNameA(full, tmp, MAX_PATH) == 0 && name > path) {
1.1.1.14  root     4498:                name[-1] = '\0';
1.1.1.60  root     4499:                DWORD len = GetShortPathNameA(full, tmp, MAX_PATH);
1.1.1.14  root     4500:                if(len == 0) {
                   4501:                        strcpy(tmp, full);
                   4502:                } else {
                   4503:                        tmp[len++] = '\\';
                   4504:                        strcpy(tmp + len, name);
                   4505:                }
                   4506:        }
1.1       root     4507:        my_strupr(tmp);
                   4508:        return(tmp);
                   4509: }
                   4510: 
1.1.1.45  root     4511: const char *msdos_short_full_dir(const char *path)
1.1       root     4512: {
                   4513:        static char tmp[MAX_PATH];
                   4514:        char full[MAX_PATH], *name;
                   4515:        
1.1.1.60  root     4516:        GetFullPathNameA(path, MAX_PATH, full, &name);
1.1       root     4517:        name[-1] = '\0';
1.1.1.60  root     4518:        if(GetShortPathNameA(full, tmp, MAX_PATH) == 0) {
1.1.1.24  root     4519:                strcpy(tmp, full);
                   4520:        }
1.1       root     4521:        my_strupr(tmp);
                   4522:        return(tmp);
                   4523: }
                   4524: 
1.1.1.45  root     4525: const char *msdos_local_file_path(const char *path, int lfn)
1.1       root     4526: {
1.1.1.45  root     4527:        static char trimmed[MAX_PATH];
                   4528:        
                   4529:        strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14  root     4530: #if 0
                   4531:        // I have forgotten the reason of this routine... :-(
1.1       root     4532:        if(_access(trimmed, 0) != 0) {
                   4533:                process_t *process = msdos_process_info_get(current_psp);
                   4534:                static char tmp[MAX_PATH];
                   4535:                
                   4536:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4537:                if(_access(tmp, 0) == 0) {
                   4538:                        return(tmp);
                   4539:                }
                   4540:        }
1.1.1.14  root     4541: #endif
1.1       root     4542:        return(trimmed);
                   4543: }
                   4544: 
1.1.1.45  root     4545: bool msdos_is_device_path(const char *path)
1.1.1.11  root     4546: {
                   4547:        char full[MAX_PATH], *name;
                   4548:        
1.1.1.60  root     4549:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4550:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4551:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4552:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4553:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4554:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4555:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4556:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4557:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4558:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4559:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4560:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4561:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4562:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4563:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4564:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4565:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4566:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4567:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4568:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4569:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4570:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4571:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4572:                        return(true);
                   4573:                } else if(name != NULL) {
                   4574:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4575:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4576:                           _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43  root     4577: //                        _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30  root     4578:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4579:                                return(true);
                   4580:                        }
                   4581:                }
1.1.1.24  root     4582:        }
                   4583:        return(false);
1.1.1.11  root     4584: }
                   4585: 
1.1.1.45  root     4586: bool msdos_is_con_path(const char *path)
1.1.1.8   root     4587: {
1.1.1.14  root     4588:        char full[MAX_PATH], *name;
1.1.1.8   root     4589:        
1.1.1.60  root     4590:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4591:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4592:        }
                   4593:        return(false);
                   4594: }
                   4595: 
1.1.1.45  root     4596: int msdos_is_comm_path(const char *path)
1.1.1.24  root     4597: {
                   4598:        char full[MAX_PATH], *name;
                   4599:        
1.1.1.60  root     4600:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4601:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4602:                        return(1);
                   4603:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4604:                        return(2);
                   4605:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4606:                        return(3);
                   4607:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4608:                        return(4);
1.1.1.24  root     4609:                }
                   4610:        }
1.1.1.29  root     4611:        return(0);
                   4612: }
                   4613: 
1.1.1.45  root     4614: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37  root     4615: {
                   4616:        // 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     4617:        const char *p = NULL;
1.1.1.37  root     4618:        
                   4619:        if((p = strstr(path, ":")) != NULL) {
                   4620:                UINT8 selector = sio_read(sio_port - 1, 3);
                   4621:                
                   4622:                // baud rate
                   4623:                int baud = max(110, min(9600, atoi(p + 1)));
                   4624:                UINT16 divisor = 115200 / baud;
                   4625:                
                   4626:                if((p = strstr(p + 1, ",")) != NULL) {
                   4627:                        // parity
                   4628:                        if(p[1] == 'N' || p[1] == 'n') {
                   4629:                                selector = (selector & ~0x38) | 0x00;
                   4630:                        } else if(p[1] == 'O' || p[1] == 'o') {
                   4631:                                selector = (selector & ~0x38) | 0x08;
                   4632:                        } else if(p[1] == 'E' || p[1] == 'e') {
                   4633:                                selector = (selector & ~0x38) | 0x18;
                   4634:                        } else if(p[1] == 'M' || p[1] == 'm') {
                   4635:                                selector = (selector & ~0x38) | 0x28;
                   4636:                        } else if(p[1] == 'S' || p[1] == 's') {
                   4637:                                selector = (selector & ~0x38) | 0x38;
                   4638:                        }
                   4639:                        if((p = strstr(p + 1, ",")) != NULL) {
                   4640:                                // word length
                   4641:                                if(p[1] == '8') {
                   4642:                                        selector = (selector & ~0x03) | 0x03;
                   4643:                                } else if(p[1] == '7') {
                   4644:                                        selector = (selector & ~0x03) | 0x02;
                   4645:                                } else if(p[1] == '6') {
                   4646:                                        selector = (selector & ~0x03) | 0x01;
                   4647:                                } else if(p[1] == '5') {
                   4648:                                        selector = (selector & ~0x03) | 0x00;
                   4649:                                }
                   4650:                                if((p = strstr(p + 1, ",")) != NULL) {
                   4651:                                        // stop bits
                   4652:                                        float bits = atof(p + 1);
                   4653:                                        if(bits > 1.0F) {
                   4654:                                                selector |= 0x04;
                   4655:                                        } else {
                   4656:                                                selector &= ~0x04;
                   4657:                                        }
                   4658:                                }
                   4659:                        }
                   4660:                }
                   4661:                sio_write(sio_port - 1, 3, selector | 0x80);
                   4662:                sio_write(sio_port - 1, 0, divisor & 0xff);
                   4663:                sio_write(sio_port - 1, 1, divisor >> 8);
                   4664:                sio_write(sio_port - 1, 3, selector);
                   4665:        }
                   4666: }
                   4667: 
1.1.1.45  root     4668: int msdos_is_prn_path(const char *path)
1.1.1.30  root     4669: {
                   4670:        char full[MAX_PATH], *name;
                   4671:        
1.1.1.60  root     4672:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.30  root     4673:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4674:                        return(1);
                   4675:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4676:                        return(1);
                   4677:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4678:                        return(2);
                   4679:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4680:                        return(3);
                   4681:                }
                   4682:        }
                   4683:        return(0);
                   4684: }
                   4685: 
1.1.1.44  root     4686: bool msdos_is_valid_drive(int drv)
                   4687: {
                   4688:        return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
                   4689: }
                   4690: 
                   4691: bool msdos_is_removable_drive(int drv)
                   4692: {
                   4693:        char volume[] = "A:\\";
                   4694:        
                   4695:        volume[0] = 'A' + drv;
                   4696:        
1.1.1.60  root     4697:        return(GetDriveTypeA(volume) == DRIVE_REMOVABLE);
1.1.1.44  root     4698: }
                   4699: 
                   4700: bool msdos_is_cdrom_drive(int drv)
                   4701: {
                   4702:        char volume[] = "A:\\";
                   4703:        
                   4704:        volume[0] = 'A' + drv;
                   4705:        
1.1.1.60  root     4706:        return(GetDriveTypeA(volume) == DRIVE_CDROM);
1.1.1.44  root     4707: }
                   4708: 
                   4709: bool msdos_is_remote_drive(int drv)
                   4710: {
                   4711:        char volume[] = "A:\\";
                   4712:        
                   4713:        volume[0] = 'A' + drv;
                   4714:        
1.1.1.60  root     4715:        return(GetDriveTypeA(volume) == DRIVE_REMOTE);
1.1.1.44  root     4716: }
                   4717: 
                   4718: bool msdos_is_subst_drive(int drv)
                   4719: {
                   4720:        char device[] = "A:", path[MAX_PATH];
                   4721:        
                   4722:        device[0] = 'A' + drv;
                   4723:        
1.1.1.60  root     4724:        if(QueryDosDeviceA(device, path, MAX_PATH)) {
1.1.1.44  root     4725:                if(strncmp(path, "\\??\\", 4) == 0) {
                   4726:                        return(true);
                   4727:                }
                   4728:        }
                   4729:        return(false);
                   4730: }
                   4731: 
1.1.1.45  root     4732: bool msdos_is_existing_file(const char *path)
1.1.1.24  root     4733: {
                   4734:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
1.1.1.60  root     4735:        WIN32_FIND_DATAA fd;
1.1.1.24  root     4736:        HANDLE hFind;
                   4737:        
1.1.1.60  root     4738:        if((hFind = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.24  root     4739:                FindClose(hFind);
1.1.1.60  root     4740:                return(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
1.1.1.24  root     4741:        }
                   4742:        return(false);
1.1.1.8   root     4743: }
                   4744: 
1.1.1.45  root     4745: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9   root     4746: {
                   4747:        static char tmp[MAX_PATH];
1.1.1.28  root     4748:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4749:        
1.1.1.28  root     4750:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.60  root     4751:        if(GetFullPathNameA(command_path, MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4752:                sprintf(file_name, "COMMAND.COM");
                   4753:                if(_access(tmp, 0) == 0) {
                   4754:                        return(tmp);
                   4755:                }
                   4756:        }
1.1.1.28  root     4757:        
                   4758:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.60  root     4759:        if(GetModuleFileNameA(NULL, path, MAX_PATH) != 0 && GetFullPathNameA(path, MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4760:                sprintf(file_name, "COMMAND.COM");
                   4761:                if(_access(tmp, 0) == 0) {
                   4762:                        return(tmp);
                   4763:                }
                   4764:        }
1.1.1.28  root     4765:        
                   4766:        // check if COMMAND.COM is in the current directory
1.1.1.60  root     4767:        if(GetFullPathNameA("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4768:                if(_access(tmp, 0) == 0) {
                   4769:                        return(tmp);
                   4770:                }
                   4771:        }
1.1.1.28  root     4772:        
                   4773:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4774:        strcpy(path, env_path);
                   4775:        char *token = my_strtok(path, ";");
1.1.1.9   root     4776:        while(token != NULL) {
1.1.1.14  root     4777:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4778:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4779:                        if(_access(tmp, 0) == 0) {
                   4780:                                return(tmp);
                   4781:                        }
                   4782:                }
                   4783:                token = my_strtok(NULL, ";");
                   4784:        }
                   4785:        return(NULL);
                   4786: }
                   4787: 
1.1.1.14  root     4788: int msdos_drive_number(const char *path)
1.1       root     4789: {
                   4790:        char tmp[MAX_PATH], *name;
                   4791:        
1.1.1.60  root     4792:        if(GetFullPathNameA(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
1.1.1.45  root     4793:                if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4794:                        return(tmp[0] - 'a');
                   4795:                } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
                   4796:                        return(tmp[0] - 'A');
                   4797:                }
1.1       root     4798:        }
1.1.1.45  root     4799: //     return(msdos_drive_number("."));
                   4800:        return(_getdrive() - 1);
1.1       root     4801: }
                   4802: 
1.1.1.45  root     4803: const char *msdos_volume_label(const char *path)
1.1       root     4804: {
                   4805:        static char tmp[MAX_PATH];
                   4806:        char volume[] = "A:\\";
                   4807:        
                   4808:        if(path[1] == ':') {
                   4809:                volume[0] = path[0];
                   4810:        } else {
                   4811:                volume[0] = 'A' + _getdrive() - 1;
                   4812:        }
1.1.1.60  root     4813:        if(!GetVolumeInformationA(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
1.1       root     4814:                memset(tmp, 0, sizeof(tmp));
                   4815:        }
                   4816:        return(tmp);
                   4817: }
                   4818: 
1.1.1.45  root     4819: const char *msdos_short_volume_label(const char *label)
1.1       root     4820: {
                   4821:        static char tmp[(8 + 1 + 3) + 1];
1.1.1.45  root     4822:        const char *src = label;
1.1       root     4823:        int remain = strlen(label);
                   4824:        char *dst_n = tmp;
                   4825:        char *dst_e = tmp + 9;
                   4826:        
                   4827:        strcpy(tmp, "        .   ");
                   4828:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4829:                if(msdos_lead_byte_check(*src)) {
                   4830:                        if(++i == 8) {
                   4831:                                break;
                   4832:                        }
                   4833:                        *dst_n++ = *src++;
                   4834:                        remain--;
                   4835:                }
                   4836:                *dst_n++ = *src++;
                   4837:                remain--;
                   4838:        }
                   4839:        if(remain > 0) {
                   4840:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4841:                        if(msdos_lead_byte_check(*src)) {
                   4842:                                if(++i == 3) {
                   4843:                                        break;
                   4844:                                }
                   4845:                                *dst_e++ = *src++;
                   4846:                                remain--;
                   4847:                        }
                   4848:                        *dst_e++ = *src++;
                   4849:                        remain--;
                   4850:                }
                   4851:                *dst_e = '\0';
                   4852:        } else {
                   4853:                *dst_n = '\0';
                   4854:        }
                   4855:        my_strupr(tmp);
                   4856:        return(tmp);
                   4857: }
                   4858: 
1.1.1.13  root     4859: errno_t msdos_maperr(unsigned long oserrno)
                   4860: {
                   4861:        _doserrno = oserrno;
1.1.1.14  root     4862:        switch(oserrno) {
1.1.1.13  root     4863:        case ERROR_FILE_NOT_FOUND:         // 2
                   4864:        case ERROR_PATH_NOT_FOUND:         // 3
                   4865:        case ERROR_INVALID_DRIVE:          // 15
                   4866:        case ERROR_NO_MORE_FILES:          // 18
                   4867:        case ERROR_BAD_NETPATH:            // 53
                   4868:        case ERROR_BAD_NET_NAME:           // 67
                   4869:        case ERROR_BAD_PATHNAME:           // 161
                   4870:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4871:                return ENOENT;
                   4872:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4873:                return EMFILE;
                   4874:        case ERROR_ACCESS_DENIED:          // 5
                   4875:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4876:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4877:        case ERROR_CANNOT_MAKE:            // 82
                   4878:        case ERROR_FAIL_I24:               // 83
                   4879:        case ERROR_DRIVE_LOCKED:           // 108
                   4880:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4881:        case ERROR_NOT_LOCKED:             // 158
                   4882:        case ERROR_LOCK_FAILED:            // 167
                   4883:                return EACCES;
                   4884:        case ERROR_INVALID_HANDLE:         // 6
                   4885:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4886:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4887:                return EBADF;
                   4888:        case ERROR_ARENA_TRASHED:          // 7
                   4889:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4890:        case ERROR_INVALID_BLOCK:          // 9
                   4891:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4892:                return ENOMEM;
                   4893:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4894:                return E2BIG;
                   4895:        case ERROR_BAD_FORMAT:             // 11
                   4896:                return ENOEXEC;
                   4897:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4898:                return EXDEV;
                   4899:        case ERROR_FILE_EXISTS:            // 80
                   4900:        case ERROR_ALREADY_EXISTS:         // 183
                   4901:                return EEXIST;
                   4902:        case ERROR_NO_PROC_SLOTS:          // 89
                   4903:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4904:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4905:                return EAGAIN;
                   4906:        case ERROR_BROKEN_PIPE:            // 109
                   4907:                return EPIPE;
                   4908:        case ERROR_DISK_FULL:              // 112
                   4909:                return ENOSPC;
                   4910:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4911:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4912:                return ECHILD;
                   4913:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4914:                return ENOTEMPTY;
                   4915:        }
1.1.1.14  root     4916:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4917:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4918:                return EACCES;
                   4919:        }
1.1.1.14  root     4920:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4921:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4922:                return ENOEXEC;
                   4923:        }
                   4924:        return EINVAL;
                   4925: }
                   4926: 
1.1.1.45  root     4927: int msdos_open(const char *path, int oflag)
1.1.1.13  root     4928: {
1.1.1.14  root     4929:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45  root     4930:                return(_open(path, oflag));
1.1.1.13  root     4931:        }
1.1.1.14  root     4932:        
                   4933:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4934:        DWORD disposition;
1.1.1.14  root     4935:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4936:        default:
1.1.1.13  root     4937:        case _O_EXCL:
                   4938:                disposition = OPEN_EXISTING;
                   4939:                break;
                   4940:        case _O_CREAT:
                   4941:                disposition = OPEN_ALWAYS;
                   4942:                break;
                   4943:        case _O_CREAT | _O_EXCL:
                   4944:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4945:                disposition = CREATE_NEW;
                   4946:                break;
                   4947:        case _O_TRUNC:
                   4948:        case _O_TRUNC | _O_EXCL:
                   4949:                disposition = TRUNCATE_EXISTING;
                   4950:                break;
                   4951:        case _O_CREAT | _O_TRUNC:
                   4952:                disposition = CREATE_ALWAYS;
                   4953:                break;
                   4954:        }
1.1.1.14  root     4955:        
1.1.1.60  root     4956:        HANDLE h = CreateFileA(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
1.1.1.13  root     4957:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4958:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4959:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4960:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4961:                // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.60  root     4962:                h = CreateFileA(path, GENERIC_READ,
1.1.1.13  root     4963:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4964:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4965:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4966:                        errno = msdos_maperr(GetLastError());
1.1.1.45  root     4967:                        return(-1);
1.1.1.13  root     4968:                }
                   4969:        }
1.1.1.14  root     4970:        
1.1.1.13  root     4971:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     4972:        if(fd == -1) {
1.1.1.13  root     4973:                CloseHandle(h);
                   4974:        }
1.1.1.45  root     4975:        return(fd);
                   4976: }
                   4977: 
                   4978: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
                   4979: {
                   4980:        int fd = -1;
                   4981:        
                   4982:        *sio_port = *lpt_port = 0;
                   4983:        
                   4984:        if(msdos_is_con_path(path)) {
                   4985:                // MODE.COM opens CON device with read/write mode :-(
                   4986:                if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
                   4987:                        oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
                   4988:                        oflag |= _O_RDONLY;
                   4989:                }
                   4990:                if((fd = msdos_open("CON", oflag)) == -1) {
                   4991: //                     fd = msdos_open("NUL", oflag);
                   4992:                }
                   4993:        } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
                   4994:                fd = msdos_open("NUL", oflag);
                   4995:                msdos_set_comm_params(*sio_port, path);
                   4996:        } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
                   4997:                fd = msdos_open("NUL", oflag);
                   4998:        } else if(msdos_is_device_path(path)) {
                   4999:                fd = msdos_open("NUL", oflag);
                   5000: //     } else if(oflag & _O_CREAT) {
                   5001: //             fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
                   5002: //     } else {
                   5003: //             fd = _open(path, oflag);
                   5004:        }
                   5005:        return(fd);
                   5006: }
                   5007: 
                   5008: UINT16 msdos_device_info(const char *path)
                   5009: {
                   5010:        if(msdos_is_con_path(path)) {
                   5011:                return(0x80d3);
                   5012:        } else if(msdos_is_comm_path(path)) {
                   5013:                return(0x80a0);
                   5014:        } else if(msdos_is_prn_path(path)) {
                   5015: //             return(0xa8c0);
                   5016:                return(0x80a0);
                   5017:        } else if(msdos_is_device_path(path)) {
                   5018:                if(strstr(path, "EMMXXXX0") != NULL) {
                   5019:                        return(0xc0c0);
                   5020:                } else if(strstr(path, "MSCD001") != NULL) {
                   5021:                        return(0xc880);
                   5022:                } else {
                   5023:                        return(0x8084);
                   5024:                }
                   5025:        } else {
                   5026:                return(msdos_drive_number(path));
                   5027:        }
1.1.1.13  root     5028: }
                   5029: 
1.1.1.52  root     5030: 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     5031: {
                   5032:        static int id = 0;
                   5033:        char full[MAX_PATH], *name;
                   5034:        
1.1.1.60  root     5035:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1       root     5036:                strcpy(file_handler[fd].path, full);
                   5037:        } else {
                   5038:                strcpy(file_handler[fd].path, path);
                   5039:        }
1.1.1.14  root     5040:        // isatty makes no distinction between CON & NUL
                   5041:        // GetFileSize fails on CON, succeeds on NUL
                   5042:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45  root     5043:                if(info == 0x80d3) {
                   5044:                        info = 0x8084;
                   5045:                }
1.1.1.14  root     5046:                atty = 0;
                   5047:        } else if(!atty && info == 0x80d3) {
1.1.1.45  root     5048: //             info = msdos_drive_number(".");
                   5049:                info = msdos_drive_number(path);
1.1.1.14  root     5050:        }
1.1       root     5051:        file_handler[fd].valid = 1;
                   5052:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37  root     5053:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     5054:        file_handler[fd].mode = mode;
                   5055:        file_handler[fd].info = info;
                   5056:        file_handler[fd].psp = psp_seg;
1.1.1.37  root     5057:        file_handler[fd].sio_port = sio_port;
                   5058:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     5059:        
                   5060:        // init system file table
                   5061:        if(fd < 20) {
                   5062:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   5063:                
                   5064:                memset(sft, 0, 0x3b);
                   5065:                
                   5066:                *(UINT16 *)(sft + 0x00) = 1;
                   5067:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
1.1.1.60  root     5068:                *(UINT8  *)(sft + 0x04) = GetFileAttributesA(file_handler[fd].path) & 0xff;
1.1.1.21  root     5069:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   5070:                
                   5071:                if(!(file_handler[fd].info & 0x80)) {
                   5072:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   5073:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   5074:                        
                   5075:                        FILETIME time, local;
                   5076:                        HANDLE hHandle;
                   5077:                        WORD dos_date = 0, dos_time = 0;
                   5078:                        DWORD file_size = 0;
                   5079:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   5080:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   5081:                                        FileTimeToLocalFileTime(&time, &local);
                   5082:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   5083:                                }
                   5084:                                file_size = GetFileSize(hHandle, NULL);
                   5085:                        }
                   5086:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   5087:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   5088:                        *(UINT32 *)(sft + 0x11) = file_size;
                   5089:                }
                   5090:                
                   5091:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   5092:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   5093:                my_strupr(fname);
                   5094:                my_strupr(ext);
                   5095:                memset(sft + 0x20, 0x20, 11);
                   5096:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   5097:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   5098:                
                   5099:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   5100:        }
1.1       root     5101: }
                   5102: 
                   5103: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   5104: {
                   5105:        strcpy(file_handler[dst].path, file_handler[src].path);
                   5106:        file_handler[dst].valid = 1;
                   5107:        file_handler[dst].id = file_handler[src].id;
                   5108:        file_handler[dst].atty = file_handler[src].atty;
                   5109:        file_handler[dst].mode = file_handler[src].mode;
                   5110:        file_handler[dst].info = file_handler[src].info;
                   5111:        file_handler[dst].psp = psp_seg;
1.1.1.37  root     5112:        file_handler[dst].sio_port = file_handler[src].sio_port;
                   5113:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     5114: }
                   5115: 
1.1.1.20  root     5116: void msdos_file_handler_close(int fd)
1.1       root     5117: {
                   5118:        file_handler[fd].valid = 0;
1.1.1.21  root     5119:        
                   5120:        if(fd < 20) {
                   5121:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   5122:        }
1.1       root     5123: }
                   5124: 
1.1.1.14  root     5125: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     5126: {
1.1.1.14  root     5127:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   5128:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   5129:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     5130: }
                   5131: 
                   5132: // find file
                   5133: 
                   5134: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   5135: {
                   5136:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   5137:                return(0);      // search directory only !!!
                   5138:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   5139:                return(0);
                   5140:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   5141:                return(0);
                   5142:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   5143:                return(0);
                   5144:        } else if((attribute & required_mask) != required_mask) {
                   5145:                return(0);
                   5146:        } else {
                   5147:                return(1);
                   5148:        }
                   5149: }
                   5150: 
1.1.1.60  root     5151: int msdos_find_file_has_8dot3name(WIN32_FIND_DATAA *fd)
1.1.1.13  root     5152: {
1.1.1.14  root     5153:        if(fd->cAlternateFileName[0]) {
1.1.1.42  root     5154:                return(1);
1.1.1.13  root     5155:        }
                   5156:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     5157:        if(len > 12) {
1.1.1.42  root     5158:                return(0);
1.1.1.13  root     5159:        }
                   5160:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     5161:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42  root     5162:                return(0);
1.1.1.13  root     5163:        }
1.1.1.42  root     5164:        return(1);
1.1.1.13  root     5165: }
                   5166: 
1.1.1.60  root     5167: void msdos_find_file_conv_local_time(WIN32_FIND_DATAA *fd)
1.1       root     5168: {
                   5169:        FILETIME local;
                   5170:        
                   5171:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   5172:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   5173:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   5174:        
                   5175:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   5176:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   5177:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   5178:        
                   5179:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   5180:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   5181:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   5182: }
                   5183: 
                   5184: // i/o
                   5185: 
                   5186: void msdos_stdio_reopen()
                   5187: {
                   5188:        if(!file_handler[0].valid) {
                   5189:                _dup2(DUP_STDIN, 0);
                   5190:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   5191:        }
                   5192:        if(!file_handler[1].valid) {
                   5193:                _dup2(DUP_STDOUT, 1);
                   5194:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   5195:        }
                   5196:        if(!file_handler[2].valid) {
                   5197:                _dup2(DUP_STDERR, 2);
                   5198:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   5199:        }
1.1.1.21  root     5200:        if(!file_handler[3].valid) {
                   5201:                _dup2(DUP_STDAUX, 3);
                   5202:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   5203:        }
                   5204:        if(!file_handler[4].valid) {
                   5205:                _dup2(DUP_STDPRN, 4);
1.1.1.45  root     5206: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   5207:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     5208:        }
                   5209:        for(int i = 0; i < 5; i++) {
                   5210:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   5211:                        msdos_psp_set_file_table(i, i, current_psp);
                   5212:                }
                   5213:        }
1.1       root     5214: }
                   5215: 
1.1.1.37  root     5216: int msdos_read(int fd, void *buffer, unsigned int count)
                   5217: {
                   5218:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5219:                // read from serial port
                   5220:                int read = 0;
                   5221:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5222:                        UINT8 *buf = (UINT8 *)buffer;
                   5223:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5224:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5225:                        DWORD timeout = timeGetTime() + 1000;
                   5226:                        while(read < count) {
                   5227:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
                   5228:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
                   5229:                                        timeout = timeGetTime() + 1000;
                   5230:                                } else {
                   5231:                                        if(timeGetTime() > timeout) {
                   5232:                                                break;
                   5233:                                        }
                   5234:                                        Sleep(10);
1.1.1.37  root     5235:                                }
                   5236:                        }
                   5237:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5238:                }
                   5239:                return(read);
                   5240:        }
                   5241:        return(_read(fd, buffer, count));
                   5242: }
                   5243: 
1.1       root     5244: int msdos_kbhit()
                   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
1.1.1.20  root     5253:                return(eof(fd) == 0);
1.1       root     5254:        }
                   5255:        
                   5256:        // check keyboard status
1.1.1.35  root     5257:        if(key_recv != 0) {
1.1       root     5258:                return(1);
                   5259:        }
1.1.1.35  root     5260:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5261: #ifdef USE_SERVICE_THREAD
                   5262:                EnterCriticalSection(&key_buf_crit_sect);
                   5263: #endif
1.1.1.55  root     5264:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5265: #ifdef USE_SERVICE_THREAD
                   5266:                LeaveCriticalSection(&key_buf_crit_sect);
                   5267: #endif
                   5268:                if(!empty) return(1);
                   5269:        }
                   5270:        return(_kbhit());
1.1       root     5271: }
                   5272: 
                   5273: int msdos_getch_ex(int echo)
                   5274: {
                   5275:        static char prev = 0;
                   5276:        
                   5277:        msdos_stdio_reopen();
                   5278:        
1.1.1.20  root     5279:        process_t *process = msdos_process_info_get(current_psp);
                   5280:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5281:        
                   5282:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5283:                // stdin is redirected to file
                   5284: retry:
                   5285:                char data;
1.1.1.37  root     5286:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     5287:                        char tmp = data;
                   5288:                        if(data == 0x0a) {
                   5289:                                if(prev == 0x0d) {
                   5290:                                        goto retry; // CRLF -> skip LF
                   5291:                                } else {
                   5292:                                        data = 0x0d; // LF only -> CR
                   5293:                                }
                   5294:                        }
                   5295:                        prev = tmp;
                   5296:                        return(data);
                   5297:                }
                   5298:                return(EOF);
                   5299:        }
                   5300:        
                   5301:        // input from console
1.1.1.5   root     5302:        int key_char, key_scan;
1.1.1.33  root     5303:        if(key_recv != 0) {
1.1.1.5   root     5304:                key_char = (key_code >> 0) & 0xff;
                   5305:                key_scan = (key_code >> 8) & 0xff;
                   5306:                key_code >>= 16;
1.1.1.33  root     5307:                key_recv >>= 16;
1.1.1.5   root     5308:        } else {
1.1.1.54  root     5309:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_exit) {
1.1.1.35  root     5310:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5311: #ifdef USE_SERVICE_THREAD
                   5312:                                EnterCriticalSection(&key_buf_crit_sect);
                   5313: #endif
1.1.1.55  root     5314:                                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5315: #ifdef USE_SERVICE_THREAD
                   5316:                                LeaveCriticalSection(&key_buf_crit_sect);
                   5317: #endif
                   5318:                                if(!empty) break;
                   5319:                        }
1.1.1.23  root     5320:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   5321:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   5322:                                if(_kbhit()) {
1.1.1.32  root     5323:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5324: #ifdef USE_SERVICE_THREAD
                   5325:                                                EnterCriticalSection(&key_buf_crit_sect);
                   5326: #endif
1.1.1.51  root     5327:                                                pcbios_set_key_buffer(_getch(), 0x00);
1.1.1.35  root     5328: #ifdef USE_SERVICE_THREAD
                   5329:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   5330: #endif
1.1.1.32  root     5331:                                        }
1.1.1.23  root     5332:                                } else {
                   5333:                                        Sleep(10);
                   5334:                                }
                   5335:                        } else {
                   5336:                                if(!update_key_buffer()) {
                   5337:                                        Sleep(10);
                   5338:                                }
1.1.1.14  root     5339:                        }
                   5340:                }
1.1.1.54  root     5341:                if(m_exit) {
1.1.1.33  root     5342:                        // insert CR to terminate input loops
1.1.1.14  root     5343:                        key_char = 0x0d;
                   5344:                        key_scan = 0;
1.1.1.32  root     5345:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5346: #ifdef USE_SERVICE_THREAD
                   5347:                        EnterCriticalSection(&key_buf_crit_sect);
                   5348: #endif
1.1.1.51  root     5349:                        pcbios_get_key_buffer(&key_char, &key_scan);
1.1.1.35  root     5350: #ifdef USE_SERVICE_THREAD
                   5351:                        LeaveCriticalSection(&key_buf_crit_sect);
                   5352: #endif
1.1.1.5   root     5353:                }
1.1       root     5354:        }
                   5355:        if(echo && key_char) {
                   5356:                msdos_putch(key_char);
                   5357:        }
                   5358:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   5359: }
                   5360: 
                   5361: inline int msdos_getch()
                   5362: {
                   5363:        return(msdos_getch_ex(0));
                   5364: }
                   5365: 
                   5366: inline int msdos_getche()
                   5367: {
                   5368:        return(msdos_getch_ex(1));
                   5369: }
                   5370: 
                   5371: int msdos_write(int fd, const void *buffer, unsigned int count)
                   5372: {
1.1.1.37  root     5373:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5374:                // write to serial port
1.1.1.38  root     5375:                int written = 0;
1.1.1.37  root     5376:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5377:                        UINT8 *buf = (UINT8 *)buffer;
                   5378:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5379:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5380:                        DWORD timeout = timeGetTime() + 1000;
                   5381:                        while(written < count) {
                   5382:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
                   5383:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
                   5384:                                        timeout = timeGetTime() + 1000;
                   5385:                                } else {
                   5386:                                        if(timeGetTime() > timeout) {
                   5387:                                                break;
                   5388:                                        }
                   5389:                                        Sleep(10);
                   5390:                                }
1.1.1.37  root     5391:                        }
                   5392:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5393:                }
1.1.1.38  root     5394:                return(written);
1.1.1.37  root     5395:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   5396:                // write to printer port
                   5397:                UINT8 *buf = (UINT8 *)buffer;
                   5398:                for(unsigned int i = 0; i < count; i++) {
                   5399: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5400:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5401:                }
                   5402:                return(count);
                   5403:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     5404:                // CR+LF -> LF
1.1.1.37  root     5405:                static int is_cr = 0;
1.1       root     5406:                UINT8 *buf = (UINT8 *)buffer;
                   5407:                for(unsigned int i = 0; i < count; i++) {
                   5408:                        UINT8 data = buf[i];
                   5409:                        if(is_cr) {
                   5410:                                if(data != 0x0a) {
                   5411:                                        UINT8 tmp = 0x0d;
                   5412:                                        _write(1, &tmp, 1);
                   5413:                                }
                   5414:                                _write(1, &data, 1);
                   5415:                                is_cr = 0;
                   5416:                        } else if(data == 0x0d) {
                   5417:                                is_cr = 1;
                   5418:                        } else {
                   5419:                                _write(1, &data, 1);
                   5420:                        }
                   5421:                }
                   5422:                return(count);
                   5423:        }
1.1.1.14  root     5424:        vram_flush();
1.1       root     5425:        return(_write(fd, buffer, count));
                   5426: }
                   5427: 
                   5428: void msdos_putch(UINT8 data)
1.1.1.50  root     5429: {
                   5430:        msdos_stdio_reopen();
                   5431:        
                   5432:        process_t *process = msdos_process_info_get(current_psp);
                   5433:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5434:        
                   5435:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
                   5436:                // stdout is redirected to file
                   5437:                msdos_write(fd, &data, 1);
                   5438:                return;
                   5439:        }
                   5440:        
                   5441:        // call int 29h ?
1.1.1.58  root     5442:        if(*(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     5443:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   5444:                // int 29h is not hooked, no need to call int 29h
                   5445:                msdos_putch_fast(data);
                   5446: #ifdef USE_SERVICE_THREAD
                   5447:        } else if(in_service && main_thread_id != GetCurrentThreadId()) {
                   5448:                // XXX: in usually we should not reach here
                   5449:                // this is called from service thread to echo the input
                   5450:                // we can not call int 29h because it causes a critial issue to control cpu running in main thread :-(
                   5451:                msdos_putch_fast(data);
                   5452: #endif
1.1.1.51  root     5453:        } else if(in_service_29h) {
1.1.1.50  root     5454:                // disallow reentering call int 29h routine to prevent an infinite loop :-(
                   5455:                msdos_putch_fast(data);
                   5456:        } else {
                   5457:                // this is called from main thread, so we can call int 29h :-)
1.1.1.51  root     5458:                in_service_29h = true;
1.1.1.50  root     5459:                try {
                   5460:                        UINT32 tmp_pc = m_pc;
                   5461:                        UINT16 tmp_ax = REG16(AX);
                   5462:                        UINT32 tmp_bx = REG16(BX); // BX may be destroyed by some versions of DOS 3.3
                   5463:                        
                   5464:                        // call int 29h routine is at fffc:0027
                   5465:                        i386_call_far(DUMMY_TOP >> 4, 0x0027);
                   5466:                        REG8(AL) = data;
                   5467:                        
                   5468:                        // run cpu until call int 29h routine is done
1.1.1.54  root     5469:                        while(!m_exit && tmp_pc != m_pc) {
1.1.1.50  root     5470:                                try {
                   5471:                                        hardware_run_cpu();
                   5472:                                } catch(...) {
                   5473:                                }
                   5474:                        }
                   5475:                        REG16(AX) = tmp_ax;
                   5476:                        REG16(BX) = tmp_bx;
                   5477:                } catch(...) {
                   5478:                }
1.1.1.51  root     5479:                in_service_29h = false;
1.1.1.50  root     5480:        }
                   5481: }
                   5482: 
                   5483: void msdos_putch_fast(UINT8 data)
1.1.1.35  root     5484: #ifdef USE_SERVICE_THREAD
                   5485: {
                   5486:        EnterCriticalSection(&putch_crit_sect);
                   5487:        msdos_putch_tmp(data);
                   5488:        LeaveCriticalSection(&putch_crit_sect);
                   5489: }
                   5490: void msdos_putch_tmp(UINT8 data)
                   5491: #endif
1.1       root     5492: {
1.1.1.34  root     5493:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5494:        SMALL_RECT rect;
                   5495:        COORD co;
1.1       root     5496:        static int p = 0;
                   5497:        static int is_kanji = 0;
                   5498:        static int is_esc = 0;
                   5499:        static int stored_x;
                   5500:        static int stored_y;
                   5501:        static WORD stored_a;
1.1.1.20  root     5502:        static char tmp[64], out[64];
1.1       root     5503:        
1.1.1.23  root     5504:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     5505:        
                   5506:        // output to console
                   5507:        tmp[p++] = data;
                   5508:        
1.1.1.14  root     5509:        vram_flush();
                   5510:        
1.1       root     5511:        if(is_kanji) {
                   5512:                // kanji character
                   5513:                is_kanji = 0;
                   5514:        } else if(is_esc) {
                   5515:                // escape sequense
                   5516:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   5517:                        p = is_esc = 0;
                   5518:                } else if(tmp[1] == '=' && p == 4) {
                   5519:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     5520:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     5521:                        SetConsoleCursorPosition(hStdout, co);
                   5522:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5523:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     5524:                        cursor_moved = false;
1.1.1.59  root     5525:                        cursor_moved_by_crtc = false;
1.1       root     5526:                        p = is_esc = 0;
                   5527:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
1.1.1.59  root     5528:                        if(cursor_moved_by_crtc) {
                   5529:                                if(!restore_console_on_exit) {
                   5530:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5531:                                        scr_top = csbi.srWindow.Top;
                   5532:                                }
                   5533:                                co.X = mem[0x450 + REG8(BH) * 2];
                   5534:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   5535:                                SetConsoleCursorPosition(hStdout, co);
                   5536:                                cursor_moved_by_crtc = false;
                   5537:                        }
1.1       root     5538:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5539:                        co.X = csbi.dwCursorPosition.X;
                   5540:                        co.Y = csbi.dwCursorPosition.Y;
                   5541:                        WORD wAttributes = csbi.wAttributes;
                   5542:                        
                   5543:                        if(tmp[1] == 'D') {
                   5544:                                co.Y++;
                   5545:                        } else if(tmp[1] == 'E') {
                   5546:                                co.X = 0;
                   5547:                                co.Y++;
                   5548:                        } else if(tmp[1] == 'M') {
                   5549:                                co.Y--;
                   5550:                        } else if(tmp[1] == '*') {
1.1.1.14  root     5551:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5552:                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5553:                                co.X = 0;
                   5554:                                co.Y = csbi.srWindow.Top;
1.1       root     5555:                        } else if(tmp[1] == '[') {
                   5556:                                int param[256], params = 0;
                   5557:                                memset(param, 0, sizeof(param));
                   5558:                                for(int i = 2; i < p; i++) {
                   5559:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   5560:                                                param[params] *= 10;
                   5561:                                                param[params] += tmp[i] - '0';
                   5562:                                        } else {
                   5563:                                                params++;
                   5564:                                        }
                   5565:                                }
                   5566:                                if(data == 'A') {
1.1.1.14  root     5567:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     5568:                                } else if(data == 'B') {
1.1.1.14  root     5569:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     5570:                                } else if(data == 'C') {
1.1.1.14  root     5571:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     5572:                                } else if(data == 'D') {
1.1.1.14  root     5573:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     5574:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     5575:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   5576:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     5577:                                } else if(data == 'J') {
1.1.1.14  root     5578:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5579:                                        if(param[0] == 0) {
                   5580:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5581:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5582:                                                if(co.Y < csbi.srWindow.Bottom) {
                   5583:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5584:                                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5585:                                                }
                   5586:                                        } else if(param[0] == 1) {
1.1.1.14  root     5587:                                                if(co.Y > csbi.srWindow.Top) {
                   5588:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
1.1.1.60  root     5589:                                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5590:                                                }
                   5591:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.60  root     5592:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5593:                                        } else if(param[0] == 2) {
1.1.1.14  root     5594:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5595:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5596:                                                co.X = co.Y = 0;
                   5597:                                        }
                   5598:                                } else if(data == 'K') {
1.1.1.14  root     5599:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5600:                                        if(param[0] == 0) {
                   5601:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5602:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5603:                                        } else if(param[0] == 1) {
                   5604:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.60  root     5605:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5606:                                        } else if(param[0] == 2) {
                   5607:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5608:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5609:                                        }
                   5610:                                } else if(data == 'L') {
1.1.1.14  root     5611:                                        if(params == 0) {
                   5612:                                                param[0] = 1;
1.1       root     5613:                                        }
1.1.1.14  root     5614:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5615:                                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5616:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5617:                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5618:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5619:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.60  root     5620:                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5621:                                        co.X = 0;
                   5622:                                } else if(data == 'M') {
1.1.1.14  root     5623:                                        if(params == 0) {
                   5624:                                                param[0] = 1;
                   5625:                                        }
                   5626:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5627:                                                clear_scr_buffer(csbi.wAttributes);
                   5628:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5629:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5630:                                        } else {
1.1.1.14  root     5631:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5632:                                                ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5633:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5634:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5635:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5636:                                        }
                   5637:                                        co.X = 0;
                   5638:                                } else if(data == 'h') {
                   5639:                                        if(tmp[2] == '>' && tmp[3] == '5') {
1.1.1.60  root     5640:                                                ci_new.bVisible = FALSE;
1.1       root     5641:                                        }
                   5642:                                } else if(data == 'l') {
                   5643:                                        if(tmp[2] == '>' && tmp[3] == '5') {
1.1.1.60  root     5644:                                                ci_new.bVisible = TRUE;
1.1       root     5645:                                        }
                   5646:                                } else if(data == 'm') {
                   5647:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5648:                                        int reverse = 0, hidden = 0;
                   5649:                                        for(int i = 0; i < params; i++) {
                   5650:                                                if(param[i] == 1) {
                   5651:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5652:                                                } else if(param[i] == 4) {
                   5653:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5654:                                                } else if(param[i] == 7) {
                   5655:                                                        reverse = 1;
                   5656:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5657:                                                        hidden = 1;
                   5658:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5659:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5660:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5661:                                                                param[i] -= 16;
                   5662:                                                        } else {
                   5663:                                                                param[i] -= 30;
                   5664:                                                        }
                   5665:                                                        if(param[i] & 1) {
                   5666:                                                                wAttributes |= FOREGROUND_RED;
                   5667:                                                        }
                   5668:                                                        if(param[i] & 2) {
                   5669:                                                                wAttributes |= FOREGROUND_GREEN;
                   5670:                                                        }
                   5671:                                                        if(param[i] & 4) {
                   5672:                                                                wAttributes |= FOREGROUND_BLUE;
                   5673:                                                        }
                   5674:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5675:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5676:                                                        if((param[i] - 40) & 1) {
                   5677:                                                                wAttributes |= BACKGROUND_RED;
                   5678:                                                        }
                   5679:                                                        if((param[i] - 40) & 2) {
                   5680:                                                                wAttributes |= BACKGROUND_GREEN;
                   5681:                                                        }
                   5682:                                                        if((param[i] - 40) & 4) {
                   5683:                                                                wAttributes |= BACKGROUND_BLUE;
                   5684:                                                        }
                   5685:                                                }
                   5686:                                        }
                   5687:                                        if(reverse) {
                   5688:                                                wAttributes &= ~0xff;
                   5689:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5690:                                        }
                   5691:                                        if(hidden) {
                   5692:                                                wAttributes &= ~0x0f;
                   5693:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5694:                                        }
                   5695:                                } else if(data == 'n') {
                   5696:                                        if(param[0] == 6) {
                   5697:                                                char tmp[16];
                   5698:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5699:                                                int len = strlen(tmp);
1.1.1.32  root     5700:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5701: #ifdef USE_SERVICE_THREAD
                   5702:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5703: #endif
1.1.1.32  root     5704:                                                        for(int i = 0; i < len; i++) {
1.1.1.51  root     5705:                                                                pcbios_set_key_buffer(tmp[i], 0x00);
1.1.1.32  root     5706:                                                        }
1.1.1.35  root     5707: #ifdef USE_SERVICE_THREAD
                   5708:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5709: #endif
1.1       root     5710:                                                }
                   5711:                                        }
                   5712:                                } else if(data == 's') {
                   5713:                                        stored_x = co.X;
                   5714:                                        stored_y = co.Y;
                   5715:                                        stored_a = wAttributes;
                   5716:                                } else if(data == 'u') {
                   5717:                                        co.X = stored_x;
                   5718:                                        co.Y = stored_y;
                   5719:                                        wAttributes = stored_a;
                   5720:                                }
                   5721:                        }
                   5722:                        if(co.X < 0) {
                   5723:                                co.X = 0;
                   5724:                        } else if(co.X >= csbi.dwSize.X) {
                   5725:                                co.X = csbi.dwSize.X - 1;
                   5726:                        }
1.1.1.14  root     5727:                        if(co.Y < csbi.srWindow.Top) {
                   5728:                                co.Y = csbi.srWindow.Top;
                   5729:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5730:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5731:                        }
                   5732:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5733:                                SetConsoleCursorPosition(hStdout, co);
                   5734:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5735:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5736:                                cursor_moved = false;
                   5737:                        }
                   5738:                        if(wAttributes != csbi.wAttributes) {
                   5739:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5740:                        }
                   5741:                        p = is_esc = 0;
                   5742:                }
                   5743:                return;
                   5744:        } else {
                   5745:                if(msdos_lead_byte_check(data)) {
                   5746:                        is_kanji = 1;
                   5747:                        return;
                   5748:                } else if(data == 0x1b) {
                   5749:                        is_esc = 1;
                   5750:                        return;
                   5751:                }
                   5752:        }
1.1.1.20  root     5753:        
                   5754:        DWORD q = 0, num;
                   5755:        is_kanji = 0;
                   5756:        for(int i = 0; i < p; i++) {
                   5757:                UINT8 c = tmp[i];
                   5758:                if(is_kanji) {
                   5759:                        is_kanji = 0;
                   5760:                } else if(msdos_lead_byte_check(data)) {
                   5761:                        is_kanji = 1;
                   5762:                } else if(msdos_ctrl_code_check(data)) {
                   5763:                        out[q++] = '^';
                   5764:                        c += 'A' - 1;
                   5765:                }
                   5766:                out[q++] = c;
                   5767:        }
1.1.1.59  root     5768:        if(cursor_moved_by_crtc) {
                   5769:                if(!restore_console_on_exit) {
                   5770:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5771:                        scr_top = csbi.srWindow.Top;
                   5772:                }
                   5773:                co.X = mem[0x450 + REG8(BH) * 2];
                   5774:                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   5775:                SetConsoleCursorPosition(hStdout, co);
                   5776:                cursor_moved_by_crtc = false;
                   5777:        }
1.1.1.34  root     5778:        if(q == 1 && out[0] == 0x08) {
                   5779:                // back space
                   5780:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5781:                if(csbi.dwCursorPosition.X > 0) {
                   5782:                        co.X = csbi.dwCursorPosition.X - 1;
                   5783:                        co.Y = csbi.dwCursorPosition.Y;
                   5784:                        SetConsoleCursorPosition(hStdout, co);
                   5785:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5786:                        co.X = csbi.dwSize.X - 1;
                   5787:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5788:                        SetConsoleCursorPosition(hStdout, co);
                   5789:                } else {
1.1.1.60  root     5790:                        WriteConsoleA(hStdout, out, q, &num, NULL); // to make sure
1.1.1.34  root     5791:                }
                   5792:        } else {
1.1.1.60  root     5793:                WriteConsoleA(hStdout, out, q, &num, NULL);
1.1.1.34  root     5794:        }
1.1       root     5795:        p = 0;
1.1.1.14  root     5796:        
1.1.1.15  root     5797:        if(!restore_console_on_exit) {
                   5798:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5799:                scr_top = csbi.srWindow.Top;
                   5800:        }
1.1       root     5801:        cursor_moved = true;
                   5802: }
                   5803: 
                   5804: int msdos_aux_in()
                   5805: {
1.1.1.21  root     5806:        msdos_stdio_reopen();
                   5807:        
1.1.1.20  root     5808:        process_t *process = msdos_process_info_get(current_psp);
                   5809:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5810:        
                   5811:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5812:                char data = 0;
1.1.1.37  root     5813:                msdos_read(fd, &data, 1);
1.1       root     5814:                return(data);
                   5815:        } else {
                   5816:                return(EOF);
                   5817:        }
                   5818: }
                   5819: 
                   5820: void msdos_aux_out(char data)
                   5821: {
1.1.1.21  root     5822:        msdos_stdio_reopen();
                   5823:        
1.1.1.20  root     5824:        process_t *process = msdos_process_info_get(current_psp);
                   5825:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5826:        
                   5827:        if(fd < process->max_files && file_handler[fd].valid) {
                   5828:                msdos_write(fd, &data, 1);
1.1       root     5829:        }
                   5830: }
                   5831: 
                   5832: void msdos_prn_out(char data)
                   5833: {
1.1.1.21  root     5834:        msdos_stdio_reopen();
                   5835:        
1.1.1.20  root     5836:        process_t *process = msdos_process_info_get(current_psp);
                   5837:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5838:        
                   5839:        if(fd < process->max_files && file_handler[fd].valid) {
                   5840:                msdos_write(fd, &data, 1);
1.1       root     5841:        }
                   5842: }
                   5843: 
                   5844: // memory control
                   5845: 
1.1.1.52  root     5846: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name = NULL)
1.1       root     5847: {
                   5848:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5849:        
                   5850:        mcb->mz = mz;
                   5851:        mcb->psp = psp;
1.1.1.30  root     5852:        mcb->paragraphs = paragraphs;
1.1.1.39  root     5853:        
                   5854:        if(prog_name != NULL) {
                   5855:                memset(mcb->prog_name, 0, 8);
                   5856:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
                   5857:        }
1.1       root     5858:        return(mcb);
                   5859: }
                   5860: 
                   5861: void msdos_mcb_check(mcb_t *mcb)
                   5862: {
                   5863:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5864:                #if 0
                   5865:                        // shutdown now !!!
                   5866:                        fatalerror("broken memory control block\n");
                   5867:                #else
                   5868:                        // return error code and continue
                   5869:                        throw(0x07); // broken memory control block
                   5870:                #endif
1.1       root     5871:        }
                   5872: }
                   5873: 
1.1.1.39  root     5874: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5875: {
                   5876:        int mcb_seg = seg - 1;
                   5877:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5878:        msdos_mcb_check(mcb);
                   5879:        
1.1.1.30  root     5880:        if(mcb->paragraphs > paragraphs) {
1.1       root     5881:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5882:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5883:                
                   5884:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5885:                mcb->mz = 'M';
1.1.1.30  root     5886:                mcb->paragraphs = paragraphs;
1.1       root     5887:        }
                   5888: }
                   5889: 
                   5890: void msdos_mem_merge(int seg)
                   5891: {
                   5892:        int mcb_seg = seg - 1;
                   5893:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5894:        msdos_mcb_check(mcb);
                   5895:        
                   5896:        while(1) {
                   5897:                if(mcb->mz == 'Z') {
                   5898:                        break;
                   5899:                }
1.1.1.30  root     5900:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5901:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5902:                msdos_mcb_check(next_mcb);
                   5903:                
                   5904:                if(next_mcb->psp != 0) {
                   5905:                        break;
                   5906:                }
                   5907:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5908:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5909:        }
                   5910: }
                   5911: 
1.1.1.8   root     5912: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5913: {
                   5914:        while(1) {
                   5915:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5916:                bool last_block;
1.1       root     5917:                
1.1.1.14  root     5918:                if(mcb->psp == 0) {
                   5919:                        msdos_mem_merge(mcb_seg + 1);
                   5920:                } else {
                   5921:                        msdos_mcb_check(mcb);
                   5922:                }
1.1.1.33  root     5923:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5924:                        // check if the next is dummy mcb to link to umb
                   5925:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5926:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5927:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5928:                }
                   5929:                if(!(new_process && !last_block)) {
1.1.1.30  root     5930:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5931:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5932:                                mcb->psp = current_psp;
                   5933:                                return(mcb_seg + 1);
                   5934:                        }
                   5935:                }
                   5936:                if(mcb->mz == 'Z') {
                   5937:                        break;
                   5938:                }
1.1.1.30  root     5939:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5940:        }
                   5941:        return(-1);
                   5942: }
                   5943: 
                   5944: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5945: {
                   5946:        int mcb_seg = seg - 1;
                   5947:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5948:        msdos_mcb_check(mcb);
1.1.1.30  root     5949:        int current_paragraphs = mcb->paragraphs;
1.1       root     5950:        
                   5951:        msdos_mem_merge(seg);
1.1.1.30  root     5952:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5953:                if(max_paragraphs) {
1.1.1.30  root     5954:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5955:                }
1.1       root     5956:                msdos_mem_split(seg, current_paragraphs);
                   5957:                return(-1);
                   5958:        }
                   5959:        msdos_mem_split(seg, paragraphs);
                   5960:        return(0);
                   5961: }
                   5962: 
                   5963: void msdos_mem_free(int seg)
                   5964: {
                   5965:        int mcb_seg = seg - 1;
                   5966:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5967:        msdos_mcb_check(mcb);
                   5968:        
                   5969:        mcb->psp = 0;
                   5970:        msdos_mem_merge(seg);
                   5971: }
                   5972: 
1.1.1.8   root     5973: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5974: {
                   5975:        int max_paragraphs = 0;
                   5976:        
                   5977:        while(1) {
                   5978:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5979:                bool last_block;
                   5980:                
1.1       root     5981:                msdos_mcb_check(mcb);
                   5982:                
1.1.1.33  root     5983:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5984:                        // check if the next is dummy mcb to link to umb
                   5985:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5986:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5987:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5988:                }
                   5989:                if(!(new_process && !last_block)) {
1.1.1.30  root     5990:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   5991:                                max_paragraphs = mcb->paragraphs;
1.1       root     5992:                        }
                   5993:                }
                   5994:                if(mcb->mz == 'Z') {
                   5995:                        break;
                   5996:                }
1.1.1.30  root     5997:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5998:        }
1.1.1.14  root     5999:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     6000: }
                   6001: 
1.1.1.8   root     6002: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   6003: {
                   6004:        int last_seg = -1;
                   6005:        
                   6006:        while(1) {
                   6007:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   6008:                msdos_mcb_check(mcb);
                   6009:                
1.1.1.14  root     6010:                if(mcb->psp == psp) {
1.1.1.8   root     6011:                        last_seg = mcb_seg;
                   6012:                }
1.1.1.14  root     6013:                if(mcb->mz == 'Z') {
                   6014:                        break;
                   6015:                }
1.1.1.30  root     6016:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     6017:        }
                   6018:        return(last_seg);
                   6019: }
                   6020: 
1.1.1.19  root     6021: int msdos_mem_get_umb_linked()
                   6022: {
1.1.1.33  root     6023:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   6024:        msdos_mcb_check(mcb);
1.1.1.19  root     6025:        
1.1.1.33  root     6026:        if(mcb->mz == 'M') {
                   6027:                return(-1);
1.1.1.19  root     6028:        }
                   6029:        return(0);
                   6030: }
                   6031: 
1.1.1.33  root     6032: void msdos_mem_link_umb()
1.1.1.19  root     6033: {
1.1.1.33  root     6034:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   6035:        msdos_mcb_check(mcb);
1.1.1.19  root     6036:        
1.1.1.33  root     6037:        mcb->mz = 'M';
                   6038:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39  root     6039:        
                   6040:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     6041: }
                   6042: 
1.1.1.33  root     6043: void msdos_mem_unlink_umb()
1.1.1.19  root     6044: {
1.1.1.33  root     6045:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   6046:        msdos_mcb_check(mcb);
1.1.1.19  root     6047:        
1.1.1.33  root     6048:        mcb->mz = 'Z';
                   6049:        mcb->paragraphs = 0;
1.1.1.39  root     6050:        
                   6051:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     6052: }
                   6053: 
1.1.1.29  root     6054: #ifdef SUPPORT_HMA
                   6055: 
                   6056: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   6057: {
                   6058:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6059:        
                   6060:        mcb->ms[0] = 'M';
                   6061:        mcb->ms[1] = 'S';
                   6062:        mcb->owner = owner;
                   6063:        mcb->size = size;
                   6064:        mcb->next = next;
                   6065:        return(mcb);
                   6066: }
                   6067: 
                   6068: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   6069: {
                   6070:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   6071: }
                   6072: 
                   6073: int msdos_hma_mem_split(int offset, int size)
                   6074: {
                   6075:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6076:        
                   6077:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6078:                return(-1);
                   6079:        }
                   6080:        if(mcb->size >= size + 0x10) {
                   6081:                int new_offset = offset + 0x10 + size;
                   6082:                int new_size = mcb->size - 0x10 - size;
                   6083:                
                   6084:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   6085:                mcb->size = size;
                   6086:                mcb->next = new_offset;
                   6087:                return(0);
                   6088:        }
                   6089:        return(-1);
                   6090: }
                   6091: 
                   6092: void msdos_hma_mem_merge(int offset)
                   6093: {
                   6094:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6095:        
                   6096:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6097:                return;
                   6098:        }
                   6099:        while(1) {
                   6100:                if(mcb->next == 0) {
                   6101:                        break;
                   6102:                }
                   6103:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   6104:                
                   6105:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   6106:                        return;
                   6107:                }
                   6108:                if(next_mcb->owner != 0) {
                   6109:                        break;
                   6110:                }
                   6111:                mcb->size += 0x10 + next_mcb->size;
                   6112:                mcb->next = next_mcb->next;
                   6113:        }
                   6114: }
                   6115: 
                   6116: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   6117: {
                   6118:        int offset = 0x10; // first mcb in HMA
                   6119:        
                   6120:        while(1) {
                   6121:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6122:                
                   6123:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   6124:                        return(-1);
                   6125:                }
                   6126:                if(mcb->owner == 0) {
                   6127:                        msdos_hma_mem_merge(offset);
                   6128:                }
                   6129:                if(mcb->owner == 0 && mcb->size >= size) {
                   6130:                        msdos_hma_mem_split(offset, size);
                   6131:                        mcb->owner = owner;
                   6132:                        return(offset);
                   6133:                }
                   6134:                if(mcb->next == 0) {
                   6135:                        break;
                   6136:                }
                   6137:                offset = mcb->next;
                   6138:        }
                   6139:        return(-1);
                   6140: }
                   6141: 
                   6142: int msdos_hma_mem_realloc(int offset, int size)
                   6143: {
                   6144:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6145:        
                   6146:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6147:                return(-1);
                   6148:        }
                   6149:        if(mcb->size < size) {
                   6150:                return(-1);
                   6151:        }
                   6152:        msdos_hma_mem_split(offset, size);
                   6153:        return(0);
                   6154: }
                   6155: 
                   6156: void msdos_hma_mem_free(int offset)
                   6157: {
                   6158:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6159:        
                   6160:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6161:                return;
                   6162:        }
                   6163:        mcb->owner = 0;
                   6164:        msdos_hma_mem_merge(offset);
                   6165: }
                   6166: 
                   6167: int msdos_hma_mem_get_free(int *available_offset)
                   6168: {
                   6169:        int offset = 0x10; // first mcb in HMA
                   6170:        int size = 0;
                   6171:        
                   6172:        while(1) {
                   6173:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6174:                
                   6175:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   6176:                        return(0);
                   6177:                }
                   6178:                if(mcb->owner == 0 && size < mcb->size) {
                   6179:                        if(available_offset != NULL) {
                   6180:                                *available_offset = offset;
                   6181:                        }
                   6182:                        size = mcb->size;
                   6183:                }
                   6184:                if(mcb->next == 0) {
                   6185:                        break;
                   6186:                }
                   6187:                offset = mcb->next;
                   6188:        }
                   6189:        return(size);
                   6190: }
                   6191: 
                   6192: #endif
                   6193: 
1.1       root     6194: // environment
                   6195: 
1.1.1.45  root     6196: void msdos_env_set_argv(int env_seg, const char *argv)
1.1       root     6197: {
                   6198:        char *dst = (char *)(mem + (env_seg << 4));
                   6199:        
                   6200:        while(1) {
                   6201:                if(dst[0] == 0) {
                   6202:                        break;
                   6203:                }
                   6204:                dst += strlen(dst) + 1;
                   6205:        }
                   6206:        *dst++ = 0; // end of environment
                   6207:        *dst++ = 1; // top of argv[0]
                   6208:        *dst++ = 0;
                   6209:        memcpy(dst, argv, strlen(argv));
                   6210:        dst += strlen(argv);
                   6211:        *dst++ = 0;
                   6212:        *dst++ = 0;
                   6213: }
                   6214: 
1.1.1.45  root     6215: const char *msdos_env_get_argv(int env_seg)
1.1       root     6216: {
                   6217:        static char env[ENV_SIZE];
                   6218:        char *src = env;
                   6219:        
                   6220:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   6221:        while(1) {
                   6222:                if(src[0] == 0) {
                   6223:                        if(src[1] == 1) {
                   6224:                                return(src + 3);
                   6225:                        }
                   6226:                        break;
                   6227:                }
                   6228:                src += strlen(src) + 1;
                   6229:        }
                   6230:        return(NULL);
                   6231: }
                   6232: 
1.1.1.45  root     6233: const char *msdos_env_get(int env_seg, const char *name)
1.1       root     6234: {
                   6235:        static char env[ENV_SIZE];
                   6236:        char *src = env;
                   6237:        
                   6238:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   6239:        while(1) {
                   6240:                if(src[0] == 0) {
                   6241:                        break;
                   6242:                }
                   6243:                int len = strlen(src);
                   6244:                char *n = my_strtok(src, "=");
                   6245:                char *v = src + strlen(n) + 1;
                   6246:                
                   6247:                if(_stricmp(name, n) == 0) {
                   6248:                        return(v);
                   6249:                }
                   6250:                src += len + 1;
                   6251:        }
                   6252:        return(NULL);
                   6253: }
                   6254: 
1.1.1.45  root     6255: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1       root     6256: {
                   6257:        char env[ENV_SIZE];
                   6258:        char *src = env;
                   6259:        char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45  root     6260:        const char *argv = msdos_env_get_argv(env_seg);
1.1       root     6261:        int done = 0;
                   6262:        
                   6263:        memcpy(src, dst, ENV_SIZE);
                   6264:        memset(dst, 0, ENV_SIZE);
                   6265:        while(1) {
                   6266:                if(src[0] == 0) {
                   6267:                        break;
                   6268:                }
                   6269:                int len = strlen(src);
                   6270:                char *n = my_strtok(src, "=");
                   6271:                char *v = src + strlen(n) + 1;
                   6272:                char tmp[1024];
                   6273:                
                   6274:                if(_stricmp(name, n) == 0) {
                   6275:                        sprintf(tmp, "%s=%s", n, value);
                   6276:                        done = 1;
                   6277:                } else {
                   6278:                        sprintf(tmp, "%s=%s", n, v);
                   6279:                }
                   6280:                memcpy(dst, tmp, strlen(tmp));
                   6281:                dst += strlen(tmp) + 1;
                   6282:                src += len + 1;
                   6283:        }
                   6284:        if(!done) {
                   6285:                char tmp[1024];
                   6286:                
                   6287:                sprintf(tmp, "%s=%s", name, value);
                   6288:                memcpy(dst, tmp, strlen(tmp));
                   6289:                dst += strlen(tmp) + 1;
                   6290:        }
                   6291:        if(argv) {
                   6292:                *dst++ = 0; // end of environment
                   6293:                *dst++ = 1; // top of argv[0]
                   6294:                *dst++ = 0;
                   6295:                memcpy(dst, argv, strlen(argv));
                   6296:                dst += strlen(argv);
                   6297:                *dst++ = 0;
                   6298:                *dst++ = 0;
                   6299:        }
                   6300: }
                   6301: 
                   6302: // process
                   6303: 
1.1.1.8   root     6304: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     6305: {
                   6306:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6307:        
                   6308:        memset(psp, 0, PSP_SIZE);
                   6309:        psp->exit[0] = 0xcd;
                   6310:        psp->exit[1] = 0x20;
1.1.1.8   root     6311:        psp->first_mcb = mcb_seg;
1.1.1.46  root     6312: #if 1
1.1.1.49  root     6313:        psp->call5[0] = 0xcd;   // int 30h
                   6314:        psp->call5[1] = 0x30;
1.1.1.46  root     6315:        psp->call5[2] = 0xc3;   // ret
                   6316: #else
                   6317:        psp->call5[0] = 0x8a;   // mov ah, cl
                   6318:        psp->call5[1] = 0xe1;
                   6319:        psp->call5[2] = 0xcd;   // int 21h
                   6320:        psp->call5[3] = 0x21;
                   6321:        psp->call5[4] = 0xc3;   // ret
                   6322: #endif
1.1       root     6323:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   6324:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   6325:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   6326:        psp->parent_psp = parent_psp;
1.1.1.20  root     6327:        if(parent_psp == (UINT16)-1) {
                   6328:                for(int i = 0; i < 20; i++) {
                   6329:                        if(file_handler[i].valid) {
                   6330:                                psp->file_table[i] = i;
                   6331:                        } else {
                   6332:                                psp->file_table[i] = 0xff;
                   6333:                        }
1.1       root     6334:                }
1.1.1.20  root     6335:        } else {
                   6336:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     6337:        }
                   6338:        psp->env_seg = env_seg;
                   6339:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     6340:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     6341:        psp->file_table_size = 20;
                   6342:        psp->file_table_ptr.w.l = 0x18;
                   6343:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     6344:        psp->service[0] = 0xcd;
                   6345:        psp->service[1] = 0x21;
                   6346:        psp->service[2] = 0xcb;
                   6347:        return(psp);
                   6348: }
                   6349: 
1.1.1.20  root     6350: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   6351: {
                   6352:        if(psp_seg && fd < 20) {
                   6353:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6354:                psp->file_table[fd] = value;
                   6355:        }
                   6356: }
                   6357: 
                   6358: int msdos_psp_get_file_table(int fd, int psp_seg)
                   6359: {
                   6360:        if(psp_seg && fd < 20) {
                   6361:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6362:                fd = psp->file_table[fd];
                   6363:        }
                   6364:        return fd;
                   6365: }
                   6366: 
1.1.1.52  root     6367: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al, bool first_process = false)
1.1       root     6368: {
                   6369:        // load command file
                   6370:        int fd = -1;
1.1.1.45  root     6371:        int sio_port = 0;
                   6372:        int lpt_port = 0;
1.1       root     6373:        int dos_command = 0;
1.1.1.24  root     6374:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     6375:        char pipe_stdin_path[MAX_PATH] = {0};
                   6376:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39  root     6377:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     6378:        
                   6379:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6380:        int opt_len = mem[opt_ofs];
                   6381:        memset(opt, 0, sizeof(opt));
                   6382:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6383:        
1.1.1.14  root     6384:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   6385:                // this is a batch file, run command.com
                   6386:                char tmp[MAX_PATH];
                   6387:                if(opt_len != 0) {
                   6388:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   6389:                } else {
                   6390:                        sprintf(tmp, "/C %s", cmd);
                   6391:                }
                   6392:                strcpy(opt, tmp);
                   6393:                opt_len = strlen(opt);
                   6394:                mem[opt_ofs] = opt_len;
                   6395:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6396:                strcpy(command, comspec_path);
                   6397:                strcpy(name_tmp, "COMMAND.COM");
                   6398:        } else {
                   6399:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   6400:                        // redirect C:\COMMAND.COM to comspec_path
                   6401:                        strcpy(command, comspec_path);
                   6402:                } else {
                   6403:                        strcpy(command, cmd);
                   6404:                }
1.1.1.60  root     6405:                if(GetFullPathNameA(command, MAX_PATH, path, &name) == 0) {
1.1.1.24  root     6406:                        return(-1);
                   6407:                }
1.1.1.14  root     6408:                memset(name_tmp, 0, sizeof(name_tmp));
                   6409:                strcpy(name_tmp, name);
                   6410:                
                   6411:                // check command.com
1.1.1.38  root     6412:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   6413:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     6414:                        if(opt_len == 0) {
                   6415: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   6416:                                process_t *current_process = NULL;
                   6417:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   6418:                                        if(process[i].psp == current_psp) {
                   6419:                                                current_process = &process[i];
                   6420:                                                break;
                   6421:                                        }
                   6422:                                }
                   6423:                                if(current_process != NULL) {
                   6424:                                        param->cmd_line.dw = current_process->dta.dw;
                   6425:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6426:                                        opt_len = mem[opt_ofs];
                   6427:                                        memset(opt, 0, sizeof(opt));
                   6428:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6429:                                }
                   6430:                        }
                   6431:                        for(int i = 0; i < opt_len; i++) {
                   6432:                                if(opt[i] == ' ') {
                   6433:                                        continue;
                   6434:                                }
                   6435:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   6436:                                        for(int j = i + 3; j < opt_len; j++) {
                   6437:                                                if(opt[j] == ' ') {
                   6438:                                                        continue;
                   6439:                                                }
                   6440:                                                char *token = my_strtok(opt + j, " ");
                   6441:                                                
1.1.1.38  root     6442:                                                strcpy(command, token);
                   6443:                                                char tmp[MAX_PATH];
                   6444:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39  root     6445:                                                strcpy(opt, "");
                   6446:                                                for(int i = 0; i < strlen(tmp); i++) {
                   6447:                                                        if(tmp[i] != ' ') {
                   6448:                                                                strcpy(opt, tmp + i);
                   6449:                                                                break;
                   6450:                                                        }
                   6451:                                                }
                   6452:                                                strcpy(tmp, opt);
1.1.1.38  root     6453:                                                
                   6454:                                                if(al == 0x00) {
1.1.1.39  root     6455:                                                        #define GET_FILE_PATH() { \
                   6456:                                                                if(token[0] != '>' && token[0] != '<') { \
                   6457:                                                                        token++; \
                   6458:                                                                } \
                   6459:                                                                token++; \
                   6460:                                                                while(*token == ' ') { \
                   6461:                                                                        token++; \
                   6462:                                                                } \
                   6463:                                                                char *ptr = token; \
                   6464:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
                   6465:                                                                        ptr++; \
                   6466:                                                                } \
                   6467:                                                                *ptr = '\0'; \
                   6468:                                                        }
                   6469:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
                   6470:                                                                GET_FILE_PATH();
1.1.1.38  root     6471:                                                                strcpy(pipe_stdin_path, token);
                   6472:                                                                strcpy(opt, tmp);
                   6473:                                                        }
1.1.1.39  root     6474:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
                   6475:                                                                GET_FILE_PATH();
1.1.1.38  root     6476:                                                                strcpy(pipe_stdout_path, token);
                   6477:                                                                strcpy(opt, tmp);
                   6478:                                                        }
1.1.1.39  root     6479:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6480:                                                                GET_FILE_PATH();
                   6481:                                                                strcpy(pipe_stderr_path, token);
                   6482:                                                                strcpy(opt, tmp);
                   6483:                                                        }
                   6484:                                                        #undef GET_FILE_PATH
                   6485:                                                        
                   6486:                                                        if((token = strstr(opt, "0<")) != NULL) {
                   6487:                                                                *token = '\0';
                   6488:                                                        }
                   6489:                                                        if((token = strstr(opt, "1>")) != NULL) {
                   6490:                                                                *token = '\0';
                   6491:                                                        }
                   6492:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6493:                                                                *token = '\0';
                   6494:                                                        }
1.1.1.38  root     6495:                                                        if((token = strstr(opt, "<")) != NULL) {
                   6496:                                                                *token = '\0';
                   6497:                                                        }
                   6498:                                                        if((token = strstr(opt, ">")) != NULL) {
                   6499:                                                                *token = '\0';
                   6500:                                                        }
1.1.1.14  root     6501:                                                }
1.1.1.39  root     6502:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
                   6503:                                                        opt[i] = '\0';
                   6504:                                                }
1.1.1.38  root     6505:                                                opt_len = strlen(opt);
                   6506:                                                mem[opt_ofs] = opt_len;
                   6507:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6508:                                                dos_command = 1;
1.1.1.14  root     6509:                                                break;
1.1       root     6510:                                        }
                   6511:                                }
1.1.1.14  root     6512:                                break;
1.1       root     6513:                        }
                   6514:                }
                   6515:        }
                   6516:        
                   6517:        // load command file
                   6518:        strcpy(path, command);
                   6519:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6520:                sprintf(path, "%s.COM", command);
                   6521:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6522:                        sprintf(path, "%s.EXE", command);
                   6523:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     6524:                                sprintf(path, "%s.BAT", command);
                   6525:                                if(_access(path, 0) == 0) {
                   6526:                                        // this is a batch file, run command.com
                   6527:                                        char tmp[MAX_PATH];
                   6528:                                        if(opt_len != 0) {
                   6529:                                                sprintf(tmp, "/C %s %s", path, opt);
                   6530:                                        } else {
                   6531:                                                sprintf(tmp, "/C %s", path);
                   6532:                                        }
                   6533:                                        strcpy(opt, tmp);
                   6534:                                        opt_len = strlen(opt);
                   6535:                                        mem[opt_ofs] = opt_len;
                   6536:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6537:                                        strcpy(path, comspec_path);
                   6538:                                        strcpy(name_tmp, "COMMAND.COM");
                   6539:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6540:                                } else {
                   6541:                                        // search path in parent environments
                   6542:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45  root     6543:                                        const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14  root     6544:                                        if(env != NULL) {
                   6545:                                                char env_path[4096];
                   6546:                                                strcpy(env_path, env);
                   6547:                                                char *token = my_strtok(env_path, ";");
                   6548:                                                
                   6549:                                                while(token != NULL) {
                   6550:                                                        if(strlen(token) != 0) {
                   6551:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   6552:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6553:                                                                        break;
                   6554:                                                                }
                   6555:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   6556:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6557:                                                                        break;
                   6558:                                                                }
                   6559:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   6560:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6561:                                                                        break;
                   6562:                                                                }
                   6563:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   6564:                                                                if(_access(path, 0) == 0) {
                   6565:                                                                        // this is a batch file, run command.com
                   6566:                                                                        char tmp[MAX_PATH];
                   6567:                                                                        if(opt_len != 0) {
                   6568:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   6569:                                                                        } else {
                   6570:                                                                                sprintf(tmp, "/C %s", path);
                   6571:                                                                        }
                   6572:                                                                        strcpy(opt, tmp);
                   6573:                                                                        opt_len = strlen(opt);
                   6574:                                                                        mem[opt_ofs] = opt_len;
                   6575:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6576:                                                                        strcpy(path, comspec_path);
                   6577:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   6578:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6579:                                                                        break;
                   6580:                                                                }
1.1.1.8   root     6581:                                                        }
1.1.1.14  root     6582:                                                        token = my_strtok(NULL, ";");
1.1       root     6583:                                                }
                   6584:                                        }
                   6585:                                }
                   6586:                        }
                   6587:                }
                   6588:        }
                   6589:        if(fd == -1) {
1.1.1.38  root     6590:                // we can not find command.com in the path, so open comspec_path
                   6591:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6592:                        strcpy(command, comspec_path);
                   6593:                        strcpy(path, command);
                   6594:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6595:                }
                   6596:        }
                   6597:        if(fd == -1) {
1.1.1.52  root     6598:                if(!first_process && al == 0 && dos_command) {
1.1       root     6599:                        // may be dos command
                   6600:                        char tmp[MAX_PATH];
1.1.1.52  root     6601:                        if(opt_len != 0) {
                   6602:                                sprintf(tmp, "%s %s", command, opt);
                   6603:                        } else {
                   6604:                                sprintf(tmp, "%s", command);
                   6605:                        }
                   6606:                        retval = system(tmp);
1.1       root     6607:                        return(0);
                   6608:                } else {
                   6609:                        return(-1);
                   6610:                }
                   6611:        }
1.1.1.52  root     6612:        memset(file_buffer, 0, sizeof(file_buffer));
1.1       root     6613:        _read(fd, file_buffer, sizeof(file_buffer));
                   6614:        _close(fd);
                   6615:        
1.1.1.52  root     6616:        // check if this is win32 program
                   6617:        if(!first_process && al == 0) {
                   6618:                UINT16 sign_dos = *(UINT16 *)(file_buffer + 0x00);
                   6619:                UINT32 e_lfanew = *(UINT32 *)(file_buffer + 0x3c);
                   6620:                if(sign_dos == IMAGE_DOS_SIGNATURE && e_lfanew >= 0x40 && e_lfanew < 0x400) {
                   6621:                        UINT32 sign_nt = *(UINT32 *)(file_buffer + e_lfanew + 0x00);
                   6622:                        UINT16 machine = *(UINT16 *)(file_buffer + e_lfanew + 0x04);
                   6623:                        if(sign_nt == IMAGE_NT_SIGNATURE && (machine == IMAGE_FILE_MACHINE_I386 || machine == IMAGE_FILE_MACHINE_AMD64)) {
                   6624:                                char tmp[MAX_PATH];
                   6625:                                if(opt_len != 0) {
                   6626:                                        sprintf(tmp, "\"%s\" %s", path, opt);
                   6627:                                } else {
                   6628:                                        sprintf(tmp, "\"%s\"", path);
                   6629:                                }
                   6630:                                retval = system(tmp);
                   6631:                                return(0);
                   6632:                        }
                   6633:                }
                   6634:        }
                   6635:        
1.1       root     6636:        // copy environment
1.1.1.29  root     6637:        int umb_linked, env_seg, psp_seg;
1.1       root     6638:        
1.1.1.29  root     6639:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6640:                msdos_mem_unlink_umb();
                   6641:        }
1.1.1.8   root     6642:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6643:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6644:                        if(umb_linked != 0) {
                   6645:                                msdos_mem_link_umb();
                   6646:                        }
                   6647:                        return(-1);
                   6648:                }
1.1       root     6649:        }
                   6650:        if(param->env_seg == 0) {
                   6651:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6652:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6653:        } else {
                   6654:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6655:        }
                   6656:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6657:        
                   6658:        // check exe header
                   6659:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6660:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6661:        UINT16 cs, ss, ip, sp;
                   6662:        
                   6663:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6664:                // memory allocation
                   6665:                int header_size = header->header_size * 16;
                   6666:                int load_size = header->pages * 512 - header_size;
                   6667:                if(header_size + load_size < 512) {
                   6668:                        load_size = 512 - header_size;
                   6669:                }
                   6670:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6671:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6672:                        msdos_mem_free(env_seg);
                   6673:                        return(-1);
                   6674:                }
                   6675:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6676:                if(paragraphs > free_paragraphs) {
                   6677:                        paragraphs = free_paragraphs;
                   6678:                }
1.1.1.8   root     6679:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6680:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6681:                                if(umb_linked != 0) {
                   6682:                                        msdos_mem_link_umb();
                   6683:                                }
                   6684:                                msdos_mem_free(env_seg);
                   6685:                                return(-1);
                   6686:                        }
1.1       root     6687:                }
                   6688:                // relocation
                   6689:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6690:                for(int i = 0; i < header->relocations; i++) {
                   6691:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6692:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6693:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6694:                }
                   6695:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6696:                // segments
                   6697:                cs = header->init_cs + start_seg;
                   6698:                ss = header->init_ss + start_seg;
                   6699:                ip = header->init_ip;
                   6700:                sp = header->init_sp - 2; // for symdeb
                   6701:        } else {
                   6702:                // memory allocation
                   6703:                paragraphs = free_paragraphs;
1.1.1.8   root     6704:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6705:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6706:                                if(umb_linked != 0) {
                   6707:                                        msdos_mem_link_umb();
                   6708:                                }
                   6709:                                msdos_mem_free(env_seg);
                   6710:                                return(-1);
                   6711:                        }
1.1       root     6712:                }
                   6713:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6714:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6715:                // segments
                   6716:                cs = ss = psp_seg;
                   6717:                ip = 0x100;
                   6718:                sp = 0xfffe;
                   6719:        }
1.1.1.29  root     6720:        if(umb_linked != 0) {
                   6721:                msdos_mem_link_umb();
                   6722:        }
1.1       root     6723:        
                   6724:        // create psp
1.1.1.3   root     6725:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6726:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6727:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6728:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6729:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6730:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6731:        
                   6732:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6733:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6734:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6735:        
1.1.1.4   root     6736:        for(int i = 0; i < 8; i++) {
                   6737:                if(name_tmp[i] == '.') {
                   6738:                        mcb_psp->prog_name[i] = '\0';
                   6739:                        break;
                   6740:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6741:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6742:                        i++;
                   6743:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6744:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6745:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6746:                } else {
                   6747:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6748:                }
                   6749:        }
                   6750:        
1.1       root     6751:        // process info
                   6752:        process_t *process = msdos_process_info_create(psp_seg);
                   6753:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6754: #ifdef USE_DEBUGGER
                   6755:        strcpy(process->module_path, path);
                   6756: #endif
1.1       root     6757:        process->dta.w.l = 0x80;
                   6758:        process->dta.w.h = psp_seg;
                   6759:        process->switchar = '/';
                   6760:        process->max_files = 20;
                   6761:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6762:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6763:        process->parent_ds = SREG(DS);
1.1.1.31  root     6764:        process->parent_es = SREG(ES);
1.1       root     6765:        
                   6766:        current_psp = psp_seg;
1.1.1.23  root     6767:        msdos_sda_update(current_psp);
1.1       root     6768:        
                   6769:        if(al == 0x00) {
                   6770:                int_10h_feh_called = int_10h_ffh_called = false;
                   6771:                
1.1.1.38  root     6772:                // pipe
                   6773:                if(pipe_stdin_path[0] != '\0') {
1.1.1.45  root     6774: //                     if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
                   6775:                        if(msdos_is_device_path(pipe_stdin_path)) {
                   6776:                                fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
                   6777:                        } else {
                   6778:                                fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
                   6779:                        }
                   6780:                        if(fd != -1) {
                   6781:                                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     6782:                                psp->file_table[0] = fd;
                   6783:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6784:                        }
                   6785:                }
                   6786:                if(pipe_stdout_path[0] != '\0') {
                   6787:                        if(_access(pipe_stdout_path, 0) == 0) {
1.1.1.60  root     6788:                                SetFileAttributesA(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6789:                                DeleteFileA(pipe_stdout_path);
1.1.1.38  root     6790:                        }
1.1.1.45  root     6791: //                     if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6792:                        if(msdos_is_device_path(pipe_stdout_path)) {
                   6793:                                fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6794:                        } else {
                   6795:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6796:                        }
                   6797:                        if(fd != -1) {
                   6798:                                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     6799:                                psp->file_table[1] = fd;
                   6800:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6801:                        }
                   6802:                }
1.1.1.39  root     6803:                if(pipe_stderr_path[0] != '\0') {
                   6804:                        if(_access(pipe_stderr_path, 0) == 0) {
1.1.1.60  root     6805:                                SetFileAttributesA(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
                   6806:                                DeleteFileA(pipe_stderr_path);
1.1.1.39  root     6807:                        }
1.1.1.45  root     6808: //                     if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6809:                        if(msdos_is_device_path(pipe_stderr_path)) {
                   6810:                                fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6811:                        } else {
                   6812:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6813:                        }
                   6814:                        if(fd != -1) {
                   6815:                                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     6816:                                psp->file_table[2] = fd;
                   6817:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6818:                        }
                   6819:                }
1.1.1.38  root     6820:                
1.1       root     6821:                // registers and segments
                   6822:                REG16(AX) = REG16(BX) = 0x00;
                   6823:                REG16(CX) = 0xff;
                   6824:                REG16(DX) = psp_seg;
                   6825:                REG16(SI) = ip;
                   6826:                REG16(DI) = sp;
                   6827:                REG16(SP) = sp;
1.1.1.3   root     6828:                SREG(DS) = SREG(ES) = psp_seg;
                   6829:                SREG(SS) = ss;
                   6830:                i386_load_segment_descriptor(DS);
                   6831:                i386_load_segment_descriptor(ES);
                   6832:                i386_load_segment_descriptor(SS);
1.1       root     6833:                
                   6834:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6835:                i386_jmp_far(cs, ip);
                   6836:        } else if(al == 0x01) {
                   6837:                // copy ss:sp and cs:ip to param block
                   6838:                param->sp = sp;
                   6839:                param->ss = ss;
                   6840:                param->ip = ip;
                   6841:                param->cs = cs;
1.1.1.31  root     6842:                
                   6843:                // the AX value to be passed to the child program is put on top of the child's stack
                   6844:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6845:        }
                   6846:        return(0);
                   6847: }
                   6848: 
                   6849: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6850: {
                   6851:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6852:        
                   6853:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6854:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6855:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6856:        
1.1.1.3   root     6857:        SREG(SS) = psp->stack.w.h;
                   6858:        i386_load_segment_descriptor(SS);
1.1       root     6859:        REG16(SP) = psp->stack.w.l;
                   6860:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6861:        
1.1.1.28  root     6862: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6863:        process_t *current_process = NULL;
                   6864:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6865:                if(process[i].psp == psp_seg) {
                   6866:                        current_process = &process[i];
                   6867:                        break;
                   6868:                }
                   6869:        }
                   6870:        if(current_process == NULL) {
                   6871:                throw(0x1f); // general failure
                   6872:        }
                   6873:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6874:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6875:        if(current_process->called_by_int2eh) {
                   6876:                REG16(AX) = ret;
                   6877:        }
                   6878:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6879:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6880:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6881:        i386_load_segment_descriptor(ES);
1.1       root     6882:        
                   6883:        if(mem_free) {
1.1.1.8   root     6884:                int mcb_seg;
                   6885:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6886:                        msdos_mem_free(mcb_seg + 1);
                   6887:                }
                   6888:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6889:                        msdos_mem_free(mcb_seg + 1);
                   6890:                }
1.1       root     6891:                
                   6892:                for(int i = 0; i < MAX_FILES; i++) {
                   6893:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6894:                                _close(i);
1.1.1.20  root     6895:                                msdos_file_handler_close(i);
                   6896:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6897:                        }
                   6898:                }
1.1.1.13  root     6899:                msdos_dta_info_free(psp_seg);
1.1       root     6900:        }
1.1.1.14  root     6901:        msdos_stdio_reopen();
1.1       root     6902:        
1.1.1.28  root     6903:        memset(current_process, 0, sizeof(process_t));
1.1       root     6904:        
                   6905:        current_psp = psp->parent_psp;
                   6906:        retval = ret;
1.1.1.23  root     6907:        msdos_sda_update(current_psp);
1.1       root     6908: }
                   6909: 
                   6910: // drive
                   6911: 
1.1.1.42  root     6912: int pcbios_update_drive_param(int drive_num, int force_update);
                   6913: 
1.1       root     6914: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6915: {
1.1.1.41  root     6916:        if(!(drive_num >= 0 && drive_num < 26)) {
                   6917:                return(0);
                   6918:        }
1.1.1.42  root     6919:        pcbios_update_drive_param(drive_num, force_update);
                   6920:        
                   6921:        drive_param_t *drive_param = &drive_params[drive_num];
1.1       root     6922:        *seg = DPB_TOP >> 4;
                   6923:        *ofs = sizeof(dpb_t) * drive_num;
                   6924:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6925:        
                   6926:        memset(dpb, 0, sizeof(dpb_t));
                   6927:        
1.1.1.41  root     6928:        dpb->drive_num = drive_num;
                   6929:        dpb->unit_num = drive_num;
1.1.1.42  root     6930:        
                   6931:        if(drive_param->valid) {
                   6932:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   6933:                
                   6934:                dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
                   6935:                dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
                   6936:                dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6937:                dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6938:                switch(geo->MediaType) {
                   6939:                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6940:                        dpb->media_type = 0xff;
                   6941:                        break;
                   6942:                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6943:                        dpb->media_type = 0xfe;
                   6944:                        break;
                   6945:                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6946:                        dpb->media_type = 0xfd;
                   6947:                        break;
                   6948:                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6949:                        dpb->media_type = 0xfc;
                   6950:                        break;
                   6951:                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6952:                case F3_1Pt2_512:
                   6953:                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6954:                case F5_720_512:
                   6955:                        dpb->media_type = 0xf9;
                   6956:                        break;
                   6957:                case FixedMedia:        // hard disk
                   6958:                case RemovableMedia:
                   6959:                case Unknown:
                   6960:                        dpb->media_type = 0xf8;
                   6961:                        break;
                   6962:                default:
                   6963:                        dpb->media_type = 0xf0;
                   6964:                        break;
                   6965:                }
                   6966:        }
1.1.1.41  root     6967:        dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
                   6968:        dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42  root     6969:        dpb->info_sector = 0xffff;
                   6970:        dpb->backup_boot_sector = 0xffff;
                   6971:        dpb->free_clusters = 0xffff;
                   6972:        dpb->free_search_cluster = 0xffffffff;
                   6973:        
                   6974:        return(drive_param->valid);
1.1       root     6975: }
                   6976: 
                   6977: // pc bios
                   6978: 
1.1.1.35  root     6979: #ifdef USE_SERVICE_THREAD
                   6980: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   6981: {
                   6982: #if defined(HAS_I386)
                   6983:        if(m_SF != 0) {
                   6984:                m_SF = 0;
1.1.1.49  root     6985:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6986:        } else {
                   6987:                m_SF = 1;
1.1.1.49  root     6988:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6989:        }
                   6990: #else
                   6991:        if(m_SignVal < 0) {
                   6992:                m_SignVal = 0;
1.1.1.49  root     6993:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6994:        } else {
                   6995:                m_SignVal = -1;
1.1.1.49  root     6996:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6997:        }
                   6998: #endif
1.1.1.59  root     6999:        // dummy loop to wait BIOS/DOS service is done is at fffc:0013
1.1.1.49  root     7000:        i386_call_far(DUMMY_TOP >> 4, 0x0013);
1.1.1.35  root     7001:        in_service = true;
                   7002:        service_exit = false;
                   7003:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   7004: }
                   7005: 
                   7006: void finish_service_loop()
                   7007: {
                   7008:        if(in_service && service_exit) {
                   7009: #if defined(HAS_I386)
                   7010:                if(m_SF != 0) {
                   7011:                        m_SF = 0;
                   7012:                } else {
                   7013:                        m_SF = 1;
                   7014:                }
                   7015: #else
                   7016:                if(m_SignVal < 0) {
                   7017:                        m_SignVal = 0;
                   7018:                } else {
                   7019:                        m_SignVal = -1;
                   7020:                }
                   7021: #endif
                   7022:                in_service = false;
                   7023:        }
                   7024: }
                   7025: #endif
                   7026: 
1.1.1.19  root     7027: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   7028: {
                   7029:        static unsigned __int64 start_msec_since_midnight = 0;
                   7030:        static unsigned __int64 start_msec_since_hostboot = 0;
                   7031:        
                   7032:        if(start_msec_since_midnight == 0) {
                   7033:                SYSTEMTIME time;
                   7034:                GetLocalTime(&time);
                   7035:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   7036:                start_msec_since_hostboot = cur_msec;
                   7037:        }
                   7038:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   7039:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   7040:        return (UINT32)tick;
                   7041: }
                   7042: 
                   7043: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   7044: {
                   7045:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   7046:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   7047:        
                   7048:        if(prev_tick > next_tick) {
                   7049:                mem[0x470] = 1;
                   7050:        }
                   7051:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   7052: }
                   7053: 
1.1.1.14  root     7054: inline void pcbios_irq0()
                   7055: {
                   7056:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     7057:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     7058: }
                   7059: 
1.1.1.16  root     7060: int pcbios_get_text_vram_address(int page)
1.1       root     7061: {
                   7062:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7063:                return TEXT_VRAM_TOP;
1.1       root     7064:        } else {
1.1.1.14  root     7065:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     7066:        }
                   7067: }
                   7068: 
1.1.1.16  root     7069: int pcbios_get_shadow_buffer_address(int page)
1.1       root     7070: {
1.1.1.14  root     7071:        if(!int_10h_feh_called) {
1.1.1.16  root     7072:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     7073:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7074:                return SHADOW_BUF_TOP;
                   7075:        } else {
1.1.1.14  root     7076:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     7077:        }
                   7078: }
                   7079: 
1.1.1.16  root     7080: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     7081: {
1.1.1.16  root     7082:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     7083: }
                   7084: 
1.1.1.56  root     7085: bool pcbios_set_font_size(int width, int height)
                   7086: {
1.1.1.61  root     7087:        if(set_console_font_size(width, height)) {
                   7088:                *(UINT16 *)(mem + 0x485) = height;
                   7089:                return(true);
                   7090:        }
                   7091:        return(false);
1.1.1.56  root     7092: }
                   7093: 
1.1.1.16  root     7094: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     7095: {
1.1.1.14  root     7096:        // clear the existing screen, not just the new one
                   7097:        int clr_height = max(height, scr_height);
                   7098:        
1.1.1.16  root     7099:        if(scr_width != width || scr_height != height) {
                   7100:                change_console_size(width, height);
1.1.1.14  root     7101:        }
                   7102:        mem[0x462] = 0;
                   7103:        *(UINT16 *)(mem + 0x44e) = 0;
                   7104:        
1.1.1.16  root     7105:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   7106:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   7107:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   7108:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1.1.51  root     7109:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     7110:        
1.1.1.23  root     7111:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     7112:        if(clr_screen) {
1.1.1.14  root     7113:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   7114:                        mem[ofs++] = 0x20;
                   7115:                        mem[ofs++] = 0x07;
                   7116:                }
                   7117:                
1.1.1.35  root     7118: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7119:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7120: #endif
1.1.1.14  root     7121:                for(int y = 0; y < clr_height; y++) {
                   7122:                        for(int x = 0; x < scr_width; x++) {
                   7123:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7124:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     7125:                        }
                   7126:                }
                   7127:                SMALL_RECT rect;
1.1.1.14  root     7128:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
1.1.1.60  root     7129:                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7130:                vram_length_char = vram_last_length_char = 0;
                   7131:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     7132: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7133:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7134: #endif
1.1       root     7135:        }
1.1.1.14  root     7136:        COORD co;
                   7137:        co.X = 0;
                   7138:        co.Y = scr_top;
                   7139:        SetConsoleCursorPosition(hStdout, co);
                   7140:        cursor_moved = true;
1.1.1.59  root     7141:        cursor_moved_by_crtc = false;
1.1.1.14  root     7142:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     7143: }
                   7144: 
1.1.1.36  root     7145: void pcbios_update_cursor_position()
                   7146: {
                   7147:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7148:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   7149:        if(!restore_console_on_exit) {
                   7150:                scr_top = csbi.srWindow.Top;
                   7151:        }
                   7152:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   7153:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   7154: }
                   7155: 
1.1.1.16  root     7156: inline void pcbios_int_10h_00h()
                   7157: {
                   7158:        switch(REG8(AL) & 0x7f) {
                   7159:        case 0x70: // v-text mode
                   7160:        case 0x71: // extended cga v-text mode
                   7161:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   7162:                break;
1.1.1.61  root     7163:        case 0x73:
                   7164:        case 0x03:
                   7165:                change_console_size(80, 25); // for Windows10
                   7166:                pcbios_set_font_size(font_width, font_height);
1.1.1.16  root     7167:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   7168:                break;
                   7169:        }
                   7170:        if(REG8(AL) & 0x80) {
                   7171:                mem[0x487] |= 0x80;
                   7172:        } else {
                   7173:                mem[0x487] &= ~0x80;
                   7174:        }
                   7175:        mem[0x449] = REG8(AL) & 0x7f;
                   7176: }
                   7177: 
1.1       root     7178: inline void pcbios_int_10h_01h()
                   7179: {
1.1.1.13  root     7180:        mem[0x460] = REG8(CL);
                   7181:        mem[0x461] = REG8(CH);
1.1.1.14  root     7182:        
1.1.1.60  root     7183:        int size = (int)(REG8(CL) & 7) - (int)(REG8(CH) & 7) + 1;
                   7184:        
                   7185:        if(!((REG8(CH) & 0x20) != 0 || size < 0)) {
                   7186:                ci_new.bVisible = TRUE;
                   7187:                ci_new.dwSize = (size + 2) * 100 / (8 + 2);
                   7188:        } else {
                   7189:                ci_new.bVisible = FALSE;
                   7190:        }
1.1       root     7191: }
                   7192: 
                   7193: inline void pcbios_int_10h_02h()
                   7194: {
1.1.1.14  root     7195:        // continuously setting the cursor effectively stops it blinking
1.1.1.59  root     7196:        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     7197:                COORD co;
                   7198:                co.X = REG8(DL);
1.1.1.14  root     7199:                co.Y = REG8(DH) + scr_top;
                   7200:                
                   7201:                // some programs hide the cursor by moving it off screen
                   7202:                static bool hidden = false;
1.1.1.23  root     7203:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7204:                
                   7205:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
1.1.1.59  root     7206:                        if(ci_new.bVisible) {
                   7207:                                ci_new.bVisible = FALSE;
1.1.1.14  root     7208:                                hidden = true;
                   7209:                        }
                   7210:                } else if(hidden) {
1.1.1.59  root     7211:                        if(!ci_new.bVisible) {
                   7212:                                ci_new.bVisible = TRUE;
1.1.1.14  root     7213:                        }
                   7214:                        hidden = false;
                   7215:                }
1.1.1.59  root     7216:                cursor_moved_by_crtc = false;
1.1       root     7217:        }
1.1.1.14  root     7218:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   7219:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     7220: }
                   7221: 
                   7222: inline void pcbios_int_10h_03h()
                   7223: {
1.1.1.14  root     7224:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7225:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7226:        REG8(CL) = mem[0x460];
                   7227:        REG8(CH) = mem[0x461];
                   7228: }
                   7229: 
                   7230: inline void pcbios_int_10h_05h()
                   7231: {
1.1.1.14  root     7232:        if(REG8(AL) >= vram_pages) {
                   7233:                return;
                   7234:        }
                   7235:        if(mem[0x462] != REG8(AL)) {
                   7236:                vram_flush();
                   7237:                
1.1.1.23  root     7238:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7239:                SMALL_RECT rect;
1.1.1.14  root     7240:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7241:                ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7242:                
1.1.1.16  root     7243:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     7244:                        for(int x = 0; x < scr_width; x++) {
                   7245:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7246:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7247:                        }
                   7248:                }
1.1.1.16  root     7249:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     7250:                        for(int x = 0; x < scr_width; x++) {
                   7251:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   7252:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     7253:                        }
                   7254:                }
1.1.1.60  root     7255:                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7256:                
                   7257:                COORD co;
1.1.1.14  root     7258:                co.X = mem[0x450 + REG8(AL) * 2];
                   7259:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   7260:                if(co.Y < scr_top + scr_height) {
                   7261:                        SetConsoleCursorPosition(hStdout, co);
                   7262:                }
1.1.1.59  root     7263:                cursor_moved_by_crtc = false;
1.1       root     7264:        }
1.1.1.14  root     7265:        mem[0x462] = REG8(AL);
                   7266:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   7267:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     7268:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     7269:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     7270:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     7271:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     7272:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     7273: }
                   7274: 
                   7275: inline void pcbios_int_10h_06h()
                   7276: {
1.1.1.14  root     7277:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7278:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7279:                return;
                   7280:        }
                   7281:        vram_flush();
                   7282:        
1.1.1.23  root     7283:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7284:        SMALL_RECT rect;
1.1.1.14  root     7285:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7286:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7287:        
                   7288:        int right = min(REG8(DL), scr_width - 1);
                   7289:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7290:        
                   7291:        if(REG8(AL) == 0) {
1.1.1.14  root     7292:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7293:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7294:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7295:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7296:                        }
                   7297:                }
                   7298:        } else {
1.1.1.14  root     7299:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     7300:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7301:                                if(y2 <= bottom) {
                   7302:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7303:                                } else {
1.1.1.14  root     7304:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7305:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7306:                                }
1.1.1.14  root     7307:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7308:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7309:                        }
                   7310:                }
                   7311:        }
1.1.1.60  root     7312:        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7313: }
                   7314: 
                   7315: inline void pcbios_int_10h_07h()
                   7316: {
1.1.1.14  root     7317:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7318:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7319:                return;
                   7320:        }
                   7321:        vram_flush();
                   7322:        
1.1.1.23  root     7323:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7324:        SMALL_RECT rect;
1.1.1.14  root     7325:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7326:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7327:        
                   7328:        int right = min(REG8(DL), scr_width - 1);
                   7329:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7330:        
                   7331:        if(REG8(AL) == 0) {
1.1.1.14  root     7332:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7333:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7334:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7335:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7336:                        }
                   7337:                }
                   7338:        } else {
1.1.1.14  root     7339:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     7340:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7341:                                if(y2 >= REG8(CH)) {
                   7342:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7343:                                } else {
1.1.1.14  root     7344:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7345:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7346:                                }
1.1.1.14  root     7347:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7348:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7349:                        }
                   7350:                }
                   7351:        }
1.1.1.60  root     7352:        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7353: }
                   7354: 
                   7355: inline void pcbios_int_10h_08h()
                   7356: {
                   7357:        COORD co;
                   7358:        DWORD num;
                   7359:        
1.1.1.14  root     7360:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7361:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7362:        
                   7363:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7364:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7365:                co.Y += scr_top;
                   7366:                vram_flush();
1.1.1.60  root     7367:                ReadConsoleOutputCharacterA(hStdout, scr_char, 1, co, &num);
1.1       root     7368:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   7369:                REG8(AL) = scr_char[0];
                   7370:                REG8(AH) = scr_attr[0];
                   7371:        } else {
1.1.1.16  root     7372:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     7373:        }
                   7374: }
                   7375: 
                   7376: inline void pcbios_int_10h_09h()
                   7377: {
                   7378:        COORD co;
                   7379:        
1.1.1.14  root     7380:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7381:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7382:        
1.1.1.16  root     7383:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7384:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7385:        
                   7386:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7387: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7388:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7389: #endif
1.1.1.16  root     7390:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7391:                while(dest < end) {
                   7392:                        write_text_vram_char(dest - vram, REG8(AL));
                   7393:                        mem[dest++] = REG8(AL);
                   7394:                        write_text_vram_attr(dest - vram, REG8(BL));
                   7395:                        mem[dest++] = REG8(BL);
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:                        mem[dest++] = REG8(BL);
                   7404:                }
                   7405:        }
                   7406: }
                   7407: 
                   7408: inline void pcbios_int_10h_0ah()
                   7409: {
                   7410:        COORD co;
                   7411:        
1.1.1.14  root     7412:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7413:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7414:        
1.1.1.16  root     7415:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7416:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7417:        
                   7418:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7419: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7420:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7421: #endif
1.1.1.16  root     7422:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7423:                while(dest < end) {
                   7424:                        write_text_vram_char(dest - vram, REG8(AL));
                   7425:                        mem[dest++] = REG8(AL);
                   7426:                        dest++;
1.1       root     7427:                }
1.1.1.35  root     7428: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7429:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7430: #endif
1.1       root     7431:        } else {
1.1.1.14  root     7432:                while(dest < end) {
1.1       root     7433:                        mem[dest++] = REG8(AL);
                   7434:                        dest++;
                   7435:                }
                   7436:        }
                   7437: }
                   7438: 
1.1.1.40  root     7439: inline void pcbios_int_10h_0ch()
                   7440: {
                   7441:        HDC hdc = get_console_window_device_context();
                   7442:        
                   7443:        if(hdc != NULL) {
                   7444:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
                   7445:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
                   7446:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
                   7447:                
                   7448:                if(REG8(AL) & 0x80) {
                   7449:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7450:                        if(color != CLR_INVALID) {
                   7451:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7452:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7453:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
                   7454:                        }
                   7455:                }
                   7456:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
                   7457:        }
                   7458: }
                   7459: 
                   7460: inline void pcbios_int_10h_0dh()
                   7461: {
                   7462:        HDC hdc = get_console_window_device_context();
                   7463:        BYTE r = 0;
                   7464:        BYTE g = 0;
                   7465:        BYTE b = 0;
                   7466:        
                   7467:        if(hdc != NULL) {
                   7468:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7469:                if(color != CLR_INVALID) {
                   7470:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7471:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7472:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
                   7473:                }
                   7474:        }
                   7475:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
                   7476: }
                   7477: 
1.1       root     7478: inline void pcbios_int_10h_0eh()
                   7479: {
1.1.1.59  root     7480:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   7481:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     7482:        DWORD num;
                   7483:        COORD co;
                   7484:        
1.1.1.59  root     7485:        if(cursor_moved_by_crtc) {
                   7486:                if(!restore_console_on_exit) {
                   7487:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7488:                        scr_top = csbi.srWindow.Top;
                   7489:                }
                   7490:                co.X = mem[0x450 + REG8(BH) * 2];
                   7491:                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   7492:                SetConsoleCursorPosition(hStdout, co);
                   7493:                cursor_moved_by_crtc = false;
                   7494:        }
1.1.1.54  root     7495:        co.X = mem[0x450 + mem[0x462] * 2];
                   7496:        co.Y = mem[0x451 + mem[0x462] * 2];
1.1.1.14  root     7497:        
                   7498:        if(REG8(AL) == 7) {
                   7499:                //MessageBeep(-1);
                   7500:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   7501:                if(REG8(AL) == 10) {
                   7502:                        vram_flush();
                   7503:                }
1.1.1.60  root     7504:                WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     7505:                cursor_moved = true;
                   7506:        } else {
1.1.1.54  root     7507:                int dest = pcbios_get_shadow_buffer_address(mem[0x462], co.X, co.Y);
1.1.1.35  root     7508: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7509:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7510: #endif
1.1.1.54  root     7511:                int vram = pcbios_get_shadow_buffer_address(mem[0x462]);
                   7512:                write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     7513: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7514:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7515: #endif
1.1.1.54  root     7516:                
                   7517:                if(++co.X == scr_width) {
                   7518:                        co.X = 0;
                   7519:                        if(++co.Y == scr_height) {
                   7520:                                vram_flush();
1.1.1.60  root     7521:                                WriteConsoleA(hStdout, "\n", 1, &num, NULL);
1.1.1.14  root     7522:                                cursor_moved = true;
                   7523:                        }
                   7524:                }
1.1.1.54  root     7525:                if(!cursor_moved) {
                   7526:                        co.Y += scr_top;
                   7527:                        SetConsoleCursorPosition(hStdout, co);
                   7528:                        cursor_moved = true;
                   7529:                }
1.1.1.14  root     7530:                mem[dest] = REG8(AL);
                   7531:        }
1.1       root     7532: }
                   7533: 
                   7534: inline void pcbios_int_10h_0fh()
                   7535: {
                   7536:        REG8(AL) = mem[0x449];
                   7537:        REG8(AH) = mem[0x44a];
                   7538:        REG8(BH) = mem[0x462];
                   7539: }
                   7540: 
1.1.1.14  root     7541: inline void pcbios_int_10h_11h()
                   7542: {
                   7543:        switch(REG8(AL)) {
1.1.1.58  root     7544:        case 0x00:
                   7545:        case 0x10:
1.1.1.61  root     7546:                if(REG8(BH)) {
                   7547:                        change_console_size(80, 25); // for Windows10
                   7548:                        if(!pcbios_set_font_size(font_width, (int)REG8(BH))) {
                   7549:                                for(int h = min(font_height, (int)REG8(BH)); h <= max(font_height, (int)REG8(BH)); h++) {
                   7550:                                        if(h != (int)REG8(BH)) {
                   7551:                                                if(pcbios_set_font_size(font_width, h)) {
                   7552:                                                        break;
                   7553:                                                }
                   7554:                                        }
                   7555:                                }
                   7556:                        }
1.1.1.58  root     7557:                        pcbios_set_console_size(80, (25 * 16) / REG8(BH), true);
                   7558:                }
                   7559:                break;
1.1.1.16  root     7560:        case 0x01:
1.1.1.14  root     7561:        case 0x11:
1.1.1.61  root     7562:                change_console_size(80, 28); // for Windows10
                   7563:                if(!pcbios_set_font_size(font_width, 14)) {
                   7564:                        for(int h = min(font_height, 14); h <= max(font_height, 14); h++) {
                   7565:                                if(h != 14) {
                   7566:                                        if(pcbios_set_font_size(font_width, h)) {
                   7567:                                                break;
                   7568:                                        }
                   7569:                                }
                   7570:                        }
1.1.1.56  root     7571:                }
1.1.1.61  root     7572:                pcbios_set_console_size(80, 28, true); // 28 = 25 * 16 / 14
1.1.1.14  root     7573:                break;
1.1.1.16  root     7574:        case 0x02:
1.1.1.14  root     7575:        case 0x12:
1.1.1.61  root     7576:                change_console_size(80, 25); // for Windows10
                   7577:                if(!pcbios_set_font_size(8, 8)) {
                   7578:                        bool success = false;
                   7579:                        for(int y = 8; y <= 14; y++) {
                   7580:                                for(int x = min(font_width, 6); x <= max(font_width, 8); x++) {
                   7581:                                        if(pcbios_set_font_size(x, y)) {
                   7582:                                                success = true;
                   7583:                                                break;
                   7584:                                        }
                   7585:                                }
                   7586:                        }
                   7587:                        if(!success) {
                   7588:                                pcbios_set_font_size(font_width, font_height);
                   7589:                        }
1.1.1.56  root     7590:                }
1.1.1.61  root     7591:                pcbios_set_console_size(80, 50, true); // 50 = 25 * 16 / 8
1.1.1.14  root     7592:                break;
1.1.1.16  root     7593:        case 0x04:
1.1.1.14  root     7594:        case 0x14:
1.1.1.61  root     7595:                change_console_size(80, 25); // for Windows10
                   7596:                pcbios_set_font_size(font_width, font_height);
                   7597:                pcbios_set_console_size(80, 25, true);
1.1.1.58  root     7598:                break;
                   7599:        case 0x18:
1.1.1.61  root     7600:                change_console_size(80, 25); // for Windows10
                   7601:                pcbios_set_font_size(font_width, font_height);
                   7602:                pcbios_set_console_size(80, 25, true);
1.1.1.14  root     7603:                break;
                   7604:        case 0x30:
                   7605:                SREG(ES) = 0;
                   7606:                i386_load_segment_descriptor(ES);
                   7607:                REG16(BP) = 0;
                   7608:                REG16(CX) = mem[0x485];
                   7609:                REG8(DL) = mem[0x484];
                   7610:                break;
1.1.1.54  root     7611:        default:
                   7612:                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));
                   7613:                m_CF = 1;
                   7614:                break;
1.1.1.14  root     7615:        }
                   7616: }
                   7617: 
                   7618: inline void pcbios_int_10h_12h()
                   7619: {
1.1.1.16  root     7620:        switch(REG8(BL)) {
                   7621:        case 0x10:
1.1.1.14  root     7622:                REG16(BX) = 0x0003;
                   7623:                REG16(CX) = 0x0009;
1.1.1.16  root     7624:                break;
1.1.1.14  root     7625:        }
                   7626: }
                   7627: 
1.1       root     7628: inline void pcbios_int_10h_13h()
                   7629: {
1.1.1.3   root     7630:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     7631:        COORD co;
                   7632:        DWORD num;
                   7633:        
                   7634:        co.X = REG8(DL);
1.1.1.14  root     7635:        co.Y = REG8(DH) + scr_top;
                   7636:        
                   7637:        vram_flush();
1.1       root     7638:        
                   7639:        switch(REG8(AL)) {
                   7640:        case 0x00:
                   7641:        case 0x01:
                   7642:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7643:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7644:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7645:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7646:                        SetConsoleCursorPosition(hStdout, co);
                   7647:                        
                   7648:                        if(csbi.wAttributes != REG8(BL)) {
                   7649:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   7650:                        }
1.1.1.60  root     7651:                        WriteConsoleA(hStdout, &mem[ofs], REG16(CX), &num, NULL);
1.1.1.14  root     7652:                        
1.1       root     7653:                        if(csbi.wAttributes != REG8(BL)) {
                   7654:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7655:                        }
                   7656:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     7657:                                if(!restore_console_on_exit) {
                   7658:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7659:                                        scr_top = csbi.srWindow.Top;
                   7660:                                }
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 0x02:
                   7673:        case 0x03:
                   7674:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7675:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7676:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7677:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7678:                        SetConsoleCursorPosition(hStdout, co);
                   7679:                        
                   7680:                        WORD wAttributes = csbi.wAttributes;
                   7681:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7682:                                if(wAttributes != mem[ofs + 1]) {
                   7683:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7684:                                        wAttributes = mem[ofs + 1];
                   7685:                                }
1.1.1.60  root     7686:                                WriteConsoleA(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7687:                        }
                   7688:                        if(csbi.wAttributes != wAttributes) {
                   7689:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7690:                        }
                   7691:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7692:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7693:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7694:                                SetConsoleCursorPosition(hStdout, co);
                   7695:                        } else {
                   7696:                                cursor_moved = true;
                   7697:                        }
1.1.1.59  root     7698:                        cursor_moved_by_crtc = false;
1.1       root     7699:                } else {
1.1.1.3   root     7700:                        m_CF = 1;
1.1       root     7701:                }
                   7702:                break;
                   7703:        case 0x10:
                   7704:        case 0x11:
                   7705:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7706:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.60  root     7707:                        ReadConsoleOutputCharacterA(hStdout, scr_char, REG16(CX), co, &num);
1.1       root     7708:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7709:                        for(int i = 0; i < num; i++) {
                   7710:                                mem[ofs++] = scr_char[i];
                   7711:                                mem[ofs++] = scr_attr[i];
1.1.1.45  root     7712:                                if(REG8(AL) & 0x01) {
1.1       root     7713:                                        mem[ofs++] = 0;
                   7714:                                        mem[ofs++] = 0;
                   7715:                                }
                   7716:                        }
                   7717:                } else {
1.1.1.16  root     7718:                        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     7719:                                mem[ofs++] = mem[src++];
                   7720:                                mem[ofs++] = mem[src++];
1.1.1.45  root     7721:                                if(REG8(AL) & 0x01) {
1.1       root     7722:                                        mem[ofs++] = 0;
                   7723:                                        mem[ofs++] = 0;
                   7724:                                }
1.1.1.14  root     7725:                                if(++co.X == scr_width) {
                   7726:                                        if(++co.Y == scr_height) {
1.1       root     7727:                                                break;
                   7728:                                        }
                   7729:                                        co.X = 0;
                   7730:                                }
                   7731:                        }
                   7732:                }
                   7733:                break;
1.1.1.45  root     7734:        case 0x12: // ???
                   7735:        case 0x13: // ???
1.1       root     7736:        case 0x20:
                   7737:        case 0x21:
                   7738:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7739:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7740:                        int len = min(REG16(CX), scr_width * scr_height);
                   7741:                        for(int i = 0; i < len; i++) {
1.1       root     7742:                                scr_char[i] = mem[ofs++];
                   7743:                                scr_attr[i] = mem[ofs++];
1.1.1.45  root     7744:                                if(REG8(AL) & 0x01) {
1.1       root     7745:                                        ofs += 2;
                   7746:                                }
                   7747:                        }
1.1.1.60  root     7748:                        WriteConsoleOutputCharacterA(hStdout, scr_char, len, co, &num);
1.1.1.14  root     7749:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7750:                } else {
1.1.1.16  root     7751:                        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     7752:                                mem[dest++] = mem[ofs++];
                   7753:                                mem[dest++] = mem[ofs++];
1.1.1.45  root     7754:                                if(REG8(AL) & 0x01) {
1.1       root     7755:                                        ofs += 2;
                   7756:                                }
1.1.1.14  root     7757:                                if(++co.X == scr_width) {
                   7758:                                        if(++co.Y == scr_height) {
1.1       root     7759:                                                break;
                   7760:                                        }
                   7761:                                        co.X = 0;
                   7762:                                }
                   7763:                        }
                   7764:                }
                   7765:                break;
                   7766:        default:
1.1.1.22  root     7767:                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     7768:                m_CF = 1;
1.1       root     7769:                break;
                   7770:        }
                   7771: }
                   7772: 
1.1.1.30  root     7773: inline void pcbios_int_10h_18h()
                   7774: {
                   7775:        switch(REG8(AL)) {
                   7776:        case 0x00:
                   7777:        case 0x01:
                   7778: //             REG8(AL) = 0x86;
                   7779:                REG8(AL) = 0x00;
                   7780:                break;
                   7781:        default:
                   7782:                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));
                   7783:                m_CF = 1;
                   7784:                break;
                   7785:        }
                   7786: }
                   7787: 
1.1.1.14  root     7788: inline void pcbios_int_10h_1ah()
                   7789: {
                   7790:        switch(REG8(AL)) {
                   7791:        case 0x00:
                   7792:                REG8(AL) = 0x1a;
                   7793:                REG8(BL) = 0x08;
                   7794:                REG8(BH) = 0x00;
                   7795:                break;
                   7796:        default:
1.1.1.22  root     7797:                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     7798:                m_CF = 1;
                   7799:                break;
                   7800:        }
                   7801: }
                   7802: 
1.1       root     7803: inline void pcbios_int_10h_1dh()
                   7804: {
                   7805:        switch(REG8(AL)) {
1.1.1.43  root     7806:        case 0x00:
                   7807:                // DOS/V Shift Status Line Control is not supported
                   7808:                m_CF = 1;
                   7809:                break;
1.1       root     7810:        case 0x01:
                   7811:                break;
                   7812:        case 0x02:
                   7813:                REG16(BX) = 0;
                   7814:                break;
                   7815:        default:
1.1.1.22  root     7816:                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));
                   7817:                m_CF = 1;
                   7818:                break;
                   7819:        }
                   7820: }
                   7821: 
                   7822: inline void pcbios_int_10h_4fh()
                   7823: {
                   7824:        switch(REG8(AL)) {
                   7825:        case 0x00:
                   7826:                REG8(AH) = 0x02; // not supported
                   7827:                break;
                   7828:        case 0x01:
                   7829:        case 0x02:
                   7830:        case 0x03:
                   7831:        case 0x04:
                   7832:        case 0x05:
                   7833:        case 0x06:
                   7834:        case 0x07:
                   7835:        case 0x08:
                   7836:        case 0x09:
                   7837:        case 0x0a:
                   7838:        case 0x0b:
                   7839:        case 0x0c:
                   7840:                REG8(AH) = 0x01; // failed
                   7841:                break;
                   7842:        default:
                   7843:                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     7844:                m_CF = 1;
1.1       root     7845:                break;
                   7846:        }
                   7847: }
                   7848: 
                   7849: inline void pcbios_int_10h_82h()
                   7850: {
                   7851:        static UINT8 mode = 0;
                   7852:        
                   7853:        switch(REG8(AL)) {
1.1.1.22  root     7854:        case 0x00:
1.1       root     7855:                if(REG8(BL) != 0xff) {
                   7856:                        mode = REG8(BL);
                   7857:                }
                   7858:                REG8(AL) = mode;
                   7859:                break;
                   7860:        default:
1.1.1.22  root     7861:                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     7862:                m_CF = 1;
1.1       root     7863:                break;
                   7864:        }
                   7865: }
                   7866: 
1.1.1.22  root     7867: inline void pcbios_int_10h_83h()
                   7868: {
                   7869:        static UINT8 mode = 0;
                   7870:        
                   7871:        switch(REG8(AL)) {
                   7872:        case 0x00:
                   7873:                REG16(AX) = 0; // offset???
                   7874:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7875:                i386_load_segment_descriptor(ES);
                   7876:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7877:                break;
                   7878:        default:
                   7879:                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));
                   7880:                m_CF = 1;
                   7881:                break;
                   7882:        }
                   7883: }
                   7884: 
                   7885: inline void pcbios_int_10h_90h()
                   7886: {
                   7887:        REG8(AL) = mem[0x449];
                   7888: }
                   7889: 
                   7890: inline void pcbios_int_10h_91h()
                   7891: {
                   7892:        REG8(AL) = 0x04; // VGA
                   7893: }
                   7894: 
                   7895: inline void pcbios_int_10h_efh()
                   7896: {
                   7897:        REG16(DX) = 0xffff;
                   7898: }
                   7899: 
1.1       root     7900: inline void pcbios_int_10h_feh()
                   7901: {
                   7902:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7903:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7904:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7905:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7906:        }
                   7907:        int_10h_feh_called = true;
                   7908: }
                   7909: 
                   7910: inline void pcbios_int_10h_ffh()
                   7911: {
                   7912:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7913:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7914:                COORD co;
                   7915:                DWORD num;
                   7916:                
1.1.1.14  root     7917:                vram_flush();
                   7918:                
                   7919:                co.X = (REG16(DI) >> 1) % scr_width;
                   7920:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7921:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7922:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7923:                int len;
                   7924:                for(len = 0; ofs < end; len++) {
                   7925:                        scr_char[len] = mem[ofs++];
                   7926:                        scr_attr[len] = mem[ofs++];
                   7927:                }
                   7928:                co.Y += scr_top;
1.1.1.60  root     7929:                WriteConsoleOutputCharacterA(hStdout, scr_char, len, co, &num);
1.1.1.14  root     7930:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7931:        }
                   7932:        int_10h_ffh_called = true;
                   7933: }
                   7934: 
1.1.1.42  root     7935: int pcbios_update_drive_param(int drive_num, int force_update)
                   7936: {
                   7937:        if(drive_num >= 0 && drive_num < 26) {
                   7938:                drive_param_t *drive_param = &drive_params[drive_num];
                   7939:                
                   7940:                if(force_update || !drive_param->initialized) {
                   7941:                        drive_param->valid = 0;
                   7942:                        char dev[64];
                   7943:                        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7944:                        
1.1.1.60  root     7945:                        HANDLE hFile = CreateFileA(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.42  root     7946:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7947:                                DWORD dwSize;
                   7948:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
                   7949:                                        drive_param->valid = 1;
                   7950:                                }
                   7951:                                CloseHandle(hFile);
                   7952:                        }
                   7953:                        drive_param->initialized = 1;
                   7954:                }
                   7955:                return(drive_param->valid);
                   7956:        }
                   7957:        return(0);
                   7958: }
                   7959: 
                   7960: inline void pcbios_int_13h_00h()
                   7961: {
                   7962:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7963:        
                   7964:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7965:                REG8(AH) = 0x00; // successful completion
                   7966:        } else {
                   7967:                if(REG8(DL) & 0x80) {
                   7968:                        REG8(AH) = 0x05; // reset failed (hard disk)
                   7969:                } else {
                   7970:                        REG8(AH) = 0x80; // timeout (not ready)
                   7971:                }
                   7972:                m_CF = 1;
                   7973:        }
                   7974: }
                   7975: 
                   7976: inline void pcbios_int_13h_02h()
                   7977: {
                   7978:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7979:        
                   7980:        if(REG8(AL) == 0) {
                   7981:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7982:                m_CF = 1;
                   7983:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7984:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7985:                m_CF = 1;
                   7986:        } else {
                   7987:                drive_param_t *drive_param = &drive_params[drive_num];
                   7988:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7989:                char dev[64];
                   7990:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7991:                
1.1.1.60  root     7992:                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     7993:                if(hFile == INVALID_HANDLE_VALUE) {
                   7994:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7995:                        m_CF = 1;
                   7996:                } else {
                   7997:                        UINT32 sector_num = REG8(AL);
                   7998:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7999:                        UINT32 head = REG8(DH);
                   8000:                        UINT32 sector = REG8(CL) & 0x3f;
                   8001:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   8002:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   8003:                        DWORD dwSize;
                   8004:                        
                   8005: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8006: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8007: //                             m_CF = 1;
                   8008: //                     } else 
                   8009:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8010:                                REG8(AH) = 0x04; // sector not found/read error
                   8011:                                m_CF = 1;
                   8012:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   8013:                                REG8(AH) = 0x04; // sector not found/read error
                   8014:                                m_CF = 1;
                   8015:                        } else {
                   8016:                                REG8(AH) = 0x00; // successful completion
                   8017:                        }
                   8018:                        CloseHandle(hFile);
                   8019:                }
                   8020:        }
                   8021: }
                   8022: 
                   8023: inline void pcbios_int_13h_03h()
                   8024: {
                   8025:        // this operation may cause serious damage for drives, so support only floppy disk...
                   8026:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8027:        
                   8028:        if(REG8(AL) == 0) {
                   8029:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   8030:                m_CF = 1;
                   8031:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   8032:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8033:                m_CF = 1;
                   8034:        } else if(!drive_params[drive_num].is_fdd()) {
                   8035:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8036:                m_CF = 1;
                   8037:        } else {
                   8038:                drive_param_t *drive_param = &drive_params[drive_num];
                   8039:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8040:                char dev[64];
                   8041:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   8042:                
1.1.1.60  root     8043:                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     8044:                if(hFile == INVALID_HANDLE_VALUE) {
                   8045:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8046:                        m_CF = 1;
                   8047:                } else {
                   8048:                        UINT32 sector_num = REG8(AL);
                   8049:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   8050:                        UINT32 head = REG8(DH);
                   8051:                        UINT32 sector = REG8(CL) & 0x3f;
                   8052:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   8053:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   8054:                        DWORD dwSize;
                   8055:                        
                   8056: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8057: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8058: //                             m_CF = 1;
                   8059: //                     } else 
                   8060:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8061:                                REG8(AH) = 0x04; // sector not found/read error
                   8062:                                m_CF = 1;
                   8063:                        } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   8064:                                REG8(AH) = 0x04; // sector not found/read error
                   8065:                                m_CF = 1;
                   8066:                        } else {
                   8067:                                REG8(AH) = 0x00; // successful completion
                   8068:                        }
                   8069:                        CloseHandle(hFile);
                   8070:                }
                   8071:        }
                   8072: }
                   8073: 
                   8074: inline void pcbios_int_13h_04h()
                   8075: {
                   8076:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8077:        
                   8078:        if(REG8(AL) == 0) {
                   8079:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   8080:                m_CF = 1;
                   8081:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   8082:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8083:                m_CF = 1;
                   8084:        } else {
                   8085:                drive_param_t *drive_param = &drive_params[drive_num];
                   8086:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8087:                char dev[64];
                   8088:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   8089:                
1.1.1.60  root     8090:                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     8091:                if(hFile == INVALID_HANDLE_VALUE) {
                   8092:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8093:                        m_CF = 1;
                   8094:                } else {
                   8095:                        UINT32 sector_num = REG8(AL);
                   8096:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   8097:                        UINT32 head = REG8(DH);
                   8098:                        UINT32 sector = REG8(CL) & 0x3f;
                   8099:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   8100:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   8101:                        DWORD dwSize;
                   8102:                        UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
                   8103:                        
                   8104: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8105: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8106: //                             m_CF = 1;
                   8107: //                     } else 
                   8108:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8109:                                REG8(AH) = 0x04; // sector not found/read error
                   8110:                                m_CF = 1;
                   8111:                        } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   8112:                                REG8(AH) = 0x04; // sector not found/read error
                   8113:                                m_CF = 1;
                   8114:                        } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
                   8115:                                REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
                   8116:                                m_CF = 1;
                   8117:                        } else {
                   8118:                                REG8(AH) = 0x00; // successful completion
                   8119:                        }
                   8120:                        free(tmp_buffer);
                   8121:                        CloseHandle(hFile);
                   8122:                }
                   8123:        }
                   8124: }
                   8125: 
                   8126: inline void pcbios_int_13h_08h()
                   8127: {
                   8128:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8129:        
                   8130:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8131:                drive_param_t *drive_param = &drive_params[drive_num];
                   8132:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8133:                
                   8134:                REG16(AX) = 0x0000;
                   8135:                switch(geo->MediaType) {
                   8136:                case F5_360_512:
                   8137:                case F5_320_512:
                   8138:                case F5_320_1024:
                   8139:                case F5_180_512:
                   8140:                case F5_160_512:
                   8141:                        REG8(BL) = 0x01; // 320K/360K disk
                   8142:                        break;
                   8143:                case F5_1Pt2_512:
                   8144:                case F3_1Pt2_512:
                   8145:                case F3_1Pt23_1024:
                   8146:                case F5_1Pt23_1024:
                   8147:                        REG8(BL) = 0x02; // 1.2M disk
                   8148:                        break;
                   8149:                case F3_720_512:
                   8150:                case F3_640_512:
                   8151:                case F5_640_512:
                   8152:                case F5_720_512:
                   8153:                        REG8(BL) = 0x03; // 720K disk
                   8154:                        break;
                   8155:                case F3_1Pt44_512:
                   8156:                        REG8(BL) = 0x04; // 1.44M disk
                   8157:                        break;
                   8158:                case F3_2Pt88_512:
                   8159:                        REG8(BL) = 0x06; // 2.88M disk
                   8160:                        break;
                   8161:                case RemovableMedia:
                   8162:                        REG8(BL) = 0x10; // ATAPI Removable Media Device
                   8163:                        break;
                   8164:                default:
                   8165:                        REG8(BL) = 0x00; // unknown
                   8166:                        break;
                   8167:                }
                   8168:                if(REG8(DL) & 0x80) {
                   8169:                        switch(GetLogicalDrives() & 0x0c) {
                   8170:                        case 0x00: REG8(DL) = 0x00; break;
                   8171:                        case 0x04:
                   8172:                        case 0x08: REG8(DL) = 0x01; break;
                   8173:                        case 0x0c: REG8(DL) = 0x02; break;
                   8174:                        }
                   8175:                } else {
                   8176:                        switch(GetLogicalDrives() & 0x03) {
                   8177:                        case 0x00: REG8(DL) = 0x00; break;
                   8178:                        case 0x01:
                   8179:                        case 0x02: REG8(DL) = 0x01; break;
                   8180:                        case 0x03: REG8(DL) = 0x02; break;
                   8181:                        }
                   8182:                }
                   8183:                REG8(DH) = drive_param->head_num();
                   8184:                int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
                   8185:                int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
                   8186:                REG8(CH) = cyl & 0xff;
                   8187:                REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
                   8188:        } else {
                   8189:                REG8(AH) = 0x07;
                   8190:                m_CF = 1;
                   8191:        }
                   8192: }
                   8193: 
                   8194: inline void pcbios_int_13h_10h()
                   8195: {
                   8196:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8197:        
                   8198:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8199:                REG8(AH) = 0x00; // successful completion
                   8200:        } else {
                   8201:                if(REG8(DL) & 0x80) {
                   8202:                        REG8(AH) = 0xaa; // drive not ready (hard disk)
                   8203:                } else {
                   8204:                        REG8(AH) = 0x80; // timeout (not ready)
                   8205:                }
                   8206:                m_CF = 1;
                   8207:        }
                   8208: }
                   8209: 
                   8210: inline void pcbios_int_13h_15h()
                   8211: {
                   8212:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8213:        
                   8214:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8215:                if(REG8(DL) & 0x80) {
                   8216:                        REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
                   8217:                } else {
                   8218:                        REG8(AH) = 0x03; // hard disk
                   8219:                }
                   8220:        } else {
                   8221:                REG8(AH) = 0x00; // no such drive
                   8222:        }
                   8223: }
                   8224: 
1.1.1.43  root     8225: inline void pcbios_int_13h_41h()
                   8226: {
                   8227:        if(REG16(BX) == 0x55aa) {
                   8228:                // IBM/MS INT 13 Extensions is not installed
                   8229:                REG8(AH) = 0x01;
                   8230:                m_CF = 1;
                   8231:        } else {
                   8232:                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));
                   8233:                REG8(AH) = 0x01;
                   8234:                m_CF = 1;
                   8235:        }
                   8236: }
                   8237: 
1.1.1.25  root     8238: inline void pcbios_int_14h_00h()
                   8239: {
1.1.1.29  root     8240:        if(REG16(DX) < 4) {
1.1.1.25  root     8241:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   8242:                UINT8 selector = sio_read(REG16(DX), 3);
                   8243:                selector &= ~0x3f;
                   8244:                selector |= REG8(AL) & 0x1f;
                   8245:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   8246:                sio_write(REG16(DX), 3, selector | 0x80);
                   8247:                sio_write(REG16(DX), 0, divisor & 0xff);
                   8248:                sio_write(REG16(DX), 1, divisor >> 8);
                   8249:                sio_write(REG16(DX), 3, selector);
                   8250:                REG8(AH) = sio_read(REG16(DX), 5);
                   8251:                REG8(AL) = sio_read(REG16(DX), 6);
                   8252:        } else {
                   8253:                REG8(AH) = 0x80;
                   8254:        }
                   8255: }
                   8256: 
                   8257: inline void pcbios_int_14h_01h()
                   8258: {
1.1.1.29  root     8259:        if(REG16(DX) < 4) {
1.1.1.25  root     8260:                UINT8 selector = sio_read(REG16(DX), 3);
                   8261:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8262:                sio_write(REG16(DX), 0, REG8(AL));
                   8263:                sio_write(REG16(DX), 3, selector);
                   8264:                REG8(AH) = sio_read(REG16(DX), 5);
                   8265:        } else {
                   8266:                REG8(AH) = 0x80;
                   8267:        }
                   8268: }
                   8269: 
                   8270: inline void pcbios_int_14h_02h()
                   8271: {
1.1.1.29  root     8272:        if(REG16(DX) < 4) {
1.1.1.25  root     8273:                UINT8 selector = sio_read(REG16(DX), 3);
                   8274:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8275:                REG8(AL) = sio_read(REG16(DX), 0);
                   8276:                sio_write(REG16(DX), 3, selector);
                   8277:                REG8(AH) = sio_read(REG16(DX), 5);
                   8278:        } else {
                   8279:                REG8(AH) = 0x80;
                   8280:        }
                   8281: }
                   8282: 
                   8283: inline void pcbios_int_14h_03h()
                   8284: {
1.1.1.29  root     8285:        if(REG16(DX) < 4) {
1.1.1.25  root     8286:                REG8(AH) = sio_read(REG16(DX), 5);
                   8287:                REG8(AL) = sio_read(REG16(DX), 6);
                   8288:        } else {
                   8289:                REG8(AH) = 0x80;
                   8290:        }
                   8291: }
                   8292: 
                   8293: inline void pcbios_int_14h_04h()
                   8294: {
1.1.1.29  root     8295:        if(REG16(DX) < 4) {
1.1.1.25  root     8296:                UINT8 selector = sio_read(REG16(DX), 3);
                   8297:                if(REG8(CH) <= 0x03) {
                   8298:                        selector = (selector & ~0x03) | REG8(CH);
                   8299:                }
                   8300:                if(REG8(BL) == 0x00) {
                   8301:                        selector &= ~0x04;
                   8302:                } else if(REG8(BL) == 0x01) {
                   8303:                        selector |= 0x04;
                   8304:                }
                   8305:                if(REG8(BH) == 0x00) {
                   8306:                        selector = (selector & ~0x38) | 0x00;
                   8307:                } else if(REG8(BH) == 0x01) {
                   8308:                        selector = (selector & ~0x38) | 0x08;
                   8309:                } else if(REG8(BH) == 0x02) {
                   8310:                        selector = (selector & ~0x38) | 0x18;
                   8311:                } else if(REG8(BH) == 0x03) {
                   8312:                        selector = (selector & ~0x38) | 0x28;
                   8313:                } else if(REG8(BH) == 0x04) {
                   8314:                        selector = (selector & ~0x38) | 0x38;
                   8315:                }
                   8316:                if(REG8(AL) == 0x00) {
                   8317:                        selector |= 0x40;
                   8318:                } else if(REG8(AL) == 0x01) {
                   8319:                        selector &= ~0x40;
                   8320:                }
                   8321:                if(REG8(CL) <= 0x0b) {
                   8322:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   8323:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   8324:                        sio_write(REG16(DX), 3, selector | 0x80);
                   8325:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   8326:                        sio_write(REG16(DX), 1, divisor >> 8);
                   8327:                }
                   8328:                sio_write(REG16(DX), 3, selector);
                   8329:                REG8(AH) = sio_read(REG16(DX), 5);
                   8330:                REG8(AL) = sio_read(REG16(DX), 6);
                   8331:        } else {
                   8332:                REG8(AH) = 0x80;
                   8333:        }
                   8334: }
                   8335: 
                   8336: inline void pcbios_int_14h_05h()
                   8337: {
1.1.1.29  root     8338:        if(REG16(DX) < 4) {
1.1.1.25  root     8339:                if(REG8(AL) == 0x00) {
                   8340:                        REG8(BL) = sio_read(REG16(DX), 4);
                   8341:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8342:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8343:                } else if(REG8(AL) == 0x01) {
                   8344:                        sio_write(REG16(DX), 4, REG8(BL));
                   8345:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8346:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8347:                } else {
                   8348:                        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));
                   8349:                }
                   8350:        } else {
                   8351:                REG8(AH) = 0x80;
                   8352:        }
                   8353: }
                   8354: 
1.1.1.14  root     8355: inline void pcbios_int_15h_10h()
                   8356: {
1.1.1.22  root     8357:        switch(REG8(AL)) {
                   8358:        case 0x00:
1.1.1.14  root     8359:                Sleep(10);
1.1.1.35  root     8360:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     8361:                break;
                   8362:        default:
                   8363:                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     8364:                REG8(AH) = 0x86;
                   8365:                m_CF = 1;
                   8366:        }
                   8367: }
                   8368: 
1.1       root     8369: inline void pcbios_int_15h_23h()
                   8370: {
                   8371:        switch(REG8(AL)) {
1.1.1.22  root     8372:        case 0x00:
1.1.1.8   root     8373:                REG8(CL) = cmos_read(0x2d);
                   8374:                REG8(CH) = cmos_read(0x2e);
1.1       root     8375:                break;
1.1.1.22  root     8376:        case 0x01:
1.1.1.8   root     8377:                cmos_write(0x2d, REG8(CL));
                   8378:                cmos_write(0x2e, REG8(CH));
1.1       root     8379:                break;
                   8380:        default:
1.1.1.22  root     8381:                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     8382:                REG8(AH) = 0x86;
1.1.1.3   root     8383:                m_CF = 1;
1.1       root     8384:                break;
                   8385:        }
                   8386: }
                   8387: 
                   8388: inline void pcbios_int_15h_24h()
                   8389: {
                   8390:        switch(REG8(AL)) {
1.1.1.22  root     8391:        case 0x00:
1.1.1.3   root     8392:                i386_set_a20_line(0);
1.1       root     8393:                REG8(AH) = 0;
                   8394:                break;
1.1.1.22  root     8395:        case 0x01:
1.1.1.3   root     8396:                i386_set_a20_line(1);
1.1       root     8397:                REG8(AH) = 0;
                   8398:                break;
1.1.1.22  root     8399:        case 0x02:
1.1       root     8400:                REG8(AH) = 0;
1.1.1.3   root     8401:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     8402:                REG16(CX) = 0;
                   8403:                break;
1.1.1.22  root     8404:        case 0x03:
1.1       root     8405:                REG16(AX) = 0;
                   8406:                REG16(BX) = 0;
                   8407:                break;
1.1.1.22  root     8408:        default:
                   8409:                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));
                   8410:                REG8(AH) = 0x86;
                   8411:                m_CF = 1;
                   8412:                break;
1.1       root     8413:        }
                   8414: }
                   8415: 
                   8416: inline void pcbios_int_15h_49h()
                   8417: {
1.1.1.27  root     8418:        REG8(AH) = 0x00;
                   8419:        REG8(BL) = 0x00; // DOS/V
1.1       root     8420: }
                   8421: 
1.1.1.22  root     8422: inline void pcbios_int_15h_50h()
                   8423: {
                   8424:        switch(REG8(AL)) {
                   8425:        case 0x00:
                   8426:        case 0x01:
                   8427:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   8428:                        REG8(AH) = 0x01; // invalid font type in bh
                   8429:                        m_CF = 1;
1.1.1.27  root     8430:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     8431:                        REG8(AH) = 0x02; // bl not zero
                   8432:                        m_CF = 1;
                   8433:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   8434:                        REG8(AH) = 0x04; // invalid code page
                   8435:                        m_CF = 1;
1.1.1.27  root     8436:                } else if(REG8(AL) == 0x01) {
                   8437:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     8438:                        m_CF = 1;
1.1.1.27  root     8439:                } else {
1.1.1.49  root     8440:                        // dummy font read routine is at fffc:000d
                   8441:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.27  root     8442:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     8443:                        REG16(BX) = 0x000d;
1.1.1.27  root     8444:                        REG8(AH) = 0x00; // success
1.1.1.22  root     8445:                }
                   8446:                break;
                   8447:        default:
                   8448:                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));
                   8449:                REG8(AH) = 0x86;
                   8450:                m_CF = 1;
                   8451:                break;
                   8452:        }
                   8453: }
                   8454: 
1.1.1.30  root     8455: inline void pcbios_int_15h_53h()
                   8456: {
                   8457:        switch(REG8(AL)) {
                   8458:        case 0x00:
                   8459:                // APM is not installed
                   8460:                REG8(AH) = 0x86;
                   8461:                m_CF = 1;
                   8462:                break;
                   8463:        default:
                   8464:                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));
                   8465:                REG8(AH) = 0x86;
                   8466:                m_CF = 1;
                   8467:                break;
                   8468:        }
                   8469: }
                   8470: 
1.1.1.43  root     8471: inline void pcbios_int_15h_84h()
                   8472: {
                   8473:        // joystick support (from DOSBox)
                   8474:        switch(REG16(DX)) {
                   8475:        case 0x00:
                   8476:                REG16(AX) = 0x00f0;
                   8477:                REG16(DX) = 0x0201;
                   8478:                m_CF = 1;
                   8479:                break;
                   8480:        case 0x01:
                   8481:                REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   8482:                m_CF = 1;
                   8483:                break;
                   8484:        default:
                   8485:                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));
                   8486:                REG8(AH) = 0x86;
                   8487:                m_CF = 1;
                   8488:                break;
                   8489:        }
                   8490: }
1.1.1.35  root     8491: 
                   8492: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     8493: {
                   8494:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     8495:        UINT32 msec = usec / 1000;
                   8496:        
1.1.1.54  root     8497:        while(msec && !m_exit) {
1.1.1.14  root     8498:                UINT32 tmp = min(msec, 100);
                   8499:                if(msec - tmp < 10) {
                   8500:                        tmp = msec;
                   8501:                }
                   8502:                Sleep(tmp);
                   8503:                msec -= tmp;
                   8504:        }
1.1.1.35  root     8505:        
                   8506: #ifdef USE_SERVICE_THREAD
                   8507:        service_exit = true;
                   8508: #endif
                   8509:        return(0);
                   8510: }
                   8511: 
                   8512: inline void pcbios_int_15h_86h()
                   8513: {
                   8514:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   8515: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8516:                if(!in_service && !in_service_29h) {
                   8517:                        start_service_loop(pcbios_int_15h_86h_thread);
                   8518:                } else {
                   8519: #endif
                   8520:                        pcbios_int_15h_86h_thread(NULL);
                   8521:                        REQUEST_HARDWRE_UPDATE();
                   8522: #ifdef USE_SERVICE_THREAD
                   8523:                }
1.1.1.35  root     8524: #endif
                   8525:        }
1.1       root     8526: }
                   8527: 
                   8528: inline void pcbios_int_15h_87h()
                   8529: {
                   8530:        // copy extended memory (from DOSBox)
                   8531:        int len = REG16(CX) * 2;
1.1.1.3   root     8532:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     8533:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   8534:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   8535:        memcpy(mem + dst, mem + src, len);
                   8536:        REG16(AX) = 0x00;
                   8537: }
                   8538: 
                   8539: inline void pcbios_int_15h_88h()
                   8540: {
1.1.1.17  root     8541:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     8542: }
                   8543: 
                   8544: inline void pcbios_int_15h_89h()
                   8545: {
1.1.1.21  root     8546: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     8547:        // switch to protected mode (from DOSBox)
                   8548:        write_io_byte(0x20, 0x10);
                   8549:        write_io_byte(0x21, REG8(BH));
                   8550:        write_io_byte(0x21, 0x00);
                   8551:        write_io_byte(0xa0, 0x10);
                   8552:        write_io_byte(0xa1, REG8(BL));
                   8553:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     8554:        i386_set_a20_line(1);
                   8555:        int ofs = SREG_BASE(ES) + REG16(SI);
                   8556:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   8557:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   8558:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   8559:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   8560: #if defined(HAS_I386)
                   8561:        m_cr[0] |= 1;
                   8562: #else
                   8563:        m_msw |= 1;
                   8564: #endif
                   8565:        SREG(DS) = 0x18;
                   8566:        SREG(ES) = 0x20;
                   8567:        SREG(SS) = 0x28;
                   8568:        i386_load_segment_descriptor(DS);
                   8569:        i386_load_segment_descriptor(ES);
                   8570:        i386_load_segment_descriptor(SS);
1.1.1.21  root     8571:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     8572:        REG16(SP) += 6;
1.1.1.3   root     8573: #if defined(HAS_I386)
1.1.1.21  root     8574:        UINT32 flags = get_flags();
                   8575:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8576:        set_flags(flags);
1.1.1.3   root     8577: #else
1.1.1.21  root     8578:        UINT32 flags = CompressFlags();
                   8579:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8580:        ExpandFlags(flags);
1.1.1.3   root     8581: #endif
1.1       root     8582:        REG16(AX) = 0x00;
1.1.1.21  root     8583:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     8584: #else
1.1.1.21  root     8585:        // i86/i186/v30: protected mode is not supported
1.1       root     8586:        REG8(AH) = 0x86;
1.1.1.3   root     8587:        m_CF = 1;
1.1       root     8588: #endif
                   8589: }
                   8590: 
1.1.1.21  root     8591: inline void pcbios_int_15h_8ah()
                   8592: {
                   8593:        UINT32 size = MAX_MEM - 0x100000;
                   8594:        REG16(AX) = size & 0xffff;
                   8595:        REG16(DX) = size >> 16;
                   8596: }
                   8597: 
1.1.1.54  root     8598: #ifdef EXT_BIOS_TOP
                   8599: inline void pcbios_int_15h_c1h()
                   8600: {
                   8601:        SREG(ES) = EXT_BIOS_TOP >> 4;
                   8602:        i386_load_segment_descriptor(ES);
                   8603: }
                   8604: #endif
                   8605: 
                   8606: void pcbios_read_from_ps2_mouse(UINT16 *data_1st, UINT16 *data_2nd, UINT16 *data_3rd)
                   8607: {
                   8608:        // from DOSBox DoPS2Callback()
                   8609:        UINT16 mdat = 0x08;
                   8610:        INT16 xdiff = mouse.position.x - mouse.prev_position.x;
                   8611:        INT16 ydiff = mouse.prev_position.y - mouse.position.y;
                   8612:        
1.1.1.59  root     8613: #if 1
                   8614:        if(xdiff > +16) xdiff = +16;
                   8615:        if(xdiff < -16) xdiff = -16;
                   8616:        if(ydiff > +16) ydiff = +16;
                   8617:        if(ydiff < -16) ydiff = -16;
                   8618: #endif
                   8619:        
1.1.1.54  root     8620:        if(mouse.buttons[0].status) {
                   8621:                mdat |= 0x01;
                   8622:        }
                   8623:        if(mouse.buttons[1].status) {
                   8624:                mdat |= 0x02;
                   8625:        }
                   8626:        mouse.prev_position.x = mouse.position.x;
                   8627:        mouse.prev_position.y = mouse.position.y;
1.1.1.59  root     8628:        
1.1.1.54  root     8629:        if((xdiff > 0xff) || (xdiff < -0xff)) {
                   8630:                mdat |= 0x40;   // x overflow
                   8631:        }
                   8632:        if((ydiff > 0xff) || (ydiff < -0xff)) {
                   8633:                mdat |= 0x80;   // y overflow
                   8634:        }
                   8635:        xdiff %= 256;
                   8636:        ydiff %= 256;
                   8637:        if(xdiff < 0) {
                   8638:                xdiff = (0x100 + xdiff);
                   8639:                mdat |= 0x10;
                   8640:        }
                   8641:        if(ydiff < 0) {
                   8642:                ydiff = (0x100 + ydiff);
                   8643:                mdat |= 0x20;
                   8644:        }
                   8645:        *data_1st = (UINT16)mdat;
                   8646:        *data_2nd = (UINT16)(xdiff % 256);
                   8647:        *data_3rd = (UINT16)(ydiff % 256);
                   8648: }
                   8649: 
                   8650: inline void pcbios_int_15h_c2h()
                   8651: {
                   8652:        static UINT8 sampling_rate = 5;
                   8653:        static UINT8 resolution = 2;
                   8654:        static UINT8 scaling = 1;
                   8655:        
                   8656:        switch(REG8(AL)) {
                   8657:        case 0x00:
1.1.1.59  root     8658:                if(REG8(BH) == 0x00) {
1.1.1.61  root     8659:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
1.1.1.59  root     8660:                        pic[1].imr |= 0x10; // disable irq12
                   8661:                        mouse.enabled_ps2 = false;
                   8662:                        REG8(AH) = 0x00; // successful
                   8663:                } else if(REG8(BH) == 0x01) {
1.1.1.61  root     8664:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), (dwConsoleMode | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS) & ~(ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE));
1.1.1.59  root     8665:                        pic[1].imr &= ~0x10; // enable irq12
                   8666:                        mouse.enabled_ps2 = true;
1.1.1.54  root     8667:                        REG8(AH) = 0x00; // successful
                   8668:                } else {
                   8669:                        REG8(AH) = 0x01; // invalid function
                   8670:                        m_CF = 1;
                   8671:                }
                   8672:                break;
                   8673:        case 0x01:
                   8674:                REG8(BH) = 0x00; // device id
                   8675:                REG8(BL) = 0xaa; // mouse
                   8676:        case 0x05:
1.1.1.61  root     8677:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
1.1.1.59  root     8678:                pic[1].imr |= 0x10; // disable irq12
                   8679:                mouse.enabled_ps2 = false;
1.1.1.54  root     8680:                sampling_rate = 5;
                   8681:                resolution = 2;
                   8682:                scaling = 1;
                   8683:                REG8(AH) = 0x00; // successful
                   8684:                break;
                   8685:        case 0x02:
                   8686:                sampling_rate = REG8(BH);
                   8687:                REG8(AH) = 0x00; // successful
                   8688:                break;
                   8689:        case 0x03:
                   8690:                resolution = REG8(BH);
                   8691:                REG8(AH) = 0x00; // successful
                   8692:                break;
                   8693:        case 0x04:
                   8694:                REG8(BH) = 0x00; // device id
                   8695:                REG8(AH) = 0x00; // successful
                   8696:                break;
                   8697:        case 0x06:
                   8698:                switch(REG8(BH)) {
                   8699:                case 0x00:
                   8700:                        REG8(BL) = 0x00;
                   8701:                        if(mouse.buttons[1].status) {
                   8702:                                REG8(BL) |= 0x01;
                   8703:                        }
                   8704:                        if(mouse.buttons[0].status) {
                   8705:                                REG8(BL) |= 0x04;
                   8706:                        }
                   8707:                        if(scaling == 2) {
                   8708:                                REG8(BL) |= 0x10;
                   8709:                        }
                   8710:                        REG8(CL) = resolution;
                   8711:                        switch(sampling_rate) {
                   8712:                        case 0:  REG8(DL) =  10; break;
                   8713:                        case 1:  REG8(DL) =  20; break;
                   8714:                        case 2:  REG8(DL) =  40; break;
                   8715:                        case 3:  REG8(DL) =  60; break;
                   8716:                        case 4:  REG8(DL) =  80; break;
                   8717: //                     case 5:  REG8(DL) = 100; break;
                   8718:                        case 6:  REG8(DL) = 200; break;
                   8719:                        default: REG8(DL) = 100; break;
                   8720:                        }
                   8721:                        REG8(AH) = 0x00; // successful
                   8722:                        break;
                   8723:                case 0x01:
                   8724:                case 0x02:
                   8725:                        scaling = REG8(BH);
                   8726:                        REG8(AH) = 0x00; // successful
                   8727:                        break;
                   8728:                default:
                   8729:                        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));
                   8730:                        REG8(AH) = 0x01; // invalid function
                   8731:                        m_CF = 1;
                   8732:                        break;
                   8733:                }
                   8734:                break;
                   8735:        case 0x07: // set device handler addr
                   8736:                mouse.call_addr_ps2.w.l = REG16(BX);
                   8737:                mouse.call_addr_ps2.w.h = SREG(ES);
                   8738:                REG8(AH) = 0x00; // successful
                   8739:                break;
                   8740:        case 0x08:
                   8741:                REG8(AH) = 0x00; // successful
                   8742:                break;
                   8743:        case 0x09:
                   8744:                {
                   8745:                        UINT16 data_1st, data_2nd, data_3rd;
                   8746:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   8747:                        REG8(BL) = (UINT8)(data_1st & 0xff);
                   8748:                        REG8(CL) = (UINT8)(data_2nd & 0xff);
                   8749:                        REG8(DL) = (UINT8)(data_3rd & 0xff);
                   8750:                }
                   8751:                REG8(AH) = 0x00; // successful
                   8752:                break;
                   8753:        default:
                   8754:                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));
                   8755: //             REG8(AH) = 0x86;
                   8756:                REG8(AH) = 0x01; // invalid function
                   8757:                m_CF = 1;
                   8758:                break;
                   8759:        }
                   8760: }
                   8761: 
1.1.1.3   root     8762: #if defined(HAS_I386)
1.1       root     8763: inline void pcbios_int_15h_c9h()
                   8764: {
                   8765:        REG8(AH) = 0x00;
                   8766:        REG8(CH) = cpu_type;
                   8767:        REG8(CL) = cpu_step;
                   8768: }
1.1.1.3   root     8769: #endif
1.1       root     8770: 
                   8771: inline void pcbios_int_15h_cah()
                   8772: {
                   8773:        switch(REG8(AL)) {
1.1.1.22  root     8774:        case 0x00:
1.1       root     8775:                if(REG8(BL) > 0x3f) {
                   8776:                        REG8(AH) = 0x03;
1.1.1.3   root     8777:                        m_CF = 1;
1.1       root     8778:                } else if(REG8(BL) < 0x0e) {
                   8779:                        REG8(AH) = 0x04;
1.1.1.3   root     8780:                        m_CF = 1;
1.1       root     8781:                } else {
1.1.1.8   root     8782:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     8783:                }
                   8784:                break;
1.1.1.22  root     8785:        case 0x01:
1.1       root     8786:                if(REG8(BL) > 0x3f) {
                   8787:                        REG8(AH) = 0x03;
1.1.1.3   root     8788:                        m_CF = 1;
1.1       root     8789:                } else if(REG8(BL) < 0x0e) {
                   8790:                        REG8(AH) = 0x04;
1.1.1.3   root     8791:                        m_CF = 1;
1.1       root     8792:                } else {
1.1.1.8   root     8793:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     8794:                }
                   8795:                break;
                   8796:        default:
1.1.1.22  root     8797:                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     8798:                REG8(AH) = 0x86;
1.1.1.3   root     8799:                m_CF = 1;
1.1       root     8800:                break;
                   8801:        }
                   8802: }
                   8803: 
1.1.1.22  root     8804: inline void pcbios_int_15h_e8h()
1.1.1.17  root     8805: {
1.1.1.22  root     8806:        switch(REG8(AL)) {
                   8807: #if defined(HAS_I386)
                   8808:        case 0x01:
                   8809:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   8810:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8811:                break;
1.1.1.17  root     8812: #endif
1.1.1.22  root     8813:        default:
                   8814:                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));
                   8815:                REG8(AH) = 0x86;
                   8816:                m_CF = 1;
                   8817:                break;
                   8818:        }
                   8819: }
1.1.1.17  root     8820: 
1.1.1.55  root     8821: bool pcbios_is_key_buffer_empty()
                   8822: {
                   8823:        return(*(UINT16 *)(mem + 0x41a) == *(UINT16 *)(mem + 0x41c));
                   8824: }
                   8825: 
1.1.1.51  root     8826: void pcbios_clear_key_buffer()
                   8827: {
                   8828:        key_buf_char->clear();
                   8829:        key_buf_scan->clear();
                   8830:        
                   8831:        // update key buffer
                   8832:        *(UINT16 *)(mem + 0x41a) = *(UINT16 *)(mem + 0x41c); // head = tail
                   8833: }
                   8834: 
                   8835: void pcbios_set_key_buffer(int key_char, int key_scan)
                   8836: {
                   8837:        // update key buffer
                   8838:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8839:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8840:        UINT16 next = tail + 2;
                   8841:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8842:                next = *(UINT16 *)(mem + 0x480);
                   8843:        }
                   8844:        if(next != head) {
                   8845:                *(UINT16 *)(mem + 0x41c) = next;
                   8846:                mem[0x400 + (tail++)] = key_char;
                   8847:                mem[0x400 + (tail++)] = key_scan;
1.1.1.55  root     8848:        } else {
                   8849:                // store to extra key buffer
                   8850:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8851:                        key_buf_char->write(key_char);
                   8852:                        key_buf_scan->write(key_scan);
                   8853:                }
1.1.1.51  root     8854:        }
                   8855: }
                   8856: 
                   8857: bool pcbios_get_key_buffer(int *key_char, int *key_scan)
                   8858: {
                   8859:        // update key buffer
                   8860:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8861:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8862:        UINT16 next = head + 2;
                   8863:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8864:                next = *(UINT16 *)(mem + 0x480);
                   8865:        }
                   8866:        if(head != tail) {
                   8867:                *(UINT16 *)(mem + 0x41a) = next;
1.1.1.55  root     8868:                *key_char = mem[0x400 + (head++)];
                   8869:                *key_scan = mem[0x400 + (head++)];
                   8870:                
                   8871:                // restore from extra key buffer
                   8872:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8873:                        if(!key_buf_char->empty()) {
                   8874:                                pcbios_set_key_buffer(key_buf_char->read(), key_buf_scan->read());
                   8875:                        }
                   8876:                }
                   8877:                return(true);
                   8878:        } else {
                   8879:                *key_char = 0x00;
                   8880:                *key_scan = 0x00;
                   8881:                return(false);
1.1.1.51  root     8882:        }
                   8883: }
                   8884: 
1.1.1.60  root     8885: bool pcbios_check_key_buffer(int *key_char, int *key_scan)
                   8886: {
                   8887:        // do not remove from key buffer
                   8888:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8889:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8890:        if(head != tail) {
                   8891:                *key_char = mem[0x400 + (head++)];
                   8892:                *key_scan = mem[0x400 + (head++)];
                   8893:                return(true);
                   8894:        } else {
                   8895:                *key_char = 0x00;
                   8896:                *key_scan = 0x00;
                   8897:                return(false);
                   8898:        }
                   8899: }
                   8900: 
1.1.1.33  root     8901: void pcbios_update_key_code(bool wait)
1.1       root     8902: {
1.1.1.32  root     8903:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8904: #ifdef USE_SERVICE_THREAD
                   8905:                EnterCriticalSection(&key_buf_crit_sect);
                   8906: #endif
1.1.1.55  root     8907:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     8908: #ifdef USE_SERVICE_THREAD
                   8909:                LeaveCriticalSection(&key_buf_crit_sect);
                   8910: #endif
                   8911:                if(empty) {
1.1.1.32  root     8912:                        if(!update_key_buffer()) {
1.1.1.33  root     8913:                                if(wait) {
1.1.1.32  root     8914:                                        Sleep(10);
                   8915:                                } else {
                   8916:                                        maybe_idle();
                   8917:                                }
1.1.1.14  root     8918:                        }
                   8919:                }
1.1.1.34  root     8920:        }
                   8921:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8922: #ifdef USE_SERVICE_THREAD
                   8923:                EnterCriticalSection(&key_buf_crit_sect);
                   8924: #endif
1.1.1.51  root     8925:                int key_char, key_scan;
                   8926:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8927:                        key_code  = key_char << 0;
                   8928:                        key_code |= key_scan << 8;
1.1.1.35  root     8929:                        key_recv  = 0x0000ffff;
1.1.1.51  root     8930:                }
                   8931:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8932:                        key_code |= key_char << 16;
                   8933:                        key_code |= key_scan << 24;
1.1.1.33  root     8934:                        key_recv |= 0xffff0000;
1.1.1.32  root     8935:                }
1.1.1.35  root     8936: #ifdef USE_SERVICE_THREAD
                   8937:                LeaveCriticalSection(&key_buf_crit_sect);
                   8938: #endif
1.1       root     8939:        }
                   8940: }
                   8941: 
1.1.1.35  root     8942: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     8943: {
1.1.1.54  root     8944:        while(key_recv == 0 && !m_exit) {
1.1.1.33  root     8945:                pcbios_update_key_code(true);
1.1       root     8946:        }
1.1.1.33  root     8947:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8948:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   8949:                        if(REG8(AH) == 0x10) {
                   8950:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   8951:                        } else {
                   8952:                                key_code = ((key_code >> 16) & 0xff00);
                   8953:                        }
                   8954:                        key_recv >>= 16;
1.1       root     8955:                }
                   8956:        }
                   8957:        REG16(AX) = key_code & 0xffff;
                   8958:        key_code >>= 16;
1.1.1.33  root     8959:        key_recv >>= 16;
1.1.1.35  root     8960:        
                   8961: #ifdef USE_SERVICE_THREAD
                   8962:        service_exit = true;
                   8963: #endif
                   8964:        return(0);
                   8965: }
                   8966: 
                   8967: inline void pcbios_int_16h_00h()
                   8968: {
                   8969: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8970:        if(!in_service && !in_service_29h) {
                   8971:                start_service_loop(pcbios_int_16h_00h_thread);
                   8972:        } else {
                   8973: #endif
                   8974:                pcbios_int_16h_00h_thread(NULL);
                   8975:                REQUEST_HARDWRE_UPDATE();
                   8976: #ifdef USE_SERVICE_THREAD
                   8977:        }
1.1.1.35  root     8978: #endif
1.1       root     8979: }
                   8980: 
                   8981: inline void pcbios_int_16h_01h()
                   8982: {
1.1.1.33  root     8983:        if(key_recv == 0) {
                   8984:                pcbios_update_key_code(false);
1.1.1.5   root     8985:        }
1.1.1.33  root     8986:        if(key_recv != 0) {
                   8987:                UINT32 key_code_tmp = key_code;
                   8988:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8989:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   8990:                                if(REG8(AH) == 0x11) {
                   8991:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   8992:                                } else {
                   8993:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   8994:                                }
                   8995:                        }
1.1       root     8996:                }
1.1.1.5   root     8997:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     8998: #if defined(HAS_I386)
1.1.1.33  root     8999:                m_ZF = 0;
                   9000: #else
                   9001:                m_ZeroVal = 1;
                   9002: #endif
                   9003:        } else {
                   9004: #if defined(HAS_I386)
                   9005:                m_ZF = 1;
1.1.1.3   root     9006: #else
1.1.1.33  root     9007:                m_ZeroVal = 0;
1.1.1.3   root     9008: #endif
1.1.1.33  root     9009:        }
1.1       root     9010: }
                   9011: 
                   9012: inline void pcbios_int_16h_02h()
                   9013: {
                   9014:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   9015:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   9016:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   9017:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   9018:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   9019:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   9020:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   9021:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   9022: }
                   9023: 
                   9024: inline void pcbios_int_16h_03h()
                   9025: {
                   9026:        static UINT16 status = 0;
                   9027:        
                   9028:        switch(REG8(AL)) {
                   9029:        case 0x05:
                   9030:                status = REG16(BX);
                   9031:                break;
                   9032:        case 0x06:
                   9033:                REG16(BX) = status;
                   9034:                break;
                   9035:        default:
1.1.1.3   root     9036:                m_CF = 1;
1.1       root     9037:                break;
                   9038:        }
                   9039: }
                   9040: 
                   9041: inline void pcbios_int_16h_05h()
                   9042: {
1.1.1.32  root     9043:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     9044: #ifdef USE_SERVICE_THREAD
                   9045:                EnterCriticalSection(&key_buf_crit_sect);
                   9046: #endif
1.1.1.51  root     9047:                pcbios_set_key_buffer(REG8(CL), REG8(CH));
1.1.1.35  root     9048: #ifdef USE_SERVICE_THREAD
                   9049:                LeaveCriticalSection(&key_buf_crit_sect);
                   9050: #endif
1.1.1.32  root     9051:        }
1.1       root     9052:        REG8(AL) = 0x00;
                   9053: }
                   9054: 
1.1.1.60  root     9055: inline void pcbios_int_16h_09h()
                   9056: {
                   9057:        REG8(AL)  = 0x00;
                   9058: //     REG8(AL) |= 0x01;       // INT 16/AX=0300h supported    (set default delay and rate (PCjr and some PS/2))
                   9059: //     REG8(AL) |= 0x02;       // INT 16/AX=0304h supported    (turn off typematic repeat (PCjr and some PS/2))
                   9060:        REG8(AL) |= 0x04;       // INT 16/AX=0305h supported    (set repeat rate and delay (AT,PS))
                   9061:        REG8(AL) |= 0x08;       // INT 16/AX=0306h supported    (get current typematic rate and delay (newer PS/2s))
                   9062:        REG8(AL) |= 0x10;       // INT 16/AH=0Ah supported      (get keyboard id)
                   9063:        REG8(AL) |= 0x20;       // INT 16/AH=10h-12h supported  (enhanced keyboard support)
                   9064: //     REG8(AL) |= 0x40;       // INT 16/AH=20h-22h supported  (122-key keyboard support)
                   9065: //     REG8(AL) |= 0x80;       // reserved
                   9066: }
                   9067: 
                   9068: inline void pcbios_int_16h_0ah()
                   9069: {
                   9070: //     REG16(BX) = 0x41ab;     // MF2 Keyboard (usually in translate mode)
                   9071:        REG16(BX) = 0x83ab;     // MF2 Keyboard (pass-through mode)
                   9072: }
                   9073: 
                   9074: inline void pcbios_int_16h_11h()
                   9075: {
                   9076:        int key_char, key_scan;
                   9077:        
                   9078: #ifdef USE_SERVICE_THREAD
                   9079:        EnterCriticalSection(&key_buf_crit_sect);
                   9080: #endif
                   9081:        if(pcbios_check_key_buffer(&key_char, &key_scan)) {
                   9082:                REG8(AL) = key_char;
                   9083:                REG8(AH) = key_scan;
                   9084: #if defined(HAS_I386)
                   9085:                m_ZF = 0;
                   9086: #else
                   9087:                m_ZeroVal = 1;
                   9088: #endif
                   9089:        } else {
                   9090: #if defined(HAS_I386)
                   9091:                m_ZF = 1;
                   9092: #else
                   9093:                m_ZeroVal = 0;
                   9094: #endif
                   9095:        }
                   9096: #ifdef USE_SERVICE_THREAD
                   9097:        LeaveCriticalSection(&key_buf_crit_sect);
                   9098: #endif
                   9099: }
                   9100: 
1.1       root     9101: inline void pcbios_int_16h_12h()
                   9102: {
                   9103:        pcbios_int_16h_02h();
                   9104:        
                   9105:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   9106:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   9107:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   9108:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   9109:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   9110:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   9111:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   9112:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   9113: }
                   9114: 
                   9115: inline void pcbios_int_16h_13h()
                   9116: {
                   9117:        static UINT16 status = 0;
                   9118:        
                   9119:        switch(REG8(AL)) {
                   9120:        case 0x00:
                   9121:                status = REG16(DX);
                   9122:                break;
                   9123:        case 0x01:
                   9124:                REG16(DX) = status;
                   9125:                break;
                   9126:        default:
1.1.1.22  root     9127:                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     9128:                m_CF = 1;
1.1       root     9129:                break;
                   9130:        }
                   9131: }
                   9132: 
                   9133: inline void pcbios_int_16h_14h()
                   9134: {
                   9135:        static UINT8 status = 0;
                   9136:        
                   9137:        switch(REG8(AL)) {
                   9138:        case 0x00:
                   9139:        case 0x01:
                   9140:                status = REG8(AL);
                   9141:                break;
                   9142:        case 0x02:
                   9143:                REG8(AL) = status;
                   9144:                break;
                   9145:        default:
1.1.1.22  root     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));
1.1.1.3   root     9147:                m_CF = 1;
1.1       root     9148:                break;
                   9149:        }
                   9150: }
                   9151: 
1.1.1.24  root     9152: inline void pcbios_int_16h_55h()
                   9153: {
                   9154:        switch(REG8(AL)) {
                   9155:        case 0x00:
                   9156:                // keyboard tsr is not present
                   9157:                break;
                   9158:        case 0xfe:
                   9159:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   9160:                break;
                   9161:        case 0xff:
                   9162:                break;
                   9163:        default:
                   9164:                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));
                   9165:                m_CF = 1;
                   9166:                break;
                   9167:        }
                   9168: }
                   9169: 
1.1.1.30  root     9170: inline void pcbios_int_16h_6fh()
                   9171: {
                   9172:        switch(REG8(AL)) {
                   9173:        case 0x00:
                   9174:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   9175:                break;
                   9176:        default:
                   9177:                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));
                   9178:                m_CF = 1;
                   9179:                break;
                   9180:        }
                   9181: }
                   9182: 
1.1.1.37  root     9183: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   9184: {
                   9185:        UINT8 hi = jis >> 8;
                   9186:        UINT8 lo = jis & 0xff;
                   9187:        
                   9188:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   9189:        hi = (hi - 0x21) / 2 + 0x81;
                   9190:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   9191:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   9192:        
                   9193:        return((hi << 8) + lo);
                   9194: }
                   9195: 
                   9196: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   9197: {
                   9198:        UINT8 hi = sjis >> 8;
                   9199:        UINT8 lo = sjis & 0xff;
                   9200:        
                   9201:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   9202:                return(0x2121);
                   9203:        }
                   9204:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   9205:                return(0x2121);
                   9206:        }
                   9207:        if(hi >= 0xf0 && hi <= 0xf3) {
                   9208:                // gaiji
                   9209:                if(lo >= 0x40 && lo <= 0x7e) {
                   9210:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   9211:                }
                   9212:                if(lo >= 0x80 && lo <= 0x9e) {
                   9213:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   9214:                }
                   9215:                if(lo >= 0x9f && lo <= 0xfc) {
                   9216:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   9217:                }
                   9218:        }
                   9219:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   9220:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   9221:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   9222:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   9223:        
                   9224:        return((hi << 8) + lo);
                   9225: }
                   9226: 
1.1.1.38  root     9227: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   9228: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   9229: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     9230: 
                   9231: void pcbios_printer_out(int c, UINT8 data)
                   9232: {
                   9233:        if(pio[c].conv_mode) {
                   9234:                if(pio[c].sjis_hi != 0) {
                   9235:                        if(!pio[c].jis_mode) {
                   9236:                                printer_out(c, 0x1c);
                   9237:                                printer_out(c, 0x26);
                   9238:                                pio[c].jis_mode = true;
                   9239:                        }
                   9240:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   9241:                        printer_out(c, jis >> 8);
                   9242:                        printer_out(c, jis & 0xff);
                   9243:                        pio[c].sjis_hi = 0;
                   9244:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   9245:                        printer_out(c, data);
                   9246:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   9247:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   9248:                        }
                   9249:                        pio[c].esc_len++;
                   9250:                        
                   9251:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     9252:                        case 0x33: // 1Bh 33h XX
                   9253:                        case 0x4a: // 1Bh 4Ah XX
                   9254:                        case 0x4e: // 1Bh 4Eh XX
                   9255:                        case 0x51: // 1Bh 51h XX
                   9256:                        case 0x55: // 1Bh 55h XX
                   9257:                        case 0x6c: // 1Bh 6Ch XX
                   9258:                        case 0x71: // 1Bh 71h XX
                   9259:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     9260:                                if(pio[c].esc_len == 3) {
                   9261:                                        pio[c].esc_buf[0] = 0x00;
                   9262:                                }
                   9263:                                break;
1.1.1.38  root     9264:                        case 0x24: // 1Bh 24h XX XX
                   9265:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     9266:                                if(pio[c].esc_len == 4) {
                   9267:                                        pio[c].esc_buf[0] = 0x00;
                   9268:                                }
                   9269:                                break;
1.1.1.38  root     9270:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     9271:                                if(pio[c].esc_len >= 3) {
                   9272:                                        switch(pio[c].esc_buf[2]) {
                   9273:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   9274:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   9275:                                                        pio[c].esc_buf[0] = 0x00;
                   9276:                                                }
                   9277:                                                break;
                   9278:                                        case 32: case 33: case 38: case 39: case 40:
                   9279:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   9280:                                                        pio[c].esc_buf[0] = 0x00;
                   9281:                                                }
                   9282:                                                break;
1.1.1.38  root     9283:                                        case 71: case 72: case 73:
                   9284:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   9285:                                                        pio[c].esc_buf[0] = 0x00;
                   9286:                                                }
                   9287:                                                break;
1.1.1.37  root     9288:                                        default:
                   9289:                                                pio[c].esc_buf[0] = 0x00;
                   9290:                                                break;
                   9291:                                        }
                   9292:                                }
                   9293:                                break;
1.1.1.38  root     9294:                        case 0x40: // 1Bh 40h
1.1.1.37  root     9295:                                if(pio[c].jis_mode) {
                   9296:                                        printer_out(c, 0x1c);
                   9297:                                        printer_out(c, 0x2e);
                   9298:                                        pio[c].jis_mode = false;
                   9299:                                }
                   9300:                                pio[c].esc_buf[0] = 0x00;
                   9301:                                break;
1.1.1.38  root     9302:                        case 0x42: // 1Bh 42h data 00h
                   9303:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     9304:                                if(pio[c].esc_len >= 3 && data == 0) {
                   9305:                                        pio[c].esc_buf[0] = 0x00;
                   9306:                                }
                   9307:                                break;
1.1.1.38  root     9308:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     9309:                                if(pio[c].esc_len >= 3 && data != 0) {
                   9310:                                        pio[c].esc_buf[0] = 0x00;
                   9311:                                }
                   9312:                                break;
1.1.1.38  root     9313:                        default: // 1Bh XX
1.1.1.37  root     9314:                                pio[c].esc_buf[0] = 0x00;
                   9315:                                break;
                   9316:                        }
                   9317:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   9318:                        printer_out(c, data);
                   9319:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   9320:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   9321:                        }
                   9322:                        pio[c].esc_len++;
                   9323:                        
                   9324:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     9325:                        case 0x21: // 1Ch 21h XX
                   9326:                        case 0x2d: // 1Ch 2Dh XX
                   9327:                        case 0x57: // 1Ch 57h XX
                   9328:                        case 0x6b: // 1Ch 6Bh XX
                   9329:                        case 0x72: // 1Ch 72h XX
                   9330:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     9331:                                if(pio[c].esc_len == 3) {
                   9332:                                        pio[c].esc_buf[0] = 0x00;
                   9333:                                }
                   9334:                                break;
1.1.1.38  root     9335:                        case 0x26: // 1Ch 26h
1.1.1.37  root     9336:                                pio[c].jis_mode = true;
                   9337:                                pio[c].esc_buf[0] = 0x00;
                   9338:                                break;
1.1.1.38  root     9339:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     9340:                                pio[c].jis_mode = false;
                   9341:                                pio[c].esc_buf[0] = 0x00;
                   9342:                                break;
1.1.1.38  root     9343:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     9344:                                if(pio[c].esc_len == 76) {
                   9345:                                        pio[c].esc_buf[0] = 0x00;
                   9346:                                }
                   9347:                                break;
1.1.1.38  root     9348:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     9349:                                if(pio[c].esc_len == 6) {
                   9350:                                        pio[c].esc_buf[0] = 0x00;
                   9351:                                }
                   9352:                                break;
1.1.1.38  root     9353:                        case 0x53: // 1Ch 53h XX XX
                   9354:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     9355:                                if(pio[c].esc_len == 4) {
                   9356:                                        pio[c].esc_buf[0] = 0x00;
                   9357:                                }
                   9358:                                break;
1.1.1.38  root     9359:                        default: // 1Ch XX
1.1.1.37  root     9360:                                pio[c].esc_buf[0] = 0x00;
                   9361:                                break;
                   9362:                        }
                   9363:                } else if(data == 0x1b || data == 0x1c) {
                   9364:                        printer_out(c, data);
                   9365:                        pio[c].esc_buf[0] = data;
                   9366:                        pio[c].esc_len = 1;
                   9367:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   9368:                        pio[c].sjis_hi = data;
                   9369:                } else {
                   9370:                        if(pio[c].jis_mode) {
                   9371:                                printer_out(c, 0x1c);
                   9372:                                printer_out(c, 0x2e);
                   9373:                                pio[c].jis_mode = false;
                   9374:                        }
                   9375:                        printer_out(c, data);
                   9376:                }
                   9377:        } else {
                   9378:                if(pio[c].jis_mode) {
                   9379:                        printer_out(c, 0x1c);
                   9380:                        printer_out(c, 0x2e);
                   9381:                        pio[c].jis_mode = false;
                   9382:                }
                   9383:                printer_out(c, data);
                   9384:        }
                   9385: }
                   9386: 
                   9387: inline void pcbios_int_17h_00h()
                   9388: {
                   9389:        if(REG16(DX) < 3) {
                   9390:                pcbios_printer_out(REG16(DX), REG8(AL));
                   9391:                REG8(AH) = 0xd0;
                   9392:        }
                   9393: }
                   9394: 
                   9395: inline void pcbios_int_17h_01h()
                   9396: {
                   9397:        if(REG16(DX) < 3) {
                   9398:                REG8(AH) = 0xd0;
                   9399:        }
                   9400: }
                   9401: 
                   9402: inline void pcbios_int_17h_02h()
                   9403: {
                   9404:        if(REG16(DX) < 3) {
                   9405:                REG8(AH) = 0xd0;
                   9406:        }
                   9407: }
                   9408: 
                   9409: inline void pcbios_int_17h_03h()
                   9410: {
                   9411:        switch(REG8(AL)) {
                   9412:        case 0x00:
                   9413:                if(REG16(DX) < 3) {
                   9414:                        if(pio[REG16(DX)].jis_mode) {
                   9415:                                printer_out(REG16(DX), 0x1c);
                   9416:                                printer_out(REG16(DX), 0x2e);
                   9417:                                pio[REG16(DX)].jis_mode = false;
                   9418:                        }
                   9419:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   9420:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   9421:                        }
                   9422:                        REG16(CX) = 0x0000;
                   9423:                        REG8(AH) = 0xd0;
                   9424:                }
                   9425:                break;
                   9426:        default:
                   9427:                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));
                   9428:                break;
                   9429:        }
                   9430: }
                   9431: 
                   9432: inline void pcbios_int_17h_50h()
                   9433: {
                   9434:        switch(REG8(AL)) {
                   9435:        case 0x00:
                   9436:                if(REG16(DX) < 3) {
                   9437:                        if(REG16(BX) = 0x0001) {
                   9438:                                pio[REG16(DX)].conv_mode = false;
                   9439:                                REG8(AL) = 0x00;
                   9440:                        } else if(REG16(BX) = 0x0051) {
                   9441:                                pio[REG16(DX)].conv_mode = true;
                   9442:                                REG8(AL) = 0x00;
                   9443:                        } else {
                   9444:                                REG8(AL) = 0x01;
                   9445:                        }
                   9446:                } else {
                   9447:                        REG8(AL) = 0x02;
                   9448:                }
                   9449:                break;
                   9450:        case 0x01:
                   9451:                if(REG16(DX) < 3) {
                   9452:                        if(pio[REG16(DX)].conv_mode) {
                   9453:                                REG16(BX) = 0x0051;
                   9454:                        } else {
                   9455:                                REG16(BX) = 0x0001;
                   9456:                        }
                   9457:                        REG8(AL) = 0x00;
                   9458:                } else {
                   9459:                        REG8(AL) = 0x02;
                   9460:                }
                   9461:                break;
                   9462:        default:
                   9463:                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));
                   9464:                break;
                   9465:        }
                   9466: }
                   9467: 
                   9468: inline void pcbios_int_17h_51h()
                   9469: {
                   9470:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   9471:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   9472:        } else {
                   9473:                REG16(DX) = 0x0000;
                   9474:        }
                   9475: }
                   9476: 
                   9477: inline void pcbios_int_17h_52h()
                   9478: {
                   9479:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   9480:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   9481:        } else {
                   9482:                REG16(DX) = 0x0000;
                   9483:        }
                   9484: }
                   9485: 
                   9486: inline void pcbios_int_17h_84h()
                   9487: {
                   9488:        if(REG16(DX) < 3) {
                   9489:                if(pio[REG16(DX)].jis_mode) {
                   9490:                        printer_out(REG16(DX), 0x1c);
                   9491:                        printer_out(REG16(DX), 0x2e);
                   9492:                        pio[REG16(DX)].jis_mode = false;
                   9493:                }
                   9494:                printer_out(REG16(DX), REG8(AL));
                   9495:                REG8(AH) = 0xd0;
                   9496:        }
                   9497: }
                   9498: 
                   9499: inline void pcbios_int_17h_85h()
                   9500: {
                   9501:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   9502: }
                   9503: 
1.1       root     9504: inline void pcbios_int_1ah_00h()
                   9505: {
1.1.1.19  root     9506:        pcbios_update_daily_timer_counter(timeGetTime());
                   9507:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   9508:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   9509:        REG8(AL) = mem[0x470];
                   9510:        mem[0x470] = 0;
1.1       root     9511: }
                   9512: 
                   9513: inline int to_bcd(int t)
                   9514: {
                   9515:        int u = (t % 100) / 10;
                   9516:        return (u << 4) | (t % 10);
                   9517: }
                   9518: 
                   9519: inline void pcbios_int_1ah_02h()
                   9520: {
                   9521:        SYSTEMTIME time;
                   9522:        
                   9523:        GetLocalTime(&time);
                   9524:        REG8(CH) = to_bcd(time.wHour);
                   9525:        REG8(CL) = to_bcd(time.wMinute);
                   9526:        REG8(DH) = to_bcd(time.wSecond);
                   9527:        REG8(DL) = 0x00;
                   9528: }
                   9529: 
                   9530: inline void pcbios_int_1ah_04h()
                   9531: {
                   9532:        SYSTEMTIME time;
                   9533:        
                   9534:        GetLocalTime(&time);
                   9535:        REG8(CH) = to_bcd(time.wYear / 100);
                   9536:        REG8(CL) = to_bcd(time.wYear);
                   9537:        REG8(DH) = to_bcd(time.wMonth);
                   9538:        REG8(DL) = to_bcd(time.wDay);
                   9539: }
                   9540: 
                   9541: inline void pcbios_int_1ah_0ah()
                   9542: {
                   9543:        SYSTEMTIME time;
                   9544:        FILETIME file_time;
                   9545:        WORD dos_date, dos_time;
                   9546:        
                   9547:        GetLocalTime(&time);
                   9548:        SystemTimeToFileTime(&time, &file_time);
                   9549:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   9550:        REG16(CX) = dos_date;
                   9551: }
                   9552: 
                   9553: // msdos system call
                   9554: 
1.1.1.43  root     9555: inline void msdos_int_21h_56h(int lfn);
                   9556: 
1.1       root     9557: inline void msdos_int_21h_00h()
                   9558: {
1.1.1.3   root     9559:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     9560: }
                   9561: 
1.1.1.35  root     9562: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     9563: {
                   9564:        REG8(AL) = msdos_getche();
1.1.1.33  root     9565:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9566:        
1.1.1.35  root     9567: #ifdef USE_SERVICE_THREAD
                   9568:        service_exit = true;
                   9569: #endif
                   9570:        return(0);
                   9571: }
                   9572: 
                   9573: inline void msdos_int_21h_01h()
                   9574: {
                   9575: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9576:        if(!in_service && !in_service_29h &&
1.1.1.58  root     9577:           *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     9578:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9579:                // msdos_putch() will be used in this service
                   9580:                // if int 29h is hooked, run this service in main thread to call int 29h
                   9581:                start_service_loop(msdos_int_21h_01h_thread);
                   9582:        } else {
                   9583: #endif
                   9584:                msdos_int_21h_01h_thread(NULL);
                   9585:                REQUEST_HARDWRE_UPDATE();
                   9586: #ifdef USE_SERVICE_THREAD
                   9587:        }
1.1.1.35  root     9588: #endif
1.1       root     9589: }
                   9590: 
                   9591: inline void msdos_int_21h_02h()
                   9592: {
1.1.1.33  root     9593:        UINT8 data = REG8(DL);
                   9594:        msdos_putch(data);
                   9595:        REG8(AL) = data;
                   9596:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9597: }
                   9598: 
                   9599: inline void msdos_int_21h_03h()
                   9600: {
                   9601:        REG8(AL) = msdos_aux_in();
                   9602: }
                   9603: 
                   9604: inline void msdos_int_21h_04h()
                   9605: {
                   9606:        msdos_aux_out(REG8(DL));
                   9607: }
                   9608: 
                   9609: inline void msdos_int_21h_05h()
                   9610: {
                   9611:        msdos_prn_out(REG8(DL));
                   9612: }
                   9613: 
                   9614: inline void msdos_int_21h_06h()
                   9615: {
                   9616:        if(REG8(DL) == 0xff) {
                   9617:                if(msdos_kbhit()) {
                   9618:                        REG8(AL) = msdos_getch();
1.1.1.3   root     9619: #if defined(HAS_I386)
                   9620:                        m_ZF = 0;
                   9621: #else
                   9622:                        m_ZeroVal = 1;
                   9623: #endif
1.1       root     9624:                } else {
                   9625:                        REG8(AL) = 0;
1.1.1.3   root     9626: #if defined(HAS_I386)
                   9627:                        m_ZF = 1;
                   9628: #else
                   9629:                        m_ZeroVal = 0;
                   9630: #endif
1.1.1.14  root     9631:                        maybe_idle();
1.1       root     9632:                }
                   9633:        } else {
1.1.1.33  root     9634:                UINT8 data = REG8(DL);
                   9635:                msdos_putch(data);
                   9636:                REG8(AL) = data;
1.1       root     9637:        }
                   9638: }
                   9639: 
1.1.1.35  root     9640: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     9641: {
                   9642:        REG8(AL) = msdos_getch();
1.1.1.26  root     9643:        
1.1.1.35  root     9644: #ifdef USE_SERVICE_THREAD
                   9645:        service_exit = true;
                   9646: #endif
                   9647:        return(0);
1.1       root     9648: }
                   9649: 
1.1.1.35  root     9650: inline void msdos_int_21h_07h()
                   9651: {
                   9652: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9653:        if(!in_service && !in_service_29h) {
                   9654:                start_service_loop(msdos_int_21h_07h_thread);
                   9655:        } else {
                   9656: #endif
                   9657:                msdos_int_21h_07h_thread(NULL);
                   9658:                REQUEST_HARDWRE_UPDATE();
                   9659: #ifdef USE_SERVICE_THREAD
                   9660:        }
1.1.1.35  root     9661: #endif
                   9662: }
                   9663: 
                   9664: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     9665: {
                   9666:        REG8(AL) = msdos_getch();
1.1.1.33  root     9667:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9668:        
1.1.1.35  root     9669: #ifdef USE_SERVICE_THREAD
                   9670:        service_exit = true;
                   9671: #endif
                   9672:        return(0);
                   9673: }
                   9674: 
                   9675: inline void msdos_int_21h_08h()
                   9676: {
                   9677: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9678:        if(!in_service && !in_service_29h) {
                   9679:                start_service_loop(msdos_int_21h_08h_thread);
                   9680:        } else {
                   9681: #endif
                   9682:                msdos_int_21h_08h_thread(NULL);
                   9683:                REQUEST_HARDWRE_UPDATE();
                   9684: #ifdef USE_SERVICE_THREAD
                   9685:        }
1.1.1.35  root     9686: #endif
1.1       root     9687: }
                   9688: 
                   9689: inline void msdos_int_21h_09h()
                   9690: {
1.1.1.21  root     9691:        msdos_stdio_reopen();
                   9692:        
1.1.1.20  root     9693:        process_t *process = msdos_process_info_get(current_psp);
                   9694:        int fd = msdos_psp_get_file_table(1, current_psp);
                   9695:        
1.1.1.14  root     9696:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   9697:        int len = 0;
1.1       root     9698:        
1.1.1.14  root     9699:        while(str[len] != '$' && len < 0x10000) {
                   9700:                len++;
                   9701:        }
1.1.1.20  root     9702:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9703:                // stdout is redirected to file
1.1.1.20  root     9704:                msdos_write(fd, str, len);
1.1       root     9705:        } else {
                   9706:                for(int i = 0; i < len; i++) {
1.1.1.14  root     9707:                        msdos_putch(str[i]);
1.1       root     9708:                }
                   9709:        }
1.1.1.33  root     9710:        REG8(AL) = '$';
                   9711:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9712: }
                   9713: 
1.1.1.35  root     9714: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     9715: {
1.1.1.3   root     9716:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     9717:        int max = mem[ofs] - 1;
                   9718:        UINT8 *buf = mem + ofs + 2;
                   9719:        int chr, p = 0;
                   9720:        
                   9721:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     9722:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     9723:                        p = 0;
1.1.1.33  root     9724:                        msdos_putch(0x03);
                   9725:                        msdos_putch(0x0d);
                   9726:                        msdos_putch(0x0a);
1.1.1.26  root     9727:                        break;
1.1.1.33  root     9728:                } else if(ctrl_break_pressed) {
                   9729:                        // skip this byte
1.1.1.26  root     9730:                } else if(chr == 0x00) {
1.1       root     9731:                        // skip 2nd byte
                   9732:                        msdos_getch();
                   9733:                } else if(chr == 0x08) {
                   9734:                        // back space
                   9735:                        if(p > 0) {
                   9736:                                p--;
1.1.1.20  root     9737:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     9738:                                        msdos_putch(0x08);
                   9739:                                        msdos_putch(0x08);
                   9740:                                        msdos_putch(0x20);
                   9741:                                        msdos_putch(0x20);
                   9742:                                        msdos_putch(0x08);
                   9743:                                        msdos_putch(0x08);
1.1.1.36  root     9744:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9745:                                        p--;
                   9746:                                        msdos_putch(0x08);
                   9747:                                        msdos_putch(0x08);
                   9748:                                        msdos_putch(0x20);
                   9749:                                        msdos_putch(0x20);
                   9750:                                        msdos_putch(0x08);
                   9751:                                        msdos_putch(0x08);
1.1.1.34  root     9752:                                } else {
                   9753:                                        msdos_putch(0x08);
                   9754:                                        msdos_putch(0x20);
                   9755:                                        msdos_putch(0x08);
                   9756:                                }
                   9757:                        }
                   9758:                } else if(chr == 0x1b) {
                   9759:                        // escape
                   9760:                        while(p > 0) {
                   9761:                                p--;
                   9762:                                if(msdos_ctrl_code_check(buf[p])) {
                   9763:                                        msdos_putch(0x08);
                   9764:                                        msdos_putch(0x08);
                   9765:                                        msdos_putch(0x20);
                   9766:                                        msdos_putch(0x20);
                   9767:                                        msdos_putch(0x08);
                   9768:                                        msdos_putch(0x08);
1.1.1.20  root     9769:                                } else {
1.1.1.34  root     9770:                                        msdos_putch(0x08);
                   9771:                                        msdos_putch(0x20);
                   9772:                                        msdos_putch(0x08);
1.1.1.20  root     9773:                                }
1.1       root     9774:                        }
                   9775:                } else if(p < max) {
                   9776:                        buf[p++] = chr;
                   9777:                        msdos_putch(chr);
                   9778:                }
                   9779:        }
                   9780:        buf[p] = 0x0d;
                   9781:        mem[ofs + 1] = p;
1.1.1.33  root     9782:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9783:        
1.1.1.35  root     9784: #ifdef USE_SERVICE_THREAD
                   9785:        service_exit = true;
                   9786: #endif
                   9787:        return(0);
                   9788: }
                   9789: 
                   9790: inline void msdos_int_21h_0ah()
                   9791: {
                   9792:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   9793: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9794:                if(!in_service && !in_service_29h &&
1.1.1.58  root     9795:                   *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     9796:                   *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9797:                        // msdos_putch() will be used in this service
                   9798:                        // if int 29h is hooked, run this service in main thread to call int 29h
                   9799:                        start_service_loop(msdos_int_21h_0ah_thread);
                   9800:                } else {
                   9801: #endif
                   9802:                        msdos_int_21h_0ah_thread(NULL);
                   9803:                        REQUEST_HARDWRE_UPDATE();
                   9804: #ifdef USE_SERVICE_THREAD
                   9805:                }
1.1.1.35  root     9806: #endif
                   9807:        }
1.1       root     9808: }
                   9809: 
                   9810: inline void msdos_int_21h_0bh()
                   9811: {
                   9812:        if(msdos_kbhit()) {
                   9813:                REG8(AL) = 0xff;
                   9814:        } else {
                   9815:                REG8(AL) = 0x00;
1.1.1.14  root     9816:                maybe_idle();
1.1       root     9817:        }
1.1.1.33  root     9818:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9819: }
                   9820: 
                   9821: inline void msdos_int_21h_0ch()
                   9822: {
                   9823:        // clear key buffer
1.1.1.21  root     9824:        msdos_stdio_reopen();
                   9825:        
1.1.1.20  root     9826:        process_t *process = msdos_process_info_get(current_psp);
                   9827:        int fd = msdos_psp_get_file_table(0, current_psp);
                   9828:        
                   9829:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9830:                // stdin is redirected to file
                   9831:        } else {
                   9832:                while(msdos_kbhit()) {
                   9833:                        msdos_getch();
                   9834:                }
                   9835:        }
                   9836:        
                   9837:        switch(REG8(AL)) {
                   9838:        case 0x01:
                   9839:                msdos_int_21h_01h();
                   9840:                break;
                   9841:        case 0x06:
                   9842:                msdos_int_21h_06h();
                   9843:                break;
                   9844:        case 0x07:
                   9845:                msdos_int_21h_07h();
                   9846:                break;
                   9847:        case 0x08:
                   9848:                msdos_int_21h_08h();
                   9849:                break;
                   9850:        case 0x0a:
                   9851:                msdos_int_21h_0ah();
                   9852:                break;
                   9853:        default:
1.1.1.48  root     9854:                // the buffer is flushed but no input is attempted
1.1       root     9855:                break;
                   9856:        }
                   9857: }
                   9858: 
                   9859: inline void msdos_int_21h_0dh()
                   9860: {
                   9861: }
                   9862: 
                   9863: inline void msdos_int_21h_0eh()
                   9864: {
                   9865:        if(REG8(DL) < 26) {
                   9866:                _chdrive(REG8(DL) + 1);
                   9867:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9868:                msdos_sda_update(current_psp);
1.1       root     9869:        }
                   9870:        REG8(AL) = 26; // zdrive
                   9871: }
                   9872: 
1.1.1.14  root     9873: inline void msdos_int_21h_0fh()
                   9874: {
                   9875:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9876:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9877:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     9878:        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     9879:        
1.1.1.14  root     9880:        if(hFile == INVALID_HANDLE_VALUE) {
                   9881:                REG8(AL) = 0xff;
                   9882:        } else {
                   9883:                REG8(AL) = 0;
                   9884:                fcb->current_block = 0;
                   9885:                fcb->record_size = 128;
                   9886:                fcb->file_size = GetFileSize(hFile, NULL);
                   9887:                fcb->handle = hFile;
                   9888:                fcb->cur_record = 0;
                   9889:        }
                   9890: }
                   9891: 
                   9892: inline void msdos_int_21h_10h()
                   9893: {
                   9894:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9895:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9896:        
                   9897:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9898: }
                   9899: 
1.1       root     9900: inline void msdos_int_21h_11h()
                   9901: {
1.1.1.3   root     9902:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9903:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9904:        
                   9905:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9906:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9907:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9908:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45  root     9909:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     9910:        WIN32_FIND_DATAA fd;
1.1       root     9911:        
1.1.1.13  root     9912:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9913:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9914:                FindClose(dtainfo->find_handle);
                   9915:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9916:        }
                   9917:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9918:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9919:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9920:        
1.1.1.14  root     9921:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9922:                dtainfo->allowable_mask &= ~8;
1.1       root     9923:        }
1.1.1.60  root     9924:        if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14  root     9925:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9926:                      !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     9927:                        if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     9928:                                FindClose(dtainfo->find_handle);
                   9929:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9930:                                break;
                   9931:                        }
                   9932:                }
                   9933:        }
1.1.1.13  root     9934:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9935:                if(ext_fcb->flag == 0xff) {
                   9936:                        ext_find->flag = 0xff;
                   9937:                        memset(ext_find->reserved, 0, 5);
                   9938:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9939:                }
                   9940:                find->drive = _getdrive();
1.1.1.13  root     9941:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9942:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9943:                find->nt_res = 0;
                   9944:                msdos_find_file_conv_local_time(&fd);
                   9945:                find->create_time_ms = 0;
                   9946:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9947:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9948:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9949:                find->cluster_hi = find->cluster_lo = 0;
                   9950:                find->file_size = fd.nFileSizeLow;
                   9951:                REG8(AL) = 0x00;
1.1.1.14  root     9952:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9953:                if(ext_fcb->flag == 0xff) {
                   9954:                        ext_find->flag = 0xff;
                   9955:                        memset(ext_find->reserved, 0, 5);
                   9956:                        ext_find->attribute = 8;
                   9957:                }
                   9958:                find->drive = _getdrive();
                   9959:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9960:                find->attribute = 8;
                   9961:                find->nt_res = 0;
                   9962:                msdos_find_file_conv_local_time(&fd);
                   9963:                find->create_time_ms = 0;
                   9964:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9965:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9966:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9967:                find->cluster_hi = find->cluster_lo = 0;
                   9968:                find->file_size = 0;
1.1.1.14  root     9969:                dtainfo->allowable_mask &= ~8;
1.1       root     9970:                REG8(AL) = 0x00;
                   9971:        } else {
                   9972:                REG8(AL) = 0xff;
                   9973:        }
                   9974: }
                   9975: 
                   9976: inline void msdos_int_21h_12h()
                   9977: {
1.1.1.3   root     9978:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     9979: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9980:        
                   9981:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9982:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9983:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9984:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.60  root     9985:        WIN32_FIND_DATAA fd;
1.1       root     9986:        
1.1.1.13  root     9987:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9988:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60  root     9989:                if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14  root     9990:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9991:                              !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     9992:                                if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     9993:                                        FindClose(dtainfo->find_handle);
                   9994:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9995:                                        break;
                   9996:                                }
                   9997:                        }
                   9998:                } else {
1.1.1.13  root     9999:                        FindClose(dtainfo->find_handle);
                   10000:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10001:                }
                   10002:        }
1.1.1.13  root     10003:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     10004:                if(ext_fcb->flag == 0xff) {
                   10005:                        ext_find->flag = 0xff;
                   10006:                        memset(ext_find->reserved, 0, 5);
                   10007:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   10008:                }
                   10009:                find->drive = _getdrive();
1.1.1.13  root     10010:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     10011:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   10012:                find->nt_res = 0;
                   10013:                msdos_find_file_conv_local_time(&fd);
                   10014:                find->create_time_ms = 0;
                   10015:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   10016:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   10017:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   10018:                find->cluster_hi = find->cluster_lo = 0;
                   10019:                find->file_size = fd.nFileSizeLow;
                   10020:                REG8(AL) = 0x00;
1.1.1.14  root     10021:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     10022:                if(ext_fcb->flag == 0xff) {
                   10023:                        ext_find->flag = 0xff;
                   10024:                        memset(ext_find->reserved, 0, 5);
                   10025:                        ext_find->attribute = 8;
                   10026:                }
                   10027:                find->drive = _getdrive();
                   10028:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   10029:                find->attribute = 8;
                   10030:                find->nt_res = 0;
                   10031:                msdos_find_file_conv_local_time(&fd);
                   10032:                find->create_time_ms = 0;
                   10033:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   10034:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   10035:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   10036:                find->cluster_hi = find->cluster_lo = 0;
                   10037:                find->file_size = 0;
1.1.1.14  root     10038:                dtainfo->allowable_mask &= ~8;
1.1       root     10039:                REG8(AL) = 0x00;
                   10040:        } else {
                   10041:                REG8(AL) = 0xff;
                   10042:        }
                   10043: }
                   10044: 
                   10045: inline void msdos_int_21h_13h()
                   10046: {
1.1.1.3   root     10047:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     10048:                REG8(AL) = 0xff;
                   10049:        } else {
                   10050:                REG8(AL) = 0x00;
                   10051:        }
                   10052: }
                   10053: 
1.1.1.16  root     10054: inline void msdos_int_21h_14h()
                   10055: {
                   10056:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10057:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10058:        process_t *process = msdos_process_info_get(current_psp);
                   10059:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10060:        DWORD num = 0;
                   10061:        
                   10062:        memset(mem + dta_laddr, 0, fcb->record_size);
                   10063:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   10064:                REG8(AL) = 1;
                   10065:        } else {
                   10066:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   10067:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   10068:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   10069:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   10070:        }
                   10071: }
                   10072: 
                   10073: inline void msdos_int_21h_15h()
1.1.1.14  root     10074: {
                   10075:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10076:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     10077:        process_t *process = msdos_process_info_get(current_psp);
                   10078:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10079:        DWORD num = 0;
1.1.1.14  root     10080:        
1.1.1.16  root     10081:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   10082:                REG8(AL) = 1;
                   10083:        } else {
                   10084:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10085:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   10086:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   10087:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   10088:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   10089:        }
                   10090: }
                   10091: 
                   10092: inline void msdos_int_21h_16h()
                   10093: {
                   10094:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10095:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     10096:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     10097:        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     10098:        
1.1.1.14  root     10099:        if(hFile == INVALID_HANDLE_VALUE) {
                   10100:                REG8(AL) = 0xff;
                   10101:        } else {
                   10102:                REG8(AL) = 0;
                   10103:                fcb->current_block = 0;
                   10104:                fcb->record_size = 128;
                   10105:                fcb->file_size = 0;
                   10106:                fcb->handle = hFile;
                   10107:                fcb->cur_record = 0;
                   10108:        }
                   10109: }
                   10110: 
1.1.1.16  root     10111: inline void msdos_int_21h_17h()
                   10112: {
                   10113:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10114:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45  root     10115: //     const char *path_src = msdos_fcb_path(fcb_src);
                   10116:        char path_src[MAX_PATH];
                   10117:        strcpy(path_src, msdos_fcb_path(fcb_src));
                   10118:        
1.1.1.16  root     10119:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   10120:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45  root     10121: //     const char *path_dst = msdos_fcb_path(fcb_dst);
                   10122:        char path_dst[MAX_PATH];
                   10123:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     10124:        
                   10125:        if(rename(path_src, path_dst)) {
                   10126:                REG8(AL) = 0xff;
                   10127:        } else {
                   10128:                REG8(AL) = 0;
                   10129:        }
                   10130: }
                   10131: 
1.1       root     10132: inline void msdos_int_21h_18h()
                   10133: {
                   10134:        REG8(AL) = 0x00;
                   10135: }
                   10136: 
                   10137: inline void msdos_int_21h_19h()
                   10138: {
                   10139:        REG8(AL) = _getdrive() - 1;
                   10140: }
                   10141: 
                   10142: inline void msdos_int_21h_1ah()
                   10143: {
                   10144:        process_t *process = msdos_process_info_get(current_psp);
                   10145:        
                   10146:        process->dta.w.l = REG16(DX);
1.1.1.3   root     10147:        process->dta.w.h = SREG(DS);
1.1.1.23  root     10148:        msdos_sda_update(current_psp);
1.1       root     10149: }
                   10150: 
                   10151: inline void msdos_int_21h_1bh()
                   10152: {
                   10153:        int drive_num = _getdrive() - 1;
                   10154:        UINT16 seg, ofs;
                   10155:        
                   10156:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10157:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   10158:                REG8(AL) = dpb->highest_sector_num + 1;
                   10159:                REG16(CX) = dpb->bytes_per_sector;
                   10160:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     10161:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     10162:        } else {
                   10163:                REG8(AL) = 0xff;
1.1.1.3   root     10164:                m_CF = 1;
1.1       root     10165:        }
                   10166: 
                   10167: }
                   10168: 
                   10169: inline void msdos_int_21h_1ch()
                   10170: {
1.1.1.41  root     10171:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     10172:        UINT16 seg, ofs;
                   10173:        
                   10174:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10175:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   10176:                REG8(AL) = dpb->highest_sector_num + 1;
                   10177:                REG16(CX) = dpb->bytes_per_sector;
                   10178:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     10179:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     10180:        } else {
                   10181:                REG8(AL) = 0xff;
1.1.1.3   root     10182:                m_CF = 1;
1.1       root     10183:        }
                   10184: 
                   10185: }
                   10186: 
                   10187: inline void msdos_int_21h_1dh()
                   10188: {
                   10189:        REG8(AL) = 0;
                   10190: }
                   10191: 
                   10192: inline void msdos_int_21h_1eh()
                   10193: {
                   10194:        REG8(AL) = 0;
                   10195: }
                   10196: 
                   10197: inline void msdos_int_21h_1fh()
                   10198: {
                   10199:        int drive_num = _getdrive() - 1;
                   10200:        UINT16 seg, ofs;
                   10201:        
                   10202:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10203:                REG8(AL) = 0;
1.1.1.3   root     10204:                SREG(DS) = seg;
                   10205:                i386_load_segment_descriptor(DS);
1.1       root     10206:                REG16(BX) = ofs;
                   10207:        } else {
                   10208:                REG8(AL) = 0xff;
1.1.1.3   root     10209:                m_CF = 1;
1.1       root     10210:        }
                   10211: }
                   10212: 
                   10213: inline void msdos_int_21h_20h()
                   10214: {
                   10215:        REG8(AL) = 0;
                   10216: }
                   10217: 
1.1.1.14  root     10218: inline void msdos_int_21h_21h()
                   10219: {
                   10220:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10221:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10222:        
                   10223:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10224:                REG8(AL) = 1;
                   10225:        } else {
                   10226:                process_t *process = msdos_process_info_get(current_psp);
                   10227:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10228:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     10229:                DWORD num = 0;
1.1.1.14  root     10230:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   10231:                        REG8(AL) = 1;
                   10232:                } else {
                   10233:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10234:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     10235:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     10236:                }
                   10237:        }
                   10238: }
                   10239: 
                   10240: inline void msdos_int_21h_22h()
                   10241: {
                   10242:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10243:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10244:        
                   10245:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10246:                REG8(AL) = 0xff;
                   10247:        } else {
                   10248:                process_t *process = msdos_process_info_get(current_psp);
                   10249:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     10250:                DWORD num = 0;
1.1.1.14  root     10251:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   10252:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10253:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10254:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     10255:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     10256:        }
                   10257: }
                   10258: 
1.1.1.16  root     10259: inline void msdos_int_21h_23h()
                   10260: {
                   10261:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10262:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     10263:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     10264:        HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16  root     10265:        
                   10266:        if(hFile == INVALID_HANDLE_VALUE) {
                   10267:                REG8(AL) = 0xff;
                   10268:        } else {
                   10269:                UINT32 size = GetFileSize(hFile, NULL);
                   10270:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   10271:                REG8(AL) = 0;
                   10272:        }
                   10273: }
                   10274: 
                   10275: inline void msdos_int_21h_24h()
                   10276: {
                   10277:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10278:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10279:        
                   10280:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   10281: }
                   10282: 
1.1       root     10283: inline void msdos_int_21h_25h()
                   10284: {
                   10285:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     10286:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     10287: }
                   10288: 
                   10289: inline void msdos_int_21h_26h()
                   10290: {
                   10291:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   10292:        
                   10293:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
1.1.1.48  root     10294:        psp->first_mcb = REG16(DX) + (PSP_SIZE >> 4);
1.1       root     10295:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   10296:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   10297:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   10298:        psp->parent_psp = 0;
                   10299: }
                   10300: 
1.1.1.16  root     10301: inline void msdos_int_21h_27h()
                   10302: {
                   10303:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10304:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10305:        
                   10306:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10307:                REG8(AL) = 1;
                   10308:        } else {
                   10309:                process_t *process = msdos_process_info_get(current_psp);
                   10310:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10311:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   10312:                DWORD num = 0;
                   10313:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   10314:                        REG8(AL) = 1;
                   10315:                } else {
                   10316:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10317:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   10318:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   10319:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   10320:                }
                   10321:        }
                   10322: }
                   10323: 
                   10324: inline void msdos_int_21h_28h()
                   10325: {
                   10326:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10327:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10328:        
                   10329:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10330:                REG8(AL) = 0xff;
                   10331:        } else {
                   10332:                process_t *process = msdos_process_info_get(current_psp);
                   10333:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10334:                DWORD num = 0;
                   10335:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   10336:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10337:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10338:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   10339:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   10340:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   10341:        }
                   10342: }
                   10343: 
1.1       root     10344: inline void msdos_int_21h_29h()
                   10345: {
1.1.1.20  root     10346:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   10347:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     10348:        UINT8 drv = 0;
                   10349:        char sep_chars[] = ":.;,=+";
                   10350:        char end_chars[] = "\\<>|/\"[]";
                   10351:        char spc_chars[] = " \t";
                   10352:        
1.1.1.20  root     10353:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   10354:        buffer[1023] = 0;
                   10355:        memset(name, 0x20, sizeof(name));
                   10356:        memset(ext, 0x20, sizeof(ext));
                   10357:        
1.1       root     10358:        if(REG8(AL) & 1) {
1.1.1.20  root     10359:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   10360:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     10361:                        ofs++;
                   10362:                }
                   10363:        }
1.1.1.20  root     10364:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     10365:        
1.1.1.24  root     10366:        if(buffer[ofs + 1] == ':') {
                   10367:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   10368:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     10369:                        ofs += 2;
1.1.1.24  root     10370:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10371:                                ofs++;
                   10372:                        }
                   10373:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   10374:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     10375:                        ofs += 2;
1.1.1.24  root     10376:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10377:                                ofs++;
                   10378:                        }
1.1       root     10379:                }
                   10380:        }
1.1.1.20  root     10381:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10382:                UINT8 c = buffer[ofs];
                   10383:                if(is_kanji) {
                   10384:                        is_kanji = 0;
                   10385:                } else if(msdos_lead_byte_check(c)) {
                   10386:                        is_kanji = 1;
                   10387:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10388:                        break;
                   10389:                } else if(c >= 'a' && c <= 'z') {
                   10390:                        c -= 0x20;
                   10391:                }
                   10392:                ofs++;
                   10393:                name[i] = c;
                   10394:        }
1.1.1.20  root     10395:        if(buffer[ofs] == '.') {
1.1       root     10396:                ofs++;
1.1.1.20  root     10397:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10398:                        UINT8 c = buffer[ofs];
                   10399:                        if(is_kanji) {
                   10400:                                is_kanji = 0;
                   10401:                        } else if(msdos_lead_byte_check(c)) {
                   10402:                                is_kanji = 1;
                   10403:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10404:                                break;
                   10405:                        } else if(c >= 'a' && c <= 'z') {
                   10406:                                c -= 0x20;
                   10407:                        }
                   10408:                        ofs++;
                   10409:                        ext[i] = c;
                   10410:                }
                   10411:        }
1.1.1.20  root     10412:        int si = REG16(SI) + ofs;
1.1.1.3   root     10413:        int ds = SREG(DS);
1.1       root     10414:        while(si > 0xffff) {
                   10415:                si -= 0x10;
                   10416:                ds++;
                   10417:        }
                   10418:        REG16(SI) = si;
1.1.1.3   root     10419:        SREG(DS) = ds;
                   10420:        i386_load_segment_descriptor(DS);
1.1       root     10421:        
1.1.1.3   root     10422:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     10423:        if(!(REG8(AL) & 2) || drv != 0) {
                   10424:                fcb[0] = drv;
                   10425:        }
                   10426:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   10427:                memcpy(fcb + 1, name, 8);
                   10428:        }
                   10429:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   10430:                memcpy(fcb + 9, ext, 3);
                   10431:        }
                   10432:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     10433:                if(fcb[i] == '*') {
                   10434:                        found_star = 1;
                   10435:                }
                   10436:                if(found_star) {
                   10437:                        fcb[i] = '?';
                   10438:                }
                   10439:        }
1.1.1.20  root     10440:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     10441:                if(fcb[i] == '*') {
                   10442:                        found_star = 1;
                   10443:                }
                   10444:                if(found_star) {
                   10445:                        fcb[i] = '?';
                   10446:                }
                   10447:        }
                   10448:        
1.1.1.44  root     10449:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     10450:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   10451:                        REG8(AL) = 0x01;
1.1.1.20  root     10452:                } else {
                   10453:                        REG8(AL) = 0x00;
1.1       root     10454:                }
                   10455:        } else {
                   10456:                REG8(AL) = 0xff;
                   10457:        }
                   10458: }
                   10459: 
                   10460: inline void msdos_int_21h_2ah()
                   10461: {
                   10462:        SYSTEMTIME sTime;
                   10463:        
                   10464:        GetLocalTime(&sTime);
                   10465:        REG16(CX) = sTime.wYear;
                   10466:        REG8(DH) = (UINT8)sTime.wMonth;
                   10467:        REG8(DL) = (UINT8)sTime.wDay;
                   10468:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   10469: }
                   10470: 
                   10471: inline void msdos_int_21h_2bh()
                   10472: {
1.1.1.14  root     10473:        REG8(AL) = 0xff;
1.1       root     10474: }
                   10475: 
                   10476: inline void msdos_int_21h_2ch()
                   10477: {
                   10478:        SYSTEMTIME sTime;
                   10479:        
                   10480:        GetLocalTime(&sTime);
                   10481:        REG8(CH) = (UINT8)sTime.wHour;
                   10482:        REG8(CL) = (UINT8)sTime.wMinute;
                   10483:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     10484:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     10485: }
                   10486: 
                   10487: inline void msdos_int_21h_2dh()
                   10488: {
                   10489:        REG8(AL) = 0x00;
                   10490: }
                   10491: 
                   10492: inline void msdos_int_21h_2eh()
                   10493: {
                   10494:        process_t *process = msdos_process_info_get(current_psp);
                   10495:        
                   10496:        process->verify = REG8(AL);
                   10497: }
                   10498: 
                   10499: inline void msdos_int_21h_2fh()
                   10500: {
                   10501:        process_t *process = msdos_process_info_get(current_psp);
                   10502:        
                   10503:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     10504:        SREG(ES) = process->dta.w.h;
                   10505:        i386_load_segment_descriptor(ES);
1.1       root     10506: }
                   10507: 
                   10508: inline void msdos_int_21h_30h()
                   10509: {
                   10510:        // Version Flag / OEM
1.1.1.27  root     10511:        if(REG8(AL) == 0x01) {
1.1.1.29  root     10512: #ifdef SUPPORT_HMA
                   10513:                REG16(BX) = 0x0000;
                   10514: #else
                   10515:                REG16(BX) = 0x1000; // DOS is in HMA
                   10516: #endif
1.1       root     10517:        } else {
1.1.1.27  root     10518:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   10519:                // but this is not correct on Windows 98 SE
                   10520: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   10521:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     10522:        }
1.1.1.27  root     10523:        REG16(CX) = 0x0000;
1.1.1.30  root     10524:        REG8(AL) = dos_major_version;   // 7
                   10525:        REG8(AH) = dos_minor_version;   // 10
1.1       root     10526: }
                   10527: 
                   10528: inline void msdos_int_21h_31h()
                   10529: {
1.1.1.29  root     10530:        try {
                   10531:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10532:        } catch(...) {
                   10533:                // recover the broken mcb
                   10534:                int mcb_seg = current_psp - 1;
                   10535:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     10536:                
1.1.1.29  root     10537:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     10538:                        mcb->mz = 'M';
                   10539:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   10540:                        
1.1.1.29  root     10541:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     10542:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     10543:                        } else {
1.1.1.39  root     10544:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     10545:                        }
                   10546:                } else {
                   10547:                        mcb->mz = 'Z';
1.1.1.30  root     10548:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     10549:                }
                   10550:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10551:        }
1.1       root     10552:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   10553: }
                   10554: 
                   10555: inline void msdos_int_21h_32h()
                   10556: {
                   10557:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   10558:        UINT16 seg, ofs;
                   10559:        
                   10560:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10561:                REG8(AL) = 0;
1.1.1.3   root     10562:                SREG(DS) = seg;
                   10563:                i386_load_segment_descriptor(DS);
1.1       root     10564:                REG16(BX) = ofs;
                   10565:        } else {
                   10566:                REG8(AL) = 0xff;
1.1.1.3   root     10567:                m_CF = 1;
1.1       root     10568:        }
                   10569: }
                   10570: 
                   10571: inline void msdos_int_21h_33h()
                   10572: {
                   10573:        char path[MAX_PATH];
1.1.1.48  root     10574:        char drive = 3; // C:
1.1       root     10575:        
                   10576:        switch(REG8(AL)) {
                   10577:        case 0x00:
1.1.1.33  root     10578:                REG8(DL) = ctrl_break_checking;
1.1       root     10579:                break;
                   10580:        case 0x01:
1.1.1.33  root     10581:                ctrl_break_checking = REG8(DL);
                   10582:                break;
                   10583:        case 0x02:
                   10584:                {
                   10585:                        UINT8 old = ctrl_break_checking;
                   10586:                        ctrl_break_checking = REG8(DL);
                   10587:                        REG8(DL) = old;
                   10588:                }
                   10589:                break;
                   10590:        case 0x03:
                   10591:        case 0x04:
                   10592:                // DOS 4.0+ - Unused
1.1       root     10593:                break;
                   10594:        case 0x05:
1.1.1.60  root     10595:                if(GetSystemDirectoryA(path, MAX_PATH) != 0) {
1.1.1.48  root     10596:                        if(path[0] >= 'a' && path[0] <= 'z') {
                   10597:                                drive = path[0] - 'a' + 1;
                   10598:                        } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   10599:                                drive = path[0] - 'A' + 1;
                   10600:                        }
1.1       root     10601:                }
1.1.1.48  root     10602:                REG8(DL) = (UINT8)drive;
1.1       root     10603:                break;
                   10604:        case 0x06:
1.1.1.2   root     10605:                // MS-DOS version (7.10)
1.1       root     10606:                REG8(BL) = 7;
1.1.1.2   root     10607:                REG8(BH) = 10;
1.1       root     10608:                REG8(DL) = 0;
1.1.1.29  root     10609: #ifdef SUPPORT_HMA
                   10610:                REG8(DH) = 0x00;
                   10611: #else
                   10612:                REG8(DH) = 0x10; // DOS is in HMA
                   10613: #endif
1.1       root     10614:                break;
1.1.1.6   root     10615:        case 0x07:
                   10616:                if(REG8(DL) == 0) {
                   10617:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   10618:                } else if(REG8(DL) == 1) {
                   10619:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   10620:                }
                   10621:                break;
1.1       root     10622:        default:
1.1.1.22  root     10623:                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     10624: //             REG16(AX) = 0x01;
                   10625: //             m_CF = 1;
                   10626:                REG8(AL) = 0xff;
1.1       root     10627:                break;
                   10628:        }
                   10629: }
                   10630: 
1.1.1.23  root     10631: inline void msdos_int_21h_34h()
                   10632: {
                   10633:        SREG(ES) = SDA_TOP >> 4;
                   10634:        i386_load_segment_descriptor(ES);
                   10635:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   10636: }
                   10637: 
1.1       root     10638: inline void msdos_int_21h_35h()
                   10639: {
                   10640:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     10641:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   10642:        i386_load_segment_descriptor(ES);
1.1       root     10643: }
                   10644: 
                   10645: inline void msdos_int_21h_36h()
                   10646: {
                   10647:        struct _diskfree_t df = {0};
                   10648:        
                   10649:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   10650:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   10651:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     10652:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   10653:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     10654:        } else {
                   10655:                REG16(AX) = 0xffff;
                   10656:        }
                   10657: }
                   10658: 
                   10659: inline void msdos_int_21h_37h()
                   10660: {
1.1.1.22  root     10661:        static UINT8 dev_flag = 0xff;
1.1       root     10662:        
                   10663:        switch(REG8(AL)) {
                   10664:        case 0x00:
1.1.1.22  root     10665:                {
                   10666:                        process_t *process = msdos_process_info_get(current_psp);
                   10667:                        REG8(AL) = 0x00;
                   10668:                        REG8(DL) = process->switchar;
                   10669:                }
1.1       root     10670:                break;
                   10671:        case 0x01:
1.1.1.22  root     10672:                {
                   10673:                        process_t *process = msdos_process_info_get(current_psp);
                   10674:                        REG8(AL) = 0x00;
                   10675:                        process->switchar = REG8(DL);
1.1.1.23  root     10676:                        msdos_sda_update(current_psp);
1.1.1.22  root     10677:                }
                   10678:                break;
                   10679:        case 0x02:
                   10680:                REG8(DL) = dev_flag;
                   10681:                break;
                   10682:        case 0x03:
                   10683:                dev_flag = REG8(DL);
                   10684:                break;
                   10685:        case 0xd0:
                   10686:        case 0xd1:
                   10687:        case 0xd2:
                   10688:        case 0xd3:
                   10689:        case 0xd4:
                   10690:        case 0xd5:
                   10691:        case 0xd6:
                   10692:        case 0xd7:
                   10693:        case 0xdc:
                   10694:        case 0xdd:
                   10695:        case 0xde:
                   10696:        case 0xdf:
1.1.1.48  root     10697:                // DIET v1.43e
                   10698: //             REG16(AX) = 1;
                   10699:                REG8(AL) = 0xff;
1.1       root     10700:                break;
                   10701:        default:
1.1.1.22  root     10702:                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     10703: //             REG16(AX) = 1;
                   10704:                REG8(AL) = 0xff;
1.1       root     10705:                break;
                   10706:        }
                   10707: }
                   10708: 
1.1.1.52  root     10709: int get_country_info(country_info_t *ci, LCID locale = LOCALE_USER_DEFAULT)
1.1.1.17  root     10710: {
                   10711:        char LCdata[80];
                   10712:        
1.1.1.19  root     10713:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.60  root     10714:        GetLocaleInfoA(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     10715:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.60  root     10716:        GetLocaleInfoA(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10717:        ci->currency_format = *LCdata - '0';
1.1.1.60  root     10718:        GetLocaleInfoA(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10719:        ci->date_format = *LCdata - '0';
1.1.1.60  root     10720:        GetLocaleInfoA(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10721:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.60  root     10722:        GetLocaleInfoA(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10723:        *ci->date_sep = *LCdata;
1.1.1.60  root     10724:        GetLocaleInfoA(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     10725:        *ci->dec_sep = *LCdata;
1.1.1.60  root     10726:        GetLocaleInfoA(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     10727:        *ci->list_sep = *LCdata;
1.1.1.60  root     10728:        GetLocaleInfoA(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     10729:        *ci->thou_sep = *LCdata;
1.1.1.60  root     10730:        GetLocaleInfoA(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     10731:        *ci->time_sep = *LCdata;
1.1.1.60  root     10732:        GetLocaleInfoA(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     10733:        if(strchr(LCdata, 'H') != NULL) {
                   10734:                ci->time_format = 1;
                   10735:        }
1.1.1.49  root     10736:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffc:000a
                   10737:        ci->case_map.w.h = DUMMY_TOP >> 4;
1.1.1.60  root     10738:        GetLocaleInfoA(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     10739:        return atoi(LCdata);
                   10740: }
                   10741: 
1.1.1.42  root     10742: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   10743: {
                   10744:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   10745: }
                   10746: 
1.1.1.43  root     10747: void set_country_info(country_info_t *ci, int size)
                   10748: {
                   10749:        char LCdata[80];
                   10750:        
                   10751:        if(size >= 0x00 + 2) {
                   10752:                memset(LCdata, 0, sizeof(LCdata));
                   10753:                *LCdata = '0' + ci->date_format;
1.1.1.60  root     10754:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
1.1.1.43  root     10755:        }
                   10756:        if(size >= 0x02 + 5) {
                   10757:                memset(LCdata, 0, sizeof(LCdata));
                   10758:                memcpy(LCdata, &ci->currency_symbol, 4);
1.1.1.60  root     10759:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
1.1.1.43  root     10760:        }
                   10761:        if(size >= 0x07 + 2) {
                   10762:                memset(LCdata, 0, sizeof(LCdata));
                   10763:                *LCdata = *ci->thou_sep;
1.1.1.60  root     10764:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
1.1.1.43  root     10765:        }
                   10766:        if(size >= 0x09 + 2) {
                   10767:                memset(LCdata, 0, sizeof(LCdata));
                   10768:                *LCdata = *ci->dec_sep;
1.1.1.60  root     10769:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
1.1.1.43  root     10770:        }
                   10771:        if(size >= 0x0b + 2) {
                   10772:                memset(LCdata, 0, sizeof(LCdata));
                   10773:                *LCdata = *ci->date_sep;
1.1.1.60  root     10774:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
1.1.1.43  root     10775:        }
                   10776:        if(size >= 0x0d + 2) {
                   10777:                memset(LCdata, 0, sizeof(LCdata));
                   10778:                *LCdata = *ci->time_sep;
1.1.1.60  root     10779:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
1.1.1.43  root     10780:        }
                   10781:        if(size >= 0x0f + 1) {
                   10782:                memset(LCdata, 0, sizeof(LCdata));
                   10783:                *LCdata = '0' + ci->currency_format;
1.1.1.60  root     10784:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
1.1.1.43  root     10785:        }
                   10786:        if(size >= 0x10 + 1) {
                   10787:                sprintf(LCdata, "%d", ci->currency_dec_digits);
1.1.1.60  root     10788:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
1.1.1.43  root     10789:        }
                   10790:        if(size >= 0x11 + 1) {
                   10791:                // FIXME: is time format always H/h:mm:ss ???
                   10792:                if(ci->time_format & 1) {
                   10793:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   10794:                } else {
                   10795:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   10796:                }
1.1.1.60  root     10797:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
1.1.1.43  root     10798:        }
                   10799:        if(size >= 0x12 + 4) {
                   10800:                // 12h  DWORD   address of case map routine
                   10801:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   10802:        }
                   10803:        if(size >= 0x16 + 2) {
                   10804:                memset(LCdata, 0, sizeof(LCdata));
                   10805:                *LCdata = *ci->list_sep;
1.1.1.60  root     10806:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
1.1.1.43  root     10807:        }
                   10808: }
                   10809: 
1.1.1.42  root     10810: #ifndef SUBLANG_SWAHILI
                   10811:        #define SUBLANG_SWAHILI 0x01
                   10812: #endif
                   10813: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10814:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10815: #endif
                   10816: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10817:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10818: #endif
                   10819: #ifndef LANG_BANGLA
                   10820:        #define LANG_BANGLA 0x45
                   10821: #endif
                   10822: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10823:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10824: #endif
                   10825: 
                   10826: static const struct {
                   10827:        int code;
                   10828:        USHORT usPrimaryLanguage;
                   10829:        USHORT usSubLanguage;
                   10830: } country_table[] = {
                   10831:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10832:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10833:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10834:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10835:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10836:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10837: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10838: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10839: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10840: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10841: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10842:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10843:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10844: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10845:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10846: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10847:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10848:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10849:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10850:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10851:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10852:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10853:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10854: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10855: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10856:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10857:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10858:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10859:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10860:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10861: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10862: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10863: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10864:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10865: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10866: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10867: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10868: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10869:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10870:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10871:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10872: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10873:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10874:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10875:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10876:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10877:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10878:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10879:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10880:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10881:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10882:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10883:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10884:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10885:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10886: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10887:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10888:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10889:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10890:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10891:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10892:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10893:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10894:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10895:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10896:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10897: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10898:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10899: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10900:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10901:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10902:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10903:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10904:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10905:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10906:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10907:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10908: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10909:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10910: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10911: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10912:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   10913: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   10914:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   10915:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   10916:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   10917:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   10918:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   10919:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   10920:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   10921: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   10922: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   10923:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   10924: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   10925:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   10926:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   10927:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   10928:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   10929: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   10930: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   10931: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   10932:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   10933:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   10934:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   10935:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   10936:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   10937:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   10938:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   10939:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   10940:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   10941:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   10942:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   10943:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   10944:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   10945:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   10946:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   10947:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   10948:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   10949:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   10950:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   10951:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   10952:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     10953: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     10954:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   10955: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   10956:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   10957:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   10958:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   10959:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   10960:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   10961:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   10962:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   10963:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   10964:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   10965:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   10966:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   10967:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   10968:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   10969:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   10970:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   10971:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   10972:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   10973:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   10974:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   10975:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   10976:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   10977:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   10978:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   10979:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   10980:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   10981: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   10982:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   10983:        {-1, 0, 0},
                   10984: };
                   10985: 
1.1.1.14  root     10986: inline void msdos_int_21h_38h()
                   10987: {
                   10988:        switch(REG8(AL)) {
                   10989:        case 0x00:
1.1.1.19  root     10990:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     10991:                break;
                   10992:        default:
1.1.1.42  root     10993:                for(int i = 0;; i++) {
                   10994:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   10995:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   10996:                                break;
                   10997:                        } else if(country_table[i].code == -1) {
                   10998: //                             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));
                   10999: //                             REG16(AX) = 2;
                   11000: //                             m_CF = 1;
1.1.1.48  root     11001:                                // get current coutry info
1.1.1.42  root     11002:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   11003:                                break;
                   11004:                        }
                   11005:                }
1.1.1.14  root     11006:                break;
                   11007:        }
                   11008: }
                   11009: 
1.1       root     11010: inline void msdos_int_21h_39h(int lfn)
                   11011: {
1.1.1.3   root     11012:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     11013:                REG16(AX) = errno;
1.1.1.3   root     11014:                m_CF = 1;
1.1       root     11015:        }
                   11016: }
                   11017: 
                   11018: inline void msdos_int_21h_3ah(int lfn)
                   11019: {
1.1.1.3   root     11020:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     11021:                REG16(AX) = errno;
1.1.1.3   root     11022:                m_CF = 1;
1.1       root     11023:        }
                   11024: }
                   11025: 
                   11026: inline void msdos_int_21h_3bh(int lfn)
                   11027: {
1.1.1.45  root     11028:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     11029:        
                   11030:        if(_chdir(path)) {
1.1.1.17  root     11031:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     11032:                m_CF = 1;
1.1.1.44  root     11033:        } else {
                   11034:                int drv = _getdrive() - 1;
                   11035:                if(path[1] == ':') {
                   11036:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   11037:                                drv = path[0] - 'A';
                   11038:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   11039:                                drv = path[0] - 'a';
                   11040:                        }
                   11041:                }
                   11042:                msdos_cds_update(drv, path);
1.1       root     11043:        }
                   11044: }
                   11045: 
                   11046: inline void msdos_int_21h_3ch()
                   11047: {
1.1.1.45  root     11048:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1.1.60  root     11049:        int attr = GetFileAttributesA(path);
1.1.1.37  root     11050:        int fd = -1;
                   11051:        int sio_port = 0;
                   11052:        int lpt_port = 0;
1.1       root     11053:        
1.1.1.45  root     11054:        if(msdos_is_device_path(path)) {
                   11055:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     11056:        } else {
                   11057:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11058:        }
                   11059:        if(fd != -1) {
                   11060:                if(attr == -1) {
                   11061:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   11062:                }
1.1.1.60  root     11063:                SetFileAttributesA(path, attr);
1.1       root     11064:                REG16(AX) = fd;
1.1.1.45  root     11065:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     11066:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11067:        } else {
                   11068:                REG16(AX) = errno;
1.1.1.3   root     11069:                m_CF = 1;
1.1       root     11070:        }
                   11071: }
                   11072: 
                   11073: inline void msdos_int_21h_3dh()
                   11074: {
1.1.1.45  root     11075:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11076:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     11077:        int fd = -1;
                   11078:        int sio_port = 0;
                   11079:        int lpt_port = 0;
1.1       root     11080:        
                   11081:        if(mode < 0x03) {
1.1.1.45  root     11082:                if(msdos_is_device_path(path)) {
                   11083:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     11084:                } else {
1.1.1.13  root     11085:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     11086:                }
1.1       root     11087:                if(fd != -1) {
                   11088:                        REG16(AX) = fd;
1.1.1.45  root     11089:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     11090:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11091:                } else {
                   11092:                        REG16(AX) = errno;
1.1.1.3   root     11093:                        m_CF = 1;
1.1       root     11094:                }
                   11095:        } else {
                   11096:                REG16(AX) = 0x0c;
1.1.1.3   root     11097:                m_CF = 1;
1.1       root     11098:        }
                   11099: }
                   11100: 
                   11101: inline void msdos_int_21h_3eh()
                   11102: {
                   11103:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11104:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11105:        
1.1.1.20  root     11106:        if(fd < process->max_files && file_handler[fd].valid) {
                   11107:                _close(fd);
                   11108:                msdos_file_handler_close(fd);
                   11109:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     11110:        } else {
                   11111:                REG16(AX) = 0x06;
1.1.1.3   root     11112:                m_CF = 1;
1.1       root     11113:        }
                   11114: }
                   11115: 
1.1.1.35  root     11116: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   11117: {
                   11118:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   11119:        int max = REG16(CX);
                   11120:        int p = 0;
                   11121:        
                   11122:        while(max > p) {
                   11123:                int chr = msdos_getch();
                   11124:                
                   11125:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   11126:                        p = 0;
                   11127:                        buf[p++] = 0x0d;
                   11128:                        if(max > p) {
                   11129:                                buf[p++] = 0x0a;
                   11130:                        }
                   11131:                        msdos_putch(0x03);
                   11132:                        msdos_putch(0x0d);
                   11133:                        msdos_putch(0x0a);
                   11134:                        break;
                   11135:                } else if(ctrl_break_pressed) {
                   11136:                        // skip this byte
                   11137:                } else if(chr == 0x00) {
                   11138:                        // skip 2nd byte
                   11139:                        msdos_getch();
                   11140:                } else if(chr == 0x0d) {
                   11141:                        // carriage return
                   11142:                        buf[p++] = 0x0d;
                   11143:                        if(max > p) {
                   11144:                                buf[p++] = 0x0a;
                   11145:                        }
                   11146:                        msdos_putch('\n');
                   11147:                        break;
                   11148:                } else if(chr == 0x08) {
                   11149:                        // back space
                   11150:                        if(p > 0) {
                   11151:                                p--;
                   11152:                                if(msdos_ctrl_code_check(buf[p])) {
                   11153:                                        msdos_putch(0x08);
                   11154:                                        msdos_putch(0x08);
                   11155:                                        msdos_putch(0x20);
                   11156:                                        msdos_putch(0x20);
                   11157:                                        msdos_putch(0x08);
                   11158:                                        msdos_putch(0x08);
1.1.1.36  root     11159:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   11160:                                        p--;
                   11161:                                        msdos_putch(0x08);
                   11162:                                        msdos_putch(0x08);
                   11163:                                        msdos_putch(0x20);
                   11164:                                        msdos_putch(0x20);
                   11165:                                        msdos_putch(0x08);
                   11166:                                        msdos_putch(0x08);
1.1.1.35  root     11167:                                } else {
                   11168:                                        msdos_putch(0x08);
                   11169:                                        msdos_putch(0x20);
                   11170:                                        msdos_putch(0x08);
                   11171:                                }
                   11172:                        }
                   11173:                } else if(chr == 0x1b) {
                   11174:                        // escape
                   11175:                        while(p > 0) {
                   11176:                                p--;
                   11177:                                if(msdos_ctrl_code_check(buf[p])) {
                   11178:                                        msdos_putch(0x08);
                   11179:                                        msdos_putch(0x08);
                   11180:                                        msdos_putch(0x20);
                   11181:                                        msdos_putch(0x20);
                   11182:                                        msdos_putch(0x08);
                   11183:                                        msdos_putch(0x08);
                   11184:                                } else {
                   11185:                                        msdos_putch(0x08);
                   11186:                                        msdos_putch(0x20);
                   11187:                                        msdos_putch(0x08);
                   11188:                                }
                   11189:                        }
                   11190:                } else {
                   11191:                        buf[p++] = chr;
                   11192:                        msdos_putch(chr);
                   11193:                }
                   11194:        }
                   11195:        REG16(AX) = p;
                   11196:        
                   11197: #ifdef USE_SERVICE_THREAD
                   11198:        service_exit = true;
                   11199: #endif
                   11200:        return(0);
                   11201: }
                   11202: 
1.1       root     11203: inline void msdos_int_21h_3fh()
                   11204: {
                   11205:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11206:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11207:        
1.1.1.20  root     11208:        if(fd < process->max_files && file_handler[fd].valid) {
                   11209:                if(file_mode[file_handler[fd].mode].in) {
                   11210:                        if(file_handler[fd].atty) {
1.1       root     11211:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     11212:                                if(REG16(CX) != 0) {
                   11213: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     11214:                                        if(!in_service && !in_service_29h &&
1.1.1.58  root     11215:                                           *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     11216:                                           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   11217:                                                // msdos_putch() will be used in this service
                   11218:                                                // if int 29h is hooked, run this service in main thread to call int 29h
                   11219:                                                start_service_loop(msdos_int_21h_3fh_thread);
                   11220:                                        } else {
                   11221: #endif
                   11222:                                                msdos_int_21h_3fh_thread(NULL);
                   11223:                                                REQUEST_HARDWRE_UPDATE();
                   11224: #ifdef USE_SERVICE_THREAD
                   11225:                                        }
1.1.1.35  root     11226: #endif
                   11227:                                } else {
                   11228:                                        REG16(AX) = 0;
1.1       root     11229:                                }
                   11230:                        } else {
1.1.1.37  root     11231:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     11232:                        }
                   11233:                } else {
                   11234:                        REG16(AX) = 0x05;
1.1.1.3   root     11235:                        m_CF = 1;
1.1       root     11236:                }
                   11237:        } else {
                   11238:                REG16(AX) = 0x06;
1.1.1.3   root     11239:                m_CF = 1;
1.1       root     11240:        }
                   11241: }
                   11242: 
                   11243: inline void msdos_int_21h_40h()
                   11244: {
                   11245:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11246:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11247:        
1.1.1.20  root     11248:        if(fd < process->max_files && file_handler[fd].valid) {
                   11249:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     11250:                        if(REG16(CX)) {
1.1.1.20  root     11251:                                if(file_handler[fd].atty) {
1.1       root     11252:                                        // BX is stdout/stderr or is redirected to stdout
                   11253:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     11254:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     11255:                                        }
                   11256:                                        REG16(AX) = REG16(CX);
                   11257:                                } else {
1.1.1.20  root     11258:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     11259:                                }
                   11260:                        } else {
1.1.1.20  root     11261:                                UINT32 pos = _tell(fd);
                   11262:                                _lseek(fd, 0, SEEK_END);
                   11263:                                UINT32 size = _tell(fd);
1.1.1.12  root     11264:                                if(pos < size) {
1.1.1.20  root     11265:                                        _lseek(fd, pos, SEEK_SET);
                   11266:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     11267:                                } else {
                   11268:                                        for(UINT32 i = size; i < pos; i++) {
                   11269:                                                UINT8 tmp = 0;
1.1.1.23  root     11270:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     11271:                                        }
1.1.1.20  root     11272:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     11273:                                }
1.1.1.23  root     11274:                                REG16(AX) = 0;
1.1       root     11275:                        }
                   11276:                } else {
                   11277:                        REG16(AX) = 0x05;
1.1.1.3   root     11278:                        m_CF = 1;
1.1       root     11279:                }
                   11280:        } else {
                   11281:                REG16(AX) = 0x06;
1.1.1.3   root     11282:                m_CF = 1;
1.1       root     11283:        }
                   11284: }
                   11285: 
                   11286: inline void msdos_int_21h_41h(int lfn)
                   11287: {
1.1.1.3   root     11288:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     11289:                REG16(AX) = errno;
1.1.1.3   root     11290:                m_CF = 1;
1.1       root     11291:        }
                   11292: }
                   11293: 
                   11294: inline void msdos_int_21h_42h()
                   11295: {
                   11296:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11297:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11298:        
1.1.1.20  root     11299:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     11300:                if(REG8(AL) < 0x03) {
1.1.1.35  root     11301:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     11302:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   11303:                        UINT32 pos = _tell(fd);
1.1       root     11304:                        REG16(AX) = pos & 0xffff;
                   11305:                        REG16(DX) = (pos >> 16);
                   11306:                } else {
                   11307:                        REG16(AX) = 0x01;
1.1.1.3   root     11308:                        m_CF = 1;
1.1       root     11309:                }
                   11310:        } else {
                   11311:                REG16(AX) = 0x06;
1.1.1.3   root     11312:                m_CF = 1;
1.1       root     11313:        }
                   11314: }
                   11315: 
                   11316: inline void msdos_int_21h_43h(int lfn)
                   11317: {
1.1.1.45  root     11318:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     11319:        int attr;
                   11320:        
1.1.1.14  root     11321:        if(!lfn && REG8(AL) > 2) {
                   11322:                REG16(AX) = 0x01;
                   11323:                m_CF = 1;
                   11324:                return;
                   11325:        }
                   11326:        switch(REG8(lfn ? BL : AL)) {
1.1       root     11327:        case 0x00:
1.1.1.60  root     11328:                if((attr = GetFileAttributesA(path)) != -1) {
1.1.1.14  root     11329:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   11330:                } else {
                   11331:                        REG16(AX) = (UINT16)GetLastError();
                   11332:                        m_CF = 1;
                   11333:                }
                   11334:                break;
                   11335:        case 0x01:
1.1.1.60  root     11336:                if(!SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)))) {
1.1.1.14  root     11337:                        REG16(AX) = (UINT16)GetLastError();
                   11338:                        m_CF = 1;
                   11339:                }
                   11340:                break;
                   11341:        case 0x02:
                   11342:                {
1.1.1.60  root     11343:                        DWORD compressed_size = GetCompressedFileSizeA(path, NULL), file_size = 0;
1.1.1.45  root     11344:                        if(compressed_size != INVALID_FILE_SIZE) {
                   11345:                                if(compressed_size != 0) {
1.1.1.60  root     11346:                                        HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.45  root     11347:                                        if(hFile != INVALID_HANDLE_VALUE) {
                   11348:                                                file_size = GetFileSize(hFile, NULL);
                   11349:                                                CloseHandle(hFile);
                   11350:                                        }
                   11351:                                        if(compressed_size == file_size) {
                   11352:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   11353:                                                // this isn't correct if the file is in the NTFS MFT
1.1.1.60  root     11354:                                                if(GetDiskFreeSpaceA(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
1.1.1.45  root     11355:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   11356:                                                }
1.1.1.14  root     11357:                                        }
                   11358:                                }
1.1.1.45  root     11359:                                REG16(AX) = LOWORD(compressed_size);
                   11360:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     11361:                        } else {
                   11362:                                REG16(AX) = (UINT16)GetLastError();
                   11363:                                m_CF = 1;
1.1       root     11364:                        }
1.1.1.14  root     11365:                }
                   11366:                break;
                   11367:        case 0x03:
                   11368:        case 0x05:
                   11369:        case 0x07:
1.1.1.48  root     11370:                if(lfn) {
1.1.1.60  root     11371:                        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     11372:                        if(hFile != INVALID_HANDLE_VALUE) {
                   11373:                                FILETIME local, time;
                   11374:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   11375:                                if(REG8(BL) == 7) {
                   11376:                                        ULARGE_INTEGER hund;
                   11377:                                        hund.LowPart = local.dwLowDateTime;
                   11378:                                        hund.HighPart = local.dwHighDateTime;
                   11379:                                        hund.QuadPart += REG16(SI) * 100000;
                   11380:                                        local.dwLowDateTime = hund.LowPart;
                   11381:                                        local.dwHighDateTime = hund.HighPart;
                   11382:                                }
                   11383:                                LocalFileTimeToFileTime(&local, &time);
                   11384:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   11385:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   11386:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   11387:                                        REG16(AX) = (UINT16)GetLastError();
                   11388:                                        m_CF = 1;
                   11389:                                }
                   11390:                                CloseHandle(hFile);
                   11391:                        } else {
                   11392:                                REG16(AX) = (UINT16)GetLastError();
                   11393:                                m_CF = 1;
1.1       root     11394:                        }
1.1.1.48  root     11395:                } else {
                   11396:                        // 214303 DR DOS 3.41+ internal - Set Access Rights And Password
                   11397:                        // 214305 DR DOS 5.0-6.0 internal - Set Extended File Attributes
                   11398:                        // 214307 DR DOS 6.0 - Set 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;
                   11404:        case 0x04:
                   11405:        case 0x06:
                   11406:        case 0x08:
1.1.1.48  root     11407:                if(lfn) {
1.1.1.14  root     11408:                        WIN32_FILE_ATTRIBUTE_DATA fad;
1.1.1.60  root     11409:                        if(GetFileAttributesExA(path, GetFileExInfoStandard, (LPVOID)&fad)) {
1.1.1.14  root     11410:                                FILETIME *time, local;
                   11411:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   11412:                                                   0x06 ? &fad.ftLastAccessTime :
                   11413:                                                          &fad.ftCreationTime;
                   11414:                                FileTimeToLocalFileTime(time, &local);
                   11415:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   11416:                                if(REG8(BL) == 0x08) {
                   11417:                                        ULARGE_INTEGER hund;
                   11418:                                        hund.LowPart = local.dwLowDateTime;
                   11419:                                        hund.HighPart = local.dwHighDateTime;
                   11420:                                        hund.QuadPart /= 100000;
                   11421:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   11422:                                }
                   11423:                        } else {
                   11424:                                REG16(AX) = (UINT16)GetLastError();
                   11425:                                m_CF = 1;
1.1       root     11426:                        }
1.1.1.48  root     11427:                } else {
                   11428:                        // 214304 DR DOS 5.0-6.0 internal - Get Encrypted Password
                   11429:                        // 214306 DR DOS 6.0 - Get File Owner
                   11430: //                     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));
                   11431:                        REG16(AX) = 0x01;
                   11432:                        m_CF = 1;
1.1.1.14  root     11433:                }
                   11434:                break;
1.1.1.43  root     11435:        case 0xff:
1.1.1.48  root     11436:                if(!lfn && REG16(BP) == 0x5053) {
1.1.1.43  root     11437:                        if(REG8(CL) == 0x39) {
                   11438:                                msdos_int_21h_39h(1);
                   11439:                                break;
                   11440:                        } else if(REG8(CL) == 0x56) {
                   11441:                                msdos_int_21h_56h(1);
                   11442:                                break;
                   11443:                        }
                   11444:                }
1.1.1.14  root     11445:        default:
1.1.1.22  root     11446:                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     11447:                REG16(AX) = lfn ? 0x7100 : 0x01;
1.1.1.14  root     11448:                m_CF = 1;
                   11449:                break;
                   11450:        }
                   11451: }
                   11452: 
                   11453: inline void msdos_int_21h_44h()
                   11454: {
1.1.1.22  root     11455:        static UINT16 iteration_count = 0;
                   11456:        
1.1.1.44  root     11457:        process_t *process;
                   11458:        int fd, drv;
1.1.1.14  root     11459:        
                   11460:        switch(REG8(AL)) {
                   11461:        case 0x00:
                   11462:        case 0x01:
                   11463:        case 0x02:
                   11464:        case 0x03:
                   11465:        case 0x04:
                   11466:        case 0x05:
                   11467:        case 0x06:
                   11468:        case 0x07:
1.1.1.44  root     11469:                process = msdos_process_info_get(current_psp);
                   11470:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     11471:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   11472:                        REG16(AX) = 0x06;
                   11473:                        m_CF = 1;
                   11474:                        return;
1.1.1.14  root     11475:                }
                   11476:                break;
                   11477:        case 0x08:
                   11478:        case 0x09:
1.1.1.44  root     11479:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11480:                if(!msdos_is_valid_drive(drv)) {
                   11481:                        // invalid drive
1.1.1.14  root     11482:                        REG16(AX) = 0x0f;
                   11483:                        m_CF = 1;
                   11484:                        return;
1.1       root     11485:                }
                   11486:                break;
                   11487:        }
                   11488:        switch(REG8(AL)) {
1.1.1.48  root     11489:        case 0x00: // Get Device Information
1.1.1.20  root     11490:                REG16(DX) = file_handler[fd].info;
1.1       root     11491:                break;
1.1.1.48  root     11492:        case 0x01: // Set Device Information
1.1.1.45  root     11493:                if(REG8(DH) != 0) {
                   11494: //                     REG16(AX) = 0x0d; // data invalid
                   11495: //                     m_CF = 1;
                   11496:                        file_handler[fd].info = REG16(DX);
                   11497:                } else {
                   11498:                        file_handler[fd].info &= 0xff00;
                   11499:                        file_handler[fd].info |= REG8(DL);
                   11500:                }
1.1       root     11501:                break;
1.1.1.48  root     11502:        case 0x02: // Read From Character Device Control Channel
1.1.1.45  root     11503:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
                   11504:                        // from DOSBox
                   11505:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
                   11506:                        case 0x00:
                   11507:                                if(REG16(CX) >= 6) {
                   11508:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
                   11509:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
                   11510:                                        REG16(AX) = 6; // number of bytes actually read
                   11511:                                } else {
                   11512:                                        REG16(AX) = 0x0d; // data invalid
                   11513:                                        m_CF = 1;
                   11514:                                }
                   11515:                                break;
                   11516:                        case 0x01:
                   11517:                                if(REG16(CX) >= 6) {
                   11518:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
                   11519:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
                   11520:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
                   11521:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
                   11522:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
                   11523:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
                   11524:                                                        int page = (addr - EMS_TOP) / 0x4000;
                   11525:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
                   11526:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11527:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
                   11528:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
                   11529:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
                   11530:                                                } else {
                   11531:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
                   11532:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11533:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
                   11534:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
                   11535:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
                   11536:                                                }
                   11537:                                        }
                   11538:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
                   11539:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
                   11540:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
                   11541:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
                   11542:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
                   11543:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
                   11544:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
                   11545:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
                   11546:                                        
                   11547:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
                   11548:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
                   11549:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
                   11550:                                        REG16(AX) = 6; // number of bytes actually read
                   11551:                                } else {
                   11552:                                        REG16(AX) = 0x0d; // data invalid
                   11553:                                        m_CF = 1;
                   11554:                                }
                   11555:                                break;
                   11556:                        case 0x02:
                   11557:                                if(REG16(CX) >= 2) {
                   11558:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
                   11559:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
                   11560:                                        REG16(AX) = 2; // number of bytes actually read
                   11561:                                } else {
                   11562:                                        REG16(AX) = 0x0d; // data invalid
                   11563:                                        m_CF = 1;
                   11564:                                }
                   11565:                                break;
                   11566:                        case 0x03:
                   11567:                                if(REG16(CX) >= 4) {
                   11568:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
                   11569:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
                   11570:                                        REG16(AX) = 4; // number of bytes actually read
                   11571:                                } else {
                   11572:                                        REG16(AX) = 0x0d; // data invalid
                   11573:                                        m_CF = 1;
                   11574:                                }
                   11575:                                break;
                   11576:                        default:
                   11577:                                REG16(AX) = 0x01; // function number invalid
                   11578:                                m_CF = 1;
                   11579:                        }
                   11580:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
                   11581:                        if(REG16(CX) >= 5) {
                   11582:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
                   11583:                                REG16(AX) = 5; // number of bytes actually read
                   11584:                        } else {
                   11585:                                REG16(AX) = 0x0d; // data invalid
                   11586:                                m_CF = 1;
                   11587:                        }
                   11588:                } else {
                   11589: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
                   11590: //                     REG16(AX) = REG16(CX);
                   11591:                        REG16(AX) = 0x05; // access denied
                   11592:                        m_CF = 1;
                   11593:                }
                   11594:                break;
1.1.1.48  root     11595:        case 0x03: // Write To Character Device Control Channel
1.1.1.45  root     11596: //             REG16(AX) = 0x05;
                   11597: //             m_CF = 1;
                   11598:                REG16(AX) = 0x00; // success
                   11599:                break;
1.1.1.48  root     11600:        case 0x04: // Read From Block Device Control Channel
                   11601:        case 0x05: // Write To Block Device Control Channel
1.1       root     11602:                REG16(AX) = 0x05;
1.1.1.3   root     11603:                m_CF = 1;
1.1       root     11604:                break;
1.1.1.48  root     11605:        case 0x06: // Get Input Status
1.1.1.20  root     11606:                if(file_mode[file_handler[fd].mode].in) {
                   11607:                        if(file_handler[fd].atty) {
1.1.1.14  root     11608:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     11609:                        } else {
1.1.1.20  root     11610:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     11611:                        }
1.1.1.14  root     11612:                } else {
                   11613:                        REG8(AL) = 0x00;
1.1       root     11614:                }
                   11615:                break;
1.1.1.48  root     11616:        case 0x07: // Get Output Status
1.1.1.20  root     11617:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     11618:                        REG8(AL) = 0xff;
                   11619:                } else {
                   11620:                        REG8(AL) = 0x00;
1.1       root     11621:                }
                   11622:                break;
1.1.1.48  root     11623:        case 0x08: // Check If Block Device Removable
1.1.1.44  root     11624:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     11625:                        // removable drive
                   11626:                        REG16(AX) = 0x00;
1.1       root     11627:                } else {
1.1.1.14  root     11628:                        // fixed drive
                   11629:                        REG16(AX) = 0x01;
1.1       root     11630:                }
                   11631:                break;
1.1.1.48  root     11632:        case 0x09: // Check If Block Device Remote
1.1.1.44  root     11633:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     11634:                        // remote drive
                   11635:                        REG16(DX) = 0x1000;
1.1.1.44  root     11636:                } else if(msdos_is_subst_drive(drv)) {
                   11637:                        // subst drive
                   11638:                        REG16(DX) = 0x8000;
1.1       root     11639:                } else {
1.1.1.14  root     11640:                        // local drive
1.1.1.44  root     11641:                        REG16(DX) = 0x0000;
1.1       root     11642:                }
                   11643:                break;
1.1.1.48  root     11644:        case 0x0a: // Check If Handle Is Remote
1.1.1.45  root     11645:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
                   11646:                        REG16(DX) = 0x8000;
                   11647:                } else {
                   11648:                        REG16(DX) = 0x0000;
                   11649:                }
1.1.1.21  root     11650:                break;
1.1.1.48  root     11651:        case 0x0b: // Set Sharing Retry Count
1.1       root     11652:                break;
1.1.1.48  root     11653:        case 0x0c: // Generic Character Device Request
1.1.1.22  root     11654:                if(REG8(CL) == 0x45) {
                   11655:                        // set iteration (retry) count
                   11656:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   11657:                } else if(REG8(CL) == 0x4a) {
                   11658:                        // select code page
                   11659:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   11660:                        msdos_nls_tables_update();
1.1.1.44  root     11661:                } else if(REG8(CL) == 0x4c) {
                   11662:                        // start code-page preparation
                   11663:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11664:                        int count = 1, offset = 0;
                   11665:                        if(active_code_page != 437) {
                   11666:                                ids[count++] = active_code_page;
                   11667:                        }
                   11668:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11669:                                ids[count++] = system_code_page;
                   11670:                        }
                   11671:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   11672:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   11673:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11674:                        for(int i = 0; i < count; i++) {
                   11675:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11676:                        }
                   11677:                } else if(REG8(CL) == 0x4d) {
                   11678:                        // end code-page preparation
                   11679:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   11680:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.50  root     11681:                } else if(REG8(CL) == 0x5f) {
                   11682:                        // set display information
                   11683:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   11684:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   11685:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   11686:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   11687:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   11688:                                
                   11689:                                if(cur_width != new_width || cur_height != new_height) {
                   11690:                                        pcbios_set_console_size(new_width, new_height, true);
                   11691:                                }
                   11692:                        }
1.1.1.22  root     11693:                } else if(REG8(CL) == 0x65) {
                   11694:                        // get iteration (retry) count
                   11695:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   11696:                } else if(REG8(CL) == 0x6a) {
                   11697:                        // query selected code page
                   11698:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   11699:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   11700:                        
                   11701:                        CPINFO info;
                   11702:                        GetCPInfo(active_code_page, &info);
                   11703:                        
                   11704:                        if(info.MaxCharSize != 1) {
                   11705:                                for(int i = 0;; i++) {
                   11706:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   11707:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   11708:                                        
                   11709:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   11710:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   11711:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   11712:                                        
                   11713:                                        if(lo == 0 && hi == 0) {
                   11714:                                                break;
                   11715:                                        }
                   11716:                                }
                   11717:                        }
1.1.1.44  root     11718:                } else if(REG8(CL) == 0x6b) {
                   11719:                        // query prepare list
                   11720:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11721:                        int count = 1, offset = 0;
                   11722:                        if(active_code_page != 437) {
                   11723:                                ids[count++] = active_code_page;
                   11724:                        }
                   11725:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11726:                                ids[count++] = system_code_page;
                   11727:                        }
                   11728:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   11729:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11730:                        for(int i = 0; i < count; i++) {
                   11731:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11732:                        }
                   11733:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11734:                        for(int i = 0; i < count; i++) {
                   11735:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11736:                        }
1.1.1.22  root     11737:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     11738:                        // get display information
1.1.1.50  root     11739:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   11740:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   11741:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   11742:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   11743:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   11744:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   11745:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   11746:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   11747:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   11748:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   11749:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.22  root     11750:                } else {
                   11751:                        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));
                   11752:                        REG16(AX) = 0x01; // invalid function
                   11753:                        m_CF = 1;
                   11754:                }
                   11755:                break;
1.1.1.48  root     11756:        case 0x0d: // Generic Block Device Request
1.1.1.22  root     11757:                if(REG8(CL) == 0x40) {
                   11758:                        // set device parameters
1.1.1.48  root     11759: //             } else if(REG8(CL) == 0x41) {
                   11760: //                     // write logical device track
                   11761: //             } else if(REG8(CL) == 0x42) {
                   11762: //                     // format and verify logical device track
1.1.1.22  root     11763:                } else if(REG8(CL) == 0x46) {
                   11764:                        // set volume serial number
1.1.1.48  root     11765:                } else if(REG8(CL) == 0x47) {
                   11766:                        // set access flag
                   11767: //             } else if(REG8(CL) == 0x48) {
                   11768: //                     // set media lock state
                   11769: //             } else if(REG8(CL) == 0x49) {
                   11770: //                     // eject media in drive
1.1.1.22  root     11771:                } else if(REG8(CL) == 0x4a) {
                   11772:                        // lock logical volume
                   11773:                } else if(REG8(CL) == 0x4b) {
                   11774:                        // lock physical volume
                   11775:                } else if(REG8(CL) == 0x60) {
                   11776:                        // get device parameters
1.1.1.42  root     11777:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11778:                        
1.1.1.42  root     11779:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11780:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11781:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11782:                                
                   11783:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   11784:                                switch(geo->MediaType) {
                   11785:                                case F5_360_512:
                   11786:                                case F5_320_512:
                   11787:                                case F5_320_1024:
                   11788:                                case F5_180_512:
                   11789:                                case F5_160_512:
                   11790:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   11791:                                        break;
                   11792:                                case F5_1Pt2_512:
                   11793:                                case F3_1Pt2_512:
                   11794:                                case F3_1Pt23_1024:
                   11795:                                case F5_1Pt23_1024:
                   11796:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   11797:                                        break;
                   11798:                                case F3_720_512:
                   11799:                                case F3_640_512:
                   11800:                                case F5_640_512:
                   11801:                                case F5_720_512:
                   11802:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   11803:                                        break;
                   11804:                                case F8_256_128:
                   11805:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   11806:                                        break;
                   11807:                                case FixedMedia:
                   11808:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11809:                                        break;
                   11810:                                case F3_1Pt44_512:
                   11811:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11812:                                        break;
                   11813:                                case F3_2Pt88_512:
                   11814:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   11815:                                        break;
                   11816:                                default:
                   11817:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11818: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11819:                                        break;
1.1.1.22  root     11820:                                }
1.1.1.42  root     11821:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   11822:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   11823:                                switch(geo->MediaType) {
                   11824:                                case F5_360_512:
                   11825:                                case F5_320_512:
                   11826:                                case F5_320_1024:
                   11827:                                case F5_180_512:
                   11828:                                case F5_160_512:
                   11829:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   11830:                                        break;
                   11831:                                default:
                   11832:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   11833:                                        break;
                   11834:                                }
                   11835:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   11836:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   11837:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   11838:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   11839:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   11840:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   11841:                                switch(geo->MediaType) {
                   11842:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   11843:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   11844:                                        break;
                   11845:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   11846:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   11847:                                        break;
                   11848:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   11849:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11850:                                        break;
                   11851:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11852:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11853:                                        break;
                   11854:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11855:                                case F3_1Pt2_512:
                   11856:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11857:                                case F5_720_512:
                   11858:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11859:                                        break;
                   11860:                                case FixedMedia:        // hard disk
                   11861:                                case RemovableMedia:
                   11862:                                case Unknown:
                   11863:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11864:                                        break;
                   11865:                                default:
                   11866:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11867:                                        break;
                   11868:                                }
                   11869:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11870:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11871:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11872:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11873:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11874:                                // 21h  BYTE    device type
                   11875:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11876:                        } else {
                   11877:                                REG16(AX) = 0x0f; // invalid drive
                   11878:                                m_CF = 1;
                   11879:                        }
1.1.1.48  root     11880: //             } else if(REG8(CL) == 0x61) {
                   11881: //                     // read logical device track
                   11882: //             } else if(REG8(CL) == 0x62) {
                   11883: //                     // verify logical device track
1.1.1.22  root     11884:                } else if(REG8(CL) == 0x66) {
                   11885:                        // get volume serial number
                   11886:                        char path[] = "A:\\";
                   11887:                        char volume_label[MAX_PATH];
                   11888:                        DWORD serial_number = 0;
                   11889:                        char file_system[MAX_PATH];
                   11890:                        
                   11891:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11892:                        
1.1.1.60  root     11893:                        if(GetVolumeInformationA(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
1.1.1.22  root     11894:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11895:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11896:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11897:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11898:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11899:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11900:                        } else {
                   11901:                                REG16(AX) = 0x0f; // invalid drive
                   11902:                                m_CF = 1;
                   11903:                        }
                   11904:                } else if(REG8(CL) == 0x67) {
                   11905:                        // get access flag
                   11906:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11907:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11908:                } else if(REG8(CL) == 0x68) {
                   11909:                        // sense media type
1.1.1.42  root     11910:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11911:                        
1.1.1.42  root     11912:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11913:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11914:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11915:                                
                   11916:                                switch(geo->MediaType) {
                   11917:                                case F3_720_512:
                   11918:                                case F5_720_512:
                   11919:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11920:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   11921:                                        break;
                   11922:                                case F3_1Pt44_512:
                   11923:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11924:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   11925:                                        break;
                   11926:                                case F3_2Pt88_512:
                   11927:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11928:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   11929:                                        break;
                   11930:                                default:
                   11931:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   11932:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   11933:                                        break;
1.1.1.22  root     11934:                                }
                   11935:                        } else {
                   11936:                                REG16(AX) = 0x0f; // invalid drive
                   11937:                                m_CF = 1;
                   11938:                        }
                   11939:                } else if(REG8(CL) == 0x6a) {
                   11940:                        // unlock logical volume
                   11941:                } else if(REG8(CL) == 0x6b) {
                   11942:                        // unlock physical volume
1.1.1.48  root     11943: //             } else if(REG8(CL) == 0x6c) {
                   11944: //                     // get lock flag
                   11945: //             } else if(REG8(CL) == 0x6d) {
                   11946: //                     // enumerate open files
                   11947: //             } else if(REG8(CL) == 0x6e) {
                   11948: //                     // find swap file
                   11949: //             } else if(REG8(CL) == 0x6f) {
                   11950: //                     // get drive map information
                   11951: //             } else if(REG8(CL) == 0x70) {
                   11952: //                     // get current lock state
                   11953: //             } else if(REG8(CL) == 0x71) {
                   11954: //                     // get first cluster
1.1.1.22  root     11955:                } else {
                   11956:                        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));
                   11957:                        REG16(AX) = 0x01; // invalid function
                   11958:                        m_CF = 1;
                   11959:                }
                   11960:                break;
1.1.1.48  root     11961:        case 0x0e: // Get Lgical Drive Map
1.1.1.44  root     11962:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11963:                        REG16(AX) = 0x0f; // invalid drive
                   11964:                        m_CF = 1;
                   11965:                } else {
                   11966:                        REG8(AL) = 0;
1.1.1.22  root     11967:                }
                   11968:                break;
1.1.1.48  root     11969:        case 0x0f: // Set Logical Drive Map
1.1.1.44  root     11970:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11971:                        REG16(AX) = 0x0f; // invalid drive
                   11972:                        m_CF = 1;
1.1.1.22  root     11973:                }
                   11974:                break;
1.1.1.48  root     11975:        case 0x10: // Query Generic IOCTRL Capability (Handle)
1.1.1.22  root     11976:                switch(REG8(CL)) {
                   11977:                case 0x45:
                   11978:                case 0x4a:
1.1.1.48  root     11979:                case 0x4c:
                   11980:                case 0x4d:
1.1.1.22  root     11981:                case 0x65:
                   11982:                case 0x6a:
1.1.1.48  root     11983:                case 0x6b:
1.1.1.22  root     11984:                case 0x7f:
                   11985:                        REG16(AX) = 0x0000; // supported
                   11986:                        break;
                   11987:                default:
                   11988:                        REG8(AL) = 0x01; // ioctl capability not available
                   11989:                        m_CF = 1;
                   11990:                        break;
                   11991:                }
                   11992:                break;
1.1.1.48  root     11993:        case 0x11: // Query Generic IOCTRL Capability (Drive)
1.1.1.22  root     11994:                switch(REG8(CL)) {
                   11995:                case 0x40:
                   11996:                case 0x46:
                   11997:                case 0x4a:
                   11998:                case 0x4b:
                   11999:                case 0x60:
                   12000:                case 0x66:
                   12001:                case 0x67:
                   12002:                case 0x68:
                   12003:                case 0x6a:
                   12004:                case 0x6b:
1.1.1.48  root     12005:                        if(REG8(CH) == 0x00 || REG8(CH) == 0x01 || REG8(CH) == 0x03 || REG8(CH) == 0x05) {
                   12006:                                // CH = 00h     Unknown
                   12007:                                // CH = 01h     COMn:
                   12008:                                // CH = 03h     CON
                   12009:                                // CH = 05h     LPTn:
                   12010:                                REG16(AX) = 0x0000; // supported
                   12011:                                break;
                   12012:                        }
1.1.1.22  root     12013:                default:
                   12014:                        REG8(AL) = 0x01; // ioctl capability not available
                   12015:                        m_CF = 1;
                   12016:                        break;
                   12017:                }
                   12018:                break;
1.1.1.48  root     12019:        case 0x12: // DR DOS 5.0-6.0 - Determine DOS Type
                   12020:        case 0x14: // DR DOS 5.0-6.0 - Set Global Password
                   12021:        case 0x16: // DR DOS 5.0-6.0 - History Buffer, Share, And Hiload Control
                   12022:        case 0x51: // Concurrent DOS v3.2+ - Installation Check
                   12023:        case 0x52: // DR DOS 3.41+ - Determine DOS tTpe/Get DR DOS Version
                   12024:        case 0x54: // DR DOS 3.41+ - Set Global Password
                   12025:        case 0x56: // DR DOS 5.0+ - History Buffer Control
                   12026:        case 0x57: // DR DOS 5.0-6.0 - Share/Hiload Control
                   12027:        case 0x58: // DR DOS 5.0+ internal - Get Pointer To Internal Variable Table
                   12028:        case 0x59: // DR Multiuser DOS 5.0 - API
                   12029:                REG16(AX) = 0x01; // this is not DR-DOS
1.1.1.22  root     12030:                m_CF = 1;
                   12031:                break;
1.1       root     12032:        default:
1.1.1.22  root     12033:                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     12034:                REG16(AX) = 0x01;
1.1.1.3   root     12035:                m_CF = 1;
1.1       root     12036:                break;
                   12037:        }
                   12038: }
                   12039: 
                   12040: inline void msdos_int_21h_45h()
                   12041: {
                   12042:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12043:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12044:        
1.1.1.20  root     12045:        if(fd < process->max_files && file_handler[fd].valid) {
                   12046:                int dup_fd = _dup(fd);
                   12047:                if(dup_fd != -1) {
                   12048:                        REG16(AX) = dup_fd;
                   12049:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   12050: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   12051:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     12052:                } else {
                   12053:                        REG16(AX) = errno;
1.1.1.3   root     12054:                        m_CF = 1;
1.1       root     12055:                }
                   12056:        } else {
                   12057:                REG16(AX) = 0x06;
1.1.1.3   root     12058:                m_CF = 1;
1.1       root     12059:        }
                   12060: }
                   12061: 
                   12062: inline void msdos_int_21h_46h()
                   12063: {
                   12064:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12065:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   12066:        int dup_fd = REG16(CX);
                   12067:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     12068:        
1.1.1.20  root     12069:        if(REG16(BX) == REG16(CX)) {
                   12070:                REG16(AX) = 0x06;
                   12071:                m_CF = 1;
                   12072:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   12073:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   12074:                        _close(tmp_fd);
                   12075:                        msdos_file_handler_close(tmp_fd);
                   12076:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   12077:                }
                   12078:                if(_dup2(fd, dup_fd) != -1) {
                   12079:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   12080: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   12081:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     12082:                } else {
                   12083:                        REG16(AX) = errno;
1.1.1.3   root     12084:                        m_CF = 1;
1.1       root     12085:                }
                   12086:        } else {
                   12087:                REG16(AX) = 0x06;
1.1.1.3   root     12088:                m_CF = 1;
1.1       root     12089:        }
                   12090: }
                   12091: 
                   12092: inline void msdos_int_21h_47h(int lfn)
                   12093: {
                   12094:        char path[MAX_PATH];
                   12095:        
                   12096:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45  root     12097:                if(!lfn) {
                   12098:                        strcpy(path, msdos_short_path(path));
                   12099:                }
1.1       root     12100:                if(path[1] == ':') {
                   12101:                        // the returned path does not include a drive or the initial backslash
1.1.1.45  root     12102:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     12103:                } else {
1.1.1.45  root     12104:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     12105:                }
                   12106:        } else {
                   12107:                REG16(AX) = errno;
1.1.1.3   root     12108:                m_CF = 1;
1.1       root     12109:        }
                   12110: }
                   12111: 
                   12112: inline void msdos_int_21h_48h()
                   12113: {
1.1.1.19  root     12114:        int seg, umb_linked;
1.1       root     12115:        
1.1.1.8   root     12116:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     12117:                // unlink umb not to allocate memory in umb
                   12118:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   12119:                        msdos_mem_unlink_umb();
                   12120:                }
1.1.1.8   root     12121:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   12122:                        REG16(AX) = seg;
                   12123:                } else {
                   12124:                        REG16(AX) = 0x08;
                   12125:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   12126:                        m_CF = 1;
                   12127:                }
1.1.1.19  root     12128:                if(umb_linked != 0) {
                   12129:                        msdos_mem_link_umb();
                   12130:                }
1.1.1.8   root     12131:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   12132:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   12133:                        REG16(AX) = seg;
                   12134:                } else {
                   12135:                        REG16(AX) = 0x08;
                   12136:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   12137:                        m_CF = 1;
                   12138:                }
                   12139:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   12140:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   12141:                        REG16(AX) = seg;
                   12142:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   12143:                        REG16(AX) = seg;
                   12144:                } else {
                   12145:                        REG16(AX) = 0x08;
                   12146:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   12147:                        m_CF = 1;
                   12148:                }
1.1       root     12149:        }
                   12150: }
                   12151: 
                   12152: inline void msdos_int_21h_49h()
                   12153: {
1.1.1.14  root     12154:        int mcb_seg = SREG(ES) - 1;
                   12155:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   12156:        
                   12157:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   12158:                msdos_mem_free(SREG(ES));
                   12159:        } else {
1.1.1.33  root     12160:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     12161:                m_CF = 1;
                   12162:        }
1.1       root     12163: }
                   12164: 
                   12165: inline void msdos_int_21h_4ah()
                   12166: {
1.1.1.14  root     12167:        int mcb_seg = SREG(ES) - 1;
                   12168:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     12169:        int max_paragraphs;
                   12170:        
1.1.1.14  root     12171:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   12172:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   12173:                        REG16(AX) = 0x08;
                   12174:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   12175:                        m_CF = 1;
                   12176:                }
                   12177:        } else {
1.1.1.33  root     12178:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     12179:                m_CF = 1;
1.1       root     12180:        }
                   12181: }
                   12182: 
                   12183: inline void msdos_int_21h_4bh()
                   12184: {
1.1.1.3   root     12185:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12186:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     12187:        
                   12188:        switch(REG8(AL)) {
                   12189:        case 0x00:
                   12190:        case 0x01:
                   12191:                if(msdos_process_exec(command, param, REG8(AL))) {
                   12192:                        REG16(AX) = 0x02;
1.1.1.3   root     12193:                        m_CF = 1;
1.1       root     12194:                }
                   12195:                break;
1.1.1.14  root     12196:        case 0x03:
                   12197:                {
                   12198:                        int fd;
                   12199:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   12200:                                REG16(AX) = 0x02;
                   12201:                                m_CF = 1;
                   12202:                                break;
                   12203:                        }
                   12204:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   12205:                        _close(fd);
                   12206:                        
                   12207:                        UINT16 *overlay = (UINT16 *)param;
                   12208:                        
                   12209:                        // check exe header
                   12210:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   12211:                        int header_size = 0;
                   12212:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   12213:                                header_size = header->header_size * 16;
                   12214:                                // relocation
                   12215:                                int start_seg = overlay[1];
                   12216:                                for(int i = 0; i < header->relocations; i++) {
                   12217:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   12218:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   12219:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   12220:                                }
                   12221:                        }
                   12222:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   12223:                }
                   12224:                break;
1.1.1.48  root     12225:        case 0x04:
                   12226:                // Load And Execute In Background (European MS-DOS 4.0 only)
                   12227: //     case 0x05:
                   12228: //             // DOS 5+ - Set Execution State
                   12229:        case 0x80:
                   12230:                // DR DOS v3.41 - Run Already-Loaded Kernel File
                   12231:        case 0xf0:
                   12232:        case 0xf1:
                   12233:                // DIET v1.10+
1.1.1.43  root     12234:        case 0xfd:
                   12235:        case 0xfe:
                   12236:                // unknown function called in FreeCOM
                   12237:                REG16(AX) = 0x01;
                   12238:                m_CF = 1;
                   12239:                break;
1.1       root     12240:        default:
1.1.1.22  root     12241:                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     12242:                REG16(AX) = 0x01;
1.1.1.3   root     12243:                m_CF = 1;
1.1       root     12244:                break;
                   12245:        }
                   12246: }
                   12247: 
                   12248: inline void msdos_int_21h_4ch()
                   12249: {
                   12250:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   12251: }
                   12252: 
                   12253: inline void msdos_int_21h_4dh()
                   12254: {
                   12255:        REG16(AX) = retval;
                   12256: }
                   12257: 
                   12258: inline void msdos_int_21h_4eh()
                   12259: {
                   12260:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     12261:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   12262:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45  root     12263:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1.1.60  root     12264:        WIN32_FIND_DATAA fd;
1.1       root     12265:        
1.1.1.14  root     12266:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12267:        find->find_magic = FIND_MAGIC;
                   12268:        find->dta_index = dtainfo - dtalist;
1.1       root     12269:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12270:        dtainfo->allowable_mask = REG8(CL);
1.1.1.58  root     12271:        // note: SO1 dir command sets 0x3f, but only directories and volue label are found if bit3 is set :-(
                   12272:        if((dtainfo->allowable_mask & 0x3f) == 0x3f) {
                   12273:                dtainfo->allowable_mask &= ~0x08;
                   12274:        }
1.1.1.14  root     12275:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12276:        
1.1.1.14  root     12277:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12278:                dtainfo->allowable_mask &= ~8;
1.1       root     12279:        }
1.1.1.60  root     12280:        if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14  root     12281:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     12282:                      !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     12283:                        if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     12284:                                FindClose(dtainfo->find_handle);
                   12285:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12286:                                break;
                   12287:                        }
                   12288:                }
                   12289:        }
1.1.1.13  root     12290:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12291:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   12292:                msdos_find_file_conv_local_time(&fd);
                   12293:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   12294:                find->size = fd.nFileSizeLow;
1.1.1.13  root     12295:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     12296:                REG16(AX) = 0;
1.1.1.14  root     12297:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12298:                find->attrib = 8;
                   12299:                find->size = 0;
                   12300:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12301:                dtainfo->allowable_mask &= ~8;
1.1       root     12302:                REG16(AX) = 0;
                   12303:        } else {
                   12304:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12305:                m_CF = 1;
1.1       root     12306:        }
                   12307: }
                   12308: 
                   12309: inline void msdos_int_21h_4fh()
                   12310: {
                   12311:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     12312:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   12313:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.60  root     12314:        WIN32_FIND_DATAA fd;
1.1       root     12315:        
1.1.1.14  root     12316:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   12317:                REG16(AX) = 0x12;
                   12318:                m_CF = 1;
                   12319:                return;
                   12320:        }
                   12321:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     12322:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60  root     12323:                if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14  root     12324:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     12325:                              !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     12326:                                if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     12327:                                        FindClose(dtainfo->find_handle);
                   12328:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12329:                                        break;
                   12330:                                }
                   12331:                        }
                   12332:                } else {
1.1.1.13  root     12333:                        FindClose(dtainfo->find_handle);
                   12334:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12335:                }
                   12336:        }
1.1.1.13  root     12337:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12338:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   12339:                msdos_find_file_conv_local_time(&fd);
                   12340:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   12341:                find->size = fd.nFileSizeLow;
1.1.1.13  root     12342:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     12343:                REG16(AX) = 0;
1.1.1.14  root     12344:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12345:                find->attrib = 8;
                   12346:                find->size = 0;
                   12347:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12348:                dtainfo->allowable_mask &= ~8;
1.1       root     12349:                REG16(AX) = 0;
                   12350:        } else {
                   12351:                REG16(AX) = 0x12;
1.1.1.3   root     12352:                m_CF = 1;
1.1       root     12353:        }
                   12354: }
                   12355: 
                   12356: inline void msdos_int_21h_50h()
                   12357: {
1.1.1.8   root     12358:        if(current_psp != REG16(BX)) {
                   12359:                process_t *process = msdos_process_info_get(current_psp);
                   12360:                if(process != NULL) {
                   12361:                        process->psp = REG16(BX);
                   12362:                }
                   12363:                current_psp = REG16(BX);
1.1.1.23  root     12364:                msdos_sda_update(current_psp);
1.1.1.8   root     12365:        }
1.1       root     12366: }
                   12367: 
                   12368: inline void msdos_int_21h_51h()
                   12369: {
                   12370:        REG16(BX) = current_psp;
                   12371: }
                   12372: 
                   12373: inline void msdos_int_21h_52h()
                   12374: {
1.1.1.25  root     12375:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     12376:        i386_load_segment_descriptor(ES);
1.1.1.25  root     12377:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     12378: }
                   12379: 
1.1.1.43  root     12380: inline void msdos_int_21h_53h()
                   12381: {
                   12382:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   12383:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   12384:        
                   12385:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   12386:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   12387:        dpb->shift_count = 0;
                   12388:        dpb->reserved_sectors = 0;
                   12389:        dpb->fat_num = bpb->fat_num;
                   12390:        dpb->root_entries = bpb->root_entries;
                   12391:        dpb->first_data_sector = 0;
                   12392:        if(bpb->sectors_per_cluster != 0) {
                   12393:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   12394:        } else {
                   12395:                dpb->highest_cluster_num = 0;
                   12396:        }
                   12397:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   12398:        dpb->first_dir_sector = 0;
                   12399:        dpb->device_driver_header = 0;
                   12400:        dpb->media_type = bpb->media_type;
                   12401:        dpb->drive_accessed = 0;
                   12402:        dpb->next_dpb_ofs = 0xffff;
                   12403:        dpb->next_dpb_seg = 0xffff;
                   12404:        dpb->first_free_cluster = 0;
                   12405:        dpb->free_clusters = 0xffff;
                   12406: }
                   12407: 
1.1       root     12408: inline void msdos_int_21h_54h()
                   12409: {
                   12410:        process_t *process = msdos_process_info_get(current_psp);
                   12411:        
                   12412:        REG8(AL) = process->verify;
                   12413: }
                   12414: 
                   12415: inline void msdos_int_21h_55h()
                   12416: {
                   12417:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   12418:        
                   12419:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   12420:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   12421:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   12422:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   12423:        psp->parent_psp = current_psp;
                   12424: }
                   12425: 
                   12426: inline void msdos_int_21h_56h(int lfn)
                   12427: {
                   12428:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     12429:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   12430:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     12431:        
                   12432:        if(rename(src, dst)) {
                   12433:                REG16(AX) = errno;
1.1.1.3   root     12434:                m_CF = 1;
1.1       root     12435:        }
                   12436: }
                   12437: 
                   12438: inline void msdos_int_21h_57h()
                   12439: {
                   12440:        FILETIME time, local;
1.1.1.14  root     12441:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     12442:        HANDLE hHandle;
1.1       root     12443:        
1.1.1.21  root     12444:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     12445:                REG16(AX) = (UINT16)GetLastError();
                   12446:                m_CF = 1;
                   12447:                return;
                   12448:        }
                   12449:        ctime = atime = mtime = NULL;
                   12450:        
1.1       root     12451:        switch(REG8(AL)) {
                   12452:        case 0x00:
1.1.1.6   root     12453:        case 0x01:
1.1.1.14  root     12454:                mtime = &time;
1.1.1.6   root     12455:                break;
                   12456:        case 0x04:
                   12457:        case 0x05:
1.1.1.14  root     12458:                atime = &time;
1.1       root     12459:                break;
1.1.1.6   root     12460:        case 0x06:
                   12461:        case 0x07:
1.1.1.14  root     12462:                ctime = &time;
                   12463:                break;
                   12464:        default:
1.1.1.22  root     12465:                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     12466:                REG16(AX) = 0x01;
                   12467:                m_CF = 1;
                   12468:                return;
                   12469:        }
                   12470:        if(REG8(AL) & 1) {
1.1       root     12471:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   12472:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     12473:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     12474:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12475:                        m_CF = 1;
1.1       root     12476:                }
1.1.1.14  root     12477:        } else {
1.1.1.21  root     12478:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     12479:                        // assume a device and use the current time
                   12480:                        GetSystemTimeAsFileTime(&time);
                   12481:                }
                   12482:                FileTimeToLocalFileTime(&time, &local);
                   12483:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     12484:        }
                   12485: }
                   12486: 
                   12487: inline void msdos_int_21h_58h()
                   12488: {
                   12489:        switch(REG8(AL)) {
                   12490:        case 0x00:
1.1.1.7   root     12491:                REG16(AX) = malloc_strategy;
                   12492:                break;
                   12493:        case 0x01:
1.1.1.24  root     12494: //             switch(REG16(BX)) {
                   12495:                switch(REG8(BL)) {
1.1.1.7   root     12496:                case 0x0000:
                   12497:                case 0x0001:
                   12498:                case 0x0002:
                   12499:                case 0x0040:
                   12500:                case 0x0041:
                   12501:                case 0x0042:
                   12502:                case 0x0080:
                   12503:                case 0x0081:
                   12504:                case 0x0082:
                   12505:                        malloc_strategy = REG16(BX);
1.1.1.23  root     12506:                        msdos_sda_update(current_psp);
1.1.1.7   root     12507:                        break;
                   12508:                default:
1.1.1.22  root     12509:                        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     12510:                        REG16(AX) = 0x01;
                   12511:                        m_CF = 1;
                   12512:                        break;
                   12513:                }
                   12514:                break;
                   12515:        case 0x02:
1.1.1.19  root     12516:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     12517:                break;
                   12518:        case 0x03:
1.1.1.24  root     12519: //             switch(REG16(BX)) {
                   12520:                switch(REG8(BL)) {
1.1.1.7   root     12521:                case 0x0000:
1.1.1.19  root     12522:                        msdos_mem_unlink_umb();
                   12523:                        break;
1.1.1.7   root     12524:                case 0x0001:
1.1.1.19  root     12525:                        msdos_mem_link_umb();
1.1.1.7   root     12526:                        break;
                   12527:                default:
1.1.1.22  root     12528:                        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     12529:                        REG16(AX) = 0x01;
                   12530:                        m_CF = 1;
                   12531:                        break;
                   12532:                }
1.1       root     12533:                break;
                   12534:        default:
1.1.1.22  root     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));
1.1       root     12536:                REG16(AX) = 0x01;
1.1.1.3   root     12537:                m_CF = 1;
1.1       root     12538:                break;
                   12539:        }
                   12540: }
                   12541: 
                   12542: inline void msdos_int_21h_59h()
                   12543: {
1.1.1.47  root     12544:        if(REG16(BX) == 0x0000) {
                   12545:                sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   12546:                
                   12547:                REG16(AX) = sda->extended_error_code;
                   12548:                REG8(BH)  = sda->error_class;
                   12549:                REG8(BL)  = sda->suggested_action;
                   12550:                REG8(CH)  = sda->locus_of_last_error;
                   12551:                // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
                   12552:                if(sda->int21h_5d0ah_called != 0) {
                   12553:                        REG8(CL)  = sda->int21h_5d0ah_cl;
                   12554:                        REG16(DX) = sda->int21h_5d0ah_dx;
                   12555: //                     REG16(SI) = sda->int21h_5d0ah_si;
                   12556:                        REG16(DI) = sda->last_error_pointer.w.l;
                   12557: //                     SREG(DS)  = sda->int21h_5d0ah_ds;
                   12558: //                     i386_load_segment_descriptor(DS);
                   12559:                        SREG(ES)  = sda->last_error_pointer.w.h;
                   12560:                        i386_load_segment_descriptor(ES);
                   12561:                }
                   12562:                sda->int21h_5d0ah_called = 0;
                   12563: //     } else if(REG16(BX) == 0x0001) {
                   12564: //             // European MS-DOS 4.0 - Get Hard Error Information
                   12565:        } else {
                   12566:                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));
                   12567:                REG16(AX) = 0x01;
                   12568:                m_CF = 1;
                   12569:        }
1.1       root     12570: }
                   12571: 
                   12572: inline void msdos_int_21h_5ah()
                   12573: {
1.1.1.3   root     12574:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12575:        int len = strlen(path);
                   12576:        char tmp[MAX_PATH];
                   12577:        
1.1.1.60  root     12578:        if(GetTempFileNameA(path, "TMP", 0, tmp)) {
1.1       root     12579:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12580:                
1.1.1.60  root     12581:                SetFileAttributesA(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     12582:                REG16(AX) = fd;
                   12583:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12584:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12585:                
                   12586:                strcpy(path, tmp);
                   12587:                int dx = REG16(DX) + len;
1.1.1.3   root     12588:                int ds = SREG(DS);
1.1       root     12589:                while(dx > 0xffff) {
                   12590:                        dx -= 0x10;
                   12591:                        ds++;
                   12592:                }
                   12593:                REG16(DX) = dx;
1.1.1.3   root     12594:                SREG(DS) = ds;
                   12595:                i386_load_segment_descriptor(DS);
1.1       root     12596:        } else {
                   12597:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12598:                m_CF = 1;
1.1       root     12599:        }
                   12600: }
                   12601: 
                   12602: inline void msdos_int_21h_5bh()
                   12603: {
1.1.1.45  root     12604:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     12605:        
1.1.1.45  root     12606: //     if(msdos_is_existing_file(path)) {
                   12607:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12608:                // already exists
                   12609:                REG16(AX) = 0x50;
1.1.1.3   root     12610:                m_CF = 1;
1.1       root     12611:        } else {
                   12612:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12613:                
                   12614:                if(fd != -1) {
1.1.1.60  root     12615:                        SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     12616:                        REG16(AX) = fd;
                   12617:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12618:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12619:                } else {
                   12620:                        REG16(AX) = errno;
1.1.1.3   root     12621:                        m_CF = 1;
1.1       root     12622:                }
                   12623:        }
                   12624: }
                   12625: 
                   12626: inline void msdos_int_21h_5ch()
                   12627: {
                   12628:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12629:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12630:        
1.1.1.20  root     12631:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     12632:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     12633:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     12634:                        UINT32 pos = _tell(fd);
                   12635:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   12636:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     12637:                                REG16(AX) = errno;
1.1.1.3   root     12638:                                m_CF = 1;
1.1       root     12639:                        }
1.1.1.20  root     12640:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     12641:                        
1.1       root     12642:                        // some seconds may be passed in _locking()
1.1.1.35  root     12643:                        REQUEST_HARDWRE_UPDATE();
1.1       root     12644:                } else {
                   12645:                        REG16(AX) = 0x01;
1.1.1.3   root     12646:                        m_CF = 1;
1.1       root     12647:                }
                   12648:        } else {
                   12649:                REG16(AX) = 0x06;
1.1.1.3   root     12650:                m_CF = 1;
1.1       root     12651:        }
                   12652: }
                   12653: 
1.1.1.22  root     12654: inline void msdos_int_21h_5dh()
                   12655: {
                   12656:        switch(REG8(AL)) {
1.1.1.45  root     12657:        case 0x00:
                   12658:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
                   12659:                        // current system
                   12660:                        static bool reenter = false;
                   12661:                        if(!reenter) {
                   12662:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
                   12663:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
                   12664:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
                   12665:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
                   12666:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
                   12667:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
                   12668:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
                   12669:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
                   12670:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
                   12671:                                i386_load_segment_descriptor(DS);
                   12672:                                i386_load_segment_descriptor(ES);
                   12673:                                reenter = true;
                   12674:                                try {
                   12675:                                        msdos_syscall(0x21);
                   12676:                                } catch(...) {
                   12677:                                }
                   12678:                                reenter = false;
                   12679:                        }
                   12680:                } else {
                   12681:                        REG16(AX) = 0x49; //  network software not installed
                   12682:                        m_CF = 1;
                   12683:                }
                   12684:                break;
1.1.1.22  root     12685:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     12686:                SREG(DS) = (SDA_TOP >> 4);
                   12687:                i386_load_segment_descriptor(DS);
                   12688:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   12689:                REG16(CX) = 0x80;
                   12690:                REG16(DX) = 0x1a;
                   12691:                break;
1.1.1.45  root     12692:        case 0x07: // get redirected printer mode
                   12693:        case 0x08: // set redirected printer mode
                   12694:        case 0x09: // flush redirected printer output
                   12695:                REG16(AX) = 0x49; //  network software not installed
                   12696:                m_CF = 1;
                   12697:                break;
1.1.1.43  root     12698:        case 0x0a: // set extended error information
                   12699:                {
                   12700:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     12701:                        sda->int21h_5d0ah_called    = 1;
1.1.1.43  root     12702:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47  root     12703:                        // XXX: which one is correct ???
                   12704: #if 1
                   12705:                        // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43  root     12706:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   12707:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47  root     12708:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43  root     12709:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47  root     12710: #else
                   12711:                        // PC DOS 7 Technical Update
                   12712:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
                   12713:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
                   12714:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
                   12715:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
                   12716: #endif
                   12717:                        sda->int21h_5d0ah_dx        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   12718: //                     sda->int21h_5d0ah_si        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43  root     12719:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47  root     12720: //                     sda->int21h_5d0ah_ds        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43  root     12721:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   12722:                }
                   12723:                break;
1.1.1.23  root     12724:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     12725:                REG16(AX) = 0x01;
                   12726:                m_CF = 1;
                   12727:                break;
                   12728:        default:
                   12729:                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));
                   12730:                REG16(AX) = 0x01;
                   12731:                m_CF = 1;
                   12732:                break;
                   12733:        }
                   12734: }
                   12735: 
1.1.1.42  root     12736: inline void msdos_int_21h_5eh()
                   12737: {
                   12738:        switch(REG8(AL)) {
                   12739:        case 0x00:
                   12740:                {
                   12741:                        char name[256] = {0};
                   12742:                        DWORD dwSize = 256;
                   12743:                        
1.1.1.60  root     12744:                        if(GetComputerNameA(name, &dwSize)) {
1.1.1.42  root     12745:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12746:                                for(int i = 0; i < 15; i++) {
                   12747:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   12748:                                }
                   12749:                                dest[15] = '\0';
                   12750:                                REG8(CH) = 0x01; // nonzero valid
                   12751:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   12752:                        } else {
                   12753:                                REG16(AX) = 0x01;
                   12754:                                m_CF = 1;
                   12755:                        }
                   12756:                }
                   12757:                break;
                   12758:        default:
1.1.1.45  root     12759: //             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));
                   12760: //             REG16(AX) = 0x01;
                   12761:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     12762:                m_CF = 1;
                   12763:                break;
                   12764:        }
                   12765: }
                   12766: 
1.1.1.30  root     12767: inline void msdos_int_21h_5fh()
                   12768: {
                   12769:        switch(REG8(AL)) {
1.1.1.42  root     12770:        case 0x05:
1.1.1.44  root     12771:                REG16(BP) = 0;
                   12772:                for(int i = 0; i < 26; i++) {
                   12773:                        if(msdos_is_remote_drive(i)) {
                   12774:                                REG16(BP)++;
1.1.1.42  root     12775:                        }
                   12776:                }
1.1.1.30  root     12777:        case 0x02:
1.1.1.44  root     12778:                for(int i = 0, index = 0; i < 26; i++) {
                   12779:                        if(msdos_is_remote_drive(i)) {
                   12780:                                if(index == REG16(BX)) {
                   12781:                                        char volume[] = "A:";
1.1.1.30  root     12782:                                        volume[0] = 'A' + i;
1.1.1.44  root     12783:                                        DWORD dwSize = 128;
                   12784:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
1.1.1.60  root     12785:                                        WNetGetConnectionA(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
1.1.1.44  root     12786:                                        REG8(BH) = 0x00; // valid
                   12787:                                        REG8(BL) = 0x04; // disk drive
                   12788:                                        REG16(CX) = 0x00; // LANtastic
                   12789:                                        return;
1.1.1.30  root     12790:                                }
1.1.1.44  root     12791:                                index++;
1.1.1.30  root     12792:                        }
                   12793:                }
                   12794:                REG16(AX) = 0x12; // no more files
                   12795:                m_CF = 1;
                   12796:                break;
1.1.1.44  root     12797:        case 0x07:
                   12798:                if(msdos_is_valid_drive(REG8(DL))) {
                   12799:                        msdos_cds_update(REG8(DL));
                   12800:                } else {
                   12801:                        REG16(AX) = 0x0f; // invalid drive
                   12802:                        m_CF = 1;
                   12803:                }
                   12804:                break;
                   12805:        case 0x08:
                   12806:                if(msdos_is_valid_drive(REG8(DL))) {
                   12807:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   12808:                        cds->drive_attrib = 0x0000;
                   12809:                } else {
                   12810:                        REG16(AX) = 0x0f; // invalid drive
                   12811:                        m_CF = 1;
                   12812:                }
                   12813:                break;
1.1.1.30  root     12814:        default:
1.1.1.45  root     12815: //             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));
                   12816: //             REG16(AX) = 0x01;
                   12817:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     12818:                m_CF = 1;
                   12819:                break;
                   12820:        }
                   12821: }
                   12822: 
1.1       root     12823: inline void msdos_int_21h_60h(int lfn)
                   12824: {
1.1.1.45  root     12825:        char full[MAX_PATH];
                   12826:        const char *path = NULL;
1.1.1.14  root     12827:        
1.1       root     12828:        if(lfn) {
1.1.1.14  root     12829:                char *name;
                   12830:                *full = '\0';
1.1.1.60  root     12831:                GetFullPathNameA((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     12832:                switch(REG8(CL)) {
                   12833:                case 1:
1.1.1.60  root     12834:                        GetShortPathNameA(full, full, MAX_PATH);
1.1.1.14  root     12835:                        my_strupr(full);
                   12836:                        break;
                   12837:                case 2:
1.1.1.60  root     12838:                        GetLongPathNameA(full, full, MAX_PATH);
1.1.1.14  root     12839:                        break;
                   12840:                }
                   12841:                path = full;
                   12842:        } else {
                   12843:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12844:        }
                   12845:        if(*path != '\0') {
                   12846:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     12847:        } else {
1.1.1.14  root     12848:                REG16(AX) = (UINT16)GetLastError();
                   12849:                m_CF = 1;
1.1       root     12850:        }
                   12851: }
                   12852: 
                   12853: inline void msdos_int_21h_61h()
                   12854: {
                   12855:        REG8(AL) = 0;
                   12856: }
                   12857: 
                   12858: inline void msdos_int_21h_62h()
                   12859: {
                   12860:        REG16(BX) = current_psp;
                   12861: }
                   12862: 
                   12863: inline void msdos_int_21h_63h()
                   12864: {
                   12865:        switch(REG8(AL)) {
                   12866:        case 0x00:
1.1.1.3   root     12867:                SREG(DS) = (DBCS_TABLE >> 4);
                   12868:                i386_load_segment_descriptor(DS);
1.1       root     12869:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   12870:                REG8(AL) = 0x00;
                   12871:                break;
1.1.1.22  root     12872:        case 0x01: // set korean input mode
                   12873:        case 0x02: // get korean input mode
                   12874:                REG8(AL) = 0xff; // not supported
                   12875:                break;
1.1       root     12876:        default:
1.1.1.22  root     12877:                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     12878:                REG16(AX) = 0x01;
1.1.1.3   root     12879:                m_CF = 1;
1.1       root     12880:                break;
                   12881:        }
                   12882: }
                   12883: 
1.1.1.25  root     12884: UINT16 get_extended_country_info(UINT8 func)
1.1       root     12885: {
1.1.1.25  root     12886:        switch(func) {
1.1.1.17  root     12887:        case 0x01:
                   12888:                if(REG16(CX) >= 5) {
1.1.1.19  root     12889:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     12890:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   12891:                                REG16(CX) = sizeof(data);
                   12892:                        ZeroMemory(data, sizeof(data));
                   12893:                        data[0] = 0x01;
                   12894:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     12895:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     12896:                        *(UINT16 *)(data + 5) = active_code_page;
                   12897:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     12898: //                     REG16(AX) = active_code_page;
1.1.1.17  root     12899:                } else {
1.1.1.25  root     12900:                        return(0x08); // insufficient memory
1.1.1.17  root     12901:                }
                   12902:                break;
                   12903:        case 0x02:
                   12904:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12905:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   12906:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     12907: //             REG16(AX) = active_code_page;
1.1.1.17  root     12908:                REG16(CX) = 0x05;
                   12909:                break;
1.1.1.23  root     12910:        case 0x03:
                   12911:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12912:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   12913:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     12914: //             REG16(AX) = active_code_page;
1.1.1.23  root     12915:                REG16(CX) = 0x05;
                   12916:                break;
1.1.1.17  root     12917:        case 0x04:
                   12918:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   12919:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   12920:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     12921: //             REG16(AX) = active_code_page;
1.1.1.17  root     12922:                REG16(CX) = 0x05;
                   12923:                break;
                   12924:        case 0x05:
                   12925:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   12926:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   12927:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     12928: //             REG16(AX) = active_code_page;
1.1.1.17  root     12929:                REG16(CX) = 0x05;
                   12930:                break;
                   12931:        case 0x06:
                   12932:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   12933:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   12934:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     12935: //             REG16(AX) = active_code_page;
1.1.1.17  root     12936:                REG16(CX) = 0x05;
                   12937:                break;
1.1       root     12938:        case 0x07:
1.1.1.3   root     12939:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   12940:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   12941:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     12942: //             REG16(AX) = active_code_page;
1.1       root     12943:                REG16(CX) = 0x05;
                   12944:                break;
1.1.1.25  root     12945:        default:
                   12946:                return(0x01); // function number invalid
                   12947:        }
                   12948:        return(0x00);
                   12949: }
                   12950: 
                   12951: inline void msdos_int_21h_65h()
                   12952: {
                   12953:        char tmp[0x10000];
                   12954:        
                   12955:        switch(REG8(AL)) {
1.1.1.43  root     12956:        case 0x00:
                   12957:                if(REG16(CX) >= 7) {
                   12958:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   12959:                        REG16(AX) = system_code_page;
                   12960:                } else {
                   12961:                        REG16(AX) = 0x0c;
                   12962:                        m_CF = 1;
                   12963:                }
                   12964:                break;
1.1.1.25  root     12965:        case 0x01:
                   12966:        case 0x02:
                   12967:        case 0x03:
                   12968:        case 0x04:
                   12969:        case 0x05:
                   12970:        case 0x06:
                   12971:        case 0x07:
                   12972:                {
                   12973:                        UINT16 result = get_extended_country_info(REG8(AL));
                   12974:                        if(result) {
                   12975:                                REG16(AX) = result;
                   12976:                                m_CF = 1;
                   12977:                        } else {
                   12978:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   12979:                        }
                   12980:                }
                   12981:                break;
1.1       root     12982:        case 0x20:
1.1.1.25  root     12983:        case 0xa0:
1.1.1.19  root     12984:                memset(tmp, 0, sizeof(tmp));
                   12985:                tmp[0] = REG8(DL);
1.1       root     12986:                my_strupr(tmp);
                   12987:                REG8(DL) = tmp[0];
                   12988:                break;
                   12989:        case 0x21:
1.1.1.25  root     12990:        case 0xa1:
1.1       root     12991:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12992:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     12993:                my_strupr(tmp);
1.1.1.3   root     12994:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     12995:                break;
                   12996:        case 0x22:
1.1.1.25  root     12997:        case 0xa2:
1.1.1.3   root     12998:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     12999:                break;
1.1.1.25  root     13000:        case 0x23:
                   13001:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45  root     13002:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     13003:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45  root     13004:                        REG16(AX) = 0x00;
                   13005:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   13006:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     13007:                        REG16(AX) = 0x01;
                   13008:                } else {
                   13009:                        REG16(AX) = 0x02;
                   13010:                }
                   13011:                break;
1.1       root     13012:        default:
1.1.1.22  root     13013:                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     13014:                REG16(AX) = 0x01;
1.1.1.3   root     13015:                m_CF = 1;
1.1       root     13016:                break;
                   13017:        }
                   13018: }
                   13019: 
                   13020: inline void msdos_int_21h_66h()
                   13021: {
                   13022:        switch(REG8(AL)) {
                   13023:        case 0x01:
                   13024:                REG16(BX) = active_code_page;
                   13025:                REG16(DX) = system_code_page;
                   13026:                break;
                   13027:        case 0x02:
                   13028:                if(active_code_page == REG16(BX)) {
                   13029:                        REG16(AX) = 0xeb41;
                   13030:                } else if(_setmbcp(REG16(BX)) == 0) {
                   13031:                        active_code_page = REG16(BX);
1.1.1.17  root     13032:                        msdos_nls_tables_update();
1.1       root     13033:                        REG16(AX) = 0xeb41;
1.1.1.32  root     13034:                        SetConsoleCP(active_code_page);
                   13035:                        SetConsoleOutputCP(active_code_page);
1.1       root     13036:                } else {
                   13037:                        REG16(AX) = 0x25;
1.1.1.3   root     13038:                        m_CF = 1;
1.1       root     13039:                }
                   13040:                break;
                   13041:        default:
1.1.1.22  root     13042:                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     13043:                REG16(AX) = 0x01;
1.1.1.3   root     13044:                m_CF = 1;
1.1       root     13045:                break;
                   13046:        }
                   13047: }
                   13048: 
                   13049: inline void msdos_int_21h_67h()
                   13050: {
                   13051:        process_t *process = msdos_process_info_get(current_psp);
                   13052:        
                   13053:        if(REG16(BX) <= MAX_FILES) {
                   13054:                process->max_files = max(REG16(BX), 20);
                   13055:        } else {
                   13056:                REG16(AX) = 0x08;
1.1.1.3   root     13057:                m_CF = 1;
1.1       root     13058:        }
                   13059: }
                   13060: 
                   13061: inline void msdos_int_21h_68h()
                   13062: {
                   13063:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     13064:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     13065:        
1.1.1.20  root     13066:        if(fd < process->max_files && file_handler[fd].valid) {
                   13067:                // fflush(_fdopen(fd, ""));
1.1       root     13068:        } else {
                   13069:                REG16(AX) = 0x06;
1.1.1.3   root     13070:                m_CF = 1;
1.1       root     13071:        }
                   13072: }
                   13073: 
                   13074: inline void msdos_int_21h_69h()
                   13075: {
1.1.1.3   root     13076:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13077:        char path[] = "A:\\";
                   13078:        char volume_label[MAX_PATH];
                   13079:        DWORD serial_number = 0;
                   13080:        char file_system[MAX_PATH];
                   13081:        
                   13082:        if(REG8(BL) == 0) {
                   13083:                path[0] = 'A' + _getdrive() - 1;
                   13084:        } else {
                   13085:                path[0] = 'A' + REG8(BL) - 1;
                   13086:        }
                   13087:        
                   13088:        switch(REG8(AL)) {
                   13089:        case 0x00:
1.1.1.60  root     13090:                if(GetVolumeInformationA(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
1.1       root     13091:                        info->info_level = 0;
                   13092:                        info->serial_number = serial_number;
                   13093:                        memset(info->volume_label, 0x20, 11);
                   13094:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   13095:                        memset(info->file_system, 0x20, 8);
                   13096:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   13097:                } else {
                   13098:                        REG16(AX) = errno;
1.1.1.3   root     13099:                        m_CF = 1;
1.1       root     13100:                }
                   13101:                break;
                   13102:        case 0x01:
                   13103:                REG16(AX) = 0x03;
1.1.1.3   root     13104:                m_CF = 1;
1.1.1.45  root     13105:                break;
                   13106:        default:
                   13107:                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));
                   13108:                REG16(AX) = 0x01;
                   13109:                m_CF = 1;
                   13110:                break;
1.1       root     13111:        }
                   13112: }
                   13113: 
                   13114: inline void msdos_int_21h_6ah()
                   13115: {
                   13116:        REG8(AH) = 0x68;
                   13117:        msdos_int_21h_68h();
                   13118: }
                   13119: 
                   13120: inline void msdos_int_21h_6bh()
                   13121: {
1.1.1.45  root     13122:        REG8(AL) = 0x00;
1.1       root     13123: }
                   13124: 
                   13125: inline void msdos_int_21h_6ch(int lfn)
                   13126: {
1.1.1.45  root     13127:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     13128:        int mode = REG8(BL) & 0x03;
                   13129:        
                   13130:        if(mode < 0x03) {
1.1.1.29  root     13131:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     13132:                        // file exists
                   13133:                        if(REG8(DL) & 1) {
1.1.1.37  root     13134:                                int fd = -1;
                   13135:                                int sio_port = 0;
                   13136:                                int lpt_port = 0;
1.1       root     13137:                                
1.1.1.45  root     13138:                                if(msdos_is_device_path(path)) {
                   13139:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     13140:                                } else {
1.1.1.13  root     13141:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     13142:                                }
1.1       root     13143:                                if(fd != -1) {
                   13144:                                        REG16(AX) = fd;
                   13145:                                        REG16(CX) = 1;
1.1.1.45  root     13146:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     13147:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13148:                                } else {
                   13149:                                        REG16(AX) = errno;
1.1.1.3   root     13150:                                        m_CF = 1;
1.1       root     13151:                                }
                   13152:                        } else if(REG8(DL) & 2) {
1.1.1.60  root     13153:                                int attr = GetFileAttributesA(path);
1.1.1.37  root     13154:                                int fd = -1;
                   13155:                                int sio_port = 0;
                   13156:                                int lpt_port = 0;
1.1       root     13157:                                
1.1.1.45  root     13158:                                if(msdos_is_device_path(path)) {
                   13159:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     13160:                                } else {
                   13161:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   13162:                                }
                   13163:                                if(fd != -1) {
                   13164:                                        if(attr == -1) {
                   13165:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   13166:                                        }
1.1.1.60  root     13167:                                        SetFileAttributesA(path, attr);
1.1       root     13168:                                        REG16(AX) = fd;
                   13169:                                        REG16(CX) = 3;
1.1.1.45  root     13170:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     13171:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13172:                                } else {
                   13173:                                        REG16(AX) = errno;
1.1.1.3   root     13174:                                        m_CF = 1;
1.1       root     13175:                                }
                   13176:                        } else {
                   13177:                                REG16(AX) = 0x50;
1.1.1.3   root     13178:                                m_CF = 1;
1.1       root     13179:                        }
                   13180:                } else {
                   13181:                        // file not exists
                   13182:                        if(REG8(DL) & 0x10) {
                   13183:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   13184:                                
                   13185:                                if(fd != -1) {
1.1.1.60  root     13186:                                        SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     13187:                                        REG16(AX) = fd;
                   13188:                                        REG16(CX) = 2;
                   13189:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     13190:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13191:                                } else {
                   13192:                                        REG16(AX) = errno;
1.1.1.3   root     13193:                                        m_CF = 1;
1.1       root     13194:                                }
                   13195:                        } else {
                   13196:                                REG16(AX) = 0x02;
1.1.1.3   root     13197:                                m_CF = 1;
1.1       root     13198:                        }
                   13199:                }
                   13200:        } else {
                   13201:                REG16(AX) = 0x0c;
1.1.1.3   root     13202:                m_CF = 1;
1.1       root     13203:        }
                   13204: }
                   13205: 
1.1.1.43  root     13206: inline void msdos_int_21h_70h()
                   13207: {
                   13208:        switch(REG8(AL)) {
1.1.1.48  root     13209:        case 0x00: // get ??? info
                   13210:        case 0x01: // set above info
                   13211: //             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));
                   13212:                REG16(AX) = 0x7000;
                   13213:                m_CF = 1;
                   13214:                break;
                   13215:        case 0x02: // set general internationalization info
1.1.1.43  root     13216:                if(REG16(CX) >= 7) {
                   13217:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   13218:                        msdos_nls_tables_update();
                   13219:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   13220:                        REG16(AX) = system_code_page;
                   13221:                } else {
                   13222:                        REG16(AX) = 0x0c;
                   13223:                        m_CF = 1;
                   13224:                }
                   13225:                break;
                   13226:        default:
                   13227:                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     13228:                REG16(AX) = 0x7000;
1.1.1.43  root     13229:                m_CF = 1;
                   13230:                break;
                   13231:        }
                   13232: }
                   13233: 
1.1       root     13234: inline void msdos_int_21h_710dh()
                   13235: {
                   13236:        // reset drive
                   13237: }
                   13238: 
1.1.1.48  root     13239: inline void msdos_int_21h_7141h()
1.1.1.17  root     13240: {
                   13241:        if(REG16(SI) == 0) {
1.1.1.48  root     13242:                msdos_int_21h_41h(1);
1.1.1.17  root     13243:                return;
                   13244:        }
                   13245:        if(REG16(SI) != 1) {
                   13246:                REG16(AX) = 5;
                   13247:                m_CF = 1;
                   13248:        }
                   13249:        /* wild card and matching attributes... */
                   13250:        char tmp[MAX_PATH * 2];
                   13251:        // copy search pathname (and quick check overrun)
                   13252:        ZeroMemory(tmp, sizeof(tmp));
                   13253:        tmp[MAX_PATH - 1] = '\0';
                   13254:        tmp[MAX_PATH] = 1;
                   13255:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   13256:        
                   13257:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   13258:                REG16(AX) = 1;
                   13259:                m_CF = 1;
                   13260:                return;
                   13261:        }
                   13262:        for(char *s = tmp; *s; ++s) {
                   13263:                if(*s == '/') {
                   13264:                        *s = '\\';
                   13265:                }
                   13266:        }
1.1.1.60  root     13267:        char *tmp_name = my_strrchr(tmp, '\\');
1.1.1.17  root     13268:        if(tmp_name) {
                   13269:                ++tmp_name;
                   13270:        } else {
                   13271:                tmp_name = strchr(tmp, ':');
                   13272:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   13273:        }
                   13274:        
                   13275:        WIN32_FIND_DATAA fd;
                   13276:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   13277:        if(fh == INVALID_HANDLE_VALUE) {
                   13278:                REG16(AX) = 2;
                   13279:                m_CF = 1;
                   13280:                return;
                   13281:        }
                   13282:        do {
                   13283:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   13284:                        strcpy(tmp_name, fd.cFileName);
1.1.1.48  root     13285:                        if(remove(msdos_trimmed_path(tmp, 1))) {
1.1.1.17  root     13286:                                REG16(AX) = 5;
                   13287:                                m_CF = 1;
                   13288:                                break;
                   13289:                        }
                   13290:                }
                   13291:        } while(FindNextFileA(fh, &fd));
                   13292:        if(!m_CF) {
                   13293:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   13294:                        m_CF = 1;
                   13295:                        REG16(AX) = 2;
                   13296:                }
                   13297:        }
                   13298:        FindClose(fh);
                   13299: }
                   13300: 
1.1       root     13301: inline void msdos_int_21h_714eh()
                   13302: {
                   13303:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13304:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   13305:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.60  root     13306:        WIN32_FIND_DATAA fd;
1.1       root     13307:        
1.1.1.13  root     13308:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   13309:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13310:                FindClose(dtainfo->find_handle);
                   13311:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13312:        }
                   13313:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     13314:        dtainfo->allowable_mask = REG8(CL);
                   13315:        dtainfo->required_mask = REG8(CH);
                   13316:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     13317:        
1.1.1.14  root     13318:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   13319:                dtainfo->allowable_mask &= ~8;
1.1       root     13320:        }
1.1.1.60  root     13321:        if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14  root     13322:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.60  root     13323:                        if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     13324:                                FindClose(dtainfo->find_handle);
                   13325:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13326:                                break;
                   13327:                        }
                   13328:                }
                   13329:        }
1.1.1.13  root     13330:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13331:                find->attrib = fd.dwFileAttributes;
                   13332:                msdos_find_file_conv_local_time(&fd);
                   13333:                if(REG16(SI) == 0) {
                   13334:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13335:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13336:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13337:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13338:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13339:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13340:                } else {
                   13341:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13342:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13343:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13344:                }
                   13345:                find->size_hi = fd.nFileSizeHigh;
                   13346:                find->size_lo = fd.nFileSizeLow;
                   13347:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13348:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13349:                REG16(AX) = dtainfo - dtalist + 1;
                   13350:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13351:                // volume label
                   13352:                find->attrib = 8;
                   13353:                find->size_hi = find->size_lo = 0;
                   13354:                strcpy(find->full_name, process->volume_label);
                   13355:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13356:                dtainfo->allowable_mask &= ~8;
                   13357:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     13358:        } else {
                   13359:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     13360:                m_CF = 1;
1.1       root     13361:        }
                   13362: }
                   13363: 
                   13364: inline void msdos_int_21h_714fh()
                   13365: {
                   13366:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13367:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1.1.60  root     13368:        WIN32_FIND_DATAA fd;
1.1       root     13369:        
1.1.1.14  root     13370:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13371:                REG16(AX) = 6;
1.1.1.13  root     13372:                m_CF = 1;
                   13373:                return;
                   13374:        }
1.1.1.14  root     13375:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13376:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60  root     13377:                if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14  root     13378:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.60  root     13379:                                if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     13380:                                        FindClose(dtainfo->find_handle);
                   13381:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13382:                                        break;
                   13383:                                }
                   13384:                        }
                   13385:                } else {
1.1.1.13  root     13386:                        FindClose(dtainfo->find_handle);
                   13387:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13388:                }
                   13389:        }
1.1.1.13  root     13390:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13391:                find->attrib = fd.dwFileAttributes;
                   13392:                msdos_find_file_conv_local_time(&fd);
                   13393:                if(REG16(SI) == 0) {
                   13394:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13395:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13396:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13397:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13398:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13399:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13400:                } else {
                   13401:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13402:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13403:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13404:                }
                   13405:                find->size_hi = fd.nFileSizeHigh;
                   13406:                find->size_lo = fd.nFileSizeLow;
                   13407:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13408:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13409:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13410:                // volume label
                   13411:                find->attrib = 8;
                   13412:                find->size_hi = find->size_lo = 0;
                   13413:                strcpy(find->full_name, process->volume_label);
                   13414:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13415:                dtainfo->allowable_mask &= ~8;
1.1       root     13416:        } else {
                   13417:                REG16(AX) = 0x12;
1.1.1.3   root     13418:                m_CF = 1;
1.1       root     13419:        }
                   13420: }
                   13421: 
                   13422: inline void msdos_int_21h_71a0h()
                   13423: {
                   13424:        DWORD max_component_len, file_sys_flag;
                   13425:        
1.1.1.60  root     13426:        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     13427:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   13428:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     13429:                REG16(CX) = (UINT16)max_component_len;          // 255
                   13430:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   13431:        } else {
                   13432:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13433:                m_CF = 1;
1.1       root     13434:        }
                   13435: }
                   13436: 
                   13437: inline void msdos_int_21h_71a1h()
                   13438: {
1.1.1.14  root     13439:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13440:                REG16(AX) = 6;
1.1.1.13  root     13441:                m_CF = 1;
                   13442:                return;
                   13443:        }
1.1.1.14  root     13444:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13445:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13446:                FindClose(dtainfo->find_handle);
                   13447:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13448:        }
                   13449: }
                   13450: 
                   13451: inline void msdos_int_21h_71a6h()
                   13452: {
                   13453:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     13454:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13455:        
1.1.1.3   root     13456:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13457:        struct _stat64 status;
                   13458:        DWORD serial_number = 0;
                   13459:        
1.1.1.20  root     13460:        if(fd < process->max_files && file_handler[fd].valid) {
                   13461:                if(_fstat64(fd, &status) == 0) {
                   13462:                        if(file_handler[fd].path[1] == ':') {
1.1       root     13463:                                // NOTE: we need to consider the network file path "\\host\share\"
                   13464:                                char volume[] = "A:\\";
1.1.1.20  root     13465:                                volume[0] = file_handler[fd].path[1];
1.1.1.60  root     13466:                                GetVolumeInformationA(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
1.1       root     13467:                        }
1.1.1.60  root     13468:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributesA(file_handler[fd].path);
1.1       root     13469:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   13470:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   13471:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   13472:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   13473:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   13474:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   13475:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   13476:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   13477:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   13478:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     13479:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     13480:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     13481:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     13482:                } else {
                   13483:                        REG16(AX) = errno;
1.1.1.3   root     13484:                        m_CF = 1;
1.1       root     13485:                }
                   13486:        } else {
                   13487:                REG16(AX) = 0x06;
1.1.1.3   root     13488:                m_CF = 1;
1.1       root     13489:        }
                   13490: }
                   13491: 
                   13492: inline void msdos_int_21h_71a7h()
                   13493: {
                   13494:        switch(REG8(BL)) {
                   13495:        case 0x00:
1.1.1.3   root     13496:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     13497:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13498:                        m_CF = 1;
1.1       root     13499:                }
                   13500:                break;
                   13501:        case 0x01:
                   13502:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     13503:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     13504:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13505:                        m_CF = 1;
1.1       root     13506:                }
                   13507:                break;
                   13508:        default:
1.1.1.22  root     13509:                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     13510:                REG16(AX) = 0x7100;
1.1.1.3   root     13511:                m_CF = 1;
1.1       root     13512:                break;
                   13513:        }
                   13514: }
                   13515: 
                   13516: inline void msdos_int_21h_71a8h()
                   13517: {
                   13518:        if(REG8(DH) == 0) {
                   13519:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     13520:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13521:                memset(fcb, 0x20, sizeof(fcb));
                   13522:                int len = strlen(tmp);
1.1.1.21  root     13523:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     13524:                        if(tmp[i] == '.') {
                   13525:                                pos = 8;
                   13526:                        } else {
                   13527:                                if(msdos_lead_byte_check(tmp[i])) {
                   13528:                                        fcb[pos++] = tmp[i++];
                   13529:                                }
                   13530:                                fcb[pos++] = tmp[i];
                   13531:                        }
                   13532:                }
1.1.1.3   root     13533:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     13534:        } else {
1.1.1.3   root     13535:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13536:        }
                   13537: }
                   13538: 
1.1.1.22  root     13539: inline void msdos_int_21h_71aah()
                   13540: {
                   13541:        char drv[] = "A:", path[MAX_PATH];
                   13542:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13543:        
                   13544:        if(REG8(BL) == 0) {
                   13545:                drv[0] = 'A' + _getdrive() - 1;
                   13546:        } else {
                   13547:                drv[0] = 'A' + REG8(BL) - 1;
                   13548:        }
                   13549:        switch(REG8(BH)) {
                   13550:        case 0x00:
1.1.1.44  root     13551:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13552:                        REG16(AX) = 0x0f; // invalid drive
                   13553:                        m_CF = 1;
1.1.1.60  root     13554:                } else if(DefineDosDeviceA(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
1.1.1.44  root     13555:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     13556:                        m_CF = 1;
                   13557:                }
                   13558:                break;
                   13559:        case 0x01:
1.1.1.44  root     13560:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13561:                        REG16(AX) = 0x0f; // invalid drive
                   13562:                        m_CF = 1;
1.1.1.60  root     13563:                } else if(DefineDosDeviceA(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     13564:                        REG16(AX) = 0x0f; // invalid drive
                   13565:                        m_CF = 1;
                   13566:                }
                   13567:                break;
                   13568:        case 0x02:
1.1.1.44  root     13569:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13570:                        REG16(AX) = 0x0f; // invalid drive
                   13571:                        m_CF = 1;
1.1.1.60  root     13572:                } else if(QueryDosDeviceA(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     13573:                        REG16(AX) = 0x0f; // invalid drive
                   13574:                        m_CF = 1;
                   13575:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   13576:                        REG16(AX) = 0x0f; // invalid drive
                   13577:                        m_CF = 1;
                   13578:                } else {
                   13579:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   13580:                }
                   13581:                break;
                   13582:        default:
                   13583:                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     13584:                REG16(AX) = 0x7100;
1.1.1.22  root     13585:                m_CF = 1;
                   13586:                break;
                   13587:        }
                   13588: }
                   13589: 
1.1.1.14  root     13590: inline void msdos_int_21h_7300h()
                   13591: {
1.1.1.44  root     13592:        REG8(AL) = REG8(CL);
                   13593:        REG8(AH) = 0;
1.1.1.14  root     13594: }
                   13595: 
                   13596: inline void msdos_int_21h_7302h()
                   13597: {
                   13598:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   13599:        UINT16 seg, ofs;
                   13600:        
                   13601:        if(REG16(CX) < 0x3f) {
                   13602:                REG8(AL) = 0x18;
                   13603:                m_CF = 1;
                   13604:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   13605:                REG8(AL) = 0xff;
                   13606:                m_CF = 1;
                   13607:        } else {
                   13608:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   13609:        }
                   13610: }
                   13611: 
1.1       root     13612: inline void msdos_int_21h_7303h()
                   13613: {
1.1.1.3   root     13614:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13615:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     13616:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   13617:        
1.1.1.60  root     13618:        if(GetDiskFreeSpaceA(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
1.1       root     13619:                info->size_of_structure = sizeof(ext_space_info_t);
                   13620:                info->structure_version = 0;
                   13621:                info->sectors_per_cluster = sectors_per_cluster;
                   13622:                info->bytes_per_sector = bytes_per_sector;
                   13623:                info->available_clusters_on_drive = free_clusters;
                   13624:                info->total_clusters_on_drive = total_clusters;
                   13625:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   13626:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   13627:                info->available_allocation_units = free_clusters;       // ???
                   13628:                info->total_allocation_units = total_clusters;          // ???
                   13629:        } else {
                   13630:                REG16(AX) = errno;
1.1.1.3   root     13631:                m_CF = 1;
1.1       root     13632:        }
                   13633: }
                   13634: 
1.1.1.30  root     13635: inline void msdos_int_21h_dbh()
                   13636: {
                   13637:        // Novell NetWare - Workstation - Get Number of Local Drives
                   13638:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   13639:        REG8(AL) = dos_info->last_drive;
                   13640: }
                   13641: 
                   13642: inline void msdos_int_21h_dch()
                   13643: {
                   13644:        // Novell NetWare - Connection Services - Get Connection Number
                   13645:        REG8(AL) = 0x00;
                   13646: }
                   13647: 
1.1.1.32  root     13648: inline void msdos_int_24h()
                   13649: {
                   13650:        const char *message = NULL;
                   13651:        int key = 0;
                   13652:        
                   13653:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   13654:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   13655:                        if(active_code_page == 932) {
                   13656:                                message = critical_error_table[i].message_japanese;
                   13657:                        }
                   13658:                        if(message == NULL) {
                   13659:                                message = critical_error_table[i].message_english;
                   13660:                        }
                   13661:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   13662:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   13663:                        
                   13664:                        SREG(ES) = WORK_TOP >> 4;
                   13665:                        i386_load_segment_descriptor(ES);
                   13666:                        REG16(DI) = 0x0000;
                   13667:                        break;
                   13668:                }
                   13669:        }
                   13670:        fprintf(stderr, "\n%s", message);
                   13671:        if(!(REG8(AH) & 0x80)) {
                   13672:                if(REG8(AH) & 0x01) {
                   13673:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   13674:                } else {
                   13675:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   13676:                }
                   13677:        }
                   13678:        fprintf(stderr, "\n");
                   13679:        
1.1.1.33  root     13680:        {
1.1.1.32  root     13681:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     13682:        }
1.1.1.32  root     13683:        if(REG8(AH) & 0x10) {
                   13684:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   13685:        }
                   13686:        if(REG8(AH) & 0x20) {
                   13687:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   13688:        }
                   13689:        if(REG8(AH) & 0x08) {
                   13690:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   13691:        }
                   13692:        fprintf(stderr, "? ");
                   13693:        
                   13694:        while(1) {
                   13695:                while(!_kbhit()) {
                   13696:                        Sleep(10);
                   13697:                }
                   13698:                key = _getch();
                   13699:                
                   13700:                if(key == 'I' || key == 'i') {
                   13701:                        if(REG8(AH) & 0x20) {
                   13702:                                REG8(AL) = 0;
                   13703:                                break;
                   13704:                        }
                   13705:                } else if(key == 'R' || key == 'r') {
                   13706:                        if(REG8(AH) & 0x10) {
                   13707:                                REG8(AL) = 1;
                   13708:                                break;
                   13709:                        }
                   13710:                } else if(key == 'A' || key == 'a') {
                   13711:                        REG8(AL) = 2;
                   13712:                        break;
                   13713:                } else if(key == 'F' || key == 'f') {
                   13714:                        if(REG8(AH) & 0x08) {
                   13715:                                REG8(AL) = 3;
                   13716:                                break;
                   13717:                        }
                   13718:                }
                   13719:        }
                   13720:        fprintf(stderr, "%c\n", key);
                   13721: }
                   13722: 
1.1       root     13723: inline void msdos_int_25h()
                   13724: {
                   13725:        UINT16 seg, ofs;
                   13726:        DWORD dwSize;
                   13727:        
1.1.1.3   root     13728: #if defined(HAS_I386)
                   13729:        I386OP(pushf)();
                   13730: #else
                   13731:        PREFIX86(_pushf());
                   13732: #endif
1.1       root     13733:        
                   13734:        if(!(REG8(AL) < 26)) {
                   13735:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13736:                m_CF = 1;
1.1       root     13737:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13738:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13739:                m_CF = 1;
1.1       root     13740:        } else {
                   13741:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13742:                char dev[64];
                   13743:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13744:                
1.1.1.60  root     13745:                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     13746:                if(hFile == INVALID_HANDLE_VALUE) {
                   13747:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13748:                        m_CF = 1;
1.1       root     13749:                } else {
1.1.1.19  root     13750:                        UINT32 top_sector  = REG16(DX);
                   13751:                        UINT16 sector_num  = REG16(CX);
                   13752:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13753:                        
                   13754:                        if(sector_num == 0xffff) {
                   13755:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13756:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13757:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13758:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13759:                                buffer_addr = (seg << 4) + ofs;
                   13760:                        }
                   13761: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13762: //                             REG8(AL) = 0x02; // drive not ready
                   13763: //                             m_CF = 1;
                   13764: //                     } else 
                   13765:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13766:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13767:                                m_CF = 1;
1.1.1.19  root     13768:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13769:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     13770:                                m_CF = 1;
1.1       root     13771:                        }
                   13772:                        CloseHandle(hFile);
                   13773:                }
                   13774:        }
                   13775: }
                   13776: 
                   13777: inline void msdos_int_26h()
                   13778: {
1.1.1.42  root     13779:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     13780:        UINT16 seg, ofs;
                   13781:        DWORD dwSize;
                   13782:        
1.1.1.3   root     13783: #if defined(HAS_I386)
                   13784:        I386OP(pushf)();
                   13785: #else
                   13786:        PREFIX86(_pushf());
                   13787: #endif
1.1       root     13788:        
                   13789:        if(!(REG8(AL) < 26)) {
                   13790:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13791:                m_CF = 1;
1.1       root     13792:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13793:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13794:                m_CF = 1;
1.1       root     13795:        } else {
                   13796:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13797:                char dev[64];
                   13798:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13799:                
                   13800:                if(dpb->media_type == 0xf8) {
                   13801:                        // this drive is not a floppy
1.1.1.6   root     13802: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   13803: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   13804: //                     }
1.1       root     13805:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13806:                        m_CF = 1;
1.1       root     13807:                } else {
1.1.1.60  root     13808:                        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     13809:                        if(hFile == INVALID_HANDLE_VALUE) {
                   13810:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13811:                                m_CF = 1;
1.1       root     13812:                        } else {
1.1.1.19  root     13813:                                UINT32 top_sector  = REG16(DX);
                   13814:                                UINT16 sector_num  = REG16(CX);
                   13815:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13816:                                
                   13817:                                if(sector_num == 0xffff) {
                   13818:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13819:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13820:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13821:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13822:                                        buffer_addr = (seg << 4) + ofs;
                   13823:                                }
1.1       root     13824:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13825:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13826:                                        m_CF = 1;
1.1.1.19  root     13827:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13828:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13829:                                        m_CF = 1;
1.1.1.19  root     13830:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13831:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     13832:                                        m_CF = 1;
1.1       root     13833:                                }
                   13834:                                CloseHandle(hFile);
                   13835:                        }
                   13836:                }
                   13837:        }
                   13838: }
                   13839: 
                   13840: inline void msdos_int_27h()
                   13841: {
1.1.1.29  root     13842:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   13843:        try {
                   13844:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13845:        } catch(...) {
                   13846:                // recover the broken mcb
                   13847:                int mcb_seg = SREG(CS) - 1;
                   13848:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     13849:                
1.1.1.29  root     13850:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     13851:                        mcb->mz = 'M';
                   13852:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   13853:                        
1.1.1.29  root     13854:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     13855:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     13856:                        } else {
1.1.1.39  root     13857:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     13858:                        }
                   13859:                } else {
                   13860:                        mcb->mz = 'Z';
1.1.1.30  root     13861:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     13862:                }
                   13863:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13864:        }
1.1.1.3   root     13865:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     13866: }
                   13867: 
                   13868: inline void msdos_int_29h()
                   13869: {
1.1.1.50  root     13870:        msdos_putch_fast(REG8(AL));
1.1       root     13871: }
                   13872: 
                   13873: inline void msdos_int_2eh()
                   13874: {
                   13875:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   13876:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13877:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     13878:        char *token = my_strtok(tmp, " ");
                   13879:        strcpy(command, token);
                   13880:        strcpy(opt, token + strlen(token) + 1);
                   13881:        
                   13882:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   13883:        param->env_seg = 0;
                   13884:        param->cmd_line.w.l = 44;
                   13885:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13886:        param->fcb1.w.l = 24;
                   13887:        param->fcb1.w.h = (WORK_TOP >> 4);
                   13888:        param->fcb2.w.l = 24;
                   13889:        param->fcb2.w.h = (WORK_TOP >> 4);
                   13890:        
                   13891:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   13892:        
                   13893:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   13894:        cmd_line->len = strlen(opt);
                   13895:        strcpy(cmd_line->cmd, opt);
                   13896:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   13897:        
1.1.1.28  root     13898:        try {
                   13899:                if(msdos_process_exec(command, param, 0)) {
                   13900:                        REG16(AX) = 0xffff; // error before processing command
                   13901:                } else {
                   13902:                        // set flag to set retval to ax when the started process is terminated
                   13903:                        process_t *process = msdos_process_info_get(current_psp);
                   13904:                        process->called_by_int2eh = true;
                   13905:                }
                   13906:        } catch(...) {
                   13907:                REG16(AX) = 0xffff; // error before processing command
                   13908:        }
1.1       root     13909: }
                   13910: 
1.1.1.29  root     13911: inline void msdos_int_2fh_05h()
                   13912: {
                   13913:        switch(REG8(AL)) {
                   13914:        case 0x00:
1.1.1.49  root     13915:                // critical error handler is installed
1.1.1.32  root     13916:                REG8(AL) = 0xff;
                   13917:                break;
                   13918:        case 0x01:
                   13919:        case 0x02:
                   13920:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   13921:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   13922:                                const char *message = NULL;
                   13923:                                if(active_code_page == 932) {
                   13924:                                        message = standard_error_table[i].message_japanese;
                   13925:                                }
                   13926:                                if(message == NULL) {
                   13927:                                        message = standard_error_table[i].message_english;
                   13928:                                }
                   13929:                                strcpy((char *)(mem + WORK_TOP), message);
                   13930:                                
                   13931:                                SREG(ES) = WORK_TOP >> 4;
                   13932:                                i386_load_segment_descriptor(ES);
                   13933:                                REG16(DI) = 0x0000;
                   13934:                                REG8(AL) = 0x01;
                   13935:                                break;
                   13936:                        }
                   13937:                }
1.1.1.29  root     13938:                break;
                   13939:        default:
                   13940:                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     13941:                REG16(AX) = 0x01;
1.1.1.29  root     13942:                m_CF = 1;
                   13943:        }
                   13944: }
                   13945: 
1.1.1.44  root     13946: inline void msdos_int_2fh_06h()
                   13947: {
                   13948:        switch(REG8(AL)) {
                   13949:        case 0x00:
                   13950:                // ASSIGN is not installed
1.1.1.49  root     13951: //             REG8(AL) = 0x00;
1.1.1.44  root     13952:                break;
                   13953:        case 0x01:
                   13954:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   13955:                REG16(AX) = 0x01;
                   13956:                m_CF = 1;
                   13957:                break;
                   13958:        default:
                   13959:                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));
                   13960:                REG16(AX) = 0x01;
                   13961:                m_CF = 1;
                   13962:                break;
                   13963:        }
                   13964: }
                   13965: 
1.1.1.22  root     13966: inline void msdos_int_2fh_11h()
                   13967: {
                   13968:        switch(REG8(AL)) {
                   13969:        case 0x00:
1.1.1.29  root     13970:                if(i386_read_stack() == 0xdada) {
1.1.1.53  root     13971: #ifdef SUPPORT_MSCDEX
                   13972:                        // MSCDEX is installed
                   13973:                        REG8(AL) = 0xff;
                   13974:                        i386_write_stack(0xadad);
                   13975: #else
1.1.1.29  root     13976:                        // MSCDEX is not installed
                   13977: //                     REG8(AL) = 0x00;
1.1.1.53  root     13978: #endif
1.1.1.29  root     13979:                } else {
                   13980:                        // Network Redirector is not installed
                   13981: //                     REG8(AL) = 0x00;
                   13982:                }
1.1.1.22  root     13983:                break;
                   13984:        default:
1.1.1.43  root     13985: //             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     13986:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     13987:                m_CF = 1;
                   13988:                break;
                   13989:        }
                   13990: }
                   13991: 
1.1.1.21  root     13992: inline void msdos_int_2fh_12h()
                   13993: {
                   13994:        switch(REG8(AL)) {
1.1.1.22  root     13995:        case 0x00:
1.1.1.29  root     13996:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     13997:                REG8(AL) = 0xff;
                   13998:                break;
1.1.1.29  root     13999: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   14000:        case 0x02:
                   14001:                {
                   14002:                        UINT16 stack = i386_read_stack();
                   14003:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   14004:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   14005:                        i386_load_segment_descriptor(ES);
                   14006:                }
                   14007:                break;
1.1.1.30  root     14008:        case 0x03:
                   14009:                SREG(DS) = (DEVICE_TOP >> 4);
                   14010:                i386_load_segment_descriptor(DS);
                   14011:                break;
1.1.1.29  root     14012:        case 0x04:
                   14013:                {
                   14014:                        UINT16 stack = i386_read_stack();
                   14015:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   14016: #if defined(HAS_I386)
                   14017:                        m_ZF = (REG8(AL) == '\\');
                   14018: #else
                   14019:                        m_ZeroVal = (REG8(AL) != '\\');
                   14020: #endif
                   14021:                }
                   14022:                break;
                   14023:        case 0x05:
1.1.1.49  root     14024:                {
                   14025:                        UINT16 c = i386_read_stack();
                   14026:                        if((c >> 0) & 0xff) {
                   14027:                                msdos_putch((c >> 0) & 0xff);
                   14028:                        }
                   14029:                        if((c >> 8) & 0xff) {
                   14030:                                msdos_putch((c >> 8) & 0xff);
                   14031:                        }
                   14032:                }
1.1.1.29  root     14033:                break;
1.1.1.49  root     14034: //     case 0x06: // DOS 3.0+ internal - Invoke Critical Error
1.1.1.29  root     14035: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   14036: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
1.1.1.49  root     14037: //     case 0x09: // DOS 3.0+ internal - Flush and Free Disk Buffer
1.1.1.29  root     14038: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   14039: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   14040: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   14041:        case 0x0d:
                   14042:                {
                   14043:                        SYSTEMTIME time;
                   14044:                        FILETIME file_time;
                   14045:                        WORD dos_date, dos_time;
                   14046:                        GetLocalTime(&time);
                   14047:                        SystemTimeToFileTime(&time, &file_time);
                   14048:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   14049:                        REG16(AX) = dos_date;
                   14050:                        REG16(DX) = dos_time;
                   14051:                }
                   14052:                break;
                   14053: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   14054: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   14055: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   14056:        case 0x11:
                   14057:                {
                   14058:                        char path[MAX_PATH], *p;
                   14059:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   14060:                        my_strupr(path);
                   14061:                        while((p = my_strchr(path, '/')) != NULL) {
                   14062:                                *p = '\\';
                   14063:                        }
                   14064:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   14065:                }
                   14066:                break;
                   14067:        case 0x12:
                   14068:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   14069:                break;
                   14070:        case 0x13:
                   14071:                {
                   14072:                        char tmp[2] = {0};
                   14073:                        tmp[0] = i386_read_stack();
                   14074:                        my_strupr(tmp);
                   14075:                        REG8(AL) = tmp[0];
                   14076:                }
                   14077:                break;
                   14078:        case 0x14:
                   14079: #if defined(HAS_I386)
                   14080:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   14081: #else
                   14082:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   14083: #endif
                   14084:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   14085:                break;
                   14086: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     14087:        case 0x16:
                   14088:                if(REG16(BX) < 20) {
                   14089:                        SREG(ES) = SFT_TOP >> 4;
                   14090:                        i386_load_segment_descriptor(ES);
                   14091:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   14092:                        
                   14093:                        // update system file table
                   14094:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   14095:                        if(file_handler[REG16(BX)].valid) {
                   14096:                                int count = 0;
                   14097:                                for(int i = 0; i < 20; i++) {
                   14098:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   14099:                                                count++;
                   14100:                                        }
                   14101:                                }
                   14102:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   14103:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   14104:                                _lseek(REG16(BX), 0, SEEK_END);
                   14105:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   14106:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   14107:                        } else {
                   14108:                                memset(sft, 0, 0x3b);
                   14109:                        }
                   14110:                } else {
                   14111:                        REG16(AX) = 0x06;
                   14112:                        m_CF = 1;
                   14113:                }
                   14114:                break;
1.1.1.49  root     14115:        case 0x17:
                   14116:                {
                   14117:                        UINT16 drive = i386_read_stack();
                   14118:                        if(msdos_is_valid_drive(drive)) {
                   14119:                                msdos_cds_update(drive);
                   14120:                        }
                   14121:                        REG16(SI) = 88 * drive;
                   14122:                        SREG(DS) = (CDS_TOP >> 4);
                   14123:                        i386_load_segment_descriptor(DS);
                   14124:                }
                   14125:                break;
1.1.1.29  root     14126: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   14127: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   14128:        case 0x1a:
                   14129:                {
                   14130:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   14131:                        if(path[1] == ':') {
                   14132:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   14133:                                        REG8(AL) = path[0] - 'a' + 1;
                   14134:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   14135:                                        REG8(AL) = path[0] - 'A' + 1;
                   14136:                                } else {
                   14137:                                        REG8(AL) = 0xff; // invalid
                   14138:                                }
                   14139:                                strcpy(full, path);
                   14140:                                strcpy(path, full + 2);
1.1.1.60  root     14141:                        } else if(GetFullPathNameA(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
1.1.1.29  root     14142:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   14143:                                        REG8(AL) = full[0] - 'a' + 1;
                   14144:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   14145:                                        REG8(AL) = full[0] - 'A' + 1;
                   14146:                                } else {
                   14147:                                        REG8(AL) = 0xff; // invalid
                   14148:                                }
                   14149:                        } else {
                   14150:                                REG8(AL) = 0x00; // default
                   14151:                        }
                   14152:                }
                   14153:                break;
                   14154:        case 0x1b:
                   14155:                {
                   14156:                        int year = REG16(CX) + 1980;
                   14157:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   14158:                }
                   14159:                break;
                   14160: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   14161: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   14162:        case 0x1e:
                   14163:                {
                   14164:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   14165:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
1.1.1.60  root     14166:                        if(GetFullPathNameA(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathNameA(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
1.1.1.29  root     14167: #if defined(HAS_I386)
                   14168:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   14169: #else
                   14170:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   14171: #endif
                   14172:                        } else {
                   14173: #if defined(HAS_I386)
                   14174:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   14175: #else
                   14176:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   14177: #endif
                   14178:                        }
                   14179:                }
                   14180:                break;
1.1.1.49  root     14181:        case 0x1f:
                   14182:                {
                   14183:                        UINT16 drive = i386_read_stack();
                   14184:                        if(msdos_is_valid_drive(drive)) {
                   14185:                                msdos_cds_update(drive);
                   14186:                        }
                   14187:                        REG16(SI) = 88 * drive;
                   14188:                        SREG(ES) = (CDS_TOP >> 4);
                   14189:                        i386_load_segment_descriptor(ES);
                   14190:                }
                   14191:                break;
1.1.1.21  root     14192:        case 0x20:
                   14193:                {
                   14194:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   14195:                        
                   14196:                        if(fd < 20) {
                   14197:                                SREG(ES) = current_psp;
                   14198:                                i386_load_segment_descriptor(ES);
                   14199:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   14200:                        } else {
                   14201:                                REG16(AX) = 0x06;
                   14202:                                m_CF = 1;
                   14203:                        }
                   14204:                }
                   14205:                break;
1.1.1.29  root     14206:        case 0x21:
                   14207:                msdos_int_21h_60h(0);
                   14208:                break;
1.1.1.49  root     14209:        case 0x22:
                   14210:                {
                   14211:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14212:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00) != 0xff) {
                   14213:                                sda->extended_error_code = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00);
                   14214:                        }
                   14215:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01) != 0xff) {
                   14216:                                sda->error_class         = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01);
                   14217:                        }
                   14218:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02) != 0xff) {
                   14219:                                sda->suggested_action    = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02);
                   14220:                        }
                   14221:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03) != 0xff) {
                   14222:                                sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03);
                   14223:                        }
                   14224:                }
                   14225:                break;
1.1.1.29  root     14226: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   14227: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   14228:        case 0x25:
                   14229:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   14230:                break;
                   14231:        case 0x26:
                   14232:                REG8(AL) = REG8(CL);
                   14233:                msdos_int_21h_3dh();
                   14234:                break;
                   14235:        case 0x27:
                   14236:                msdos_int_21h_3eh();
                   14237:                break;
                   14238:        case 0x28:
                   14239:                REG16(AX) = REG16(BP);
                   14240:                msdos_int_21h_42h();
                   14241:                break;
                   14242:        case 0x29:
                   14243:                msdos_int_21h_3fh();
                   14244:                break;
                   14245: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   14246:        case 0x2b:
                   14247:                REG16(AX) = REG16(BP);
                   14248:                msdos_int_21h_44h();
                   14249:                break;
                   14250:        case 0x2c:
                   14251:                REG16(BX) = DEVICE_TOP >> 4;
                   14252:                REG16(AX) = 22;
                   14253:                break;
                   14254:        case 0x2d:
                   14255:                {
                   14256:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14257:                        REG16(AX) = sda->extended_error_code;
                   14258:                }
                   14259:                break;
                   14260:        case 0x2e:
                   14261:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     14262:                        SREG(ES) = 0x0001;
                   14263:                        i386_load_segment_descriptor(ES);
                   14264:                        REG16(DI) = 0x00;
                   14265:                } else if(REG8(DL) == 0x08) {
1.1.1.49  root     14266:                        // dummy parameter error message read routine is at fffc:0010
                   14267:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.22  root     14268:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     14269:                        REG16(DI) = 0x0010;
1.1.1.22  root     14270:                }
                   14271:                break;
1.1.1.29  root     14272:        case 0x2f:
                   14273:                if(REG16(DX) != 0) {
1.1.1.30  root     14274:                        dos_major_version = REG8(DL);
                   14275:                        dos_minor_version = REG8(DH);
1.1.1.29  root     14276:                } else {
                   14277:                        REG8(DL) = 7;
                   14278:                        REG8(DH) = 10;
                   14279:                }
                   14280:                break;
                   14281: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   14282: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     14283:        default:
                   14284:                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));
                   14285:                REG16(AX) = 0x01;
                   14286:                m_CF = 1;
                   14287:                break;
                   14288:        }
                   14289: }
                   14290: 
1.1.1.30  root     14291: inline void msdos_int_2fh_13h()
                   14292: {
                   14293:        static UINT16 prevDS = 0, prevDX = 0;
                   14294:        static UINT16 prevES = 0, prevBX = 0;
                   14295:        UINT16 tmp;
                   14296:        
                   14297:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   14298:        i386_load_segment_descriptor(DS);
                   14299:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   14300:        
                   14301:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   14302:        i386_load_segment_descriptor(ES);
                   14303:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   14304: }
                   14305: 
1.1.1.22  root     14306: inline void msdos_int_2fh_14h()
                   14307: {
                   14308:        switch(REG8(AL)) {
                   14309:        case 0x00:
1.1.1.29  root     14310:                // NLSFUNC.COM is installed
                   14311:                REG8(AL) = 0xff;
1.1.1.25  root     14312:                break;
                   14313:        case 0x01:
                   14314:        case 0x03:
                   14315:                REG8(AL) = 0x00;
                   14316:                active_code_page = REG16(BX);
                   14317:                msdos_nls_tables_update();
                   14318:                break;
                   14319:        case 0x02:
                   14320:                REG8(AL) = get_extended_country_info(REG16(BP));
                   14321:                break;
                   14322:        case 0x04:
1.1.1.42  root     14323:                for(int i = 0;; i++) {
                   14324:                        if(country_table[i].code == REG16(DX)) {
                   14325:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   14326:                                break;
                   14327:                        } else if(country_table[i].code == -1) {
                   14328:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   14329:                                break;
                   14330:                        }
                   14331:                }
1.1.1.25  root     14332:                REG8(AL) = 0x00;
1.1.1.22  root     14333:                break;
                   14334:        default:
                   14335:                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));
                   14336:                REG16(AX) = 0x01;
                   14337:                m_CF = 1;
                   14338:                break;
                   14339:        }
                   14340: }
                   14341: 
                   14342: inline void msdos_int_2fh_15h()
                   14343: {
                   14344:        switch(REG8(AL)) {
1.1.1.29  root     14345:        case 0x00: // CD-ROM - Installation Check
                   14346:                if(REG16(BX) == 0x0000) {
1.1.1.53  root     14347: #ifdef SUPPORT_MSCDEX
1.1.1.43  root     14348:                        // MSCDEX is installed
                   14349:                        REG16(BX) = 0;
                   14350:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     14351:                                if(msdos_is_cdrom_drive(i)) {
                   14352:                                        if(REG16(BX) == 0) {
                   14353:                                                REG16(CX) = i;
1.1.1.43  root     14354:                                        }
1.1.1.44  root     14355:                                        REG16(BX)++;
1.1.1.43  root     14356:                                }
                   14357:                        }
                   14358: #else
1.1.1.29  root     14359:                        // MSCDEX is not installed
                   14360: //                     REG8(AL) = 0x00;
1.1.1.43  root     14361: #endif
1.1.1.29  root     14362:                } else {
                   14363:                        // GRAPHICS.COM is not installed
                   14364: //                     REG8(AL) = 0x00;
                   14365:                }
1.1.1.22  root     14366:                break;
1.1.1.43  root     14367:        case 0x0b:
1.1.1.44  root     14368:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   14369:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   14370:                REG16(BX) = 0xadad;
1.1.1.43  root     14371:                break;
                   14372:        case 0x0d:
1.1.1.44  root     14373:                for(int i = 0, n = 0; i < 26; i++) {
                   14374:                        if(msdos_is_cdrom_drive(i)) {
                   14375:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     14376:                        }
                   14377:                }
                   14378:                break;
1.1.1.22  root     14379:        case 0xff:
1.1.1.29  root     14380:                if(REG16(BX) == 0x0000) {
                   14381:                        // CORELCDX is not installed
                   14382:                } else {
                   14383:                        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));
                   14384:                        REG16(AX) = 0x01;
                   14385:                        m_CF = 1;
                   14386:                }
1.1.1.22  root     14387:                break;
1.1.1.21  root     14388:        default:
1.1.1.22  root     14389:                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     14390:                REG16(AX) = 0x01;
                   14391:                m_CF = 1;
                   14392:                break;
                   14393:        }
                   14394: }
                   14395: 
1.1       root     14396: inline void msdos_int_2fh_16h()
                   14397: {
                   14398:        switch(REG8(AL)) {
                   14399:        case 0x00:
1.1.1.14  root     14400:                if(no_windows) {
1.1.1.29  root     14401:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   14402: //                     REG8(AL) = 0x00;
1.1.1.14  root     14403:                } else {
1.1.1.30  root     14404:                        REG8(AL) = win_major_version;
                   14405:                        REG8(AH) = win_minor_version;
1.1       root     14406:                }
                   14407:                break;
1.1.1.43  root     14408:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     14409:                // from DOSBox
                   14410:                i386_set_a20_line(1);
                   14411:                break;
1.1.1.49  root     14412:        case 0x06: // Windows Enhanced Mode & 286 DOSX Exit Broadcast
1.1.1.43  root     14413:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   14414:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   14415:                break;
                   14416:        case 0x07:
                   14417:                // Virtual Device Call API
                   14418:                break;
1.1.1.22  root     14419:        case 0x0a:
                   14420:                if(!no_windows) {
                   14421:                        REG16(AX) = 0x0000;
1.1.1.30  root     14422:                        REG8(BH) = win_major_version;
                   14423:                        REG8(BL) = win_minor_version;
1.1.1.49  root     14424: //                     REG16(CX) = 0x0002; // standard
1.1.1.22  root     14425:                        REG16(CX) = 0x0003; // enhanced
                   14426:                }
                   14427:                break;
1.1.1.30  root     14428:        case 0x0b:
                   14429:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     14430:        case 0x0e:
                   14431:        case 0x0f:
1.1.1.30  root     14432:        case 0x10:
1.1.1.22  root     14433:        case 0x11:
                   14434:        case 0x12:
                   14435:        case 0x13:
                   14436:        case 0x14:
1.1.1.30  root     14437:        case 0x15:
1.1.1.43  root     14438:        case 0x81:
                   14439:        case 0x82:
1.1.1.44  root     14440:        case 0x84:
1.1.1.49  root     14441:        case 0x85:
1.1.1.33  root     14442:        case 0x86:
1.1.1.22  root     14443:        case 0x87:
1.1.1.30  root     14444:        case 0x89:
1.1.1.33  root     14445:        case 0x8a:
1.1.1.22  root     14446:                // function not supported, do not clear AX
                   14447:                break;
1.1.1.14  root     14448:        case 0x80:
                   14449:                Sleep(10);
1.1.1.35  root     14450:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     14451:                REG8(AL) = 0x00;
1.1.1.14  root     14452:                break;
1.1.1.33  root     14453:        case 0x83:
                   14454:                REG16(BX) = 0x01; // system vm id
                   14455:                break;
1.1.1.22  root     14456:        case 0x8e:
                   14457:                REG16(AX) = 0x00; // failed
                   14458:                break;
1.1.1.20  root     14459:        case 0x8f:
                   14460:                switch(REG8(DH)) {
                   14461:                case 0x01:
1.1.1.49  root     14462: //                     REG16(AX) = 0x0000; // close command selected but not yet acknowledged
                   14463: //                     REG16(AX) = 0x0001; // close command issued and acknowledged
                   14464:                        REG16(AX) = 0x168f; // close command not selected -- application should continue
                   14465:                        break;
                   14466:                default:
                   14467:                        REG16(AX) = 0x0000; // successful
1.1.1.20  root     14468:                        break;
                   14469:                }
                   14470:                break;
1.1       root     14471:        default:
1.1.1.22  root     14472:                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));
                   14473:                REG16(AX) = 0x01;
                   14474:                m_CF = 1;
                   14475:                break;
                   14476:        }
                   14477: }
                   14478: 
                   14479: inline void msdos_int_2fh_19h()
                   14480: {
                   14481:        switch(REG8(AL)) {
                   14482:        case 0x00:
1.1.1.29  root     14483:                // SHELLB.COM is not installed
                   14484: //             REG8(AL) = 0x00;
1.1.1.22  root     14485:                break;
                   14486:        case 0x01:
                   14487:        case 0x02:
                   14488:        case 0x03:
                   14489:        case 0x04:
                   14490:                REG16(AX) = 0x01;
                   14491:                m_CF = 1;
                   14492:                break;
1.1.1.29  root     14493:        case 0x80:
                   14494:                // IBM ROM-DOS v4.0 is not installed
                   14495: //             REG8(AL) = 0x00;
                   14496:                break;
1.1.1.22  root     14497:        default:
                   14498:                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     14499:                REG16(AX) = 0x01;
1.1.1.3   root     14500:                m_CF = 1;
1.1       root     14501:                break;
                   14502:        }
                   14503: }
                   14504: 
                   14505: inline void msdos_int_2fh_1ah()
                   14506: {
                   14507:        switch(REG8(AL)) {
                   14508:        case 0x00:
1.1.1.29  root     14509:                // ANSI.SYS is installed
1.1       root     14510:                REG8(AL) = 0xff;
                   14511:                break;
1.1.1.49  root     14512:        case 0x01:
1.1.1.50  root     14513:                if(REG8(CL) == 0x5f) {
                   14514:                        // set display information
                   14515:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   14516:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   14517:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   14518:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   14519:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   14520:                                
                   14521:                                if(cur_width != new_width || cur_height != new_height) {
                   14522:                                        pcbios_set_console_size(new_width, new_height, true);
                   14523:                                }
                   14524:                        }
                   14525:                } else if(REG8(CL) == 0x7f) {
1.1.1.49  root     14526:                        // get display information
1.1.1.50  root     14527:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   14528:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   14529:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   14530:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   14531:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   14532:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   14533:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   14534:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   14535:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   14536:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   14537:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.49  root     14538:                } else {
                   14539:                        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));
                   14540:                        REG16(AX) = 0x01;
                   14541:                        m_CF = 1;
                   14542:                }
                   14543:                break;
1.1       root     14544:        default:
1.1.1.22  root     14545:                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));
                   14546:                REG16(AX) = 0x01;
                   14547:                m_CF = 1;
                   14548:                break;
                   14549:        }
                   14550: }
                   14551: 
1.1.1.30  root     14552: inline void msdos_int_2fh_40h()
1.1.1.22  root     14553: {
                   14554:        switch(REG8(AL)) {
                   14555:        case 0x00:
1.1.1.30  root     14556:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   14557:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     14558:                break;
1.1.1.43  root     14559:        case 0x10:
                   14560:                // OS/2 v2.0+ - Installation Check
                   14561:                REG16(AX) = 0x01;
                   14562:                m_CF = 1;
                   14563:                break;
1.1.1.22  root     14564:        default:
                   14565:                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     14566:                REG16(AX) = 0x01;
1.1.1.3   root     14567:                m_CF = 1;
1.1       root     14568:                break;
                   14569:        }
                   14570: }
                   14571: 
                   14572: inline void msdos_int_2fh_43h()
                   14573: {
                   14574:        switch(REG8(AL)) {
                   14575:        case 0x00:
1.1.1.29  root     14576:                // XMS is installed ?
1.1.1.19  root     14577: #ifdef SUPPORT_XMS
                   14578:                if(support_xms) {
                   14579:                        REG8(AL) = 0x80;
1.1.1.44  root     14580:                }
                   14581: #endif
                   14582:                break;
                   14583:        case 0x08:
                   14584: #ifdef SUPPORT_XMS
                   14585:                if(support_xms) {
                   14586:                        REG8(AL) = 0x43;
                   14587:                        REG8(BL) = 0x01; // IBM PC/AT
                   14588:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   14589:                }
1.1.1.19  root     14590: #endif
                   14591:                break;
                   14592:        case 0x10:
                   14593:                SREG(ES) = XMS_TOP >> 4;
                   14594:                i386_load_segment_descriptor(ES);
1.1.1.26  root     14595:                REG16(BX) = 0x15;
1.1       root     14596:                break;
1.1.1.44  root     14597:        case 0xe0:
                   14598:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   14599:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   14600:                        break;
                   14601:                }
1.1       root     14602:        default:
1.1.1.22  root     14603:                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));
                   14604:                REG16(AX) = 0x01;
                   14605:                m_CF = 1;
                   14606:                break;
                   14607:        }
                   14608: }
                   14609: 
                   14610: inline void msdos_int_2fh_46h()
                   14611: {
                   14612:        switch(REG8(AL)) {
                   14613:        case 0x80:
1.1.1.29  root     14614:                // Windows v3.0 is not installed
                   14615: //             REG8(AL) = 0x00;
1.1.1.22  root     14616:                break;
                   14617:        default:
                   14618:                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));
                   14619:                REG16(AX) = 0x01;
                   14620:                m_CF = 1;
                   14621:                break;
                   14622:        }
                   14623: }
                   14624: 
                   14625: inline void msdos_int_2fh_48h()
                   14626: {
                   14627:        switch(REG8(AL)) {
                   14628:        case 0x00:
1.1.1.29  root     14629:                // DOSKEY is not installed
                   14630: //             REG8(AL) = 0x00;
1.1.1.22  root     14631:                break;
                   14632:        case 0x10:
                   14633:                msdos_int_21h_0ah();
                   14634:                REG16(AX) = 0x00;
                   14635:                break;
                   14636:        default:
                   14637:                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     14638:                REG16(AX) = 0x01;
1.1.1.3   root     14639:                m_CF = 1;
1.1       root     14640:                break;
                   14641:        }
                   14642: }
                   14643: 
                   14644: inline void msdos_int_2fh_4ah()
                   14645: {
                   14646:        switch(REG8(AL)) {
1.1.1.29  root     14647: #ifdef SUPPORT_HMA
                   14648:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   14649:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14650:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14651:                                // restore first free mcb in high memory area
                   14652:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14653:                        }
                   14654:                        int offset = 0xffff;
                   14655:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   14656:                                REG16(DI) = offset + 0x10;
                   14657:                        } else {
                   14658:                                REG16(DI) = 0xffff;
                   14659:                        }
                   14660:                } else {
                   14661:                        // HMA is already used
                   14662:                        REG16(BX) = 0;
                   14663:                        REG16(DI) = 0xffff;
                   14664:                }
                   14665:                SREG(ES) = 0xffff;
                   14666:                i386_load_segment_descriptor(ES);
                   14667:                break;
                   14668:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   14669:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14670:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14671:                                // restore first free mcb in high memory area
                   14672:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14673:                        }
                   14674:                        int size = REG16(BX), offset;
                   14675:                        if((size % 16) != 0) {
                   14676:                                size &= ~15;
                   14677:                                size += 16;
                   14678:                        }
                   14679:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   14680:                                REG16(BX) = size;
                   14681:                                REG16(DI) = offset + 0x10;
                   14682:                                is_hma_used_by_int_2fh = true;
                   14683:                        } else {
                   14684:                                REG16(BX) = 0;
                   14685:                                REG16(DI) = 0xffff;
                   14686:                        }
                   14687:                } else {
                   14688:                        // HMA is already used
                   14689:                        REG16(BX) = 0;
                   14690:                        REG16(DI) = 0xffff;
                   14691:                }
                   14692:                SREG(ES) = 0xffff;
                   14693:                i386_load_segment_descriptor(ES);
                   14694:                break;
                   14695:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   14696:                if(REG8(DL) == 0x00) {
                   14697:                        if(!is_hma_used_by_xms) {
                   14698:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14699:                                        // restore first free mcb in high memory area
                   14700:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14701:                                        is_hma_used_by_int_2fh = false;
                   14702:                                }
                   14703:                                int size = REG16(BX), offset;
                   14704:                                if((size % 16) != 0) {
                   14705:                                        size &= ~15;
                   14706:                                        size += 16;
                   14707:                                }
                   14708:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   14709: //                                     REG16(BX) = size;
                   14710:                                        SREG(ES) = 0xffff;
                   14711:                                        i386_load_segment_descriptor(ES);
                   14712:                                        REG16(DI) = offset + 0x10;
                   14713:                                        is_hma_used_by_int_2fh = true;
                   14714:                                } else {
                   14715:                                        REG16(DI) = 0xffff;
                   14716:                                }
                   14717:                        } else {
                   14718:                                REG16(DI) = 0xffff;
                   14719:                        }
                   14720:                } else if(REG8(DL) == 0x01) {
                   14721:                        if(!is_hma_used_by_xms) {
                   14722:                                int size = REG16(BX);
                   14723:                                if((size % 16) != 0) {
                   14724:                                        size &= ~15;
                   14725:                                        size += 16;
                   14726:                                }
                   14727:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   14728:                                        // memory block address is not changed
                   14729:                                } else {
                   14730:                                        REG16(DI) = 0xffff;
                   14731:                                }
                   14732:                        } else {
                   14733:                                REG16(DI) = 0xffff;
                   14734:                        }
                   14735:                } else if(REG8(DL) == 0x02) {
                   14736:                        if(!is_hma_used_by_xms) {
                   14737:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14738:                                        // restore first free mcb in high memory area
                   14739:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14740:                                        is_hma_used_by_int_2fh = false;
                   14741:                                } else {
                   14742:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   14743:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   14744:                                                is_hma_used_by_int_2fh = false;
                   14745:                                        }
                   14746:                                }
                   14747:                        }
                   14748:                } else {
                   14749:                        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));
                   14750:                        REG16(AX) = 0x01;
                   14751:                        m_CF = 1;
                   14752:                }
                   14753:                break;
                   14754:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   14755:                if(!is_hma_used_by_xms) {
                   14756:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14757:                                // restore first free mcb in high memory area
                   14758:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14759:                                is_hma_used_by_int_2fh = false;
                   14760:                        }
                   14761:                        REG16(AX) = 0x0000;
                   14762:                        SREG(ES) = 0xffff;
                   14763:                        i386_load_segment_descriptor(ES);
                   14764:                        REG16(DI) = 0x10;
                   14765:                }
                   14766:                break;
                   14767: #else
1.1       root     14768:        case 0x01:
                   14769:        case 0x02:
1.1.1.29  root     14770:                // HMA is already used
1.1.1.27  root     14771:                REG16(BX) = 0x0000;
1.1.1.3   root     14772:                SREG(ES) = 0xffff;
                   14773:                i386_load_segment_descriptor(ES);
1.1       root     14774:                REG16(DI) = 0xffff;
                   14775:                break;
1.1.1.19  root     14776:        case 0x03:
                   14777:                // unable to allocate
                   14778:                REG16(DI) = 0xffff;
                   14779:                break;
                   14780:        case 0x04:
                   14781:                // function not supported, do not clear AX
                   14782:                break;
1.1.1.29  root     14783: #endif
                   14784:        case 0x10:
1.1.1.42  root     14785:                switch(REG16(BX)) {
                   14786:                case 0x0000:
                   14787:                case 0x0001:
                   14788:                case 0x0002:
                   14789:                case 0x0003:
                   14790:                case 0x0004:
                   14791:                case 0x0005:
                   14792:                case 0x0006:
                   14793:                case 0x0007:
                   14794:                case 0x0008:
                   14795:                case 0x000a:
                   14796:                case 0x1234:
                   14797:                        // SMARTDRV v4.00+ is not installed
                   14798:                        break;
                   14799:                default:
1.1.1.29  root     14800:                        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));
                   14801:                        REG16(AX) = 0x01;
                   14802:                        m_CF = 1;
1.1.1.42  root     14803:                        break;
1.1.1.29  root     14804:                }
                   14805:                break;
                   14806:        case 0x11:
1.1.1.42  root     14807:                switch(REG16(BX)) {
                   14808:                case 0x0000:
                   14809:                case 0x0001:
                   14810:                case 0x0002:
                   14811:                case 0x0003:
                   14812:                case 0x0004:
                   14813:                case 0x0005:
                   14814:                case 0x0006:
                   14815:                case 0x0007:
                   14816:                case 0x0008:
                   14817:                case 0x0009:
                   14818:                case 0x000a:
                   14819:                case 0x000b:
                   14820:                case 0xfffe:
                   14821:                case 0xffff:
1.1.1.29  root     14822:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     14823:                        break;
                   14824:                default:
                   14825:                        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));
                   14826:                        REG16(AX) = 0x01;
                   14827:                        m_CF = 1;
                   14828:                        break;
                   14829:                }
                   14830:                break;
                   14831:        case 0x12:
                   14832:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   14833:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   14834:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   14835:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     14836:                } else {
                   14837:                        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));
                   14838:                        REG16(AX) = 0x01;
                   14839:                        m_CF = 1;
                   14840:                }
1.1.1.22  root     14841:                break;
1.1.1.42  root     14842:        case 0x13:
                   14843:                // DBLSPACE.BIN is not installed
                   14844:                break;
1.1.1.22  root     14845:        default:
                   14846:                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));
                   14847:                REG16(AX) = 0x01;
                   14848:                m_CF = 1;
                   14849:                break;
                   14850:        }
                   14851: }
                   14852: 
                   14853: inline void msdos_int_2fh_4bh()
                   14854: {
                   14855:        switch(REG8(AL)) {
1.1.1.24  root     14856:        case 0x01:
1.1.1.22  root     14857:        case 0x02:
1.1.1.29  root     14858:                // Task Switcher is not installed
1.1.1.24  root     14859:                break;
                   14860:        case 0x03:
                   14861:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   14862:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     14863:                break;
1.1.1.30  root     14864:        case 0x04:
                   14865:                REG16(BX) = 0x0000; // free switcher id successfully
                   14866:                break;
1.1.1.43  root     14867:        case 0x05:
                   14868:                REG16(BX) = 0x0000; // no instance data chain
                   14869:                SREG(ES) = 0x0000;
                   14870:                i386_load_segment_descriptor(ES);
                   14871:                break;
1.1       root     14872:        default:
1.1.1.22  root     14873:                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     14874:                REG16(AX) = 0x01;
1.1.1.3   root     14875:                m_CF = 1;
1.1       root     14876:                break;
                   14877:        }
                   14878: }
                   14879: 
1.1.1.44  root     14880: inline void msdos_int_2fh_4dh()
                   14881: {
                   14882:        switch(REG8(AL)) {
                   14883:        case 0x00:
                   14884:                // KKCFUNC is not installed ???
                   14885:                break;
                   14886:        default:
                   14887: //             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));
                   14888:                REG16(AX) = 0x01; // invalid function
                   14889:                m_CF = 1;
                   14890:                break;
                   14891:        }
                   14892: }
                   14893: 
1.1       root     14894: inline void msdos_int_2fh_4fh()
                   14895: {
                   14896:        switch(REG8(AL)) {
                   14897:        case 0x00:
1.1.1.29  root     14898:                // BILING is installed
1.1.1.27  root     14899:                REG16(AX) = 0x0000;
                   14900:                REG8(DL) = 0x01;        // major version
                   14901:                REG8(DH) = 0x00;        // minor version
1.1       root     14902:                break;
                   14903:        case 0x01:
1.1.1.27  root     14904:                REG16(AX) = 0x0000;
1.1       root     14905:                REG16(BX) = active_code_page;
                   14906:                break;
                   14907:        default:
1.1.1.22  root     14908:                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));
                   14909:                REG16(AX) = 0x01;
                   14910:                m_CF = 1;
                   14911:                break;
                   14912:        }
                   14913: }
                   14914: 
                   14915: inline void msdos_int_2fh_55h()
                   14916: {
                   14917:        switch(REG8(AL)) {
                   14918:        case 0x00:
                   14919:        case 0x01:
                   14920: //             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));
                   14921:                break;
                   14922:        default:
                   14923:                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     14924:                REG16(AX) = 0x01;
1.1.1.3   root     14925:                m_CF = 1;
1.1       root     14926:                break;
                   14927:        }
                   14928: }
                   14929: 
1.1.1.44  root     14930: inline void msdos_int_2fh_56h()
                   14931: {
                   14932:        switch(REG8(AL)) {
                   14933:        case 0x00:
                   14934:                // INTERLNK is not installed
                   14935:                break;
                   14936:        case 0x01:
                   14937:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   14938: //             if(msdos_is_remote_drive(REG8(BH))) {
                   14939: //                     REG8(AL) = 0x00;
                   14940: //             }
                   14941:                break;
                   14942:        default:
                   14943:                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));
                   14944:                REG16(AX) = 0x01;
                   14945:                m_CF = 1;
                   14946:                break;
                   14947:        }
                   14948: }
                   14949: 
1.1.1.24  root     14950: inline void msdos_int_2fh_adh()
                   14951: {
                   14952:        switch(REG8(AL)) {
                   14953:        case 0x00:
1.1.1.29  root     14954:                // DISPLAY.SYS is installed
1.1.1.24  root     14955:                REG8(AL) = 0xff;
                   14956:                REG16(BX) = 0x100; // ???
                   14957:                break;
                   14958:        case 0x01:
                   14959:                active_code_page = REG16(BX);
                   14960:                msdos_nls_tables_update();
                   14961:                REG16(AX) = 0x01;
                   14962:                break;
                   14963:        case 0x02:
                   14964:                REG16(BX) = active_code_page;
                   14965:                break;
                   14966:        case 0x03:
                   14967:                // FIXME
                   14968:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   14969:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   14970:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   14971:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   14972:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   14973:                break;
                   14974:        case 0x80:
1.1.1.49  root     14975:                // KEYB.COM is not installed
                   14976:                break;
1.1.1.24  root     14977:        default:
                   14978:                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));
                   14979:                REG16(AX) = 0x01;
                   14980:                m_CF = 1;
                   14981:                break;
                   14982:        }
                   14983: }
                   14984: 
1.1       root     14985: inline void msdos_int_2fh_aeh()
                   14986: {
                   14987:        switch(REG8(AL)) {
                   14988:        case 0x00:
1.1.1.28  root     14989:                // FIXME: we need to check the given command line
                   14990:                REG8(AL) = 0x00; // the command should be executed as usual
                   14991: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     14992:                break;
                   14993:        case 0x01:
                   14994:                {
                   14995:                        char command[MAX_PATH];
                   14996:                        memset(command, 0, sizeof(command));
1.1.1.3   root     14997:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     14998:                        
                   14999:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   15000:                        param->env_seg = 0;
                   15001:                        param->cmd_line.w.l = 44;
                   15002:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   15003:                        param->fcb1.w.l = 24;
                   15004:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   15005:                        param->fcb2.w.l = 24;
                   15006:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   15007:                        
                   15008:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   15009:                        
                   15010:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     15011:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   15012:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     15013:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   15014:                        
1.1.1.28  root     15015:                        try {
                   15016:                                msdos_process_exec(command, param, 0);
                   15017:                        } catch(...) {
                   15018:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     15019:                        }
                   15020:                }
                   15021:                break;
                   15022:        default:
1.1.1.22  root     15023:                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     15024:                REG16(AX) = 0x01;
1.1.1.3   root     15025:                m_CF = 1;
1.1       root     15026:                break;
                   15027:        }
                   15028: }
                   15029: 
1.1.1.34  root     15030: inline void msdos_int_2fh_b7h()
                   15031: {
                   15032:        switch(REG8(AL)) {
                   15033:        case 0x00:
                   15034:                // APPEND is not installed
                   15035: //             REG8(AL) = 0x00;
                   15036:                break;
1.1.1.44  root     15037:        case 0x06:
                   15038:                REG16(BX) = 0x0000;
                   15039:                break;
1.1.1.34  root     15040:        case 0x07:
1.1.1.43  root     15041:        case 0x11:
1.1.1.34  root     15042:                // COMMAND.COM calls this service without checking APPEND is installed
                   15043:                break;
                   15044:        default:
                   15045:                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));
                   15046:                REG16(AX) = 0x01;
                   15047:                m_CF = 1;
                   15048:                break;
                   15049:        }
                   15050: }
                   15051: 
1.1.1.24  root     15052: inline void msdos_int_33h_0000h()
                   15053: {
                   15054:        REG16(AX) = 0xffff; // hardware/driver installed
                   15055:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15056: }
                   15057: 
                   15058: inline void msdos_int_33h_0001h()
                   15059: {
1.1.1.34  root     15060:        if(mouse.hidden > 0) {
                   15061:                mouse.hidden--;
                   15062:        }
                   15063:        if(mouse.hidden == 0) {
1.1.1.61  root     15064:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), (dwConsoleMode | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS) & ~(ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE));
1.1.1.59  root     15065:                pic[1].imr &= ~0x10; // enable irq12
1.1.1.24  root     15066:        }
                   15067: }
                   15068: 
                   15069: inline void msdos_int_33h_0002h()
                   15070: {
1.1.1.34  root     15071:        mouse.hidden++;
1.1.1.61  root     15072:        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
1.1.1.59  root     15073:        pic[1].imr |= 0x10; // disable irq12
1.1.1.24  root     15074: }
                   15075: 
                   15076: inline void msdos_int_33h_0003h()
                   15077: {
1.1.1.34  root     15078: //     if(mouse.hidden > 0) {
                   15079:                update_console_input();
                   15080: //     }
1.1.1.24  root     15081:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     15082:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   15083:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   15084: }
                   15085: 
                   15086: inline void msdos_int_33h_0004h()
                   15087: {
                   15088:        mouse.position.x = REG16(CX);
                   15089:        mouse.position.x = REG16(DX);
1.1.1.24  root     15090: }
                   15091: 
                   15092: inline void msdos_int_33h_0005h()
                   15093: {
1.1.1.34  root     15094: //     if(mouse.hidden > 0) {
                   15095:                update_console_input();
                   15096: //     }
1.1.1.24  root     15097:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   15098:                int idx = REG16(BX);
1.1.1.34  root     15099:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   15100:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   15101:                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     15102:                mouse.buttons[idx].pressed_times = 0;
                   15103:        } else {
                   15104:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   15105:        }
                   15106:        REG16(AX) = mouse.get_buttons();
                   15107: }
                   15108: 
                   15109: inline void msdos_int_33h_0006h()
                   15110: {
1.1.1.34  root     15111: //     if(mouse.hidden > 0) {
                   15112:                update_console_input();
                   15113: //     }
1.1.1.24  root     15114:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   15115:                int idx = REG16(BX);
1.1.1.34  root     15116:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   15117:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   15118:                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     15119:                mouse.buttons[idx].released_times = 0;
                   15120:        } else {
                   15121:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   15122:        }
                   15123:        REG16(AX) = mouse.get_buttons();
                   15124: }
                   15125: 
                   15126: inline void msdos_int_33h_0007h()
                   15127: {
                   15128:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   15129:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   15130: }
                   15131: 
                   15132: inline void msdos_int_33h_0008h()
                   15133: {
                   15134:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   15135:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   15136: }
                   15137: 
                   15138: inline void msdos_int_33h_0009h()
                   15139: {
                   15140:        mouse.hot_spot[0] = REG16(BX);
                   15141:        mouse.hot_spot[1] = REG16(CX);
                   15142: }
                   15143: 
1.1.1.49  root     15144: inline void msdos_int_33h_000ah()
                   15145: {
                   15146:        mouse.screen_mask = REG16(CX);
                   15147:        mouse.cursor_mask = REG16(DX);
                   15148: }
                   15149: 
1.1.1.24  root     15150: inline void msdos_int_33h_000bh()
                   15151: {
1.1.1.34  root     15152: //     if(mouse.hidden > 0) {
                   15153:                update_console_input();
                   15154: //     }
1.1.1.24  root     15155:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   15156:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   15157:        mouse.prev_position.x = mouse.position.x;
                   15158:        mouse.prev_position.y = mouse.position.y;
                   15159:        REG16(CX) = dx;
                   15160:        REG16(DX) = dy;
                   15161: }
                   15162: 
                   15163: inline void msdos_int_33h_000ch()
                   15164: {
                   15165:        mouse.call_mask = REG16(CX);
                   15166:        mouse.call_addr.w.l = REG16(DX);
                   15167:        mouse.call_addr.w.h = SREG(ES);
                   15168: }
                   15169: 
                   15170: inline void msdos_int_33h_000fh()
                   15171: {
                   15172:        mouse.mickey.x = REG16(CX);
                   15173:        mouse.mickey.y = REG16(DX);
                   15174: }
                   15175: 
                   15176: inline void msdos_int_33h_0011h()
                   15177: {
                   15178:        REG16(AX) = 0xffff;
                   15179:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15180: }
                   15181: 
                   15182: inline void msdos_int_33h_0014h()
                   15183: {
                   15184:        UINT16 old_mask = mouse.call_mask;
                   15185:        UINT16 old_ofs = mouse.call_addr.w.l;
                   15186:        UINT16 old_seg = mouse.call_addr.w.h;
                   15187:        
                   15188:        mouse.call_mask = REG16(CX);
                   15189:        mouse.call_addr.w.l = REG16(DX);
                   15190:        mouse.call_addr.w.h = SREG(ES);
                   15191:        
                   15192:        REG16(CX) = old_mask;
                   15193:        REG16(DX) = old_ofs;
                   15194:        SREG(ES) = old_seg;
                   15195:        i386_load_segment_descriptor(ES);
                   15196: }
                   15197: 
                   15198: inline void msdos_int_33h_0015h()
                   15199: {
                   15200:        REG16(BX) = sizeof(mouse);
                   15201: }
                   15202: 
                   15203: inline void msdos_int_33h_0016h()
                   15204: {
                   15205:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   15206: }
                   15207: 
                   15208: inline void msdos_int_33h_0017h()
                   15209: {
                   15210:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   15211: }
                   15212: 
1.1.1.43  root     15213: inline void msdos_int_33h_0018h()
                   15214: {
                   15215:        for(int i = 0; i < 8; i++) {
                   15216:                if(REG16(CX) & (1 << i)) {
                   15217:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   15218:                                // event handler already exists
                   15219:                                REG16(AX) = 0xffff;
                   15220:                                break;
                   15221:                        }
                   15222:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   15223:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   15224:                }
                   15225:        }
                   15226: }
                   15227: 
                   15228: inline void msdos_int_33h_0019h()
                   15229: {
                   15230:        UINT16 call_mask = REG16(CX);
                   15231:        
                   15232:        REG16(CX) = 0;
                   15233:        
                   15234:        for(int i = 0; i < 8; i++) {
                   15235:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   15236:                        for(int j = 0; j < 8; j++) {
                   15237:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   15238:                                        REG16(CX) |= (1 << j);
                   15239:                                }
                   15240:                        }
                   15241:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   15242:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   15243:                        break;
                   15244:                }
                   15245:        }
                   15246: }
                   15247: 
1.1.1.24  root     15248: inline void msdos_int_33h_001ah()
                   15249: {
                   15250:        mouse.sensitivity[0] = REG16(BX);
                   15251:        mouse.sensitivity[1] = REG16(CX);
                   15252:        mouse.sensitivity[2] = REG16(DX);
                   15253: }
                   15254: 
                   15255: inline void msdos_int_33h_001bh()
                   15256: {
                   15257:        REG16(BX) = mouse.sensitivity[0];
                   15258:        REG16(CX) = mouse.sensitivity[1];
                   15259:        REG16(DX) = mouse.sensitivity[2];
                   15260: }
                   15261: 
                   15262: inline void msdos_int_33h_001dh()
                   15263: {
                   15264:        mouse.display_page = REG16(BX);
                   15265: }
                   15266: 
                   15267: inline void msdos_int_33h_001eh()
                   15268: {
                   15269:        REG16(BX) = mouse.display_page;
                   15270: }
                   15271: 
1.1.1.34  root     15272: inline void msdos_int_33h_001fh()
                   15273: {
                   15274:        // from DOSBox
                   15275:        REG16(BX) = 0x0000;
                   15276:        SREG(ES) = 0x0000;
                   15277:        i386_load_segment_descriptor(ES);
                   15278:        mouse.enabled = false;
                   15279:        mouse.old_hidden = mouse.hidden;
                   15280:        mouse.hidden = 1;
                   15281: }
                   15282: 
                   15283: inline void msdos_int_33h_0020h()
                   15284: {
                   15285:        // from DOSBox
                   15286:        mouse.enabled = true;
                   15287:        mouse.hidden = mouse.old_hidden;
                   15288: }
                   15289: 
1.1.1.24  root     15290: inline void msdos_int_33h_0021h()
                   15291: {
                   15292:        REG16(AX) = 0xffff;
                   15293:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15294: }
                   15295: 
                   15296: inline void msdos_int_33h_0022h()
                   15297: {
                   15298:        mouse.language = REG16(BX);
                   15299: }
                   15300: 
                   15301: inline void msdos_int_33h_0023h()
                   15302: {
                   15303:        REG16(BX) = mouse.language;
                   15304: }
                   15305: 
                   15306: inline void msdos_int_33h_0024h()
                   15307: {
                   15308:        REG16(BX) = 0x0805; // V8.05
                   15309:        REG16(CX) = 0x0400; // PS/2
                   15310: }
                   15311: 
1.1.1.49  root     15312: inline void msdos_int_33h_0025h()
                   15313: {
                   15314:        REG16(AX) = 0x8000; // driver (not TSR), software text cursor
                   15315: }
                   15316: 
1.1.1.24  root     15317: inline void msdos_int_33h_0026h()
                   15318: {
                   15319:        REG16(BX) = 0x0000;
                   15320:        REG16(CX) = mouse.max_position.x;
                   15321:        REG16(DX) = mouse.max_position.y;
                   15322: }
                   15323: 
1.1.1.49  root     15324: inline void msdos_int_33h_0027h()
                   15325: {
                   15326: //     if(mouse.hidden > 0) {
                   15327:                update_console_input();
                   15328: //     }
                   15329:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   15330:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   15331:        mouse.prev_position.x = mouse.position.x;
                   15332:        mouse.prev_position.y = mouse.position.y;
                   15333:        REG16(AX) = mouse.screen_mask;
                   15334:        REG16(BX) = mouse.cursor_mask;
                   15335:        REG16(CX) = dx;
                   15336:        REG16(DX) = dy;
                   15337: }
                   15338: 
                   15339: inline void msdos_int_33h_0028h()
                   15340: {
                   15341:        if(REG16(CX) != 0) {
                   15342:                UINT8 tmp = REG8(AL);
                   15343:                REG8(AL) = REG8(CL);
                   15344:                pcbios_int_10h_00h();
                   15345:                REG8(AL) = tmp;
                   15346:        }
                   15347:        REG8(CL) = 0x00; // successful
                   15348: }
                   15349: 
                   15350: inline void msdos_int_33h_0029h()
                   15351: {
                   15352:        switch(REG16(CX)) {
                   15353:        case 0x0000:
                   15354:                REG16(CX) = 0x0003;
                   15355:                sprintf((char *)(mem + WORK_TOP), "TEXT Mode (80x25)$");
                   15356:                break;
                   15357:        case 0x0003:
                   15358:                REG16(CX) = 0x0070;
                   15359:                sprintf((char *)(mem + WORK_TOP), "V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15360:                break;
                   15361:        case 0x0070:
                   15362:                REG16(CX) = 0x0071;
                   15363:                sprintf((char *)(mem + WORK_TOP), "Extended CGA V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15364:                break;
                   15365:        case 0x0071:
                   15366:                REG16(CX) = 0x0073;
                   15367:                sprintf((char *)(mem + WORK_TOP), "Extended CGA TEXT Mode (80x25)$");
                   15368:                break;
                   15369:        default:
                   15370:                REG16(CX) = 0x0000;
                   15371:                break;
                   15372:        }
                   15373:        if(REG16(CX) != 0) {
                   15374:                SREG(DS) = (WORK_TOP >> 4);
                   15375:        } else {
                   15376:                SREG(DS) = 0x0000;
                   15377:        }
                   15378:        i386_load_segment_descriptor(DS);
                   15379:        REG16(DX) = 0x0000;
                   15380: }
                   15381: 
1.1.1.24  root     15382: inline void msdos_int_33h_002ah()
                   15383: {
1.1.1.34  root     15384:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     15385:        REG16(BX) = mouse.hot_spot[0];
                   15386:        REG16(CX) = mouse.hot_spot[1];
                   15387:        REG16(DX) = 4; // PS/2
                   15388: }
                   15389: 
                   15390: inline void msdos_int_33h_0031h()
                   15391: {
                   15392:        REG16(AX) = mouse.min_position.x;
                   15393:        REG16(BX) = mouse.min_position.y;
                   15394:        REG16(CX) = mouse.max_position.x;
                   15395:        REG16(DX) = mouse.max_position.y;
                   15396: }
                   15397: 
                   15398: inline void msdos_int_33h_0032h()
                   15399: {
                   15400:        REG16(AX) = 0;
1.1.1.49  root     15401:        REG16(AX) |= 0x8000; // 0025h
1.1.1.24  root     15402:        REG16(AX) |= 0x4000; // 0026h
1.1.1.49  root     15403:        REG16(AX) |= 0x2000; // 0027h
1.1.1.24  root     15404: //     REG16(AX) |= 0x1000; // 0028h
                   15405: //     REG16(AX) |= 0x0800; // 0029h
                   15406:        REG16(AX) |= 0x0400; // 002ah
                   15407: //     REG16(AX) |= 0x0200; // 002bh
                   15408: //     REG16(AX) |= 0x0100; // 002ch
                   15409: //     REG16(AX) |= 0x0080; // 002dh
                   15410: //     REG16(AX) |= 0x0040; // 002eh
                   15411:        REG16(AX) |= 0x0020; // 002fh
                   15412: //     REG16(AX) |= 0x0010; // 0030h
                   15413:        REG16(AX) |= 0x0008; // 0031h
                   15414:        REG16(AX) |= 0x0004; // 0032h
                   15415: //     REG16(AX) |= 0x0002; // 0033h
                   15416: //     REG16(AX) |= 0x0001; // 0034h
                   15417: }
                   15418: 
1.1.1.49  root     15419: inline void msdos_int_33h_004dh()
                   15420: {
                   15421:        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), "Copyright 2017 MS-DOS Player");
                   15422: }
                   15423: 
                   15424: inline void msdos_int_33h_006dh()
                   15425: {
                   15426:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x08; // V8.05
                   15427:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = 0x05;
                   15428: }
                   15429: 
1.1.1.19  root     15430: inline void msdos_int_67h_40h()
                   15431: {
                   15432:        if(!support_ems) {
                   15433:                REG8(AH) = 0x84;
                   15434:        } else {
                   15435:                REG8(AH) = 0x00;
                   15436:        }
                   15437: }
                   15438: 
                   15439: inline void msdos_int_67h_41h()
                   15440: {
                   15441:        if(!support_ems) {
                   15442:                REG8(AH) = 0x84;
                   15443:        } else {
                   15444:                REG8(AH) = 0x00;
                   15445:                REG16(BX) = EMS_TOP >> 4;
                   15446:        }
                   15447: }
                   15448: 
                   15449: inline void msdos_int_67h_42h()
                   15450: {
                   15451:        if(!support_ems) {
                   15452:                REG8(AH) = 0x84;
                   15453:        } else {
                   15454:                REG8(AH) = 0x00;
                   15455:                REG16(BX) = free_ems_pages;
                   15456:                REG16(DX) = MAX_EMS_PAGES;
                   15457:        }
                   15458: }
                   15459: 
                   15460: inline void msdos_int_67h_43h()
                   15461: {
                   15462:        if(!support_ems) {
                   15463:                REG8(AH) = 0x84;
                   15464:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15465:                REG8(AH) = 0x87;
                   15466:        } else if(REG16(BX) > free_ems_pages) {
                   15467:                REG8(AH) = 0x88;
                   15468:        } else if(REG16(BX) == 0) {
                   15469:                REG8(AH) = 0x89;
                   15470:        } else {
1.1.1.31  root     15471:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15472:                        if(!ems_handles[i].allocated) {
                   15473:                                ems_allocate_pages(i, REG16(BX));
                   15474:                                REG8(AH) = 0x00;
                   15475:                                REG16(DX) = i;
                   15476:                                return;
                   15477:                        }
                   15478:                }
                   15479:                REG8(AH) = 0x85;
                   15480:        }
                   15481: }
                   15482: 
                   15483: inline void msdos_int_67h_44h()
                   15484: {
                   15485:        if(!support_ems) {
                   15486:                REG8(AH) = 0x84;
1.1.1.31  root     15487:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15488:                REG8(AH) = 0x83;
                   15489:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   15490:                REG8(AH) = 0x8a;
                   15491: //     } else if(!(REG8(AL) < 4)) {
                   15492: //             REG8(AH) = 0x8b;
                   15493:        } else if(REG16(BX) == 0xffff) {
                   15494:                ems_unmap_page(REG8(AL) & 3);
                   15495:                REG8(AH) = 0x00;
                   15496:        } else {
                   15497:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   15498:                REG8(AH) = 0x00;
                   15499:        }
                   15500: }
                   15501: 
                   15502: inline void msdos_int_67h_45h()
                   15503: {
                   15504:        if(!support_ems) {
                   15505:                REG8(AH) = 0x84;
1.1.1.31  root     15506:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15507:                REG8(AH) = 0x83;
                   15508:        } else {
                   15509:                ems_release_pages(REG16(DX));
                   15510:                REG8(AH) = 0x00;
                   15511:        }
                   15512: }
                   15513: 
                   15514: inline void msdos_int_67h_46h()
                   15515: {
                   15516:        if(!support_ems) {
                   15517:                REG8(AH) = 0x84;
                   15518:        } else {
1.1.1.29  root     15519: //             REG16(AX) = 0x0032; // EMS 3.2
                   15520:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     15521:        }
                   15522: }
                   15523: 
                   15524: inline void msdos_int_67h_47h()
                   15525: {
                   15526:        // NOTE: the map data should be stored in the specified ems page, not process data
                   15527:        process_t *process = msdos_process_info_get(current_psp);
                   15528:        
                   15529:        if(!support_ems) {
                   15530:                REG8(AH) = 0x84;
1.1.1.31  root     15531: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15532: //             REG8(AH) = 0x83;
                   15533:        } else if(process->ems_pages_stored) {
                   15534:                REG8(AH) = 0x8d;
                   15535:        } else {
                   15536:                for(int i = 0; i < 4; i++) {
                   15537:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   15538:                        process->ems_pages[i].page   = ems_pages[i].page;
                   15539:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   15540:                }
                   15541:                process->ems_pages_stored = true;
                   15542:                REG8(AH) = 0x00;
                   15543:        }
                   15544: }
                   15545: 
                   15546: inline void msdos_int_67h_48h()
                   15547: {
                   15548:        // NOTE: the map data should be restored from the specified ems page, not process data
                   15549:        process_t *process = msdos_process_info_get(current_psp);
                   15550:        
                   15551:        if(!support_ems) {
                   15552:                REG8(AH) = 0x84;
1.1.1.31  root     15553: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15554: //             REG8(AH) = 0x83;
                   15555:        } else if(!process->ems_pages_stored) {
                   15556:                REG8(AH) = 0x8e;
                   15557:        } else {
                   15558:                for(int i = 0; i < 4; i++) {
                   15559:                        if(process->ems_pages[i].mapped) {
                   15560:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   15561:                        } else {
                   15562:                                ems_unmap_page(i);
                   15563:                        }
                   15564:                }
                   15565:                process->ems_pages_stored = false;
                   15566:                REG8(AH) = 0x00;
                   15567:        }
                   15568: }
                   15569: 
                   15570: inline void msdos_int_67h_4bh()
                   15571: {
                   15572:        if(!support_ems) {
                   15573:                REG8(AH) = 0x84;
                   15574:        } else {
                   15575:                REG8(AH) = 0x00;
                   15576:                REG16(BX) = 0;
1.1.1.31  root     15577:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15578:                        if(ems_handles[i].allocated) {
                   15579:                                REG16(BX)++;
                   15580:                        }
                   15581:                }
                   15582:        }
                   15583: }
                   15584: 
                   15585: inline void msdos_int_67h_4ch()
                   15586: {
                   15587:        if(!support_ems) {
                   15588:                REG8(AH) = 0x84;
1.1.1.31  root     15589:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15590:                REG8(AH) = 0x83;
                   15591:        } else {
                   15592:                REG8(AH) = 0x00;
                   15593:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   15594:        }
                   15595: }
                   15596: 
                   15597: inline void msdos_int_67h_4dh()
                   15598: {
                   15599:        if(!support_ems) {
                   15600:                REG8(AH) = 0x84;
                   15601:        } else {
                   15602:                REG8(AH) = 0x00;
                   15603:                REG16(BX) = 0;
1.1.1.31  root     15604:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15605:                        if(ems_handles[i].allocated) {
                   15606:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   15607:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   15608:                                REG16(BX)++;
                   15609:                        }
                   15610:                }
                   15611:        }
                   15612: }
                   15613: 
1.1.1.20  root     15614: inline void msdos_int_67h_4eh()
                   15615: {
                   15616:        if(!support_ems) {
                   15617:                REG8(AH) = 0x84;
                   15618:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15619:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   15620:                        // save page map
                   15621:                        for(int i = 0; i < 4; i++) {
                   15622:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15623:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15624:                        }
                   15625:                }
                   15626:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15627:                        // restore page map
                   15628:                        for(int i = 0; i < 4; i++) {
                   15629:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15630:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15631:                                
1.1.1.31  root     15632:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     15633:                                        ems_map_page(i, handle, page);
                   15634:                                } else {
                   15635:                                        ems_unmap_page(i);
                   15636:                                }
                   15637:                        }
                   15638:                }
                   15639:                REG8(AH) = 0x00;
                   15640:        } else if(REG8(AL) == 0x03) {
                   15641:                REG8(AH) = 0x00;
1.1.1.21  root     15642:                REG8(AL) = 4 * 4;
                   15643:        } else {
1.1.1.22  root     15644:                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     15645:                REG8(AH) = 0x8f;
                   15646:        }
                   15647: }
                   15648: 
                   15649: inline void msdos_int_67h_4fh()
                   15650: {
                   15651:        if(!support_ems) {
                   15652:                REG8(AH) = 0x84;
                   15653:        } else if(REG8(AL) == 0x00) {
                   15654:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15655:                
                   15656:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   15657:                for(int i = 0; i < count; i++) {
                   15658:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   15659:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15660:                        
                   15661: //                     if(!(physical < 4)) {
                   15662: //                             REG8(AH) = 0x8b;
                   15663: //                             return;
                   15664: //                     }
                   15665:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     15666:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   15667:                        *(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     15668:                }
                   15669:                REG8(AH) = 0x00;
                   15670:        } else if(REG8(AL) == 0x01) {
                   15671:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15672:                
                   15673:                for(int i = 0; i < count; i++) {
                   15674:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   15675:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15676:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   15677:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   15678:                        
                   15679: //                     if(!(physical < 4)) {
                   15680: //                             REG8(AH) = 0x8b;
                   15681: //                             return;
                   15682: //                     } else
1.1.1.41  root     15683:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     15684:                                ems_map_page(physical & 3, handle, logical);
                   15685:                        } else {
1.1.1.41  root     15686:                                ems_unmap_page(physical & 3);
1.1.1.21  root     15687:                        }
                   15688:                }
                   15689:                REG8(AH) = 0x00;
                   15690:        } else if(REG8(AL) == 0x02) {
                   15691:                REG8(AH) = 0x00;
                   15692:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     15693:        } else {
1.1.1.22  root     15694:                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     15695:                REG8(AH) = 0x8f;
                   15696:        }
                   15697: }
                   15698: 
                   15699: inline void msdos_int_67h_50h()
                   15700: {
                   15701:        if(!support_ems) {
                   15702:                REG8(AH) = 0x84;
1.1.1.31  root     15703:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     15704:                REG8(AH) = 0x83;
                   15705:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15706:                for(int i = 0; i < REG16(CX); i++) {
                   15707:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15708:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15709:                        
                   15710:                        if(REG8(AL) == 0x01) {
                   15711:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15712:                        }
                   15713: //                     if(!(physical < 4)) {
                   15714: //                             REG8(AH) = 0x8b;
                   15715: //                             return;
                   15716: //                     } else
                   15717:                        if(logical == 0xffff) {
                   15718:                                ems_unmap_page(physical & 3);
                   15719:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15720:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15721:                        } else {
                   15722:                                REG8(AH) = 0x8a;
                   15723:                                return;
                   15724:                        }
                   15725:                }
                   15726:                REG8(AH) = 0x00;
                   15727:        } else {
1.1.1.22  root     15728:                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     15729:                REG8(AH) = 0x8f;
                   15730:        }
                   15731: }
                   15732: 
1.1.1.19  root     15733: inline void msdos_int_67h_51h()
                   15734: {
                   15735:        if(!support_ems) {
                   15736:                REG8(AH) = 0x84;
1.1.1.31  root     15737:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15738:                REG8(AH) = 0x83;
                   15739:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15740:                REG8(AH) = 0x87;
                   15741:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   15742:                REG8(AH) = 0x88;
                   15743:        } else {
                   15744:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   15745:                REG8(AH) = 0x00;
                   15746:        }
                   15747: }
                   15748: 
1.1.1.20  root     15749: inline void msdos_int_67h_52h()
                   15750: {
                   15751:        if(!support_ems) {
                   15752:                REG8(AH) = 0x84;
1.1.1.31  root     15753: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15754: //             REG8(AH) = 0x83;
1.1.1.20  root     15755:        } else if(REG8(AL) == 0x00) {
                   15756:                REG8(AL) = 0x00; // handle is volatile
                   15757:                REG8(AH) = 0x00;
                   15758:        } else if(REG8(AL) == 0x01) {
                   15759:                if(REG8(BL) == 0x00) {
                   15760:                        REG8(AH) = 0x00;
                   15761:                } else {
                   15762:                        REG8(AH) = 0x90; // undefined attribute type
                   15763:                }
                   15764:        } else if(REG8(AL) == 0x02) {
                   15765:                REG8(AL) = 0x00; // only volatile handles supported
                   15766:                REG8(AH) = 0x00;
                   15767:        } else {
1.1.1.22  root     15768:                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     15769:                REG8(AH) = 0x8f;
                   15770:        }
                   15771: }
                   15772: 
1.1.1.19  root     15773: inline void msdos_int_67h_53h()
                   15774: {
                   15775:        if(!support_ems) {
                   15776:                REG8(AH) = 0x84;
1.1.1.31  root     15777:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15778:                REG8(AH) = 0x83;
                   15779:        } else if(REG8(AL) == 0x00) {
                   15780:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   15781:                REG8(AH) = 0x00;
                   15782:        } else if(REG8(AL) == 0x01) {
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) = 0xa1;
                   15786:                                return;
                   15787:                        }
                   15788:                }
                   15789:                REG8(AH) = 0x00;
                   15790:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   15791:        } else {
1.1.1.22  root     15792:                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     15793:                REG8(AH) = 0x8f;
1.1.1.19  root     15794:        }
                   15795: }
                   15796: 
                   15797: inline void msdos_int_67h_54h()
                   15798: {
                   15799:        if(!support_ems) {
                   15800:                REG8(AH) = 0x84;
                   15801:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     15802:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15803:                        if(ems_handles[i].allocated) {
                   15804:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   15805:                        } else {
                   15806:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   15807:                        }
                   15808:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   15809:                }
                   15810:                REG8(AH) = 0x00;
                   15811:                REG8(AL) = MAX_EMS_HANDLES;
                   15812:        } else if(REG8(AL) == 0x01) {
                   15813:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     15814:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15815:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15816:                                REG8(AH) = 0x00;
                   15817:                                REG16(DX) = i;
                   15818:                                break;
                   15819:                        }
                   15820:                }
                   15821:        } else if(REG8(AL) == 0x02) {
                   15822:                REG8(AH) = 0x00;
                   15823:                REG16(BX) = MAX_EMS_HANDLES;
                   15824:        } else {
1.1.1.22  root     15825:                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     15826:                REG8(AH) = 0x8f;
                   15827:        }
                   15828: }
                   15829: 
1.1.1.49  root     15830: inline void msdos_int_67h_55h()
                   15831: {
                   15832:        if(!support_ems) {
                   15833:                REG8(AH) = 0x84;
                   15834:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15835:                REG8(AH) = 0x83;
                   15836:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15837:                UINT16 jump_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
                   15838:                UINT16 jump_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
                   15839:                UINT8  entries  = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
                   15840:                UINT16 map_ofs  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   15841:                UINT16 map_seg  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
                   15842:                
                   15843:                for(int i = 0; i < (int)entries; i++) {
                   15844:                        int logical  = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 0);
                   15845:                        int physical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 2);
                   15846:                        
                   15847:                        if(REG8(AL) == 0x01) {
                   15848:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15849:                        }
                   15850: //                     if(!(physical < 4)) {
                   15851: //                             REG8(AH) = 0x8b;
                   15852: //                             return;
                   15853: //                     } else
                   15854:                        if(logical == 0xffff) {
                   15855:                                ems_unmap_page(physical & 3);
                   15856:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15857:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15858:                        } else {
                   15859:                                REG8(AH) = 0x8a;
                   15860:                                return;
                   15861:                        }
                   15862:                }
                   15863:                i386_jmp_far(jump_seg, jump_ofs);
                   15864:                REG8(AH) = 0x00;
                   15865:        } else {
                   15866:                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));
                   15867:                REG8(AH) = 0x8f;
                   15868:        }
                   15869: }
                   15870: 
                   15871: inline void msdos_int_67h_56h()
                   15872: {
                   15873:        if(!support_ems) {
                   15874:                REG8(AH) = 0x84;
                   15875:        } else if(REG8(AL) == 0x02) {
                   15876:                REG16(BX) = (2 + 2) * 4;
                   15877:                REG8(AH) = 0x00;
                   15878:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15879:                REG8(AH) = 0x83;
                   15880:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15881:                UINT16 call_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  0);
                   15882:                UINT16 call_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  2);
                   15883:                UINT8  new_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  4);
                   15884:                UINT16 new_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  5);
                   15885:                UINT16 new_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  7);
                   15886: #if 0
                   15887:                UINT8  old_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  9);
                   15888:                UINT16 old_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 10);
                   15889:                UINT16 old_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 12);
                   15890: #endif
                   15891:                UINT16 handles[4], pages[4];
                   15892:                
                   15893:                // alter page map and call routine is at fffc:001f
                   15894:                if(!(call_seg == 0 && call_ofs == 0)) {
                   15895:                        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   15896:                        mem[DUMMY_TOP + 0x20] = (call_ofs >> 0) & 0xff;
                   15897:                        mem[DUMMY_TOP + 0x21] = (call_ofs >> 8) & 0xff;
                   15898:                        mem[DUMMY_TOP + 0x22] = (call_seg >> 0) & 0xff;
                   15899:                        mem[DUMMY_TOP + 0x23] = (call_seg >> 8) & 0xff;
                   15900:                } else {
                   15901:                        // invalid call addr :-(
                   15902:                        mem[DUMMY_TOP + 0x1f] = 0x90;   // nop
                   15903:                        mem[DUMMY_TOP + 0x20] = 0x90;   // nop
                   15904:                        mem[DUMMY_TOP + 0x21] = 0x90;   // nop
                   15905:                        mem[DUMMY_TOP + 0x22] = 0x90;   // nop
                   15906:                        mem[DUMMY_TOP + 0x23] = 0x90;   // nop
                   15907:                }
                   15908:                // do call far (push cs/ip) in old mapping
                   15909:                i386_call_far(DUMMY_TOP >> 4, 0x001f);
                   15910:                
                   15911:                // get old mapping data
                   15912: #if 0
                   15913:                for(int i = 0; i < (int)old_entries; i++) {
                   15914:                        int logical  = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 0);
                   15915:                        int physical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 2);
                   15916:                        
                   15917:                        if(REG8(AL) == 0x01) {
                   15918:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15919:                        }
                   15920: //                     if(!(physical < 4)) {
                   15921: //                             REG8(AH) = 0x8b;
                   15922: //                             return;
                   15923: //                     } else
                   15924:                        if(logical == 0xffff) {
                   15925:                                ems_unmap_page(physical & 3);
                   15926:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15927:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15928:                        } else {
                   15929:                                REG8(AH) = 0x8a;
                   15930:                                return;
                   15931:                        }
                   15932:                }
                   15933: #endif
                   15934:                for(int i = 0; i < 4; i++) {
                   15935:                        handles[i] = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15936:                        pages  [i] = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15937:                }
                   15938:                
                   15939:                // set new mapping
                   15940:                for(int i = 0; i < (int)new_entries; i++) {
                   15941:                        int logical  = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 0);
                   15942:                        int physical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 2);
                   15943:                        
                   15944:                        if(REG8(AL) == 0x01) {
                   15945:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15946:                        }
                   15947: //                     if(!(physical < 4)) {
                   15948: //                             REG8(AH) = 0x8b;
                   15949: //                             return;
                   15950: //                     } else
                   15951:                        if(logical == 0xffff) {
                   15952:                                ems_unmap_page(physical & 3);
                   15953:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15954:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15955:                        } else {
                   15956:                                REG8(AH) = 0x8a;
                   15957:                                return;
                   15958:                        }
                   15959:                }
                   15960:                
                   15961:                // push old mapping data in new mapping
                   15962:                for(int i = 0; i < 4; i++) {
                   15963:                        i386_push16(handles[i]);
                   15964:                        i386_push16(pages  [i]);
                   15965:                }
                   15966:                REG8(AH) = 0x00;
                   15967:        } else {
                   15968:                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));
                   15969:                REG8(AH) = 0x8f;
                   15970:        }
                   15971: }
                   15972: 
1.1.1.20  root     15973: inline void msdos_int_67h_57h_tmp()
                   15974: {
                   15975:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15976:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15977:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   15978:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   15979:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   15980:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   15981:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15982:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   15983:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   15984:        
1.1.1.32  root     15985:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     15986:        UINT32 src_addr, dest_addr;
                   15987:        UINT32 src_addr_max, dest_addr_max;
                   15988:        
                   15989:        if(src_type == 0) {
                   15990:                src_buffer = mem;
                   15991:                src_addr = (src_seg << 4) + src_ofs;
                   15992:                src_addr_max = MAX_MEM;
                   15993:        } else {
1.1.1.31  root     15994:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     15995:                        REG8(AH) = 0x83;
                   15996:                        return;
                   15997:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   15998:                        REG8(AH) = 0x8a;
                   15999:                        return;
                   16000:                }
1.1.1.32  root     16001:                if(ems_handles[src_handle].buffer != NULL) {
                   16002:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   16003:                }
1.1.1.20  root     16004:                src_addr = src_ofs;
1.1.1.32  root     16005:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     16006:        }
                   16007:        if(dest_type == 0) {
                   16008:                dest_buffer = mem;
                   16009:                dest_addr = (dest_seg << 4) + dest_ofs;
                   16010:                dest_addr_max = MAX_MEM;
                   16011:        } else {
1.1.1.31  root     16012:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     16013:                        REG8(AH) = 0x83;
                   16014:                        return;
                   16015:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   16016:                        REG8(AH) = 0x8a;
                   16017:                        return;
                   16018:                }
1.1.1.32  root     16019:                if(ems_handles[dest_handle].buffer != NULL) {
                   16020:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   16021:                }
1.1.1.20  root     16022:                dest_addr = dest_ofs;
1.1.1.32  root     16023:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     16024:        }
1.1.1.32  root     16025:        if(src_buffer != NULL && dest_buffer != NULL) {
                   16026:                for(int i = 0; i < copy_length; i++) {
                   16027:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   16028:                                if(REG8(AL) == 0x00) {
                   16029:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   16030:                                } else if(REG8(AL) == 0x01) {
                   16031:                                        UINT8 tmp = dest_buffer[dest_addr];
                   16032:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   16033:                                        src_buffer[src_addr++] = tmp;
                   16034:                                }
                   16035:                        } else {
                   16036:                                REG8(AH) = 0x93;
                   16037:                                return;
1.1.1.20  root     16038:                        }
                   16039:                }
1.1.1.32  root     16040:                REG8(AH) = 0x00;
                   16041:        } else {
                   16042:                REG8(AH) = 0x80;
1.1.1.20  root     16043:        }
                   16044: }
                   16045: 
                   16046: inline void msdos_int_67h_57h()
                   16047: {
                   16048:        if(!support_ems) {
                   16049:                REG8(AH) = 0x84;
                   16050:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   16051:                struct {
                   16052:                        UINT16 handle;
                   16053:                        UINT16 page;
                   16054:                        bool mapped;
                   16055:                } tmp_pages[4];
                   16056:                
                   16057:                // unmap pages to copy memory data to ems buffer
                   16058:                for(int i = 0; i < 4; i++) {
                   16059:                        tmp_pages[i].handle = ems_pages[i].handle;
                   16060:                        tmp_pages[i].page   = ems_pages[i].page;
                   16061:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   16062:                        ems_unmap_page(i);
                   16063:                }
                   16064:                
                   16065:                // run move/exchange operation
                   16066:                msdos_int_67h_57h_tmp();
                   16067:                
                   16068:                // restore unmapped pages
                   16069:                for(int i = 0; i < 4; i++) {
                   16070:                        if(tmp_pages[i].mapped) {
                   16071:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   16072:                        }
                   16073:                }
                   16074:        } else {
1.1.1.22  root     16075:                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     16076:                REG8(AH) = 0x8f;
                   16077:        }
                   16078: }
                   16079: 
                   16080: inline void msdos_int_67h_58h()
                   16081: {
                   16082:        if(!support_ems) {
                   16083:                REG8(AH) = 0x84;
                   16084:        } else if(REG8(AL) == 0x00) {
                   16085:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     16086:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   16087:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     16088:                }
                   16089:                REG8(AH) = 0x00;
                   16090:                REG16(CX) = 4;
                   16091:        } else if(REG8(AL) == 0x01) {
                   16092:                REG8(AH) = 0x00;
                   16093:                REG16(CX) = 4;
                   16094:        } else {
1.1.1.22  root     16095:                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     16096:                REG8(AH) = 0x8f;
                   16097:        }
                   16098: }
                   16099: 
1.1.1.42  root     16100: inline void msdos_int_67h_59h()
                   16101: {
                   16102:        if(!support_ems) {
                   16103:                REG8(AH) = 0x84;
                   16104:        } else if(REG8(AL) == 0x00) {
1.1.1.49  root     16105:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1024;
                   16106:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 0;
                   16107:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 4 * 4;
                   16108:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = 0;
                   16109:                REG8(AH) = 0x00;
                   16110: //             REG8(AH) = 0xa4; // access denied by operating system
1.1.1.42  root     16111:        } else if(REG8(AL) == 0x01) {
                   16112:                REG8(AH) = 0x00;
                   16113:                REG16(BX) = free_ems_pages;
                   16114:                REG16(DX) = MAX_EMS_PAGES;
                   16115:        } else {
                   16116:                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));
                   16117:                REG8(AH) = 0x8f;
                   16118:        }
                   16119: }
                   16120: 
1.1.1.20  root     16121: inline void msdos_int_67h_5ah()
                   16122: {
                   16123:        if(!support_ems) {
1.1.1.19  root     16124:                REG8(AH) = 0x84;
1.1.1.20  root     16125:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   16126:                REG8(AH) = 0x87;
                   16127:        } else if(REG16(BX) > free_ems_pages) {
                   16128:                REG8(AH) = 0x88;
                   16129: //     } else if(REG16(BX) == 0) {
                   16130: //             REG8(AH) = 0x89;
                   16131:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     16132:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     16133:                        if(!ems_handles[i].allocated) {
                   16134:                                ems_allocate_pages(i, REG16(BX));
                   16135:                                REG8(AH) = 0x00;
                   16136:                                REG16(DX) = i;
                   16137:                                return;
                   16138:                        }
                   16139:                }
                   16140:                REG8(AH) = 0x85;
                   16141:        } else {
1.1.1.22  root     16142:                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     16143:                REG8(AH) = 0x8f;
1.1.1.19  root     16144:        }
                   16145: }
                   16146: 
1.1.1.49  root     16147: inline void msdos_int_67h_5bh()
                   16148: {
                   16149:        static UINT8  stored_bl = 0x00;
                   16150:        static UINT16 stored_es = 0x0000;
                   16151:        static UINT16 stored_di = 0x0000;
                   16152:        
                   16153:        if(!support_ems) {
                   16154:                REG8(AH) = 0x84;
                   16155:        } else if(REG8(AL) == 0x00) {
                   16156:                if(stored_bl == 0x00) {
                   16157:                        if(!(stored_es == 0 && stored_di == 0)) {
                   16158:                                for(int i = 0; i < 4; i++) {
                   16159:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   16160:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   16161:                                }
                   16162:                        }
                   16163:                        SREG(ES) = stored_es;
                   16164:                        i386_load_segment_descriptor(ES);
                   16165:                        REG16(DI) = stored_di;
                   16166:                } else {
                   16167:                        REG8(BL) = stored_bl;
                   16168:                }
                   16169:                REG8(AH) = 0x00;
                   16170:        } else if(REG8(AL) == 0x01) {
                   16171:                if(REG8(BL) == 0x00) {
                   16172:                        if(!(SREG(ES) == 0 && REG16(DI) == 0)) {
                   16173:                                for(int i = 0; i < 4; i++) {
                   16174:                                        UINT16 handle = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0);
                   16175:                                        UINT16 page   = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2);
                   16176:                                        
                   16177:                                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   16178:                                                ems_map_page(i, handle, page);
                   16179:                                        } else {
                   16180:                                                ems_unmap_page(i);
                   16181:                                        }
                   16182:                                }
                   16183:                        }
                   16184:                }
                   16185:                stored_bl = REG8(BL);
                   16186:                stored_es = SREG(ES);
                   16187:                stored_di = REG16(DI);
                   16188:                REG8(AH) = 0x00;
                   16189:        } else if(REG8(AL) == 0x02) {
                   16190:                REG16(DX) = 4 * 4;
                   16191:                REG8(AH) = 0x00;
                   16192:        } else if(REG8(AL) == 0x03) {
                   16193:                REG8(BL) = 0x00; // not supported
                   16194:                REG8(AH) = 0x00;
                   16195:        } else if(REG8(AL) == 0x04) {
                   16196:                REG8(AH) = 0x00;
                   16197:        } else if(REG8(AL) == 0x05) {
                   16198:                REG8(BL) = 0x00; // not supported
                   16199:                REG8(AH) = 0x00;
                   16200:        } else {
                   16201:                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));
                   16202:                REG8(AH) = 0x8f;
                   16203:        }
                   16204: }
                   16205: 
1.1.1.43  root     16206: inline void msdos_int_67h_5dh()
                   16207: {
                   16208:        if(!support_ems) {
                   16209:                REG8(AH) = 0x84;
                   16210:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   16211:                REG8(AH) = 0xa4; // operating system denied access
                   16212:        } else {
                   16213:                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));
                   16214:                REG8(AH) = 0x8f;
                   16215:        }
                   16216: }
                   16217: 
1.1.1.49  root     16218: inline void msdos_int_67h_70h()
                   16219: {
                   16220:        if(!support_ems) {
                   16221:                REG8(AH) = 0x84;
                   16222:        } else if(REG8(AL) == 0x00) {
                   16223:                REG8(AL) = 0x00;
                   16224:                REG8(AH) = 0x00;
                   16225:        } else if(REG8(AL) == 0x01) {
                   16226:                REG8(AL) = 0x00;
                   16227: //             REG8(AH) = (REG8(BL) == 0x00) ? 0x00 : 0x80;
                   16228:                REG8(AH) = 0x00;
                   16229:        } else {
                   16230:                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));
                   16231:                REG8(AH) = 0x8f;
                   16232:        }
                   16233: }
                   16234: 
1.1.1.30  root     16235: inline void msdos_int_67h_deh()
                   16236: {
                   16237:        REG8(AH) = 0x84;
                   16238: }
                   16239: 
1.1.1.19  root     16240: #ifdef SUPPORT_XMS
                   16241: 
1.1.1.32  root     16242: void msdos_xms_init()
1.1.1.26  root     16243: {
1.1.1.30  root     16244:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   16245:        emb_handle_top->address = EMB_TOP;
                   16246:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     16247:        xms_a20_local_enb_count = 0;
                   16248: }
                   16249: 
1.1.1.32  root     16250: void msdos_xms_finish()
                   16251: {
                   16252:        msdos_xms_release();
                   16253: }
                   16254: 
                   16255: void msdos_xms_release()
1.1.1.30  root     16256: {
                   16257:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   16258:                emb_handle_t *next_handle = emb_handle->next;
                   16259:                free(emb_handle);
                   16260:                emb_handle = next_handle;
                   16261:        }
                   16262: }
                   16263: 
                   16264: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   16265: {
                   16266:        if(handle != 0) {
                   16267:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16268:                        if(emb_handle->handle == handle) {
                   16269:                                return(emb_handle);
                   16270:                        }
                   16271:                }
                   16272:        }
                   16273:        return(NULL);
                   16274: }
                   16275: 
                   16276: int msdos_xms_get_unused_emb_handle_id()
                   16277: {
                   16278:        for(int handle = 1;; handle++) {
                   16279:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   16280:                        return(handle);
                   16281:                }
                   16282:        }
                   16283:        return(0);
                   16284: }
                   16285: 
                   16286: int msdos_xms_get_unused_emb_handle_count()
                   16287: {
                   16288:        int count = 64; //255;
                   16289:        
                   16290:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16291:                if(emb_handle->handle != 0) {
                   16292:                        if(--count == 1) {
                   16293:                                break;
                   16294:                        }
                   16295:                }
                   16296:        }
                   16297:        return(count);
                   16298: }
                   16299: 
                   16300: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   16301: {
                   16302:        if(emb_handle->size_kb > size_kb) {
                   16303:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   16304:                
                   16305:                new_handle->address = emb_handle->address + size_kb * 1024;
                   16306:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   16307:                emb_handle->size_kb = size_kb;
                   16308:                
                   16309:                new_handle->prev = emb_handle;
                   16310:                new_handle->next = emb_handle->next;
                   16311:                if(emb_handle->next != NULL) {
                   16312:                        emb_handle->next->prev = new_handle;
                   16313:                }
                   16314:                emb_handle->next = new_handle;
                   16315:        }
                   16316: }
                   16317: 
                   16318: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   16319: {
                   16320:        emb_handle_t *next_handle = emb_handle->next;
                   16321:        
                   16322:        if(next_handle != NULL) {
                   16323:                emb_handle->size_kb += next_handle->size_kb;
                   16324:                
                   16325:                if(next_handle->next != NULL) {
                   16326:                        next_handle->next->prev = emb_handle;
                   16327:                }
                   16328:                emb_handle->next = next_handle->next;
                   16329:                free(next_handle);
                   16330:        }
                   16331: }
                   16332: 
                   16333: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   16334: {
                   16335:        emb_handle_t *target_handle = NULL;
                   16336:        
                   16337:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16338:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   16339:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   16340:                                target_handle = emb_handle;
                   16341:                        }
                   16342:                }
                   16343:        }
                   16344:        if(target_handle != NULL) {
                   16345:                if(target_handle->size_kb > size_kb) {
                   16346:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   16347:                }
                   16348: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16349:                return(target_handle);
                   16350:        }
                   16351:        return(NULL);
                   16352: }
                   16353: 
                   16354: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   16355: {
                   16356:        emb_handle_t *prev_handle = emb_handle->prev;
                   16357:        emb_handle_t *next_handle = emb_handle->next;
                   16358:        
                   16359:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   16360:                msdos_xms_combine_emb_handles(prev_handle);
                   16361:                emb_handle = prev_handle;
                   16362:        }
                   16363:        if(next_handle != NULL && next_handle->handle == 0) {
                   16364:                msdos_xms_combine_emb_handles(emb_handle);
                   16365:        }
                   16366:        emb_handle->handle = 0;
                   16367: }
                   16368: 
1.1.1.19  root     16369: inline void msdos_call_xms_00h()
                   16370: {
1.1.1.29  root     16371: #if defined(HAS_I386)
                   16372:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.45  root     16373: //     REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
1.1.1.29  root     16374:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   16375: #else
                   16376:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   16377:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   16378: #endif
                   16379: //     REG16(DX) = 0x0000; // HMA does not exist
                   16380:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     16381: }
                   16382: 
                   16383: inline void msdos_call_xms_01h()
                   16384: {
1.1.1.29  root     16385:        if(REG8(AL) == 0x40) {
                   16386:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   16387:                // DX=KB free extended memory returned by last call of function 08h
                   16388:                REG16(AX) = 0x0000;
                   16389:                REG8(BL) = 0x91;
                   16390:                REG16(DX) = xms_dx_after_call_08h;
                   16391:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16392:                REG16(AX) = 0x0000;
                   16393:                REG8(BL) = 0x81; // Vdisk was detected
                   16394: #ifdef SUPPORT_HMA
                   16395:        } else if(is_hma_used_by_int_2fh) {
                   16396:                REG16(AX) = 0x0000;
                   16397:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16398:        } else if(is_hma_used_by_xms) {
                   16399:                REG16(AX) = 0x0000;
                   16400:                REG8(BL) = 0x91; // HMA is already in use
                   16401:        } else {
                   16402:                REG16(AX) = 0x0001;
                   16403:                is_hma_used_by_xms = true;
                   16404: #else
                   16405:        } else {
                   16406:                REG16(AX) = 0x0000;
                   16407:                REG8(BL) = 0x91; // HMA is already in use
                   16408: #endif
                   16409:        }
1.1.1.19  root     16410: }
                   16411: 
                   16412: inline void msdos_call_xms_02h()
                   16413: {
1.1.1.29  root     16414:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16415:                REG16(AX) = 0x0000;
                   16416:                REG8(BL) = 0x81; // Vdisk was detected
                   16417: #ifdef SUPPORT_HMA
                   16418:        } else if(is_hma_used_by_int_2fh) {
                   16419:                REG16(AX) = 0x0000;
                   16420:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16421:        } else if(!is_hma_used_by_xms) {
                   16422:                REG16(AX) = 0x0000;
                   16423:                REG8(BL) = 0x93; // HMA is not allocated
                   16424:        } else {
                   16425:                REG16(AX) = 0x0001;
                   16426:                is_hma_used_by_xms = false;
                   16427:                // restore first free mcb in high memory area
                   16428:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   16429: #else
                   16430:        } else {
                   16431:                REG16(AX) = 0x0000;
                   16432:                REG8(BL) = 0x91; // HMA is already in use
                   16433: #endif
                   16434:        }
1.1.1.19  root     16435: }
                   16436: 
                   16437: inline void msdos_call_xms_03h()
                   16438: {
                   16439:        i386_set_a20_line(1);
                   16440:        REG16(AX) = 0x0001;
                   16441:        REG8(BL) = 0x00;
                   16442: }
                   16443: 
                   16444: inline void msdos_call_xms_04h()
                   16445: {
1.1.1.21  root     16446:        i386_set_a20_line(0);
                   16447:        REG16(AX) = 0x0001;
                   16448:        REG8(BL) = 0x00;
1.1.1.19  root     16449: }
                   16450: 
                   16451: inline void msdos_call_xms_05h()
                   16452: {
                   16453:        i386_set_a20_line(1);
                   16454:        REG16(AX) = 0x0001;
                   16455:        REG8(BL) = 0x00;
1.1.1.21  root     16456:        xms_a20_local_enb_count++;
1.1.1.19  root     16457: }
                   16458: 
                   16459: void msdos_call_xms_06h()
                   16460: {
1.1.1.21  root     16461:        if(xms_a20_local_enb_count > 0) {
1.1.1.45  root     16462:                if(--xms_a20_local_enb_count == 0) {
                   16463:                        i386_set_a20_line(0);
                   16464:                        REG16(AX) = 0x0001;
                   16465:                        REG8(BL) = 0x00;
                   16466:                } else {
                   16467:                        REG16(AX) = 0x0000;
                   16468:                        REG8(BL) = 0x94;
                   16469:                }
1.1.1.21  root     16470:        } else {
1.1.1.45  root     16471:                i386_set_a20_line(0);
1.1.1.21  root     16472:                REG16(AX) = 0x0001;
                   16473:                REG8(BL) = 0x00;
1.1.1.19  root     16474:        }
                   16475: }
                   16476: 
                   16477: inline void msdos_call_xms_07h()
                   16478: {
                   16479:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   16480:        REG8(BL) = 0x00;
                   16481: }
                   16482: 
                   16483: inline void msdos_call_xms_08h()
                   16484: {
1.1.1.45  root     16485:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     16486:        
1.1.1.30  root     16487:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16488:                if(emb_handle->handle == 0) {
1.1.1.45  root     16489:                        if(eax < emb_handle->size_kb) {
                   16490:                                eax = emb_handle->size_kb;
1.1.1.19  root     16491:                        }
1.1.1.45  root     16492:                        edx += emb_handle->size_kb;
1.1.1.19  root     16493:                }
                   16494:        }
1.1.1.45  root     16495:        if(eax > 65535) {
                   16496:                eax = 65535;
                   16497:        }
                   16498:        if(edx > 65535) {
                   16499:                edx = 65535;
                   16500:        }
                   16501:        if(eax == 0 && edx == 0) {
1.1.1.19  root     16502:                REG8(BL) = 0xa0;
                   16503:        } else {
                   16504:                REG8(BL) = 0x00;
                   16505:        }
1.1.1.45  root     16506: #if defined(HAS_I386)
                   16507:        REG32(EAX) = eax;
                   16508:        REG32(EDX) = edx;
                   16509: #else
                   16510:        REG16(AX) = (UINT16)eax;
                   16511:        REG16(DX) = (UINT16)edx;
                   16512: #endif
1.1.1.29  root     16513:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     16514: }
                   16515: 
1.1.1.30  root     16516: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     16517: {
1.1.1.30  root     16518:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   16519:        
                   16520:        if(emb_handle != NULL) {
                   16521:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16522:                
                   16523:                REG16(AX) = 0x0001;
                   16524:                REG16(DX) = emb_handle->handle;
                   16525:                REG8(BL) = 0x00;
                   16526:        } else {
                   16527:                REG16(AX) = REG16(DX) = 0x0000;
                   16528:                REG8(BL) = 0xa0;
1.1.1.19  root     16529:        }
1.1.1.30  root     16530: }
                   16531: 
                   16532: inline void msdos_call_xms_09h()
                   16533: {
                   16534:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     16535: }
                   16536: 
                   16537: inline void msdos_call_xms_0ah()
                   16538: {
1.1.1.30  root     16539:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16540:        
                   16541:        if(emb_handle == NULL) {
1.1.1.19  root     16542:                REG16(AX) = 0x0000;
                   16543:                REG8(BL) = 0xa2;
1.1.1.45  root     16544: //     } else if(emb_handle->lock > 0) {
                   16545: //             REG16(AX) = 0x0000;
                   16546: //             REG8(BL) = 0xab;
1.1.1.19  root     16547:        } else {
1.1.1.30  root     16548:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     16549:                
                   16550:                REG16(AX) = 0x0001;
                   16551:                REG8(BL) = 0x00;
                   16552:        }
                   16553: }
                   16554: 
                   16555: inline void msdos_call_xms_0bh()
                   16556: {
                   16557:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   16558:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   16559:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   16560:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   16561:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   16562:        
                   16563:        UINT8 *src_buffer, *dest_buffer;
                   16564:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     16565:        emb_handle_t *emb_handle;
1.1.1.19  root     16566:        
                   16567:        if(src_handle == 0) {
                   16568:                src_buffer = mem;
                   16569:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   16570:                src_addr_max = MAX_MEM;
                   16571:        } else {
1.1.1.30  root     16572:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     16573:                        REG16(AX) = 0x0000;
                   16574:                        REG8(BL) = 0xa3;
                   16575:                        return;
                   16576:                }
1.1.1.30  root     16577:                src_buffer = mem + emb_handle->address;
                   16578:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16579:        }
                   16580:        if(dest_handle == 0) {
                   16581:                dest_buffer = mem;
                   16582:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   16583:                dest_addr_max = MAX_MEM;
                   16584:        } else {
1.1.1.30  root     16585:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     16586:                        REG16(AX) = 0x0000;
                   16587:                        REG8(BL) = 0xa5;
                   16588:                        return;
                   16589:                }
1.1.1.30  root     16590:                dest_buffer = mem + emb_handle->address;
                   16591:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16592:        }
                   16593:        for(int i = 0; i < copy_length; i++) {
                   16594:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   16595:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   16596:                } else {
                   16597:                        break;
                   16598:                }
                   16599:        }
                   16600:        REG16(AX) = 0x0001;
                   16601:        REG8(BL) = 0x00;
                   16602: }
                   16603: 
                   16604: inline void msdos_call_xms_0ch()
                   16605: {
1.1.1.30  root     16606:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16607:        
                   16608:        if(emb_handle == NULL) {
1.1.1.19  root     16609:                REG16(AX) = 0x0000;
                   16610:                REG8(BL) = 0xa2;
                   16611:        } else {
1.1.1.45  root     16612:                if(emb_handle->lock < 255) {
                   16613:                        emb_handle->lock++;
                   16614:                }
1.1.1.19  root     16615:                REG16(AX) = 0x0001;
1.1.1.30  root     16616:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   16617:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     16618:        }
                   16619: }
                   16620: 
                   16621: inline void msdos_call_xms_0dh()
                   16622: {
1.1.1.30  root     16623:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16624:        
                   16625:        if(emb_handle == NULL) {
1.1.1.19  root     16626:                REG16(AX) = 0x0000;
                   16627:                REG8(BL) = 0xa2;
1.1.1.30  root     16628:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     16629:                REG16(AX) = 0x0000;
                   16630:                REG8(BL) = 0xaa;
                   16631:        } else {
1.1.1.30  root     16632:                emb_handle->lock--;
1.1.1.19  root     16633:                REG16(AX) = 0x0001;
                   16634:                REG8(BL) = 0x00;
                   16635:        }
                   16636: }
                   16637: 
                   16638: inline void msdos_call_xms_0eh()
                   16639: {
1.1.1.30  root     16640:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16641:        
                   16642:        if(emb_handle == NULL) {
1.1.1.19  root     16643:                REG16(AX) = 0x0000;
                   16644:                REG8(BL) = 0xa2;
                   16645:        } else {
                   16646:                REG16(AX) = 0x0001;
1.1.1.30  root     16647:                REG8(BH) = emb_handle->lock;
                   16648:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   16649:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     16650:        }
                   16651: }
                   16652: 
1.1.1.30  root     16653: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     16654: {
1.1.1.30  root     16655:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16656:        
                   16657:        if(emb_handle == NULL) {
1.1.1.19  root     16658:                REG16(AX) = 0x0000;
                   16659:                REG8(BL) = 0xa2;
1.1.1.30  root     16660:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     16661:                REG16(AX) = 0x0000;
                   16662:                REG8(BL) = 0xab;
                   16663:        } else {
1.1.1.30  root     16664:                if(emb_handle->size_kb < size_kb) {
                   16665:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   16666:                                msdos_xms_combine_emb_handles(emb_handle);
                   16667:                                if(emb_handle->size_kb > size_kb) {
                   16668:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16669:                                }
                   16670:                        } else {
                   16671:                                int old_handle = emb_handle->handle;
                   16672:                                int old_size_kb = emb_handle->size_kb;
                   16673:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   16674:                                
                   16675:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   16676:                                msdos_xms_free_emb_handle(emb_handle);
                   16677:                                
                   16678:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   16679:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   16680:                                }
                   16681:                                emb_handle->handle = old_handle;
                   16682:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   16683:                                free(buffer);
                   16684:                        }
                   16685:                } else if(emb_handle->size_kb > size_kb) {
                   16686:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16687:                }
                   16688:                if(emb_handle->size_kb != size_kb) {
                   16689:                        REG16(AX) = 0x0000;
                   16690:                        REG8(BL) = 0xa0;
                   16691:                } else {
                   16692:                        REG16(AX) = 0x0001;
                   16693:                        REG8(BL) = 0x00;
                   16694:                }
1.1.1.19  root     16695:        }
                   16696: }
                   16697: 
1.1.1.30  root     16698: inline void msdos_call_xms_0fh()
                   16699: {
                   16700:        msdos_call_xms_0fh(REG16(BX));
                   16701: }
                   16702: 
1.1.1.19  root     16703: inline void msdos_call_xms_10h()
                   16704: {
                   16705:        int seg;
                   16706:        
                   16707:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   16708:                REG16(AX) = 0x0001;
                   16709:                REG16(BX) = seg;
                   16710:        } else {
                   16711:                REG16(AX) = 0x0000;
                   16712:                REG8(BL) = 0xb0;
                   16713:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   16714:        }
                   16715: }
                   16716: 
                   16717: inline void msdos_call_xms_11h()
                   16718: {
                   16719:        int mcb_seg = REG16(DX) - 1;
                   16720:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16721:        
                   16722:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16723:                msdos_mem_free(REG16(DX));
                   16724:                REG16(AX) = 0x0001;
                   16725:                REG8(BL) = 0x00;
                   16726:        } else {
                   16727:                REG16(AX) = 0x0000;
                   16728:                REG8(BL) = 0xb2;
                   16729:        }
                   16730: }
                   16731: 
                   16732: inline void msdos_call_xms_12h()
                   16733: {
                   16734:        int mcb_seg = REG16(DX) - 1;
                   16735:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16736:        int max_paragraphs;
                   16737:        
                   16738:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16739:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   16740:                        REG16(AX) = 0x0001;
                   16741:                        REG8(BL) = 0x00;
                   16742:                } else {
                   16743:                        REG16(AX) = 0x0000;
                   16744:                        REG8(BL) = 0xb0;
                   16745:                        REG16(DX) = max_paragraphs;
                   16746:                }
                   16747:        } else {
                   16748:                REG16(AX) = 0x0000;
                   16749:                REG8(BL) = 0xb2;
                   16750:        }
                   16751: }
                   16752: 
1.1.1.29  root     16753: #if defined(HAS_I386)
                   16754: 
                   16755: inline void msdos_call_xms_88h()
                   16756: {
                   16757:        REG32(EAX) = REG32(EDX) = 0x0000;
                   16758:        
1.1.1.30  root     16759:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16760:                if(emb_handle->handle == 0) {
                   16761:                        if(REG32(EAX) < emb_handle->size_kb) {
                   16762:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     16763:                        }
1.1.1.30  root     16764:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     16765:                }
                   16766:        }
                   16767:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   16768:                REG8(BL) = 0xa0;
                   16769:        } else {
                   16770:                REG8(BL) = 0x00;
                   16771:        }
                   16772:        REG32(ECX) = EMB_END - 1;
                   16773: }
                   16774: 
                   16775: inline void msdos_call_xms_89h()
                   16776: {
1.1.1.30  root     16777:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     16778: }
                   16779: 
                   16780: inline void msdos_call_xms_8eh()
                   16781: {
1.1.1.30  root     16782:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16783:        
                   16784:        if(emb_handle == NULL) {
1.1.1.29  root     16785:                REG16(AX) = 0x0000;
                   16786:                REG8(BL) = 0xa2;
                   16787:        } else {
                   16788:                REG16(AX) = 0x0001;
1.1.1.30  root     16789:                REG8(BH) = emb_handle->lock;
                   16790:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   16791:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     16792:        }
                   16793: }
                   16794: 
                   16795: inline void msdos_call_xms_8fh()
                   16796: {
1.1.1.30  root     16797:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     16798: }
                   16799: 
                   16800: #endif
1.1.1.19  root     16801: #endif
                   16802: 
1.1.1.26  root     16803: UINT16 msdos_get_equipment()
                   16804: {
                   16805:        static UINT16 equip = 0;
                   16806:        
                   16807:        if(equip == 0) {
                   16808: #ifdef SUPPORT_FPU
                   16809:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   16810: #endif
                   16811:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   16812:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   16813: //             equip |= (1 << 8);      // 0 if DMA installed
                   16814:                equip |= (2 << 9);      // number of serial ports
                   16815:                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     16816:                
                   16817:                // check only A: and B: if it is floppy drive
                   16818:                int n = 0;
                   16819:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     16820:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   16821:                                n++;
1.1.1.28  root     16822:                        }
                   16823:                }
                   16824:                if(n != 0) {
                   16825:                        equip |= (1 << 0);      // floppy disk(s) installed
                   16826:                        n--;
                   16827:                        equip |= (n << 6);      // number of floppies installed less 1
                   16828:                }
                   16829: //             if(joyGetNumDevs() != 0) {
                   16830: //                     equip |= (1 << 12);     // game port installed
                   16831: //             }
1.1.1.26  root     16832:        }
                   16833:        return(equip);
                   16834: }
                   16835: 
1.1       root     16836: void msdos_syscall(unsigned num)
                   16837: {
1.1.1.22  root     16838: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     16839:        if(num == 0x08 || num == 0x1c) {
                   16840:                // don't log the timer interrupts
1.1.1.45  root     16841: //             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     16842:        } else if(num == 0x30) {
                   16843:                // dummy interrupt for call 0005h (call near)
                   16844:                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     16845:        } else if(num == 0x65) {
1.1.1.22  root     16846:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     16847:                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     16848:        } else if(num == 0x66) {
1.1.1.22  root     16849:                // dummy interrupt for XMS (call far)
1.1.1.33  root     16850:                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     16851:        } else if(num >= 0x68 && num <= 0x6f) {
1.1.1.45  root     16852:                // dummy interrupt
1.1.1.22  root     16853:        } else {
1.1.1.33  root     16854:                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     16855:        }
                   16856: #endif
1.1.1.36  root     16857:        // update cursor position
                   16858:        if(cursor_moved) {
                   16859:                pcbios_update_cursor_position();
                   16860:                cursor_moved = false;
                   16861:        }
1.1.1.50  root     16862: #ifdef USE_SERVICE_THREAD
                   16863:        // this is called from dummy loop to wait until a serive that waits input is done
                   16864:        if(!in_service)
                   16865: #endif
1.1.1.33  root     16866:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     16867:        
1.1       root     16868:        switch(num) {
                   16869:        case 0x00:
1.1.1.28  root     16870:                try {
                   16871:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16872:                        error("division by zero\n");
                   16873:                } catch(...) {
                   16874:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   16875:                }
1.1       root     16876:                break;
                   16877:        case 0x04:
1.1.1.28  root     16878:                try {
                   16879:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16880:                        error("overflow\n");
                   16881:                } catch(...) {
                   16882:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   16883:                }
1.1       root     16884:                break;
                   16885:        case 0x06:
                   16886:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     16887:                if(!ignore_illegal_insn) {
1.1.1.28  root     16888:                        try {
                   16889:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16890:                                error("illegal instruction\n");
                   16891:                        } catch(...) {
                   16892:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   16893:                        }
1.1.1.14  root     16894:                } else {
                   16895: #if defined(HAS_I386)
1.1.1.39  root     16896:                        m_eip = m_int6h_skip_eip;
                   16897: #elif defined(HAS_I286)
                   16898:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     16899: #else
1.1.1.39  root     16900:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     16901: #endif
                   16902:                }
1.1       root     16903:                break;
1.1.1.33  root     16904:        case 0x09:
                   16905:                // ctrl-break is pressed
                   16906:                if(raise_int_1bh) {
                   16907: #if defined(HAS_I386)
                   16908:                        m_ext = 0; // not an external interrupt
                   16909:                        i386_trap(0x1b, 1, 0);
                   16910:                        m_ext = 1;
                   16911: #else
                   16912:                        PREFIX86(_interrupt)(0x1b);
                   16913: #endif
                   16914:                        raise_int_1bh = false;
                   16915:                }
1.1.1.8   root     16916:        case 0x08:
1.1.1.14  root     16917: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     16918:        case 0x0b:
                   16919:        case 0x0c:
                   16920:        case 0x0d:
                   16921:        case 0x0e:
                   16922:        case 0x0f:
                   16923:                // EOI
                   16924:                pic[0].isr &= ~(1 << (num - 0x08));
                   16925:                pic_update();
                   16926:                break;
1.1       root     16927:        case 0x10:
                   16928:                // PC BIOS - Video
1.1.1.14  root     16929:                if(!restore_console_on_exit) {
1.1.1.15  root     16930:                        change_console_size(scr_width, scr_height);
1.1       root     16931:                }
1.1.1.3   root     16932:                m_CF = 0;
1.1       root     16933:                switch(REG8(AH)) {
1.1.1.16  root     16934:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     16935:                case 0x01: pcbios_int_10h_01h(); break;
                   16936:                case 0x02: pcbios_int_10h_02h(); break;
                   16937:                case 0x03: pcbios_int_10h_03h(); break;
                   16938:                case 0x05: pcbios_int_10h_05h(); break;
                   16939:                case 0x06: pcbios_int_10h_06h(); break;
                   16940:                case 0x07: pcbios_int_10h_07h(); break;
                   16941:                case 0x08: pcbios_int_10h_08h(); break;
                   16942:                case 0x09: pcbios_int_10h_09h(); break;
                   16943:                case 0x0a: pcbios_int_10h_0ah(); break;
                   16944:                case 0x0b: break;
1.1.1.40  root     16945:                case 0x0c: pcbios_int_10h_0ch(); break;
                   16946:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     16947:                case 0x0e: pcbios_int_10h_0eh(); break;
                   16948:                case 0x0f: pcbios_int_10h_0fh(); break;
                   16949:                case 0x10: break;
1.1.1.14  root     16950:                case 0x11: pcbios_int_10h_11h(); break;
                   16951:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     16952:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     16953:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     16954:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     16955:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   16956:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     16957:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     16958:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   16959:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     16960:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     16961:                case 0x6f: break;
1.1.1.22  root     16962:                case 0x80: m_CF = 1; break; // unknown
                   16963:                case 0x81: m_CF = 1; break; // unknown
1.1       root     16964:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     16965:                case 0x83: pcbios_int_10h_83h(); break;
                   16966:                case 0x8b: break;
                   16967:                case 0x8c: m_CF = 1; break; // unknown
                   16968:                case 0x8d: m_CF = 1; break; // unknown
                   16969:                case 0x8e: m_CF = 1; break; // unknown
                   16970:                case 0x90: pcbios_int_10h_90h(); break;
                   16971:                case 0x91: pcbios_int_10h_91h(); break;
                   16972:                case 0x92: break;
                   16973:                case 0x93: break;
                   16974:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     16975:                case 0xfa: break; // ega register interface library is not installed
1.1       root     16976:                case 0xfe: pcbios_int_10h_feh(); break;
                   16977:                case 0xff: pcbios_int_10h_ffh(); break;
                   16978:                default:
1.1.1.22  root     16979:                        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));
                   16980:                        m_CF = 1;
1.1       root     16981:                        break;
                   16982:                }
                   16983:                break;
                   16984:        case 0x11:
                   16985:                // PC BIOS - Get Equipment List
1.1.1.26  root     16986:                REG16(AX) = msdos_get_equipment();
1.1       root     16987:                break;
                   16988:        case 0x12:
                   16989:                // PC BIOS - Get Memory Size
1.1.1.33  root     16990:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     16991:                break;
                   16992:        case 0x13:
1.1.1.42  root     16993:                // PC BIOS - Disk I/O
                   16994:                {
                   16995:                        static UINT8 last = 0x00;
                   16996:                        switch(REG8(AH)) {
                   16997:                        case 0x00: pcbios_int_13h_00h(); break;
                   16998:                        case 0x01: // get last status
                   16999:                                REG8(AH) = last;
                   17000:                                break;
                   17001:                        case 0x02: pcbios_int_13h_02h(); break;
                   17002:                        case 0x03: pcbios_int_13h_03h(); break;
                   17003:                        case 0x04: pcbios_int_13h_04h(); break;
                   17004:                        case 0x08: pcbios_int_13h_08h(); break;
                   17005:                        case 0x0a: pcbios_int_13h_02h(); break;
                   17006:                        case 0x0b: pcbios_int_13h_03h(); break;
                   17007:                        case 0x0d: pcbios_int_13h_00h(); break;
                   17008:                        case 0x10: pcbios_int_13h_10h(); break;
                   17009:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     17010:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     17011:                        case 0x05: // format
                   17012:                        case 0x06:
                   17013:                        case 0x07:
                   17014:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   17015:                                m_CF = 1;
                   17016:                                break;
                   17017:                        case 0x09:
                   17018:                        case 0x0c: // seek
                   17019:                        case 0x11: // recalib
                   17020:                        case 0x14:
                   17021:                        case 0x17:
                   17022:                                REG8(AH) = 0x00; // successful completion
                   17023:                                break;
1.1.1.43  root     17024:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   17025:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   17026:                                REG8(AH) = 0x01; // invalid function
                   17027:                                m_CF = 1;
                   17028:                                break;
1.1.1.42  root     17029:                        default:
                   17030:                                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));
                   17031:                                REG8(AH) = 0x01; // invalid function
                   17032:                                m_CF = 1;
                   17033:                                break;
                   17034:                        }
                   17035:                        last = REG8(AH);
                   17036:                }
1.1       root     17037:                break;
                   17038:        case 0x14:
                   17039:                // PC BIOS - Serial I/O
1.1.1.25  root     17040:                switch(REG8(AH)) {
                   17041:                case 0x00: pcbios_int_14h_00h(); break;
                   17042:                case 0x01: pcbios_int_14h_01h(); break;
                   17043:                case 0x02: pcbios_int_14h_02h(); break;
                   17044:                case 0x03: pcbios_int_14h_03h(); break;
                   17045:                case 0x04: pcbios_int_14h_04h(); break;
                   17046:                case 0x05: pcbios_int_14h_05h(); break;
                   17047:                default:
                   17048:                        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));
                   17049:                        break;
                   17050:                }
1.1       root     17051:                break;
                   17052:        case 0x15:
                   17053:                // PC BIOS
1.1.1.3   root     17054:                m_CF = 0;
1.1       root     17055:                switch(REG8(AH)) {
1.1.1.14  root     17056:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     17057:                case 0x23: pcbios_int_15h_23h(); break;
                   17058:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     17059:                case 0x41: break;
1.1       root     17060:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     17061:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     17062:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     17063:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     17064:                case 0x86: pcbios_int_15h_86h(); break;
                   17065:                case 0x87: pcbios_int_15h_87h(); break;
                   17066:                case 0x88: pcbios_int_15h_88h(); break;
                   17067:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     17068:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     17069:                case 0xc0: // PS/2 ???
1.1.1.54  root     17070: #ifndef EXT_BIOS_TOP
1.1.1.22  root     17071:                case 0xc1:
1.1.1.54  root     17072: #endif
1.1.1.30  root     17073:                case 0xc3: // PS50+ ???
                   17074:                case 0xc4:
1.1.1.22  root     17075:                        REG8(AH) = 0x86;
                   17076:                        m_CF = 1;
                   17077:                        break;
1.1.1.54  root     17078: #ifdef EXT_BIOS_TOP
                   17079:                case 0xc1: pcbios_int_15h_c1h(); break;
                   17080: #endif
                   17081:                case 0xc2: pcbios_int_15h_c2h(); break;
1.1.1.3   root     17082: #if defined(HAS_I386)
1.1       root     17083:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     17084: #endif
1.1       root     17085:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     17086:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     17087:                default:
1.1.1.22  root     17088:                        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));
                   17089:                        REG8(AH) = 0x86;
1.1.1.3   root     17090:                        m_CF = 1;
1.1       root     17091:                        break;
                   17092:                }
                   17093:                break;
                   17094:        case 0x16:
                   17095:                // PC BIOS - Keyboard
1.1.1.3   root     17096:                m_CF = 0;
1.1       root     17097:                switch(REG8(AH)) {
                   17098:                case 0x00: pcbios_int_16h_00h(); break;
                   17099:                case 0x01: pcbios_int_16h_01h(); break;
                   17100:                case 0x02: pcbios_int_16h_02h(); break;
                   17101:                case 0x03: pcbios_int_16h_03h(); break;
                   17102:                case 0x05: pcbios_int_16h_05h(); break;
1.1.1.60  root     17103:                case 0x09: pcbios_int_16h_09h(); break;
                   17104:                case 0x0a: pcbios_int_16h_0ah(); break;
1.1       root     17105:                case 0x10: pcbios_int_16h_00h(); break;
1.1.1.60  root     17106:                case 0x11: pcbios_int_16h_11h(); break;
1.1       root     17107:                case 0x12: pcbios_int_16h_12h(); break;
                   17108:                case 0x13: pcbios_int_16h_13h(); break;
                   17109:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     17110:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     17111:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     17112:                case 0xda: break; // unknown
1.1.1.43  root     17113:                case 0xdb: break; // unknown
1.1.1.22  root     17114:                case 0xff: break; // unknown
1.1       root     17115:                default:
1.1.1.22  root     17116:                        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     17117:                        break;
                   17118:                }
                   17119:                break;
                   17120:        case 0x17:
                   17121:                // PC BIOS - Printer
1.1.1.37  root     17122:                m_CF = 0;
                   17123:                switch(REG8(AH)) {
                   17124:                case 0x00: pcbios_int_17h_00h(); break;
                   17125:                case 0x01: pcbios_int_17h_01h(); break;
                   17126:                case 0x02: pcbios_int_17h_02h(); break;
                   17127:                case 0x03: pcbios_int_17h_03h(); break;
                   17128:                case 0x50: pcbios_int_17h_50h(); break;
                   17129:                case 0x51: pcbios_int_17h_51h(); break;
                   17130:                case 0x52: pcbios_int_17h_52h(); break;
                   17131:                case 0x84: pcbios_int_17h_84h(); break;
                   17132:                case 0x85: pcbios_int_17h_85h(); break;
                   17133:                default:
                   17134:                        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));
                   17135:                        break;
                   17136:                }
1.1       root     17137:                break;
                   17138:        case 0x1a:
                   17139:                // PC BIOS - Timer
1.1.1.3   root     17140:                m_CF = 0;
1.1       root     17141:                switch(REG8(AH)) {
                   17142:                case 0x00: pcbios_int_1ah_00h(); break;
                   17143:                case 0x01: break;
                   17144:                case 0x02: pcbios_int_1ah_02h(); break;
                   17145:                case 0x03: break;
                   17146:                case 0x04: pcbios_int_1ah_04h(); break;
                   17147:                case 0x05: break;
                   17148:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   17149:                case 0x0b: break;
1.1.1.14  root     17150:                case 0x35: break; // Word Perfect Third Party Interface?
                   17151:                case 0x36: break; // Word Perfect Third Party Interface
                   17152:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     17153:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     17154:                case 0xb1: break; // PCI BIOS v2.0c+
                   17155:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     17156:                default:
1.1.1.22  root     17157:                        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     17158:                        break;
                   17159:                }
                   17160:                break;
1.1.1.33  root     17161:        case 0x1b:
                   17162:                mem[0x471] = 0x00;
                   17163:                break;
1.1       root     17164:        case 0x20:
1.1.1.28  root     17165:                try {
                   17166:                        msdos_process_terminate(SREG(CS), retval, 1);
                   17167:                } catch(...) {
                   17168:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   17169:                }
1.1       root     17170:                break;
1.1.1.49  root     17171:        case 0x30:
1.1.1.46  root     17172:                // dummy interrupt for case map routine pointed in the country info
                   17173: //             if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
                   17174: //                     REG8(AL) = 0x00;
                   17175: //                     break;
                   17176: //             }
1.1       root     17177:        case 0x21:
                   17178:                // MS-DOS System Call
1.1.1.3   root     17179:                m_CF = 0;
1.1.1.28  root     17180:                try {
1.1.1.46  root     17181:                        switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28  root     17182:                        case 0x00: msdos_int_21h_00h(); break;
                   17183:                        case 0x01: msdos_int_21h_01h(); break;
                   17184:                        case 0x02: msdos_int_21h_02h(); break;
                   17185:                        case 0x03: msdos_int_21h_03h(); break;
                   17186:                        case 0x04: msdos_int_21h_04h(); break;
                   17187:                        case 0x05: msdos_int_21h_05h(); break;
                   17188:                        case 0x06: msdos_int_21h_06h(); break;
                   17189:                        case 0x07: msdos_int_21h_07h(); break;
                   17190:                        case 0x08: msdos_int_21h_08h(); break;
                   17191:                        case 0x09: msdos_int_21h_09h(); break;
                   17192:                        case 0x0a: msdos_int_21h_0ah(); break;
                   17193:                        case 0x0b: msdos_int_21h_0bh(); break;
                   17194:                        case 0x0c: msdos_int_21h_0ch(); break;
                   17195:                        case 0x0d: msdos_int_21h_0dh(); break;
                   17196:                        case 0x0e: msdos_int_21h_0eh(); break;
                   17197:                        case 0x0f: msdos_int_21h_0fh(); break;
                   17198:                        case 0x10: msdos_int_21h_10h(); break;
                   17199:                        case 0x11: msdos_int_21h_11h(); break;
                   17200:                        case 0x12: msdos_int_21h_12h(); break;
                   17201:                        case 0x13: msdos_int_21h_13h(); break;
                   17202:                        case 0x14: msdos_int_21h_14h(); break;
                   17203:                        case 0x15: msdos_int_21h_15h(); break;
                   17204:                        case 0x16: msdos_int_21h_16h(); break;
                   17205:                        case 0x17: msdos_int_21h_17h(); break;
                   17206:                        case 0x18: msdos_int_21h_18h(); break;
                   17207:                        case 0x19: msdos_int_21h_19h(); break;
                   17208:                        case 0x1a: msdos_int_21h_1ah(); break;
                   17209:                        case 0x1b: msdos_int_21h_1bh(); break;
                   17210:                        case 0x1c: msdos_int_21h_1ch(); break;
                   17211:                        case 0x1d: msdos_int_21h_1dh(); break;
                   17212:                        case 0x1e: msdos_int_21h_1eh(); break;
                   17213:                        case 0x1f: msdos_int_21h_1fh(); break;
                   17214:                        case 0x20: msdos_int_21h_20h(); break;
                   17215:                        case 0x21: msdos_int_21h_21h(); break;
                   17216:                        case 0x22: msdos_int_21h_22h(); break;
                   17217:                        case 0x23: msdos_int_21h_23h(); break;
                   17218:                        case 0x24: msdos_int_21h_24h(); break;
                   17219:                        case 0x25: msdos_int_21h_25h(); break;
                   17220:                        case 0x26: msdos_int_21h_26h(); break;
                   17221:                        case 0x27: msdos_int_21h_27h(); break;
                   17222:                        case 0x28: msdos_int_21h_28h(); break;
                   17223:                        case 0x29: msdos_int_21h_29h(); break;
                   17224:                        case 0x2a: msdos_int_21h_2ah(); break;
                   17225:                        case 0x2b: msdos_int_21h_2bh(); break;
                   17226:                        case 0x2c: msdos_int_21h_2ch(); break;
                   17227:                        case 0x2d: msdos_int_21h_2dh(); break;
                   17228:                        case 0x2e: msdos_int_21h_2eh(); break;
                   17229:                        case 0x2f: msdos_int_21h_2fh(); break;
                   17230:                        case 0x30: msdos_int_21h_30h(); break;
                   17231:                        case 0x31: msdos_int_21h_31h(); break;
                   17232:                        case 0x32: msdos_int_21h_32h(); break;
                   17233:                        case 0x33: msdos_int_21h_33h(); break;
                   17234:                        case 0x34: msdos_int_21h_34h(); break;
                   17235:                        case 0x35: msdos_int_21h_35h(); break;
                   17236:                        case 0x36: msdos_int_21h_36h(); break;
                   17237:                        case 0x37: msdos_int_21h_37h(); break;
                   17238:                        case 0x38: msdos_int_21h_38h(); break;
                   17239:                        case 0x39: msdos_int_21h_39h(0); break;
                   17240:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   17241:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   17242:                        case 0x3c: msdos_int_21h_3ch(); break;
                   17243:                        case 0x3d: msdos_int_21h_3dh(); break;
                   17244:                        case 0x3e: msdos_int_21h_3eh(); break;
                   17245:                        case 0x3f: msdos_int_21h_3fh(); break;
                   17246:                        case 0x40: msdos_int_21h_40h(); break;
                   17247:                        case 0x41: msdos_int_21h_41h(0); break;
                   17248:                        case 0x42: msdos_int_21h_42h(); break;
                   17249:                        case 0x43: msdos_int_21h_43h(0); break;
                   17250:                        case 0x44: msdos_int_21h_44h(); break;
                   17251:                        case 0x45: msdos_int_21h_45h(); break;
                   17252:                        case 0x46: msdos_int_21h_46h(); break;
                   17253:                        case 0x47: msdos_int_21h_47h(0); break;
                   17254:                        case 0x48: msdos_int_21h_48h(); break;
                   17255:                        case 0x49: msdos_int_21h_49h(); break;
                   17256:                        case 0x4a: msdos_int_21h_4ah(); break;
                   17257:                        case 0x4b: msdos_int_21h_4bh(); break;
                   17258:                        case 0x4c: msdos_int_21h_4ch(); break;
                   17259:                        case 0x4d: msdos_int_21h_4dh(); break;
                   17260:                        case 0x4e: msdos_int_21h_4eh(); break;
                   17261:                        case 0x4f: msdos_int_21h_4fh(); break;
                   17262:                        case 0x50: msdos_int_21h_50h(); break;
                   17263:                        case 0x51: msdos_int_21h_51h(); break;
                   17264:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     17265:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     17266:                        case 0x54: msdos_int_21h_54h(); break;
                   17267:                        case 0x55: msdos_int_21h_55h(); break;
                   17268:                        case 0x56: msdos_int_21h_56h(0); break;
                   17269:                        case 0x57: msdos_int_21h_57h(); break;
                   17270:                        case 0x58: msdos_int_21h_58h(); break;
                   17271:                        case 0x59: msdos_int_21h_59h(); break;
                   17272:                        case 0x5a: msdos_int_21h_5ah(); break;
                   17273:                        case 0x5b: msdos_int_21h_5bh(); break;
                   17274:                        case 0x5c: msdos_int_21h_5ch(); break;
                   17275:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     17276:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     17277:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     17278:                        case 0x60: msdos_int_21h_60h(0); break;
                   17279:                        case 0x61: msdos_int_21h_61h(); break;
                   17280:                        case 0x62: msdos_int_21h_62h(); break;
                   17281:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     17282:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     17283:                        case 0x65: msdos_int_21h_65h(); break;
                   17284:                        case 0x66: msdos_int_21h_66h(); break;
                   17285:                        case 0x67: msdos_int_21h_67h(); break;
                   17286:                        case 0x68: msdos_int_21h_68h(); break;
                   17287:                        case 0x69: msdos_int_21h_69h(); break;
                   17288:                        case 0x6a: msdos_int_21h_6ah(); break;
                   17289:                        case 0x6b: msdos_int_21h_6bh(); break;
                   17290:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.48  root     17291:                        case 0x6d: // Find First ROM Program
                   17292:                        case 0x6e: // Find Next ROM Program
                   17293:                        case 0x6f: // Get/Set ROM Scan Start Address
                   17294:                                REG8(AL) = 0x00; // if not supported (DOS <5, MS-DOS 5+ non-ROM versions)
                   17295:                                break;
1.1.1.43  root     17296:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.48  root     17297:                        case 0x71: // Windows95 - Long Filename Functions
1.1.1.28  root     17298:                                switch(REG8(AL)) {
                   17299:                                case 0x0d: msdos_int_21h_710dh(); break;
                   17300:                                case 0x39: msdos_int_21h_39h(1); break;
                   17301:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   17302:                                case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.48  root     17303:                                case 0x41: msdos_int_21h_7141h(); break;
1.1.1.28  root     17304:                                case 0x43: msdos_int_21h_43h(1); break;
                   17305:                                case 0x47: msdos_int_21h_47h(1); break;
                   17306:                                case 0x4e: msdos_int_21h_714eh(); break;
                   17307:                                case 0x4f: msdos_int_21h_714fh(); break;
                   17308:                                case 0x56: msdos_int_21h_56h(1); break;
                   17309:                                case 0x60: msdos_int_21h_60h(1); break;
                   17310:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   17311:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   17312:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   17313:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   17314:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   17315:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45  root     17316:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     17317:                                case 0xaa: msdos_int_21h_71aah(); break;
                   17318:                                default:
                   17319:                                        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));
                   17320:                                        REG16(AX) = 0x7100;
                   17321:                                        m_CF = 1;
                   17322:                                        break;
                   17323:                                }
                   17324:                                break;
1.1.1.48  root     17325:                        case 0x72: // Windows95 beta - LFN FindClose
                   17326: //                             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));
                   17327:                                REG16(AX) = 0x7200;
                   17328:                                m_CF = 1;
                   17329:                                break;
                   17330:                        case 0x73: // Windows95 - FAT32 Functions
1.1.1.28  root     17331:                                switch(REG8(AL)) {
                   17332:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     17333:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     17334:                                case 0x02: msdos_int_21h_7302h(); break;
                   17335:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     17336:                                // 0x04: Set DPB to Use for Formatting
                   17337:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     17338:                                default:
                   17339:                                        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));
                   17340:                                        REG16(AX) = 0x7300;
                   17341:                                        m_CF = 1;
                   17342:                                        break;
                   17343:                                }
1.1       root     17344:                                break;
1.1.1.30  root     17345:                        case 0xdb: msdos_int_21h_dbh(); break;
                   17346:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     17347:                        default:
1.1.1.22  root     17348:                                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     17349:                                REG16(AX) = 0x01;
1.1.1.3   root     17350:                                m_CF = 1;
1.1       root     17351:                                break;
                   17352:                        }
1.1.1.28  root     17353:                } catch(int error) {
                   17354:                        REG16(AX) = error;
                   17355:                        m_CF = 1;
                   17356:                } catch(...) {
                   17357:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     17358:                        m_CF = 1;
1.1       root     17359:                }
1.1.1.3   root     17360:                if(m_CF) {
1.1.1.23  root     17361:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     17362:                        sda->int21h_5d0ah_called = 0;
1.1.1.23  root     17363:                        sda->extended_error_code = REG16(AX);
                   17364:                        switch(sda->extended_error_code) {
                   17365:                        case  4: // Too many open files
                   17366:                        case  8: // Insufficient memory
                   17367:                                sda->error_class = 1; // Out of resource
                   17368:                                break;
                   17369:                        case  5: // Access denied
                   17370:                                sda->error_class = 3; // Authorization
                   17371:                                break;
                   17372:                        case  7: // Memory control block destroyed
                   17373:                                sda->error_class = 4; // Internal
                   17374:                                break;
                   17375:                        case  2: // File not found
                   17376:                        case  3: // Path not found
                   17377:                        case 15: // Invaid drive specified
                   17378:                        case 18: // No more files
                   17379:                                sda->error_class = 8; // Not found
                   17380:                                break;
                   17381:                        case 32: // Sharing violation
                   17382:                        case 33: // Lock violation
                   17383:                                sda->error_class = 10; // Locked
                   17384:                                break;
                   17385: //                     case 16: // Removal of current directory attempted
                   17386:                        case 19: // Attempted write on protected disk
                   17387:                        case 21: // Drive not ready
                   17388: //                     case 29: // Write failure
                   17389: //                     case 30: // Read failure
                   17390: //                     case 82: // Cannot create subdirectory
                   17391:                                sda->error_class = 11; // Media
                   17392:                                break;
                   17393:                        case 80: // File already exists
                   17394:                                sda->error_class = 12; // Already exist
                   17395:                                break;
                   17396:                        default:
                   17397:                                sda->error_class = 13; // Unknown
                   17398:                                break;
                   17399:                        }
                   17400:                        sda->suggested_action = 1; // Retry
                   17401:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     17402:                }
1.1.1.33  root     17403:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     17404:                        // raise int 23h
                   17405: #if defined(HAS_I386)
                   17406:                        m_ext = 0; // not an external interrupt
                   17407:                        i386_trap(0x23, 1, 0);
                   17408:                        m_ext = 1;
                   17409: #else
                   17410:                        PREFIX86(_interrupt)(0x23);
                   17411: #endif
                   17412:                }
1.1       root     17413:                break;
                   17414:        case 0x22:
                   17415:                fatalerror("int 22h (terminate address)\n");
                   17416:        case 0x23:
1.1.1.28  root     17417:                try {
                   17418:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   17419:                } catch(...) {
                   17420:                        fatalerror("failed to terminate the current process by int 23h\n");
                   17421:                }
1.1       root     17422:                break;
                   17423:        case 0x24:
1.1.1.32  root     17424: /*
1.1.1.28  root     17425:                try {
                   17426:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17427:                } catch(...) {
                   17428:                        fatalerror("failed to terminate the current process by int 24h\n");
                   17429:                }
1.1.1.32  root     17430: */
                   17431:                msdos_int_24h();
1.1       root     17432:                break;
                   17433:        case 0x25:
                   17434:                msdos_int_25h();
                   17435:                break;
                   17436:        case 0x26:
                   17437:                msdos_int_26h();
                   17438:                break;
                   17439:        case 0x27:
1.1.1.28  root     17440:                try {
                   17441:                        msdos_int_27h();
                   17442:                } catch(...) {
                   17443:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   17444:                }
1.1       root     17445:                break;
                   17446:        case 0x28:
                   17447:                Sleep(10);
1.1.1.35  root     17448:                REQUEST_HARDWRE_UPDATE();
1.1       root     17449:                break;
                   17450:        case 0x29:
                   17451:                msdos_int_29h();
                   17452:                break;
                   17453:        case 0x2e:
                   17454:                msdos_int_2eh();
                   17455:                break;
                   17456:        case 0x2f:
                   17457:                // multiplex interrupt
                   17458:                switch(REG8(AH)) {
1.1.1.22  root     17459:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     17460:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     17461:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     17462:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     17463:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     17464:                case 0x14: msdos_int_2fh_14h(); break;
                   17465:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     17466:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     17467:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     17468:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     17469:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     17470:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     17471:                case 0x46: msdos_int_2fh_46h(); break;
                   17472:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     17473:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     17474:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     17475:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     17476:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     17477:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     17478:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     17479:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     17480:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     17481:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     17482:                default:
1.1.1.30  root     17483:                        switch(REG8(AL)) {
                   17484:                        case 0x00:
                   17485:                                // This is not installed
                   17486: //                             REG8(AL) = 0x00;
                   17487:                                break;
1.1.1.33  root     17488:                        case 0x01:
1.1.1.42  root     17489:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   17490:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   17491:                                        break;
                   17492:                                }
1.1.1.33  root     17493:                                // Banyan VINES v4+ is not installed
                   17494:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   17495:                                        break;
                   17496:                                }
1.1.1.42  root     17497:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   17498:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   17499:                                        break;
                   17500:                                }
1.1.1.30  root     17501:                        default:
1.1.1.42  root     17502:                                // NORTON UTILITIES 5.0+
                   17503:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   17504:                                        break;
                   17505:                                }
1.1.1.30  root     17506:                                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     17507:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     17508:                                m_CF = 1;
                   17509:                                break;
                   17510:                        }
                   17511:                        break;
1.1       root     17512:                }
                   17513:                break;
1.1.1.24  root     17514:        case 0x33:
                   17515:                switch(REG8(AH)) {
                   17516:                case 0x00:
                   17517:                        // Mouse
1.1.1.49  root     17518:                        switch(REG16(AX)) {
                   17519:                        case 0x0000: msdos_int_33h_0000h(); break;
                   17520:                        case 0x0001: msdos_int_33h_0001h(); break;
                   17521:                        case 0x0002: msdos_int_33h_0002h(); break;
                   17522:                        case 0x0003: msdos_int_33h_0003h(); break;
                   17523:                        case 0x0004: msdos_int_33h_0004h(); break;
                   17524:                        case 0x0005: msdos_int_33h_0005h(); break;
                   17525:                        case 0x0006: msdos_int_33h_0006h(); break;
                   17526:                        case 0x0007: msdos_int_33h_0007h(); break;
                   17527:                        case 0x0008: msdos_int_33h_0008h(); break;
                   17528:                        case 0x0009: msdos_int_33h_0009h(); break;
                   17529:                        case 0x000a: msdos_int_33h_000ah(); break;
                   17530:                        case 0x000b: msdos_int_33h_000bh(); break;
                   17531:                        case 0x000c: msdos_int_33h_000ch(); break;
                   17532:                        case 0x000d: break; // MS MOUSE v1.0+ - Light Pen Emulation On
                   17533:                        case 0x000e: break; // MS MOUSE v1.0+ - Light Pen Emulation Off
                   17534:                        case 0x000f: msdos_int_33h_000fh(); break;
                   17535:                        case 0x0010: break; // MS MOUSE v1.0+ - Define Screen Region for Updating
                   17536:                        case 0x0011: msdos_int_33h_0011h(); break;
                   17537:                        case 0x0012: REG16(AX) = 0xffff; break; // MS MOUSE - Set Large Graphics Cursor Block
                   17538:                        case 0x0013: break; // MS MOUSE v5.0+ - Define Double-Speed Threshold
                   17539:                        case 0x0014: msdos_int_33h_0014h(); break;
                   17540:                        case 0x0015: msdos_int_33h_0015h(); break;
                   17541:                        case 0x0016: msdos_int_33h_0016h(); break;
                   17542:                        case 0x0017: msdos_int_33h_0017h(); break;
                   17543:                        case 0x0018: msdos_int_33h_0018h(); break;
                   17544:                        case 0x0019: msdos_int_33h_0019h(); break;
                   17545:                        case 0x001a: msdos_int_33h_001ah(); break;
                   17546:                        case 0x001b: msdos_int_33h_001bh(); break;
                   17547:                        case 0x001c: break; // MS MOUSE v6.0+ - Set Interrupt Rate
                   17548:                        case 0x001d: msdos_int_33h_001dh(); break;
                   17549:                        case 0x001e: msdos_int_33h_001eh(); break;
                   17550:                        case 0x001f: msdos_int_33h_001fh(); break;
                   17551:                        case 0x0020: msdos_int_33h_0020h(); break;
                   17552:                        case 0x0021: msdos_int_33h_0021h(); break;
                   17553:                        case 0x0022: msdos_int_33h_0022h(); break;
                   17554:                        case 0x0023: msdos_int_33h_0023h(); break;
                   17555:                        case 0x0024: msdos_int_33h_0024h(); break;
                   17556:                        case 0x0025: msdos_int_33h_0025h(); break;
                   17557:                        case 0x0026: msdos_int_33h_0026h(); break;
                   17558:                        case 0x0027: msdos_int_33h_0027h(); break;
                   17559:                        case 0x0028: msdos_int_33h_0028h(); break;
                   17560:                        case 0x0029: msdos_int_33h_0029h(); break;
                   17561:                        case 0x002a: msdos_int_33h_002ah(); break;
                   17562:                        // 0x002b: MS MOUSE v7.0+ - Load Acceleration Profiles
                   17563:                        // 0x002c: MS MOUSE v7.0+ - Get Acceleration Profiles
                   17564:                        // 0x002d: MS MOUSE v7.0+ - Select Acceleration Profile
                   17565:                        // 0x002e: MS MOUSE v8.10+ - Set Acceleration Profile Names
                   17566:                        case 0x002f: break; // Mouse Hardware Reset
                   17567:                        // 0x0030: MS MOUSE v7.04+ - Get/Set BallPoint Information
                   17568:                        case 0x0031: msdos_int_33h_0031h(); break;
                   17569:                        case 0x0032: msdos_int_33h_0032h(); break;
                   17570:                        // 0x0033: MS MOUSE v7.05+ - Get Switch Settings And Acceleration Profile Data
                   17571:                        // 0x0034: MS MOUSE v8.0+ - Get Initialization File
                   17572:                        // 0x0035: MS MOUSE v8.10+ - LCD Screen Large Pointer Support
                   17573:                        case 0x004d: msdos_int_33h_004dh(); break;
                   17574:                        case 0x006d: msdos_int_33h_006dh(); break;
1.1.1.24  root     17575:                        default:
                   17576:                                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));
                   17577:                                break;
                   17578:                        }
                   17579:                        break;
                   17580:                default:
                   17581:                        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));
                   17582:                        break;
                   17583:                }
                   17584:                break;
1.1.1.59  root     17585:        case 0x65:
1.1.1.19  root     17586:                // dummy interrupt for EMS (int 67h)
                   17587:                switch(REG8(AH)) {
                   17588:                case 0x40: msdos_int_67h_40h(); break;
                   17589:                case 0x41: msdos_int_67h_41h(); break;
                   17590:                case 0x42: msdos_int_67h_42h(); break;
                   17591:                case 0x43: msdos_int_67h_43h(); break;
                   17592:                case 0x44: msdos_int_67h_44h(); break;
                   17593:                case 0x45: msdos_int_67h_45h(); break;
                   17594:                case 0x46: msdos_int_67h_46h(); break;
                   17595:                case 0x47: msdos_int_67h_47h(); break;
                   17596:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     17597:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   17598:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     17599:                case 0x4b: msdos_int_67h_4bh(); break;
                   17600:                case 0x4c: msdos_int_67h_4ch(); break;
                   17601:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     17602:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     17603:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     17604:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     17605:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     17606:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     17607:                case 0x53: msdos_int_67h_53h(); break;
                   17608:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.49  root     17609:                case 0x55: msdos_int_67h_55h(); break;
                   17610:                case 0x56: msdos_int_67h_56h(); break;
1.1.1.20  root     17611:                case 0x57: msdos_int_67h_57h(); break;
                   17612:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     17613:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     17614:                case 0x5a: msdos_int_67h_5ah(); break;
1.1.1.49  root     17615:                case 0x5b: msdos_int_67h_5bh(); break;
1.1.1.43  root     17616:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   17617:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.49  root     17618:                case 0x70: msdos_int_67h_70h(); break;
1.1.1.31  root     17619:                // 0xde: VCPI
1.1.1.30  root     17620:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     17621:                default:
1.1.1.22  root     17622:                        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     17623:                        REG8(AH) = 0x84;
                   17624:                        break;
                   17625:                }
                   17626:                break;
                   17627: #ifdef SUPPORT_XMS
1.1.1.59  root     17628:        case 0x66:
1.1.1.19  root     17629:                // dummy interrupt for XMS (call far)
1.1.1.28  root     17630:                try {
                   17631:                        switch(REG8(AH)) {
                   17632:                        case 0x00: msdos_call_xms_00h(); break;
                   17633:                        case 0x01: msdos_call_xms_01h(); break;
                   17634:                        case 0x02: msdos_call_xms_02h(); break;
                   17635:                        case 0x03: msdos_call_xms_03h(); break;
                   17636:                        case 0x04: msdos_call_xms_04h(); break;
                   17637:                        case 0x05: msdos_call_xms_05h(); break;
                   17638:                        case 0x06: msdos_call_xms_06h(); break;
                   17639:                        case 0x07: msdos_call_xms_07h(); break;
                   17640:                        case 0x08: msdos_call_xms_08h(); break;
                   17641:                        case 0x09: msdos_call_xms_09h(); break;
                   17642:                        case 0x0a: msdos_call_xms_0ah(); break;
                   17643:                        case 0x0b: msdos_call_xms_0bh(); break;
                   17644:                        case 0x0c: msdos_call_xms_0ch(); break;
                   17645:                        case 0x0d: msdos_call_xms_0dh(); break;
                   17646:                        case 0x0e: msdos_call_xms_0eh(); break;
                   17647:                        case 0x0f: msdos_call_xms_0fh(); break;
                   17648:                        case 0x10: msdos_call_xms_10h(); break;
                   17649:                        case 0x11: msdos_call_xms_11h(); break;
                   17650:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     17651: #if defined(HAS_I386)
                   17652:                        case 0x88: msdos_call_xms_88h(); break;
                   17653:                        case 0x89: msdos_call_xms_89h(); break;
                   17654:                        case 0x8e: msdos_call_xms_8eh(); break;
                   17655:                        case 0x8f: msdos_call_xms_8fh(); break;
                   17656: #endif
1.1.1.28  root     17657:                        default:
                   17658:                                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));
                   17659:                                REG16(AX) = 0x0000;
                   17660:                                REG8(BL) = 0x80; // function not implemented
                   17661:                                break;
                   17662:                        }
                   17663:                } catch(...) {
1.1.1.19  root     17664:                        REG16(AX) = 0x0000;
1.1.1.28  root     17665:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     17666:                }
                   17667:                break;
                   17668: #endif
1.1.1.59  root     17669: /*
                   17670:        case 0x67:
                   17671:                // int 67h handler is in EMS device driver (EMMXXXX0) and it calls int 65h
                   17672:                // NOTE: some softwares get address of int 67h handler and recognize the address is in EMS device driver
                   17673:                break;
                   17674: */
                   17675:        case 0x69:
1.1.1.24  root     17676:                // irq12 (mouse)
                   17677:                mouse_push_ax = REG16(AX);
                   17678:                mouse_push_bx = REG16(BX);
                   17679:                mouse_push_cx = REG16(CX);
                   17680:                mouse_push_dx = REG16(DX);
                   17681:                mouse_push_si = REG16(SI);
                   17682:                mouse_push_di = REG16(DI);
                   17683:                
1.1.1.43  root     17684:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     17685:                        REG16(AX) = mouse.status_irq;
                   17686:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     17687:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17688:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     17689:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17690:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17691:                        
1.1.1.49  root     17692:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17693:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr.w.l & 0xff;
                   17694:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr.w.l >> 8;
                   17695:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr.w.h & 0xff;
                   17696:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.59  root     17697:                        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17698:                        mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.43  root     17699:                        break;
1.1.1.24  root     17700:                }
1.1.1.43  root     17701:                for(int i = 0; i < 8; i++) {
                   17702:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17703:                                REG16(AX) = mouse.status_irq_alt;
                   17704:                                REG16(BX) = mouse.get_buttons();
                   17705:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17706:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   17707:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17708:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17709:                                
1.1.1.49  root     17710:                                mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17711:                                mem[DUMMY_TOP + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   17712:                                mem[DUMMY_TOP + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   17713:                                mem[DUMMY_TOP + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   17714:                                mem[DUMMY_TOP + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
1.1.1.59  root     17715:                                mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17716:                                mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.43  root     17717:                                break;
                   17718:                        }
                   17719:                }
1.1.1.59  root     17720:                if(mouse.status_irq_ps2 && mouse.call_addr_ps2.dw && mouse.enabled_ps2) {
1.1.1.54  root     17721:                        UINT16 data_1st, data_2nd, data_3rd;
                   17722:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   17723:                        i386_push16(data_1st);
                   17724:                        i386_push16(data_2nd);
                   17725:                        i386_push16(data_3rd);
                   17726:                        i386_push16(0x0000);
                   17727:                        
                   17728:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17729:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr_ps2.w.l & 0xff;
                   17730:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr_ps2.w.l >> 8;
                   17731:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr_ps2.w.h & 0xff;
                   17732:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr_ps2.w.h >> 8;
1.1.1.59  root     17733:                        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6ah (dummy)
                   17734:                        mem[DUMMY_TOP + 0x08] = 0x6a;
1.1.1.54  root     17735:                        break;
                   17736:                }
1.1.1.43  root     17737:                // invalid call addr :-(
1.1.1.49  root     17738:                mem[DUMMY_TOP + 0x02] = 0x90;   // nop
                   17739:                mem[DUMMY_TOP + 0x03] = 0x90;   // nop
                   17740:                mem[DUMMY_TOP + 0x04] = 0x90;   // nop
                   17741:                mem[DUMMY_TOP + 0x05] = 0x90;   // nop
                   17742:                mem[DUMMY_TOP + 0x06] = 0x90;   // nop
1.1.1.59  root     17743:                mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17744:                mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.24  root     17745:                break;
1.1.1.59  root     17746:        case 0x6a:
                   17747:                // end of ps/2 mouse bios
                   17748:                i386_pop16();
                   17749:                i386_pop16();
                   17750:                i386_pop16();
                   17751:                i386_pop16();
1.1.1.24  root     17752:        case 0x6b:
                   17753:                // end of irq12 (mouse)
                   17754:                REG16(AX) = mouse_push_ax;
                   17755:                REG16(BX) = mouse_push_bx;
                   17756:                REG16(CX) = mouse_push_cx;
                   17757:                REG16(DX) = mouse_push_dx;
                   17758:                REG16(SI) = mouse_push_si;
                   17759:                REG16(DI) = mouse_push_di;
                   17760:                
                   17761:                // EOI
                   17762:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   17763:                        pic[0].isr &= ~(1 << 2); // master
                   17764:                }
                   17765:                pic_update();
                   17766:                break;
                   17767:        case 0x6c:
1.1.1.19  root     17768:                // dummy interrupt for case map routine pointed in the country info
                   17769:                if(REG8(AL) >= 0x80) {
                   17770:                        char tmp[2] = {0};
                   17771:                        tmp[0] = REG8(AL);
                   17772:                        my_strupr(tmp);
                   17773:                        REG8(AL) = tmp[0];
                   17774:                }
                   17775:                break;
1.1.1.27  root     17776:        case 0x6d:
                   17777:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   17778:                REG8(AL) = 0x86; // not supported
                   17779:                m_CF = 1;
                   17780:                break;
1.1.1.32  root     17781:        case 0x6e:
                   17782:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   17783:                {
                   17784:                        UINT16 code = REG16(AX);
                   17785:                        if(code & 0xf0) {
                   17786:                                code = (code & 7) | ((code & 0x10) >> 1);
                   17787:                        }
                   17788:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   17789:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   17790:                                        const char *message = NULL;
                   17791:                                        if(active_code_page == 932) {
                   17792:                                                message = param_error_table[i].message_japanese;
                   17793:                                        }
                   17794:                                        if(message == NULL) {
                   17795:                                                message = param_error_table[i].message_english;
                   17796:                                        }
                   17797:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   17798:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   17799:                                        
                   17800:                                        SREG(ES) = WORK_TOP >> 4;
                   17801:                                        i386_load_segment_descriptor(ES);
                   17802:                                        REG16(DI) = 0x0000;
                   17803:                                        break;
                   17804:                                }
                   17805:                        }
                   17806:                }
                   17807:                break;
1.1.1.49  root     17808:        case 0x6f:
                   17809:                // dummy interrupt for end of alter page map and call
                   17810:                {
                   17811:                        UINT16 handles[4], pages[4];
                   17812:                        
                   17813:                        // pop old mapping data in new mapping
                   17814:                        for(int i = 0; i < 4; i++) {
                   17815:                                pages  [3 - i] = i386_pop16();
                   17816:                                handles[3 - i] = i386_pop16();
                   17817:                        }
                   17818:                        
                   17819:                        // restore old mapping
                   17820:                        for(int i = 0; i < 4; i++) {
                   17821:                                UINT16 handle = handles[i];
                   17822:                                UINT16 page   = pages  [i];
                   17823:                                
                   17824:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   17825:                                        ems_map_page(i, handle, page);
                   17826:                                } else {
                   17827:                                        ems_unmap_page(i);
                   17828:                                }
                   17829:                        }
                   17830:                        // do ret_far (pop cs/ip) in old mapping
                   17831:                }
                   17832:                break;
1.1.1.8   root     17833:        case 0x70:
                   17834:        case 0x71:
                   17835:        case 0x72:
                   17836:        case 0x73:
                   17837:        case 0x74:
                   17838:        case 0x75:
                   17839:        case 0x76:
                   17840:        case 0x77:
                   17841:                // EOI
                   17842:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   17843:                        pic[0].isr &= ~(1 << 2); // master
                   17844:                }
                   17845:                pic_update();
                   17846:                break;
1.1       root     17847:        default:
1.1.1.22  root     17848: //             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     17849:                break;
                   17850:        }
                   17851:        
                   17852:        // update cursor position
                   17853:        if(cursor_moved) {
1.1.1.36  root     17854:                pcbios_update_cursor_position();
1.1       root     17855:                cursor_moved = false;
                   17856:        }
                   17857: }
                   17858: 
                   17859: // init
                   17860: 
                   17861: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   17862: {
                   17863:        // init file handler
                   17864:        memset(file_handler, 0, sizeof(file_handler));
                   17865:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   17866:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   17867:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     17868: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     17869:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     17870: #else
                   17871:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   17872: #endif
                   17873:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     17874:        }
1.1.1.21  root     17875:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45  root     17876: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   17877:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     17878:        }
1.1       root     17879:        _dup2(0, DUP_STDIN);
                   17880:        _dup2(1, DUP_STDOUT);
                   17881:        _dup2(2, DUP_STDERR);
1.1.1.21  root     17882:        _dup2(3, DUP_STDAUX);
                   17883:        _dup2(4, DUP_STDPRN);
1.1       root     17884:        
1.1.1.24  root     17885:        // init mouse
                   17886:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     17887:        mouse.enabled = true;   // from DOSBox
                   17888:        mouse.hidden = 1;       // hidden in default ???
                   17889:        mouse.old_hidden = 1;   // from DOSBox
                   17890:        mouse.max_position.x = 8 * (scr_width  - 1);
                   17891:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     17892:        mouse.mickey.x = 8;
                   17893:        mouse.mickey.y = 16;
                   17894:        
1.1.1.26  root     17895: #ifdef SUPPORT_XMS
                   17896:        // init xms
                   17897:        msdos_xms_init();
                   17898: #endif
                   17899:        
1.1       root     17900:        // init process
                   17901:        memset(process, 0, sizeof(process));
                   17902:        
1.1.1.13  root     17903:        // init dtainfo
                   17904:        msdos_dta_info_init();
                   17905:        
1.1       root     17906:        // init memory
                   17907:        memset(mem, 0, sizeof(mem));
                   17908:        
                   17909:        // bios data area
1.1.1.23  root     17910:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     17911:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   17912:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.58  root     17913: //     CONSOLE_FONT_INFO cfi;
1.1.1.57  root     17914: //     GetCurrentConsoleFont(hStdout, FALSE, &cfi);
1.1.1.14  root     17915:        
                   17916:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   17917:        text_vram_top_address = TEXT_VRAM_TOP;
                   17918:        text_vram_end_address = text_vram_top_address + regen;
                   17919:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   17920:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     17921:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1.1.14  root     17922:        
                   17923:        if(regen > 0x4000) {
                   17924:                regen = 0x8000;
                   17925:                vram_pages = 1;
                   17926:        } else if(regen > 0x2000) {
                   17927:                regen = 0x4000;
                   17928:                vram_pages = 2;
                   17929:        } else if(regen > 0x1000) {
                   17930:                regen = 0x2000;
                   17931:                vram_pages = 4;
                   17932:        } else {
                   17933:                regen = 0x1000;
                   17934:                vram_pages = 8;
                   17935:        }
1.1       root     17936:        
1.1.1.25  root     17937:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   17938:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     17939:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   17940:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     17941:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     17942:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   17943:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     17944: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17945:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     17946: #endif
1.1.1.26  root     17947:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     17948:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     17949:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   17950:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     17951:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     17952:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   17953:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     17954:        *(UINT16 *)(mem + 0x44e) = 0;
                   17955:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     17956:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     17957:        *(UINT8  *)(mem + 0x460) = 7;
                   17958:        *(UINT8  *)(mem + 0x461) = 7;
                   17959:        *(UINT8  *)(mem + 0x462) = 0;
                   17960:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     17961:        *(UINT8  *)(mem + 0x465) = 0x09;
                   17962:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     17963:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     17964:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   17965:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     17966:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
1.1.1.58  root     17967:        *(UINT16 *)(mem + 0x485) = font_height;
1.1.1.14  root     17968:        *(UINT8  *)(mem + 0x487) = 0x60;
                   17969:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     17970: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17971:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     17972: #endif
1.1.1.14  root     17973:        
                   17974:        // initial screen
                   17975:        SMALL_RECT rect;
                   17976:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     17977:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     17978:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   17979:                for(int x = 0; x < scr_width; x++) {
                   17980:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   17981:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   17982:                }
                   17983:        }
1.1       root     17984:        
1.1.1.19  root     17985:        // init mcb
1.1       root     17986:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     17987:        
                   17988:        // iret table
                   17989:        // note: int 2eh vector should address the routine in command.com,
                   17990:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   17991:        // so move iret table into allocated memory block
                   17992:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.58  root     17993:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, (IRET_SIZE + 5 * 128) >> 4);
1.1.1.19  root     17994:        IRET_TOP = seg << 4;
1.1.1.58  root     17995:        seg += (IRET_SIZE + 5 * 128) >> 4;
1.1.1.25  root     17996:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     17997:        
1.1.1.58  root     17998:        // note: SO1 checks int 21h vector and if it aims iret (cfh)
                   17999:        // it is recognized SO1 is not running on MS-DOS environment
                   18000:        for(int i = 0; i < 128; i++) {
                   18001:                // jmp far (IRET_TOP >> 4):(interrupt number)
                   18002:                *(UINT8  *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 0) = 0xea;
                   18003:                *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 1) = i;
                   18004:                *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 3) = IRET_TOP >> 4;
                   18005:        }
                   18006:        
1.1.1.19  root     18007:        // dummy xms/ems device
1.1.1.33  root     18008:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     18009:        XMS_TOP = seg << 4;
                   18010:        seg += XMS_SIZE >> 4;
                   18011:        
                   18012:        // environment
1.1.1.33  root     18013:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     18014:        int env_seg = seg;
                   18015:        int ofs = 0;
1.1.1.32  root     18016:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   18017:        char comspec_added = 0;
1.1.1.33  root     18018:        char lastdrive_added = 0;
1.1.1.32  root     18019:        char env_msdos_path[ENV_SIZE] = {0};
                   18020:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     18021:        char prompt_added = 0;
1.1.1.32  root     18022:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     18023:        char tz_added = 0;
1.1.1.45  root     18024:        const char *path, *short_path;
1.1.1.32  root     18025:        
                   18026:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   18027:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18028:                        strcpy(env_append, short_path);
                   18029:                }
                   18030:        }
                   18031:        if((path = getenv("APPEND")) != NULL) {
                   18032:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18033:                        if(env_append[0] != '\0') {
                   18034:                                strcat(env_append, ";");
                   18035:                        }
                   18036:                        strcat(env_append, short_path);
                   18037:                }
                   18038:        }
                   18039:        
                   18040:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   18041:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18042:                        strcpy(comspec_path, short_path);
                   18043:                }
                   18044:        }
                   18045:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   18046:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18047:                        strcpy(comspec_path, short_path);
                   18048:                }
                   18049:        }
1.1       root     18050:        
1.1.1.28  root     18051:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     18052:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18053:                        strcpy(env_msdos_path, short_path);
                   18054:                        strcpy(env_path, short_path);
1.1.1.14  root     18055:                }
                   18056:        }
1.1.1.28  root     18057:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     18058:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18059:                        if(env_path[0] != '\0') {
                   18060:                                strcat(env_path, ";");
                   18061:                        }
                   18062:                        strcat(env_path, short_path);
1.1.1.9   root     18063:                }
                   18064:        }
1.1.1.32  root     18065:        
1.1.1.60  root     18066:        if(GetTempPathA(ENV_SIZE, env_temp) != 0) {
1.1.1.32  root     18067:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     18068:        }
1.1.1.32  root     18069:        for(int i = 0; i < 4; i++) {
                   18070:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   18071:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   18072:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18073:                                strcpy(env_temp, short_path);
                   18074:                                break;
                   18075:                        }
                   18076:                }
1.1.1.24  root     18077:        }
1.1.1.32  root     18078:        
1.1.1.9   root     18079:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     18080:                // lower to upper
1.1.1.28  root     18081:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     18082:                strcpy(tmp, *p);
                   18083:                for(int i = 0;; i++) {
                   18084:                        if(tmp[i] == '=') {
                   18085:                                tmp[i] = '\0';
                   18086:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     18087:                                my_strupr(name);
1.1       root     18088:                                tmp[i] = '=';
                   18089:                                break;
                   18090:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     18091:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     18092:                        }
                   18093:                }
1.1.1.33  root     18094:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   18095:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   18096:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     18097:                        // ignore non standard environments
                   18098:                } else {
1.1.1.33  root     18099:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     18100:                                if(env_append[0] != '\0') {
                   18101:                                        sprintf(tmp, "APPEND=%s", env_append);
                   18102:                                } else {
                   18103:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   18104:                                }
                   18105:                                append_added = 1;
                   18106:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     18107:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     18108:                                comspec_added = 1;
1.1.1.33  root     18109:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   18110:                                char *env = getenv("MSDOS_LASTDRIVE");
                   18111:                                if(env != NULL) {
                   18112:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   18113:                                }
                   18114:                                lastdrive_added = 1;
                   18115:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     18116:                                if(env_msdos_path[0] != '\0') {
                   18117:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   18118:                                } else {
                   18119:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   18120:                                }
1.1.1.33  root     18121:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     18122:                                if(env_path[0] != '\0') {
                   18123:                                        sprintf(tmp, "PATH=%s", env_path);
                   18124:                                } else {
                   18125:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   18126:                                }
1.1.1.32  root     18127:                                path_added = 1;
1.1.1.33  root     18128:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   18129:                                prompt_added = 1;
1.1.1.28  root     18130:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   18131:                                if(env_temp[0] != '\0') {
                   18132:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   18133:                                } else {
                   18134:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   18135:                                }
1.1.1.32  root     18136:                                temp_added = 1;
1.1.1.33  root     18137:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     18138:                                if(env_temp[0] != '\0') {
                   18139:                                        sprintf(tmp, "TMP=%s", env_temp);
                   18140:                                } else {
                   18141:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     18142:                                }
1.1.1.32  root     18143:                                tmp_added = 1;
1.1.1.33  root     18144:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   18145:                                char *env = getenv("MSDOS_TZ");
                   18146:                                if(env != NULL) {
                   18147:                                        sprintf(tmp, "TZ=%s", env);
                   18148:                                }
                   18149:                                tz_added = 1;
1.1       root     18150:                        }
                   18151:                        int len = strlen(tmp);
1.1.1.14  root     18152:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     18153:                                fatalerror("too many environments\n");
                   18154:                        }
                   18155:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   18156:                        ofs += len + 1;
                   18157:                }
                   18158:        }
1.1.1.32  root     18159:        if(!append_added && env_append[0] != '\0') {
                   18160:                #define SET_ENV(name, value) { \
                   18161:                        char tmp[ENV_SIZE]; \
                   18162:                        sprintf(tmp, "%s=%s", name, value); \
                   18163:                        int len = strlen(tmp); \
                   18164:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   18165:                                fatalerror("too many environments\n"); \
                   18166:                        } \
                   18167:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   18168:                        ofs += len + 1; \
                   18169:                }
                   18170:                SET_ENV("APPEND", env_append);
                   18171:        }
                   18172:        if(!comspec_added) {
                   18173:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   18174:        }
1.1.1.33  root     18175:        if(!lastdrive_added) {
                   18176:                SET_ENV("LASTDRIVE", "Z");
                   18177:        }
1.1.1.32  root     18178:        if(!path_added) {
                   18179:                SET_ENV("PATH", env_path);
                   18180:        }
1.1.1.33  root     18181:        if(!prompt_added) {
                   18182:                SET_ENV("PROMPT", "$P$G");
                   18183:        }
1.1.1.32  root     18184:        if(!temp_added) {
                   18185:                SET_ENV("TEMP", env_temp);
                   18186:        }
                   18187:        if(!tmp_added) {
                   18188:                SET_ENV("TMP", env_temp);
                   18189:        }
1.1.1.33  root     18190:        if(!tz_added) {
                   18191:                TIME_ZONE_INFORMATION tzi;
                   18192:                HKEY hKey, hSubKey;
                   18193:                char tzi_std_name[64];
                   18194:                char tz_std[8] = "GMT";
                   18195:                char tz_dlt[8] = "GST";
                   18196:                char tz_value[32];
                   18197:                
                   18198:                // timezone name from GetTimeZoneInformation may not be english
                   18199:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   18200:                setlocale(LC_CTYPE, "");
                   18201:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   18202:                
                   18203:                // get english timezone name from registry
1.1.1.60  root     18204:                if(RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
1.1.1.33  root     18205:                        for(DWORD i = 0; !tz_added; i++) {
                   18206:                                char reg_name[256], sub_key[1024], std_name[256];
                   18207:                                DWORD size;
                   18208:                                FILETIME ftTime;
1.1.1.60  root     18209:                                LONG result = RegEnumKeyExA(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
1.1.1.33  root     18210:                                
                   18211:                                if(result == ERROR_SUCCESS) {
                   18212:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
1.1.1.60  root     18213:                                        if(RegOpenKeyExA(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   18214:                                                if(RegQueryValueExA(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
1.1.1.33  root     18215:                                                        // search english timezone name from table
1.1.1.37  root     18216:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     18217:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   18218:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   18219:                                                                                if(tz_table[j].std != NULL) {
                   18220:                                                                                        strcpy(tz_std, tz_table[j].std);
                   18221:                                                                                }
                   18222:                                                                                if(tz_table[j].dlt != NULL) {
                   18223:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   18224:                                                                                }
                   18225:                                                                                tz_added = 1;
                   18226:                                                                                break;
                   18227:                                                                        }
                   18228:                                                                }
                   18229:                                                        }
                   18230:                                                }
                   18231:                                                RegCloseKey(hSubKey);
                   18232:                                        }
                   18233:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   18234:                                        break;
                   18235:                                }
                   18236:                        }
                   18237:                        RegCloseKey(hKey);
                   18238:                }
                   18239:                if((tzi.Bias % 60) != 0) {
                   18240:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   18241:                } else {
                   18242:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   18243:                }
                   18244:                if(daylight) {
                   18245:                        strcat(tz_value, tz_dlt);
                   18246:                }
                   18247:                SET_ENV("TZ", tz_value);
                   18248:        }
1.1       root     18249:        seg += (ENV_SIZE >> 4);
                   18250:        
                   18251:        // psp
1.1.1.33  root     18252:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     18253:        current_psp = seg;
1.1.1.35  root     18254:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   18255:        psp->parent_psp = current_psp;
1.1       root     18256:        seg += (PSP_SIZE >> 4);
                   18257:        
1.1.1.19  root     18258:        // first free mcb in conventional memory
1.1.1.33  root     18259:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     18260:        first_mcb = seg;
                   18261:        
1.1.1.19  root     18262:        // dummy mcb to link to umb
1.1.1.33  root     18263: #if 0
1.1.1.39  root     18264:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     18265: #else
1.1.1.39  root     18266:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     18267: #endif
1.1.1.19  root     18268:        
                   18269:        // first free mcb in upper memory block
1.1.1.8   root     18270:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     18271:        
1.1.1.29  root     18272: #ifdef SUPPORT_HMA
                   18273:        // first free mcb in high memory area
                   18274:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   18275: #endif
                   18276:        
1.1.1.26  root     18277:        // interrupt vector
1.1.1.58  root     18278:        for(int i = 0; i < 256; i++) {
                   18279:                // 00-07: CPU exception handler
                   18280:                // 08-0F: IRQ 0-7
                   18281:                // 10-1F: PC BIOS
                   18282:                // 20-3F: MS-DOS system call
                   18283:                // 70-77: IRQ 8-15
                   18284:                *(UINT16 *)(mem + 4 * i + 0) = (i <= 0x3f || (i >= 0x70 && i <= 0x77)) ? (IRET_SIZE + 5 * i) : i;
1.1.1.26  root     18285:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   18286:        }
1.1.1.49  root     18287:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffc:0018 irq0 (system timer)
                   18288:        *(UINT16 *)(mem + 4 * 0x08 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     18289:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   18290:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   18291:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   18292:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
1.1.1.49  root     18293:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffc:0000 irq12 (mouse)
                   18294:        *(UINT16 *)(mem + 4 * 0x74 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     18295:        
1.1.1.29  root     18296:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     18297:        static const struct {
                   18298:                UINT16 attributes;
                   18299:                char *dev_name;
                   18300:        } dummy_devices[] = {
                   18301:                {0x8013, "CON     "},
                   18302:                {0x8000, "AUX     "},
                   18303:                {0xa0c0, "PRN     "},
                   18304:                {0x8008, "CLOCK$  "},
                   18305:                {0x8000, "COM1    "},
                   18306:                {0xa0c0, "LPT1    "},
                   18307:                {0xa0c0, "LPT2    "},
                   18308:                {0xa0c0, "LPT3    "},
                   18309:                {0x8000, "COM2    "},
                   18310:                {0x8000, "COM3    "},
                   18311:                {0x8000, "COM4    "},
1.1.1.30  root     18312: //             {0xc000, "CONFIG$ "},
                   18313:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     18314:        };
                   18315:        static const UINT8 dummy_device_routine[] = {
                   18316:                // from NUL device of Windows 98 SE
                   18317:                // or word ptr ES:[BX+03],0100
                   18318:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   18319:                // retf
                   18320:                0xcb,
                   18321:        };
1.1.1.29  root     18322:        device_t *last = NULL;
1.1.1.32  root     18323:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     18324:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     18325:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   18326:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     18327:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     18328:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   18329:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     18330:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     18331:                last = device;
                   18332:        }
                   18333:        if(last != NULL) {
                   18334:                last->next_driver.w.l = 0;
                   18335:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     18336:        }
1.1.1.29  root     18337:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     18338:        
1.1.1.25  root     18339:        // dos info
                   18340:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   18341:        dos_info->magic_word = 1;
                   18342:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   18343:        dos_info->first_dpb.w.l = 0;
                   18344:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   18345:        dos_info->first_sft.w.l = 0;
                   18346:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     18347:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     18348:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     18349:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     18350:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   18351:        dos_info->max_sector_len = 512;
                   18352:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   18353:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   18354:        dos_info->cds.w.l = 0;
                   18355:        dos_info->cds.w.h = CDS_TOP >> 4;
                   18356:        dos_info->fcb_table.w.l = 0;
                   18357:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   18358:        dos_info->last_drive = 'Z' - 'A' + 1;
                   18359:        dos_info->buffers_x = 20;
                   18360:        dos_info->buffers_y = 0;
                   18361:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     18362:        dos_info->nul_device.next_driver.w.l = 22;
                   18363:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     18364:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     18365:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   18366:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18367:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   18368:        dos_info->disk_buf_heads.w.l = 0;
                   18369:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     18370:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     18371:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   18372:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     18373:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     18374:        
                   18375:        char *env;
                   18376:        if((env = getenv("LASTDRIVE")) != NULL) {
                   18377:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18378:                        dos_info->last_drive = env[0] - 'A' + 1;
                   18379:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18380:                        dos_info->last_drive = env[0] - 'a' + 1;
                   18381:                }
                   18382:        }
                   18383:        if((env = getenv("windir")) != NULL) {
                   18384:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18385:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   18386:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18387:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   18388:                }
                   18389:        }
                   18390: #if defined(HAS_I386)
                   18391:        dos_info->i386_or_later = 1;
                   18392: #else
                   18393:        dos_info->i386_or_later = 0;
                   18394: #endif
                   18395:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   18396:        
1.1.1.27  root     18397:        // ems (int 67h) and xms
1.1.1.25  root     18398:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   18399:        xms_device->next_driver.w.l = 0xffff;
                   18400:        xms_device->next_driver.w.h = 0xffff;
                   18401:        xms_device->attributes = 0xc000;
1.1.1.29  root     18402:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   18403:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18404:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   18405:        
1.1.1.59  root     18406:        mem[XMS_TOP + 0x12] = 0xcd;     // int 65h (dummy)
                   18407:        mem[XMS_TOP + 0x13] = 0x65;
1.1.1.26  root     18408:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     18409: #ifdef SUPPORT_XMS
                   18410:        if(support_xms) {
1.1.1.59  root     18411:                mem[XMS_TOP + 0x15] = 0xcd;     // int 66h (dummy)
                   18412:                mem[XMS_TOP + 0x16] = 0x66;
1.1.1.26  root     18413:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     18414:        } else
                   18415: #endif
1.1.1.26  root     18416:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     18417:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     18418:        
1.1.1.26  root     18419:        // irq12 routine (mouse)
1.1.1.59  root     18420:        mem[DUMMY_TOP + 0x00] = 0xcd;   // int 69h (dummy)
                   18421:        mem[DUMMY_TOP + 0x01] = 0x69;
1.1.1.49  root     18422:        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far mouse
                   18423:        mem[DUMMY_TOP + 0x03] = 0xff;
                   18424:        mem[DUMMY_TOP + 0x04] = 0xff;
                   18425:        mem[DUMMY_TOP + 0x05] = 0xff;
                   18426:        mem[DUMMY_TOP + 0x06] = 0xff;
1.1.1.59  root     18427: //     mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6ah (dummy)
                   18428: //     mem[DUMMY_TOP + 0x08] = 0x6a;
1.1.1.49  root     18429:        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   18430:        mem[DUMMY_TOP + 0x08] = 0x6b;
                   18431:        mem[DUMMY_TOP + 0x09] = 0xcf;   // iret
1.1.1.24  root     18432:        
1.1.1.27  root     18433:        // case map routine
1.1.1.49  root     18434:        mem[DUMMY_TOP + 0x0a] = 0xcd;   // int 6ch (dummy)
                   18435:        mem[DUMMY_TOP + 0x0b] = 0x6c;
                   18436:        mem[DUMMY_TOP + 0x0c] = 0xcb;   // retf
1.1.1.27  root     18437:        
                   18438:        // font read routine
1.1.1.49  root     18439:        mem[DUMMY_TOP + 0x0d] = 0xcd;   // int 6dh (dummy)
                   18440:        mem[DUMMY_TOP + 0x0e] = 0x6d;
                   18441:        mem[DUMMY_TOP + 0x0f] = 0xcb;   // retf
1.1.1.19  root     18442:        
1.1.1.32  root     18443:        // error message read routine
1.1.1.49  root     18444:        mem[DUMMY_TOP + 0x10] = 0xcd;   // int 6eh (dummy)
                   18445:        mem[DUMMY_TOP + 0x11] = 0x6e;
                   18446:        mem[DUMMY_TOP + 0x12] = 0xcb;   // retf
1.1.1.32  root     18447:        
1.1.1.35  root     18448:        // dummy loop to wait BIOS/DOS service is done
1.1.1.49  root     18449:        mem[DUMMY_TOP + 0x13] = 0xe6;   // out f7h, al
                   18450:        mem[DUMMY_TOP + 0x14] = 0xf7;
                   18451:        mem[DUMMY_TOP + 0x15] = 0x78;   // js/jns -4
                   18452:        mem[DUMMY_TOP + 0x16] = 0xfc;
                   18453:        mem[DUMMY_TOP + 0x17] = 0xcb;   // retf
1.1.1.35  root     18454:        
1.1.1.50  root     18455:        // irq0 routine (system timer)
1.1.1.49  root     18456:        mem[DUMMY_TOP + 0x18] = 0xcd;   // int 1ch
                   18457:        mem[DUMMY_TOP + 0x19] = 0x1c;
                   18458:        mem[DUMMY_TOP + 0x1a] = 0xea;   // jmp far (IRET_TOP >> 4):0008
                   18459:        mem[DUMMY_TOP + 0x1b] = 0x08;
                   18460:        mem[DUMMY_TOP + 0x1c] = 0x00;
                   18461:        mem[DUMMY_TOP + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   18462:        mem[DUMMY_TOP + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
                   18463:        
                   18464:        // alter page map and call routine
                   18465:        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   18466:        mem[DUMMY_TOP + 0x20] = 0xff;
                   18467:        mem[DUMMY_TOP + 0x21] = 0xff;
                   18468:        mem[DUMMY_TOP + 0x22] = 0xff;
                   18469:        mem[DUMMY_TOP + 0x23] = 0xff;
                   18470:        mem[DUMMY_TOP + 0x24] = 0xcd;   // int 6fh (dummy)
                   18471:        mem[DUMMY_TOP + 0x25] = 0x6f;
                   18472:        mem[DUMMY_TOP + 0x26] = 0xcb;   // retf
1.1.1.14  root     18473:        
1.1.1.50  root     18474:        // call int 29h routine
                   18475:        mem[DUMMY_TOP + 0x27] = 0xcd;   // int 29h
                   18476:        mem[DUMMY_TOP + 0x28] = 0x29;
                   18477:        mem[DUMMY_TOP + 0x29] = 0xcb;   // retf
                   18478:        
1.1.1.26  root     18479:        // boot routine
1.1.1.59  root     18480:        mem[0xffff0 + 0x00] = 0xf4;     // halt to exit MS-DOS Player
                   18481: #if 1
                   18482:        mem[0xffff0 + 0x05] = '0';      // rom date (same as DOSBox)
                   18483:        mem[0xffff0 + 0x06] = '1';
                   18484:        mem[0xffff0 + 0x07] = '/';
                   18485:        mem[0xffff0 + 0x08] = '0';
                   18486:        mem[0xffff0 + 0x09] = '1';
                   18487:        mem[0xffff0 + 0x0a] = '/';
                   18488:        mem[0xffff0 + 0x0b] = '9';
                   18489:        mem[0xffff0 + 0x0c] = '2';
                   18490:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id (pc/at)
                   18491:        mem[0xffff0 + 0x0f] = 0x55;     // signature
                   18492: #else
                   18493:        mem[0xffff0 + 0x05] = '0';      // rom date (same as Windows 98 SE)
1.1.1.49  root     18494:        mem[0xffff0 + 0x06] = '2';
                   18495:        mem[0xffff0 + 0x07] = '/';
                   18496:        mem[0xffff0 + 0x08] = '2';
                   18497:        mem[0xffff0 + 0x09] = '2';
                   18498:        mem[0xffff0 + 0x0a] = '/';
                   18499:        mem[0xffff0 + 0x0b] = '0';
                   18500:        mem[0xffff0 + 0x0c] = '6';
1.1.1.59  root     18501:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id (pc/at)
1.1.1.49  root     18502:        mem[0xffff0 + 0x0f] = 0x00;
1.1.1.59  root     18503: #endif
1.1.1.24  root     18504:        
1.1       root     18505:        // param block
                   18506:        // + 0: param block (22bytes)
                   18507:        // +24: fcb1/2 (20bytes)
                   18508:        // +44: command tail (128bytes)
                   18509:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   18510:        param->env_seg = 0;
                   18511:        param->cmd_line.w.l = 44;
                   18512:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   18513:        param->fcb1.w.l = 24;
                   18514:        param->fcb1.w.h = (WORK_TOP >> 4);
                   18515:        param->fcb2.w.l = 24;
                   18516:        param->fcb2.w.h = (WORK_TOP >> 4);
                   18517:        
                   18518:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   18519:        
                   18520:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   18521:        if(argc > 1) {
                   18522:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   18523:                for(int i = 2; i < argc; i++) {
                   18524:                        char tmp[128];
                   18525:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   18526:                        strcpy(cmd_line->cmd, tmp);
                   18527:                }
                   18528:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   18529:        } else {
                   18530:                cmd_line->len = 0;
                   18531:        }
                   18532:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   18533:        
                   18534:        // system file table
1.1.1.21  root     18535:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   18536:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     18537:        
1.1.1.19  root     18538:        // disk buffer header (from DOSBox)
                   18539:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   18540:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   18541:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   18542:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   18543:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   18544:        
1.1       root     18545:        // fcb table
                   18546:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     18547:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     18548:        
1.1.1.41  root     18549:        // drive parameter block
1.1.1.42  root     18550:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     18551:                // may be a floppy drive
1.1.1.44  root     18552:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   18553:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   18554:                cds->drive_attrib = 0x4000;     // physical drive
                   18555:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   18556:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   18557:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   18558:                cds->bs_offset = 2;
                   18559:                
1.1.1.41  root     18560:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   18561:                dpb->drive_num = i;
                   18562:                dpb->unit_num = i;
1.1.1.43  root     18563:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   18564:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     18565:        }
                   18566:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     18567:                msdos_cds_update(i);
1.1.1.42  root     18568:                UINT16 seg, ofs;
                   18569:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     18570:        }
                   18571:        
1.1.1.17  root     18572:        // nls stuff
                   18573:        msdos_nls_tables_init();
1.1       root     18574:        
                   18575:        // execute command
1.1.1.28  root     18576:        try {
1.1.1.52  root     18577:                if(msdos_process_exec(argv[0], param, 0, true)) {
1.1.1.28  root     18578:                        fatalerror("'%s' not found\n", argv[0]);
                   18579:                }
                   18580:        } catch(...) {
                   18581:                // we should not reach here :-(
                   18582:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     18583:        }
                   18584:        retval = 0;
                   18585:        return(0);
                   18586: }
                   18587: 
                   18588: #define remove_std_file(path) { \
                   18589:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   18590:        if(fd != -1) { \
                   18591:                _lseek(fd, 0, SEEK_END); \
                   18592:                int size = _tell(fd); \
                   18593:                _close(fd); \
                   18594:                if(size == 0) { \
                   18595:                        remove(path); \
                   18596:                } \
                   18597:        } \
                   18598: }
                   18599: 
                   18600: void msdos_finish()
                   18601: {
                   18602:        for(int i = 0; i < MAX_FILES; i++) {
                   18603:                if(file_handler[i].valid) {
                   18604:                        _close(i);
                   18605:                }
                   18606:        }
1.1.1.21  root     18607: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     18608:        remove_std_file("stdaux.txt");
1.1.1.21  root     18609: #endif
1.1.1.30  root     18610: #ifdef SUPPORT_XMS
                   18611:        msdos_xms_finish();
                   18612: #endif
1.1       root     18613:        msdos_dbcs_table_finish();
                   18614: }
                   18615: 
                   18616: /* ----------------------------------------------------------------------------
                   18617:        PC/AT hardware emulation
                   18618: ---------------------------------------------------------------------------- */
                   18619: 
                   18620: void hardware_init()
                   18621: {
1.1.1.3   root     18622:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     18623:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     18624:        m_IF = 1;
1.1.1.3   root     18625: #if defined(HAS_I386)
1.1       root     18626:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   18627:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     18628: #endif
                   18629:        i386_set_a20_line(0);
1.1.1.14  root     18630:        
1.1.1.19  root     18631:        ems_init();
1.1.1.25  root     18632:        dma_init();
1.1       root     18633:        pic_init();
1.1.1.25  root     18634:        pio_init();
1.1.1.8   root     18635: #ifdef PIT_ALWAYS_RUNNING
                   18636:        pit_init();
                   18637: #else
1.1       root     18638:        pit_active = 0;
                   18639: #endif
1.1.1.25  root     18640:        sio_init();
1.1.1.8   root     18641:        cmos_init();
                   18642:        kbd_init();
1.1       root     18643: }
                   18644: 
1.1.1.10  root     18645: void hardware_finish()
                   18646: {
                   18647: #if defined(HAS_I386)
                   18648:        vtlb_free(m_vtlb);
                   18649: #endif
1.1.1.19  root     18650:        ems_finish();
1.1.1.37  root     18651:        pio_finish();
1.1.1.25  root     18652:        sio_finish();
1.1.1.10  root     18653: }
                   18654: 
1.1.1.28  root     18655: void hardware_release()
                   18656: {
                   18657:        // release hardware resources when this program will be terminated abnormally
                   18658: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18659:        if(fp_debug_log != NULL) {
                   18660:                fclose(fp_debug_log);
                   18661:                fp_debug_log = NULL;
1.1.1.28  root     18662:        }
                   18663: #endif
                   18664: #if defined(HAS_I386)
                   18665:        vtlb_free(m_vtlb);
                   18666: #endif
                   18667:        ems_release();
1.1.1.37  root     18668:        pio_release();
1.1.1.28  root     18669:        sio_release();
                   18670: }
                   18671: 
1.1       root     18672: void hardware_run()
                   18673: {
1.1.1.22  root     18674: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     18675:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     18676:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     18677: #endif
1.1.1.51  root     18678: #ifdef USE_DEBUGGER
                   18679:        m_int_num = -1;
                   18680: #endif
1.1.1.54  root     18681:        while(!m_exit) {
1.1.1.50  root     18682:                hardware_run_cpu();
1.1       root     18683:        }
1.1.1.22  root     18684: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18685:        if(fp_debug_log != NULL) {
                   18686:                fclose(fp_debug_log);
                   18687:                fp_debug_log = NULL;
1.1.1.28  root     18688:        }
1.1.1.22  root     18689: #endif
1.1       root     18690: }
                   18691: 
1.1.1.50  root     18692: inline void hardware_run_cpu()
                   18693: {
                   18694: #if defined(HAS_I386)
                   18695:        CPU_EXECUTE_CALL(i386);
                   18696:        if(m_eip != m_prev_eip) {
                   18697:                idle_ops++;
                   18698:        }
                   18699: #else
                   18700:        CPU_EXECUTE_CALL(CPU_MODEL);
                   18701:        if(m_pc != m_prevpc) {
                   18702:                idle_ops++;
                   18703:        }
                   18704: #endif
                   18705: #ifdef USE_DEBUGGER
                   18706:        // Disallow reentering CPU_EXECUTE() in msdos_syscall()
                   18707:        if(m_int_num >= 0) {
                   18708:                unsigned num = (unsigned)m_int_num;
                   18709:                m_int_num = -1;
                   18710:                msdos_syscall(num);
                   18711:        }
                   18712: #endif
                   18713:        if(++update_ops == UPDATE_OPS) {
                   18714:                update_ops = 0;
                   18715:                hardware_update();
                   18716:        }
                   18717: }
                   18718: 
1.1       root     18719: void hardware_update()
                   18720: {
1.1.1.8   root     18721:        static UINT32 prev_time = 0;
                   18722:        UINT32 cur_time = timeGetTime();
                   18723:        
                   18724:        if(prev_time != cur_time) {
                   18725:                // update pit and raise irq0
                   18726: #ifndef PIT_ALWAYS_RUNNING
                   18727:                if(pit_active)
                   18728: #endif
                   18729:                {
                   18730:                        if(pit_run(0, cur_time)) {
                   18731:                                pic_req(0, 0, 1);
                   18732:                        }
                   18733:                        pit_run(1, cur_time);
                   18734:                        pit_run(2, cur_time);
                   18735:                }
1.1.1.24  root     18736:                
1.1.1.25  root     18737:                // update sio and raise irq4/3
1.1.1.29  root     18738:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     18739:                        sio_update(c);
                   18740:                }
                   18741:                
1.1.1.24  root     18742:                // update keyboard and mouse
1.1.1.14  root     18743:                static UINT32 prev_tick = 0;
                   18744:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     18745:                
1.1.1.14  root     18746:                if(prev_tick != cur_tick) {
                   18747:                        // update keyboard flags
                   18748:                        UINT8 state;
1.1.1.24  root     18749:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   18750:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   18751:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   18752:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   18753:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   18754:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   18755:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   18756:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     18757:                        mem[0x417] = state;
                   18758:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   18759:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   18760:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   18761:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     18762: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   18763: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     18764:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   18765:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   18766:                        mem[0x418] = state;
                   18767:                        
1.1.1.24  root     18768:                        // update console input if needed
1.1.1.34  root     18769:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     18770:                                update_console_input();
                   18771:                        }
1.1.1.57  root     18772:                        if(!(kbd_status & 1)) {
                   18773:                                if(key_buf_data != NULL) {
                   18774: #ifdef USE_SERVICE_THREAD
                   18775:                                        EnterCriticalSection(&key_buf_crit_sect);
                   18776: #endif
                   18777:                                        if(!key_buf_data->empty()) {
                   18778:                                                kbd_data = key_buf_data->read();
                   18779:                                                kbd_status |= 1;
                   18780:                                                key_changed = true;
                   18781:                                        }
                   18782: #ifdef USE_SERVICE_THREAD
                   18783:                                        LeaveCriticalSection(&key_buf_crit_sect);
                   18784: #endif
                   18785:                                }
                   18786:                        }
1.1.1.24  root     18787:                        
1.1.1.57  root     18788:                        // raise irq1 if key is pressed/released or key buffer is not empty
1.1.1.56  root     18789:                        if(!key_changed) {
1.1.1.55  root     18790: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     18791:                                EnterCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     18792: #endif
1.1.1.57  root     18793:                                if(!pcbios_is_key_buffer_empty()) {
                   18794: /*
                   18795:                                        if(!(kbd_status & 1)) {
                   18796:                                                UINT16 head = *(UINT16 *)(mem + 0x41a);
                   18797:                                                UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   18798:                                                if(head != tail) {
                   18799:                                                        int key_char = mem[0x400 + (head++)];
                   18800:                                                        int key_scan = mem[0x400 + (head++)];
                   18801:                                                        kbd_data = key_char ? key_char : key_scan;
                   18802:                                                        kbd_status |= 1;
                   18803:                                                }
                   18804:                                        }
                   18805: */
                   18806:                                        key_changed = true;
                   18807:                                }
1.1.1.55  root     18808: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     18809:                                LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     18810: #endif
1.1.1.56  root     18811:                        }
                   18812:                        if(key_changed) {
1.1.1.8   root     18813:                                pic_req(0, 1, 1);
1.1.1.56  root     18814:                                key_changed = false;
1.1.1.24  root     18815:                        }
                   18816:                        
                   18817:                        // raise irq12 if mouse status is changed
1.1.1.59  root     18818:                        if((mouse.status & 0x1f) && mouse.call_addr_ps2.dw && mouse.enabled_ps2) {
1.1.1.54  root     18819:                                mouse.status_irq = 0; // ???
                   18820:                                mouse.status_irq_alt = 0; // ???
                   18821:                                mouse.status_irq_ps2 = mouse.status & 0x1f;
                   18822:                                mouse.status = 0;
                   18823:                                pic_req(1, 4, 1);
1.1.1.59  root     18824:                        } else if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
1.1.1.43  root     18825:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   18826:                                mouse.status_irq_alt = 0; // ???
1.1.1.54  root     18827:                                mouse.status_irq_ps2 = 0; // ???
1.1.1.24  root     18828:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     18829:                                pic_req(1, 4, 1);
                   18830:                        } else {
                   18831:                                for(int i = 0; i < 8; i++) {
                   18832:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   18833:                                                mouse.status_irq = 0; // ???
                   18834:                                                mouse.status_irq_alt = 0;
1.1.1.54  root     18835:                                                mouse.status_irq_ps2 = 0; // ???
1.1.1.43  root     18836:                                                for(int j = 0; j < 8; j++) {
                   18837:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   18838:                                                                mouse.status_irq_alt |=  (1 << j);
                   18839:                                                                mouse.status_alt     &= ~(1 << j);
                   18840:                                                        }
                   18841:                                                }
                   18842:                                                pic_req(1, 4, 1);
                   18843:                                                break;
                   18844:                                        }
                   18845:                                }
1.1.1.8   root     18846:                        }
1.1.1.24  root     18847:                        
1.1.1.60  root     18848:                        prev_tick = cur_tick;
                   18849:                }
                   18850:                
                   18851:                // update cursor size/position by crtc
                   18852:                if(crtc_changed[10] != 0 || crtc_changed[11] != 0) {
                   18853:                        int size = (int)(crtc_regs[11] & 7) - (int)(crtc_regs[10] & 7) + 1;
                   18854:                        if(!((crtc_regs[10] & 0x20) != 0 || size < 0)) {
                   18855:                                ci_new.bVisible = TRUE;
                   18856:                                ci_new.dwSize = (size + 2) * 100 / (8 + 2);
1.1.1.59  root     18857:                        } else {
1.1.1.60  root     18858:                                ci_new.bVisible = FALSE;
                   18859:                        }
                   18860:                        crtc_changed[10] = crtc_changed[11] = 0;
                   18861:                }
                   18862:                if(crtc_changed[14] != 0 || crtc_changed[15] != 0) {
                   18863:                        if(cursor_moved) {
                   18864:                                pcbios_update_cursor_position();
                   18865:                                cursor_moved = false;
1.1.1.59  root     18866:                        }
1.1.1.60  root     18867:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   18868:                        int position = crtc_regs[14] * 256 + crtc_regs[15];
                   18869:                        int width = *(UINT16 *)(mem + 0x44a);
                   18870:                        COORD co;
                   18871:                        co.X = position % width;
                   18872:                        co.Y = position / width + scr_top;
                   18873:                        SetConsoleCursorPosition(hStdout, co);
1.1.1.59  root     18874:                        
1.1.1.60  root     18875:                        crtc_changed[14] = crtc_changed[15] = 0;
                   18876:                        cursor_moved_by_crtc = true;
                   18877:                }
                   18878:                
                   18879:                // update cursor info
                   18880:                if(!is_cursor_blink_off()) {
                   18881:                        ci_new.bVisible = TRUE;
                   18882:                }
                   18883:                if(!(ci_old.dwSize == ci_new.dwSize && ci_old.bVisible == ci_new.bVisible)) {
                   18884:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   18885:                        SetConsoleCursorInfo(hStdout, &ci_new);
1.1.1.8   root     18886:                }
1.1.1.60  root     18887:                ci_old = ci_new;
1.1.1.24  root     18888:                
1.1.1.19  root     18889:                // update daily timer counter
                   18890:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     18891:                
1.1.1.8   root     18892:                prev_time = cur_time;
1.1       root     18893:        }
                   18894: }
                   18895: 
1.1.1.19  root     18896: // ems
                   18897: 
                   18898: void ems_init()
                   18899: {
                   18900:        memset(ems_handles, 0, sizeof(ems_handles));
                   18901:        memset(ems_pages, 0, sizeof(ems_pages));
                   18902:        free_ems_pages = MAX_EMS_PAGES;
                   18903: }
                   18904: 
                   18905: void ems_finish()
                   18906: {
1.1.1.28  root     18907:        ems_release();
                   18908: }
                   18909: 
                   18910: void ems_release()
                   18911: {
1.1.1.31  root     18912:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     18913:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     18914:                        free(ems_handles[i].buffer);
                   18915:                        ems_handles[i].buffer = NULL;
                   18916:                }
                   18917:        }
                   18918: }
                   18919: 
                   18920: void ems_allocate_pages(int handle, int pages)
                   18921: {
                   18922:        if(pages > 0) {
                   18923:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18924:        } else {
                   18925:                ems_handles[handle].buffer = NULL;
                   18926:        }
                   18927:        ems_handles[handle].pages = pages;
                   18928:        ems_handles[handle].allocated = true;
                   18929:        free_ems_pages -= pages;
                   18930: }
                   18931: 
                   18932: void ems_reallocate_pages(int handle, int pages)
                   18933: {
                   18934:        if(ems_handles[handle].allocated) {
                   18935:                if(ems_handles[handle].pages != pages) {
                   18936:                        UINT8 *new_buffer = NULL;
                   18937:                        
                   18938:                        if(pages > 0) {
                   18939:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18940:                        }
1.1.1.32  root     18941:                        if(ems_handles[handle].buffer != NULL) {
                   18942:                                if(new_buffer != NULL) {
1.1.1.19  root     18943:                                        if(pages > ems_handles[handle].pages) {
                   18944:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   18945:                                        } else {
                   18946:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   18947:                                        }
                   18948:                                }
                   18949:                                free(ems_handles[handle].buffer);
                   18950:                                ems_handles[handle].buffer = NULL;
                   18951:                        }
                   18952:                        free_ems_pages += ems_handles[handle].pages;
                   18953:                        
                   18954:                        ems_handles[handle].buffer = new_buffer;
                   18955:                        ems_handles[handle].pages = pages;
                   18956:                        free_ems_pages -= pages;
                   18957:                }
                   18958:        } else {
                   18959:                ems_allocate_pages(handle, pages);
                   18960:        }
                   18961: }
                   18962: 
                   18963: void ems_release_pages(int handle)
                   18964: {
                   18965:        if(ems_handles[handle].allocated) {
1.1.1.32  root     18966:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     18967:                        free(ems_handles[handle].buffer);
                   18968:                        ems_handles[handle].buffer = NULL;
                   18969:                }
                   18970:                free_ems_pages += ems_handles[handle].pages;
                   18971:                ems_handles[handle].allocated = false;
                   18972:        }
                   18973: }
                   18974: 
                   18975: void ems_map_page(int physical, int handle, int logical)
                   18976: {
                   18977:        if(ems_pages[physical].mapped) {
                   18978:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   18979:                        return;
                   18980:                }
                   18981:                ems_unmap_page(physical);
                   18982:        }
1.1.1.32  root     18983:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18984:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   18985:        }
                   18986:        ems_pages[physical].handle = handle;
                   18987:        ems_pages[physical].page = logical;
                   18988:        ems_pages[physical].mapped = true;
                   18989: }
                   18990: 
                   18991: void ems_unmap_page(int physical)
                   18992: {
                   18993:        if(ems_pages[physical].mapped) {
                   18994:                int handle = ems_pages[physical].handle;
                   18995:                int logical = ems_pages[physical].page;
                   18996:                
1.1.1.32  root     18997:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18998:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   18999:                }
                   19000:                ems_pages[physical].mapped = false;
                   19001:        }
                   19002: }
                   19003: 
1.1.1.25  root     19004: // dma
1.1       root     19005: 
1.1.1.25  root     19006: void dma_init()
1.1       root     19007: {
1.1.1.26  root     19008:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     19009:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     19010: //             for(int ch = 0; ch < 4; ch++) {
                   19011: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   19012: //             }
1.1.1.25  root     19013:                dma_reset(c);
                   19014:        }
1.1       root     19015: }
                   19016: 
1.1.1.25  root     19017: void dma_reset(int c)
1.1       root     19018: {
1.1.1.25  root     19019:        dma[c].low_high = false;
                   19020:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   19021:        dma[c].mask = 0xff;
                   19022: }
                   19023: 
                   19024: void dma_write(int c, UINT32 addr, UINT8 data)
                   19025: {
                   19026:        int ch = (addr >> 1) & 3;
                   19027:        UINT8 bit = 1 << (data & 3);
                   19028:        
                   19029:        switch(addr & 0x0f) {
                   19030:        case 0x00: case 0x02: case 0x04: case 0x06:
                   19031:                if(dma[c].low_high) {
                   19032:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     19033:                } else {
1.1.1.25  root     19034:                        dma[c].ch[ch].bareg.b.l = data;
                   19035:                }
                   19036:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   19037:                dma[c].low_high = !dma[c].low_high;
                   19038:                break;
                   19039:        case 0x01: case 0x03: case 0x05: case 0x07:
                   19040:                if(dma[c].low_high) {
                   19041:                        dma[c].ch[ch].bcreg.b.h = data;
                   19042:                } else {
                   19043:                        dma[c].ch[ch].bcreg.b.l = data;
                   19044:                }
                   19045:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   19046:                dma[c].low_high = !dma[c].low_high;
                   19047:                break;
                   19048:        case 0x08:
                   19049:                // command register
                   19050:                dma[c].cmd = data;
                   19051:                break;
                   19052:        case 0x09:
                   19053:                // dma[c].request register
                   19054:                if(data & 4) {
                   19055:                        if(!(dma[c].req & bit)) {
                   19056:                                dma[c].req |= bit;
                   19057: //                             dma_run(c, ch);
                   19058:                        }
                   19059:                } else {
                   19060:                        dma[c].req &= ~bit;
                   19061:                }
                   19062:                break;
                   19063:        case 0x0a:
                   19064:                // single mask register
                   19065:                if(data & 4) {
                   19066:                        dma[c].mask |= bit;
                   19067:                } else {
                   19068:                        dma[c].mask &= ~bit;
                   19069:                }
                   19070:                break;
                   19071:        case 0x0b:
                   19072:                // mode register
                   19073:                dma[c].ch[data & 3].mode = data;
                   19074:                break;
                   19075:        case 0x0c:
                   19076:                dma[c].low_high = false;
                   19077:                break;
                   19078:        case 0x0d:
                   19079:                // clear master
                   19080:                dma_reset(c);
                   19081:                break;
                   19082:        case 0x0e:
                   19083:                // clear mask register
                   19084:                dma[c].mask = 0;
                   19085:                break;
                   19086:        case 0x0f:
                   19087:                // all mask register
                   19088:                dma[c].mask = data & 0x0f;
                   19089:                break;
                   19090:        }
                   19091: }
                   19092: 
                   19093: UINT8 dma_read(int c, UINT32 addr)
                   19094: {
                   19095:        int ch = (addr >> 1) & 3;
                   19096:        UINT8 val = 0xff;
                   19097:        
                   19098:        switch(addr & 0x0f) {
                   19099:        case 0x00: case 0x02: case 0x04: case 0x06:
                   19100:                if(dma[c].low_high) {
                   19101:                        val = dma[c].ch[ch].areg.b.h;
                   19102:                } else {
                   19103:                        val = dma[c].ch[ch].areg.b.l;
                   19104:                }
                   19105:                dma[c].low_high = !dma[c].low_high;
                   19106:                return(val);
                   19107:        case 0x01: case 0x03: case 0x05: case 0x07:
                   19108:                if(dma[c].low_high) {
                   19109:                        val = dma[c].ch[ch].creg.b.h;
                   19110:                } else {
                   19111:                        val = dma[c].ch[ch].creg.b.l;
                   19112:                }
                   19113:                dma[c].low_high = !dma[c].low_high;
                   19114:                return(val);
                   19115:        case 0x08:
                   19116:                // status register
                   19117:                val = (dma[c].req << 4) | dma[c].tc;
                   19118:                dma[c].tc = 0;
                   19119:                return(val);
                   19120:        case 0x0d:
1.1.1.26  root     19121:                // temporary register (intel 82374 does not support)
1.1.1.25  root     19122:                return(dma[c].tmp & 0xff);
1.1.1.26  root     19123:        case 0x0f:
                   19124:                // mask register (intel 82374 does support)
                   19125:                return(dma[c].mask);
1.1.1.25  root     19126:        }
                   19127:        return(0xff);
                   19128: }
                   19129: 
                   19130: void dma_page_write(int c, int ch, UINT8 data)
                   19131: {
                   19132:        dma[c].ch[ch].pagereg = data;
                   19133: }
                   19134: 
                   19135: UINT8 dma_page_read(int c, int ch)
                   19136: {
                   19137:        return(dma[c].ch[ch].pagereg);
                   19138: }
                   19139: 
                   19140: void dma_run(int c, int ch)
                   19141: {
                   19142:        UINT8 bit = 1 << ch;
                   19143:        
                   19144:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   19145:                // execute dma
                   19146:                while(dma[c].req & bit) {
                   19147:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   19148:                                // memory -> memory
                   19149:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   19150:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   19151:                                
                   19152:                                if(c == 0) {
                   19153:                                        dma[c].tmp = read_byte(saddr);
                   19154:                                        write_byte(daddr, dma[c].tmp);
                   19155:                                } else {
                   19156:                                        dma[c].tmp = read_word(saddr << 1);
                   19157:                                        write_word(daddr << 1, dma[c].tmp);
                   19158:                                }
                   19159:                                if(!(dma[c].cmd & 0x02)) {
                   19160:                                        if(dma[c].ch[0].mode & 0x20) {
                   19161:                                                dma[c].ch[0].areg.w--;
                   19162:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   19163:                                                        dma[c].ch[0].pagereg--;
                   19164:                                                }
                   19165:                                        } else {
                   19166:                                                dma[c].ch[0].areg.w++;
                   19167:                                                if(dma[c].ch[0].areg.w == 0) {
                   19168:                                                        dma[c].ch[0].pagereg++;
                   19169:                                                }
                   19170:                                        }
                   19171:                                }
                   19172:                                if(dma[c].ch[1].mode & 0x20) {
                   19173:                                        dma[c].ch[1].areg.w--;
                   19174:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   19175:                                                dma[c].ch[1].pagereg--;
                   19176:                                        }
                   19177:                                } else {
                   19178:                                        dma[c].ch[1].areg.w++;
                   19179:                                        if(dma[c].ch[1].areg.w == 0) {
                   19180:                                                dma[c].ch[1].pagereg++;
                   19181:                                        }
                   19182:                                }
                   19183:                                
                   19184:                                // check dma condition
                   19185:                                if(dma[c].ch[0].creg.w-- == 0) {
                   19186:                                        if(dma[c].ch[0].mode & 0x10) {
                   19187:                                                // self initialize
                   19188:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   19189:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   19190:                                        } else {
                   19191: //                                             dma[c].mask |= bit;
                   19192:                                        }
                   19193:                                }
                   19194:                                if(dma[c].ch[1].creg.w-- == 0) {
                   19195:                                        // terminal count
                   19196:                                        if(dma[c].ch[1].mode & 0x10) {
                   19197:                                                // self initialize
                   19198:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   19199:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   19200:                                        } else {
                   19201:                                                dma[c].mask |= bit;
                   19202:                                        }
                   19203:                                        dma[c].req &= ~bit;
                   19204:                                        dma[c].tc |= bit;
                   19205:                                }
                   19206:                        } else {
                   19207:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   19208:                                
                   19209:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   19210:                                        // verify
                   19211:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   19212:                                        // io -> memory
                   19213:                                        if(c == 0) {
                   19214:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   19215:                                                write_byte(addr, dma[c].tmp);
                   19216:                                        } else {
                   19217:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   19218:                                                write_word(addr << 1, dma[c].tmp);
                   19219:                                        }
                   19220:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   19221:                                        // memory -> io
                   19222:                                        if(c == 0) {
                   19223:                                                dma[c].tmp = read_byte(addr);
                   19224:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   19225:                                        } else {
                   19226:                                                dma[c].tmp = read_word(addr << 1);
                   19227:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   19228:                                        }
                   19229:                                }
                   19230:                                if(dma[c].ch[ch].mode & 0x20) {
                   19231:                                        dma[c].ch[ch].areg.w--;
                   19232:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   19233:                                                dma[c].ch[ch].pagereg--;
                   19234:                                        }
                   19235:                                } else {
                   19236:                                        dma[c].ch[ch].areg.w++;
                   19237:                                        if(dma[c].ch[ch].areg.w == 0) {
                   19238:                                                dma[c].ch[ch].pagereg++;
                   19239:                                        }
                   19240:                                }
                   19241:                                
                   19242:                                // check dma condition
                   19243:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   19244:                                        // terminal count
                   19245:                                        if(dma[c].ch[ch].mode & 0x10) {
                   19246:                                                // self initialize
                   19247:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   19248:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   19249:                                        } else {
                   19250:                                                dma[c].mask |= bit;
                   19251:                                        }
                   19252:                                        dma[c].req &= ~bit;
                   19253:                                        dma[c].tc |= bit;
                   19254:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   19255:                                        // single mode
                   19256:                                        break;
                   19257:                                }
                   19258:                        }
                   19259:                }
                   19260:        }
                   19261: }
                   19262: 
                   19263: // pic
                   19264: 
                   19265: void pic_init()
                   19266: {
                   19267:        memset(pic, 0, sizeof(pic));
                   19268:        pic[0].imr = pic[1].imr = 0xff;
                   19269:        
                   19270:        // from bochs bios
                   19271:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   19272:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   19273:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   19274:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   19275:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   19276:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   19277:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   19278:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   19279:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   19280: }
                   19281: 
                   19282: void pic_write(int c, UINT32 addr, UINT8 data)
                   19283: {
                   19284:        if(addr & 1) {
                   19285:                if(pic[c].icw2_r) {
                   19286:                        // icw2
                   19287:                        pic[c].icw2 = data;
                   19288:                        pic[c].icw2_r = 0;
                   19289:                } else if(pic[c].icw3_r) {
                   19290:                        // icw3
                   19291:                        pic[c].icw3 = data;
                   19292:                        pic[c].icw3_r = 0;
                   19293:                } else if(pic[c].icw4_r) {
                   19294:                        // icw4
                   19295:                        pic[c].icw4 = data;
                   19296:                        pic[c].icw4_r = 0;
                   19297:                } else {
                   19298:                        // ocw1
1.1       root     19299:                        pic[c].imr = data;
                   19300:                }
                   19301:        } else {
                   19302:                if(data & 0x10) {
                   19303:                        // icw1
                   19304:                        pic[c].icw1 = data;
                   19305:                        pic[c].icw2_r = 1;
                   19306:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   19307:                        pic[c].icw4_r = data & 1;
                   19308:                        pic[c].irr = 0;
                   19309:                        pic[c].isr = 0;
                   19310:                        pic[c].imr = 0;
                   19311:                        pic[c].prio = 0;
                   19312:                        if(!(pic[c].icw1 & 1)) {
                   19313:                                pic[c].icw4 = 0;
                   19314:                        }
                   19315:                        pic[c].ocw3 = 0;
                   19316:                } else if(data & 8) {
                   19317:                        // ocw3
                   19318:                        if(!(data & 2)) {
                   19319:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   19320:                        }
                   19321:                        if(!(data & 0x40)) {
                   19322:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   19323:                        }
                   19324:                        pic[c].ocw3 = data;
                   19325:                } else {
                   19326:                        // ocw2
                   19327:                        int level = 0;
                   19328:                        if(data & 0x40) {
                   19329:                                level = data & 7;
                   19330:                        } else {
                   19331:                                if(!pic[c].isr) {
                   19332:                                        return;
                   19333:                                }
                   19334:                                level = pic[c].prio;
                   19335:                                while(!(pic[c].isr & (1 << level))) {
                   19336:                                        level = (level + 1) & 7;
                   19337:                                }
                   19338:                        }
                   19339:                        if(data & 0x80) {
                   19340:                                pic[c].prio = (level + 1) & 7;
                   19341:                        }
                   19342:                        if(data & 0x20) {
                   19343:                                pic[c].isr &= ~(1 << level);
                   19344:                        }
                   19345:                }
                   19346:        }
                   19347:        pic_update();
                   19348: }
                   19349: 
                   19350: UINT8 pic_read(int c, UINT32 addr)
                   19351: {
                   19352:        if(addr & 1) {
                   19353:                return(pic[c].imr);
                   19354:        } else {
                   19355:                // polling mode is not supported...
                   19356:                //if(pic[c].ocw3 & 4) {
                   19357:                //      return ???;
                   19358:                //}
                   19359:                if(pic[c].ocw3 & 1) {
                   19360:                        return(pic[c].isr);
                   19361:                } else {
                   19362:                        return(pic[c].irr);
                   19363:                }
                   19364:        }
                   19365: }
                   19366: 
                   19367: void pic_req(int c, int level, int signal)
                   19368: {
                   19369:        if(signal) {
                   19370:                pic[c].irr |= (1 << level);
                   19371:        } else {
                   19372:                pic[c].irr &= ~(1 << level);
                   19373:        }
                   19374:        pic_update();
                   19375: }
                   19376: 
                   19377: int pic_ack()
                   19378: {
                   19379:        // ack (INTA=L)
                   19380:        pic[pic_req_chip].isr |= pic_req_bit;
                   19381:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   19382:        if(pic_req_chip > 0) {
                   19383:                // update isr and irr of master
                   19384:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   19385:                pic[pic_req_chip - 1].isr |= slave;
                   19386:                pic[pic_req_chip - 1].irr &= ~slave;
                   19387:        }
                   19388:        //if(pic[pic_req_chip].icw4 & 1) {
                   19389:                // 8086 mode
                   19390:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   19391:        //} else {
                   19392:        //      // 8080 mode
                   19393:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   19394:        //      if(pic[pic_req_chip].icw1 & 4) {
                   19395:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   19396:        //      } else {
                   19397:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   19398:        //      }
                   19399:        //      vector = 0xcd | (addr << 8);
                   19400:        //}
                   19401:        if(pic[pic_req_chip].icw4 & 2) {
                   19402:                // auto eoi
                   19403:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   19404:        }
                   19405:        return(vector);
                   19406: }
                   19407: 
                   19408: void pic_update()
                   19409: {
                   19410:        for(int c = 0; c < 2; c++) {
                   19411:                UINT8 irr = pic[c].irr;
                   19412:                if(c + 1 < 2) {
                   19413:                        // this is master
                   19414:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   19415:                                // request from slave
                   19416:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   19417:                        }
                   19418:                }
                   19419:                irr &= (~pic[c].imr);
                   19420:                if(!irr) {
                   19421:                        break;
                   19422:                }
                   19423:                if(!(pic[c].ocw3 & 0x20)) {
                   19424:                        irr |= pic[c].isr;
                   19425:                }
                   19426:                int level = pic[c].prio;
                   19427:                UINT8 bit = 1 << level;
                   19428:                while(!(irr & bit)) {
                   19429:                        level = (level + 1) & 7;
                   19430:                        bit = 1 << level;
                   19431:                }
                   19432:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   19433:                        // check slave
                   19434:                        continue;
                   19435:                }
                   19436:                if(pic[c].isr & bit) {
                   19437:                        break;
                   19438:                }
                   19439:                // interrupt request
                   19440:                pic_req_chip = c;
                   19441:                pic_req_level = level;
                   19442:                pic_req_bit = bit;
1.1.1.3   root     19443:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     19444:                return;
                   19445:        }
1.1.1.3   root     19446:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     19447: }
1.1       root     19448: 
1.1.1.25  root     19449: // pio
                   19450: 
                   19451: void pio_init()
                   19452: {
1.1.1.38  root     19453: //     bool conv_mode = (GetConsoleCP() == 932);
                   19454:        
1.1.1.26  root     19455:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     19456:        
1.1.1.25  root     19457:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     19458:                pio[c].stat = 0xdf;
1.1.1.25  root     19459:                pio[c].ctrl = 0x0c;
1.1.1.38  root     19460: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     19461:        }
                   19462: }
                   19463: 
1.1.1.37  root     19464: void pio_finish()
                   19465: {
                   19466:        pio_release();
                   19467: }
                   19468: 
                   19469: void pio_release()
                   19470: {
                   19471:        for(int c = 0; c < 2; c++) {
                   19472:                if(pio[c].fp != NULL) {
1.1.1.38  root     19473:                        if(pio[c].jis_mode) {
                   19474:                                fputc(0x1c, pio[c].fp);
                   19475:                                fputc(0x2e, pio[c].fp);
                   19476:                        }
1.1.1.37  root     19477:                        fclose(pio[c].fp);
                   19478:                        pio[c].fp = NULL;
                   19479:                }
                   19480:        }
                   19481: }
                   19482: 
1.1.1.25  root     19483: void pio_write(int c, UINT32 addr, UINT8 data)
                   19484: {
                   19485:        switch(addr & 3) {
                   19486:        case 0:
                   19487:                pio[c].data = data;
                   19488:                break;
                   19489:        case 2:
1.1.1.37  root     19490:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   19491:                        // strobe H -> L
                   19492:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   19493:                                // auto feed
                   19494:                                printer_out(c, 0x0d);
                   19495:                                printer_out(c, 0x0a);
                   19496:                        } else {
                   19497:                                printer_out(c, pio[c].data);
                   19498:                        }
                   19499:                        pio[c].stat &= ~0x40; // set ack
                   19500:                }
1.1.1.25  root     19501:                pio[c].ctrl = data;
                   19502:                break;
                   19503:        }
                   19504: }
                   19505: 
                   19506: UINT8 pio_read(int c, UINT32 addr)
                   19507: {
                   19508:        switch(addr & 3) {
                   19509:        case 0:
1.1.1.37  root     19510:                if(pio[c].ctrl & 0x20) {
                   19511:                        // input mode
                   19512:                        return(0xff);
                   19513:                }
1.1.1.25  root     19514:                return(pio[c].data);
                   19515:        case 1:
1.1.1.37  root     19516:                {
                   19517:                        UINT8 stat = pio[c].stat;
                   19518:                        pio[c].stat |= 0x40; // clear ack
                   19519:                        return(stat);
                   19520:                }
1.1.1.25  root     19521:        case 2:
                   19522:                return(pio[c].ctrl);
                   19523:        }
                   19524:        return(0xff);
                   19525: }
                   19526: 
1.1.1.37  root     19527: void printer_out(int c, UINT8 data)
                   19528: {
                   19529:        SYSTEMTIME time;
1.1.1.38  root     19530:        bool jis_mode = false;
1.1.1.37  root     19531:        
                   19532:        GetLocalTime(&time);
                   19533:        
                   19534:        if(pio[c].fp != NULL) {
                   19535:                // if at least 1000ms passed from last written, close the current file
                   19536:                FILETIME ftime1;
                   19537:                FILETIME ftime2;
                   19538:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   19539:                SystemTimeToFileTime(&time, &ftime2);
                   19540:                INT64 *time1 = (INT64 *)&ftime1;
                   19541:                INT64 *time2 = (INT64 *)&ftime2;
                   19542:                INT64 msec = (*time2 - *time1) / 10000;
                   19543:                
                   19544:                if(msec >= 1000) {
1.1.1.38  root     19545:                        if(pio[c].jis_mode) {
                   19546:                                fputc(0x1c, pio[c].fp);
                   19547:                                fputc(0x2e, pio[c].fp);
                   19548:                                jis_mode = true;
                   19549:                        }
1.1.1.37  root     19550:                        fclose(pio[c].fp);
                   19551:                        pio[c].fp = NULL;
                   19552:                }
                   19553:        }
                   19554:        if(pio[c].fp == NULL) {
                   19555:                // create a new file in the temp folder
                   19556:                char file_name[MAX_PATH];
                   19557:                
                   19558:                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     19559:                if(GetTempPathA(MAX_PATH, pio[c].path)) {
1.1.1.37  root     19560:                        strcat(pio[c].path, file_name);
                   19561:                } else {
                   19562:                        strcpy(pio[c].path, file_name);
                   19563:                }
1.1.1.38  root     19564:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     19565:        }
                   19566:        if(pio[c].fp != NULL) {
1.1.1.38  root     19567:                if(jis_mode) {
                   19568:                        fputc(0x1c, pio[c].fp);
                   19569:                        fputc(0x26, pio[c].fp);
                   19570:                }
1.1.1.37  root     19571:                fputc(data, pio[c].fp);
1.1.1.38  root     19572:                
                   19573:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   19574:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   19575:                        UINT8 buffer[4];
                   19576:                        fseek(pio[c].fp, 0, SEEK_SET);
                   19577:                        fread(buffer, 4, 1, pio[c].fp);
                   19578:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   19579:                                fclose(pio[c].fp);
                   19580:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   19581:                        }
                   19582:                }
1.1.1.37  root     19583:                pio[c].time = time;
                   19584:        }
                   19585: }
                   19586: 
1.1       root     19587: // pit
                   19588: 
1.1.1.22  root     19589: #define PIT_FREQ 1193182ULL
1.1       root     19590: #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)
                   19591: 
                   19592: void pit_init()
                   19593: {
1.1.1.8   root     19594:        memset(pit, 0, sizeof(pit));
1.1       root     19595:        for(int ch = 0; ch < 3; ch++) {
                   19596:                pit[ch].count = 0x10000;
                   19597:                pit[ch].ctrl_reg = 0x34;
                   19598:                pit[ch].mode = 3;
                   19599:        }
                   19600:        
                   19601:        // from bochs bios
                   19602:        pit_write(3, 0x34);
                   19603:        pit_write(0, 0x00);
                   19604:        pit_write(0, 0x00);
                   19605: }
                   19606: 
                   19607: void pit_write(int ch, UINT8 val)
                   19608: {
1.1.1.8   root     19609: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19610:        if(!pit_active) {
                   19611:                pit_active = 1;
                   19612:                pit_init();
                   19613:        }
1.1.1.8   root     19614: #endif
1.1       root     19615:        switch(ch) {
                   19616:        case 0:
                   19617:        case 1:
                   19618:        case 2:
                   19619:                // write count register
                   19620:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19621:                        if(pit[ch].ctrl_reg & 0x10) {
                   19622:                                pit[ch].low_write = 1;
                   19623:                        }
                   19624:                        if(pit[ch].ctrl_reg & 0x20) {
                   19625:                                pit[ch].high_write = 1;
                   19626:                        }
                   19627:                }
                   19628:                if(pit[ch].low_write) {
                   19629:                        pit[ch].count_reg = val;
                   19630:                        pit[ch].low_write = 0;
                   19631:                } else if(pit[ch].high_write) {
                   19632:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19633:                                pit[ch].count_reg = val << 8;
                   19634:                        } else {
                   19635:                                pit[ch].count_reg |= val << 8;
                   19636:                        }
                   19637:                        pit[ch].high_write = 0;
                   19638:                }
                   19639:                // start count
1.1.1.8   root     19640:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19641:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   19642:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   19643:                                pit[ch].prev_time = timeGetTime();
                   19644:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19645:                        }
                   19646:                }
                   19647:                break;
                   19648:        case 3: // ctrl reg
                   19649:                if((val & 0xc0) == 0xc0) {
                   19650:                        // i8254 read-back command
                   19651:                        for(ch = 0; ch < 3; ch++) {
                   19652:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   19653:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   19654:                                        pit[ch].status_latched = 1;
                   19655:                                }
                   19656:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   19657:                                        pit_latch_count(ch);
                   19658:                                }
                   19659:                        }
                   19660:                        break;
                   19661:                }
                   19662:                ch = (val >> 6) & 3;
                   19663:                if(val & 0x30) {
1.1.1.35  root     19664:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     19665:                        pit[ch].mode = modes[(val >> 1) & 7];
                   19666:                        pit[ch].count_latched = 0;
                   19667:                        pit[ch].low_read = pit[ch].high_read = 0;
                   19668:                        pit[ch].low_write = pit[ch].high_write = 0;
                   19669:                        pit[ch].ctrl_reg = val;
                   19670:                        // stop count
1.1.1.8   root     19671:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     19672:                        pit[ch].count_reg = 0;
                   19673:                } else if(!pit[ch].count_latched) {
                   19674:                        pit_latch_count(ch);
                   19675:                }
                   19676:                break;
                   19677:        }
                   19678: }
                   19679: 
                   19680: UINT8 pit_read(int ch)
                   19681: {
1.1.1.8   root     19682: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19683:        if(!pit_active) {
                   19684:                pit_active = 1;
                   19685:                pit_init();
                   19686:        }
1.1.1.8   root     19687: #endif
1.1       root     19688:        switch(ch) {
                   19689:        case 0:
                   19690:        case 1:
                   19691:        case 2:
                   19692:                if(pit[ch].status_latched) {
                   19693:                        pit[ch].status_latched = 0;
                   19694:                        return(pit[ch].status);
                   19695:                }
                   19696:                // if not latched, through current count
                   19697:                if(!pit[ch].count_latched) {
                   19698:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   19699:                                pit_latch_count(ch);
                   19700:                        }
                   19701:                }
                   19702:                // return latched count
                   19703:                if(pit[ch].low_read) {
                   19704:                        pit[ch].low_read = 0;
                   19705:                        if(!pit[ch].high_read) {
                   19706:                                pit[ch].count_latched = 0;
                   19707:                        }
                   19708:                        return(pit[ch].latch & 0xff);
                   19709:                } else if(pit[ch].high_read) {
                   19710:                        pit[ch].high_read = 0;
                   19711:                        pit[ch].count_latched = 0;
                   19712:                        return((pit[ch].latch >> 8) & 0xff);
                   19713:                }
                   19714:        }
                   19715:        return(0xff);
                   19716: }
                   19717: 
1.1.1.8   root     19718: int pit_run(int ch, UINT32 cur_time)
1.1       root     19719: {
1.1.1.8   root     19720:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     19721:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     19722:                pit[ch].prev_time = pit[ch].expired_time;
                   19723:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   19724:                if(cur_time >= pit[ch].expired_time) {
                   19725:                        pit[ch].prev_time = cur_time;
                   19726:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19727:                }
1.1.1.8   root     19728:                return(1);
1.1       root     19729:        }
1.1.1.8   root     19730:        return(0);
1.1       root     19731: }
                   19732: 
                   19733: void pit_latch_count(int ch)
                   19734: {
1.1.1.8   root     19735:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     19736:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     19737:                pit_run(ch, cur_time);
                   19738:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     19739:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19740:                
                   19741:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   19742:                        // decrement counter in 1msec period
                   19743:                        if(pit[ch].next_latch == 0) {
                   19744:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   19745:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19746:                        }
                   19747:                        if(pit[ch].latch > pit[ch].next_latch) {
                   19748:                                pit[ch].latch--;
                   19749:                        }
                   19750:                } else {
                   19751:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   19752:                        pit[ch].next_latch = 0;
                   19753:                }
1.1.1.8   root     19754:        } else {
                   19755:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     19756:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     19757:        }
                   19758:        pit[ch].count_latched = 1;
                   19759:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   19760:                // lower byte
                   19761:                pit[ch].low_read = 1;
                   19762:                pit[ch].high_read = 0;
                   19763:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19764:                // upper byte
                   19765:                pit[ch].low_read = 0;
                   19766:                pit[ch].high_read = 1;
                   19767:        } else {
                   19768:                // lower -> upper
1.1.1.14  root     19769:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     19770:        }
                   19771: }
                   19772: 
1.1.1.8   root     19773: int pit_get_expired_time(int ch)
1.1       root     19774: {
1.1.1.22  root     19775:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   19776:        UINT64 val = pit[ch].accum >> 10;
                   19777:        pit[ch].accum -= val << 10;
                   19778:        return((val != 0) ? val : 1);
1.1.1.8   root     19779: }
                   19780: 
1.1.1.25  root     19781: // sio
                   19782: 
                   19783: void sio_init()
                   19784: {
1.1.1.26  root     19785:        memset(sio, 0, sizeof(sio));
                   19786:        memset(sio_mt, 0, sizeof(sio_mt));
                   19787:        
1.1.1.29  root     19788:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19789:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19790:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19791:                
                   19792:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   19793:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     19794:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   19795:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     19796:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   19797:                sio[c].irq_identify = 0x01;     // no pending irq
                   19798:                
                   19799:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   19800:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   19801:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   19802:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   19803:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   19804:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   19805:                
1.1.1.26  root     19806:                if(sio_port_number[c] != 0) {
1.1.1.25  root     19807:                        sio[c].channel = c;
                   19808:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   19809:                }
                   19810:        }
                   19811: }
                   19812: 
                   19813: void sio_finish()
                   19814: {
1.1.1.29  root     19815:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19816:                if(sio_mt[c].hThread != NULL) {
                   19817:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   19818:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     19819:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     19820:                }
                   19821:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   19822:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   19823:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   19824:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   19825:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   19826:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     19827:        }
                   19828:        sio_release();
                   19829: }
                   19830: 
                   19831: void sio_release()
                   19832: {
1.1.1.29  root     19833:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     19834:                // sio_thread() may access the resources :-(
1.1.1.32  root     19835:                bool running = (sio_mt[c].hThread != NULL);
                   19836:                
                   19837:                if(running) {
                   19838:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   19839:                }
                   19840:                if(sio[c].send_buffer != NULL) {
                   19841:                        sio[c].send_buffer->release();
                   19842:                        delete sio[c].send_buffer;
                   19843:                        sio[c].send_buffer = NULL;
                   19844:                }
                   19845:                if(running) {
                   19846:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19847:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   19848:                }
                   19849:                if(sio[c].recv_buffer != NULL) {
                   19850:                        sio[c].recv_buffer->release();
                   19851:                        delete sio[c].recv_buffer;
                   19852:                        sio[c].recv_buffer = NULL;
                   19853:                }
                   19854:                if(running) {
                   19855:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     19856:                }
1.1.1.25  root     19857:        }
                   19858: }
                   19859: 
                   19860: void sio_write(int c, UINT32 addr, UINT8 data)
                   19861: {
                   19862:        switch(addr & 7) {
                   19863:        case 0:
                   19864:                if(sio[c].selector & 0x80) {
                   19865:                        if(sio[c].divisor.b.l != data) {
                   19866:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19867:                                sio[c].divisor.b.l = data;
                   19868:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19869:                        }
                   19870:                } else {
                   19871:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19872:                        if(sio[c].send_buffer != NULL) {
                   19873:                                sio[c].send_buffer->write(data);
                   19874:                        }
1.1.1.25  root     19875:                        // transmitter holding/shift registers are not empty
                   19876:                        sio[c].line_stat_buf &= ~0x60;
                   19877:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19878:                        
                   19879:                        if(sio[c].irq_enable & 0x02) {
                   19880:                                sio_update_irq(c);
                   19881:                        }
                   19882:                }
                   19883:                break;
                   19884:        case 1:
                   19885:                if(sio[c].selector & 0x80) {
                   19886:                        if(sio[c].divisor.b.h != data) {
                   19887:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19888:                                sio[c].divisor.b.h = data;
                   19889:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19890:                        }
                   19891:                } else {
                   19892:                        if(sio[c].irq_enable != data) {
                   19893:                                sio[c].irq_enable = data;
                   19894:                                sio_update_irq(c);
                   19895:                        }
                   19896:                }
                   19897:                break;
                   19898:        case 3:
                   19899:                {
                   19900:                        UINT8 line_ctrl = data & 0x3f;
                   19901:                        bool set_brk = ((data & 0x40) != 0);
                   19902:                        
                   19903:                        if(sio[c].line_ctrl != line_ctrl) {
                   19904:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19905:                                sio[c].line_ctrl = line_ctrl;
                   19906:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19907:                        }
                   19908:                        if(sio[c].set_brk != set_brk) {
                   19909:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   19910:                                sio[c].set_brk = set_brk;
                   19911:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19912:                        }
                   19913:                }
                   19914:                sio[c].selector = data;
                   19915:                break;
                   19916:        case 4:
                   19917:                {
                   19918:                        bool set_dtr = ((data & 0x01) != 0);
                   19919:                        bool set_rts = ((data & 0x02) != 0);
                   19920:                        
                   19921:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     19922: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     19923:                                sio[c].set_dtr = set_dtr;
                   19924:                                sio[c].set_rts = set_rts;
1.1.1.26  root     19925: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19926:                                
                   19927:                                bool state_changed = false;
                   19928:                                
                   19929:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19930:                                if(set_dtr) {
                   19931:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   19932:                                } else {
                   19933:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   19934:                                }
                   19935:                                if(set_rts) {
                   19936:                                        sio[c].modem_stat |= 0x10;      // cts on
                   19937:                                } else {
                   19938:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   19939:                                }
                   19940:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   19941:                                        if(!(sio[c].modem_stat & 0x02)) {
                   19942:                                                if(sio[c].irq_enable & 0x08) {
                   19943:                                                        state_changed = true;
                   19944:                                                }
                   19945:                                                sio[c].modem_stat |= 0x02;
                   19946:                                        }
                   19947:                                }
                   19948:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   19949:                                        if(!(sio[c].modem_stat & 0x01)) {
                   19950:                                                if(sio[c].irq_enable & 0x08) {
                   19951:                                                        state_changed = true;
                   19952:                                                }
                   19953:                                                sio[c].modem_stat |= 0x01;
                   19954:                                        }
                   19955:                                }
                   19956:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19957:                                
                   19958:                                if(state_changed) {
                   19959:                                        sio_update_irq(c);
                   19960:                                }
1.1.1.25  root     19961:                        }
                   19962:                }
                   19963:                sio[c].modem_ctrl = data;
                   19964:                break;
                   19965:        case 7:
                   19966:                sio[c].scratch = data;
                   19967:                break;
                   19968:        }
                   19969: }
                   19970: 
                   19971: UINT8 sio_read(int c, UINT32 addr)
                   19972: {
                   19973:        switch(addr & 7) {
                   19974:        case 0:
                   19975:                if(sio[c].selector & 0x80) {
                   19976:                        return(sio[c].divisor.b.l);
                   19977:                } else {
                   19978:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19979:                        UINT8 data = 0;
                   19980:                        if(sio[c].recv_buffer != NULL) {
                   19981:                                data = sio[c].recv_buffer->read();
                   19982:                        }
1.1.1.25  root     19983:                        // data is not ready
                   19984:                        sio[c].line_stat_buf &= ~0x01;
                   19985:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19986:                        
                   19987:                        if(sio[c].irq_enable & 0x01) {
                   19988:                                sio_update_irq(c);
                   19989:                        }
                   19990:                        return(data);
                   19991:                }
                   19992:        case 1:
                   19993:                if(sio[c].selector & 0x80) {
                   19994:                        return(sio[c].divisor.b.h);
                   19995:                } else {
                   19996:                        return(sio[c].irq_enable);
                   19997:                }
                   19998:        case 2:
                   19999:                return(sio[c].irq_identify);
                   20000:        case 3:
                   20001:                return(sio[c].selector);
                   20002:        case 4:
                   20003:                return(sio[c].modem_ctrl);
                   20004:        case 5:
                   20005:                {
                   20006:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   20007:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   20008:                        sio[c].line_stat_err = 0x00;
                   20009:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   20010:                        
                   20011:                        bool state_changed = false;
                   20012:                        
                   20013:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   20014:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     20015:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     20016:                                        // transmitter holding register will be empty first
                   20017:                                        if(sio[c].irq_enable & 0x02) {
                   20018:                                                state_changed = true;
                   20019:                                        }
                   20020:                                        sio[c].line_stat_buf |= 0x20;
                   20021:                                }
                   20022:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   20023:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   20024:                                // transmitter shift register will be empty later
                   20025:                                sio[c].line_stat_buf |= 0x40;
                   20026:                        }
                   20027:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   20028:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     20029:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     20030:                                        // data is ready
                   20031:                                        if(sio[c].irq_enable & 0x01) {
                   20032:                                                state_changed = true;
                   20033:                                        }
                   20034:                                        sio[c].line_stat_buf |= 0x01;
                   20035:                                }
                   20036:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   20037:                        }
                   20038:                        if(state_changed) {
                   20039:                                sio_update_irq(c);
                   20040:                        }
                   20041:                        return(val);
                   20042:                }
                   20043:        case 6:
                   20044:                {
                   20045:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   20046:                        UINT8 val = sio[c].modem_stat;
                   20047:                        sio[c].modem_stat &= 0xf0;
                   20048:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   20049:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   20050:                        
                   20051:                        if(sio[c].modem_ctrl & 0x10) {
                   20052:                                // loop-back
                   20053:                                val &= 0x0f;
                   20054:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   20055:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   20056:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   20057:                        }
                   20058:                        return(val);
                   20059:                }
                   20060:        case 7:
                   20061:                return(sio[c].scratch);
                   20062:        }
                   20063:        return(0xff);
                   20064: }
                   20065: 
                   20066: void sio_update(int c)
                   20067: {
                   20068:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   20069:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     20070:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     20071:                        // transmitter holding/shift registers will be empty
                   20072:                        sio[c].line_stat_buf |= 0x60;
                   20073:                }
                   20074:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   20075:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   20076:                // transmitter shift register will be empty
                   20077:                sio[c].line_stat_buf |= 0x40;
                   20078:        }
                   20079:        if(!(sio[c].line_stat_buf & 0x01)) {
                   20080:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     20081:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     20082:                        // data is ready
                   20083:                        sio[c].line_stat_buf |= 0x01;
                   20084:                }
                   20085:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   20086:        }
                   20087:        sio_update_irq(c);
                   20088: }
                   20089: 
                   20090: void sio_update_irq(int c)
                   20091: {
                   20092:        int level = -1;
                   20093:        
                   20094:        if(sio[c].irq_enable & 0x08) {
                   20095:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   20096:                if((sio[c].modem_stat & 0x0f) != 0) {
                   20097:                        level = 0;
                   20098:                }
                   20099:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   20100:        }
                   20101:        if(sio[c].irq_enable & 0x02) {
                   20102:                if(sio[c].line_stat_buf & 0x20) {
                   20103:                        level = 1;
                   20104:                }
                   20105:        }
                   20106:        if(sio[c].irq_enable & 0x01) {
                   20107:                if(sio[c].line_stat_buf & 0x01) {
                   20108:                        level = 2;
                   20109:                }
                   20110:        }
                   20111:        if(sio[c].irq_enable & 0x04) {
                   20112:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   20113:                if(sio[c].line_stat_err != 0) {
                   20114:                        level = 3;
                   20115:                }
                   20116:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   20117:        }
1.1.1.29  root     20118:        
                   20119:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     20120:        if(level != -1) {
                   20121:                sio[c].irq_identify = level << 1;
1.1.1.29  root     20122:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     20123:        } else {
                   20124:                sio[c].irq_identify = 1;
1.1.1.29  root     20125:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     20126:        }
                   20127: }
                   20128: 
                   20129: DWORD WINAPI sio_thread(void *lpx)
                   20130: {
                   20131:        volatile sio_t *p = (sio_t *)lpx;
                   20132:        sio_mt_t *q = &sio_mt[p->channel];
                   20133:        
                   20134:        char name[] = "COM1";
1.1.1.26  root     20135:        name[3] = '0' + sio_port_number[p->channel];
                   20136:        HANDLE hComm = NULL;
                   20137:        COMMPROP commProp;
                   20138:        DCB dcb;
                   20139:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   20140:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   20141:        
1.1.1.60  root     20142:        if((hComm = CreateFileA(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
1.1.1.26  root     20143:                if(GetCommProperties(hComm, &commProp)) {
                   20144:                        dwSettableBaud = commProp.dwSettableBaud;
                   20145:                }
1.1.1.25  root     20146:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     20147: //             EscapeCommFunction(hComm, SETRTS);
                   20148: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     20149:                
1.1.1.54  root     20150:                while(!m_exit) {
1.1.1.25  root     20151:                        // setup comm port
                   20152:                        bool comm_state_changed = false;
                   20153:                        
                   20154:                        EnterCriticalSection(&q->csLineCtrl);
                   20155:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   20156:                                p->prev_divisor = p->divisor.w;
                   20157:                                p->prev_line_ctrl = p->line_ctrl;
                   20158:                                comm_state_changed = true;
                   20159:                        }
                   20160:                        LeaveCriticalSection(&q->csLineCtrl);
                   20161:                        
                   20162:                        if(comm_state_changed) {
1.1.1.26  root     20163:                                if(GetCommState(hComm, &dcb)) {
                   20164: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   20165:                                        DWORD baud = 115200 / p->prev_divisor;
                   20166:                                        dcb.BaudRate = 9600; // default
                   20167:                                        
                   20168:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   20169:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   20170:                                        // 134.5bps is not supported ???
                   20171: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   20172:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   20173:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   20174:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   20175:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   20176:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   20177:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   20178:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   20179:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   20180:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   20181: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   20182: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   20183: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   20184:                                        
                   20185:                                        switch(p->prev_line_ctrl & 0x03) {
                   20186:                                        case 0x00: dcb.ByteSize = 5; break;
                   20187:                                        case 0x01: dcb.ByteSize = 6; break;
                   20188:                                        case 0x02: dcb.ByteSize = 7; break;
                   20189:                                        case 0x03: dcb.ByteSize = 8; break;
                   20190:                                        }
                   20191:                                        switch(p->prev_line_ctrl & 0x04) {
                   20192:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   20193:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   20194:                                        }
                   20195:                                        switch(p->prev_line_ctrl & 0x38) {
                   20196:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   20197:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   20198:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   20199:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   20200:                                        default:   dcb.Parity = NOPARITY;    break;
                   20201:                                        }
                   20202:                                        dcb.fBinary = TRUE;
                   20203:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   20204:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   20205:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   20206:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   20207:                                        dcb.fTXContinueOnXoff = TRUE;
                   20208:                                        dcb.fOutX = dcb.fInX = FALSE;
                   20209:                                        dcb.fErrorChar = FALSE;
                   20210:                                        dcb.fNull = FALSE;
                   20211:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   20212:                                        dcb.fAbortOnError = FALSE;
                   20213:                                        
                   20214:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     20215:                                }
                   20216:                                
                   20217:                                // check again to apply all comm state changes
                   20218:                                Sleep(10);
                   20219:                                continue;
                   20220:                        }
                   20221:                        
                   20222:                        // set comm pins
                   20223:                        bool change_brk = false;
1.1.1.26  root     20224: //                     bool change_rts = false;
                   20225: //                     bool change_dtr = false;
1.1.1.25  root     20226:                        
                   20227:                        EnterCriticalSection(&q->csModemCtrl);
                   20228:                        if(p->prev_set_brk != p->set_brk) {
                   20229:                                p->prev_set_brk = p->set_brk;
                   20230:                                change_brk = true;
                   20231:                        }
1.1.1.26  root     20232: //                     if(p->prev_set_rts != p->set_rts) {
                   20233: //                             p->prev_set_rts = p->set_rts;
                   20234: //                             change_rts = true;
                   20235: //                     }
                   20236: //                     if(p->prev_set_dtr != p->set_dtr) {
                   20237: //                             p->prev_set_dtr = p->set_dtr;
                   20238: //                             change_dtr = true;
                   20239: //                     }
1.1.1.25  root     20240:                        LeaveCriticalSection(&q->csModemCtrl);
                   20241:                        
                   20242:                        if(change_brk) {
1.1.1.26  root     20243:                                static UINT32 clear_time = 0;
                   20244:                                if(p->prev_set_brk) {
                   20245:                                        EscapeCommFunction(hComm, SETBREAK);
                   20246:                                        clear_time = timeGetTime() + 200;
                   20247:                                } else {
                   20248:                                        // keep break for at least 200msec
                   20249:                                        UINT32 cur_time = timeGetTime();
                   20250:                                        if(clear_time > cur_time) {
                   20251:                                                Sleep(clear_time - cur_time);
                   20252:                                        }
                   20253:                                        EscapeCommFunction(hComm, CLRBREAK);
                   20254:                                }
1.1.1.25  root     20255:                        }
1.1.1.26  root     20256: //                     if(change_rts) {
                   20257: //                             if(p->prev_set_rts) {
                   20258: //                                     EscapeCommFunction(hComm, SETRTS);
                   20259: //                             } else {
                   20260: //                                     EscapeCommFunction(hComm, CLRRTS);
                   20261: //                             }
                   20262: //                     }
                   20263: //                     if(change_dtr) {
                   20264: //                             if(p->prev_set_dtr) {
                   20265: //                                     EscapeCommFunction(hComm, SETDTR);
                   20266: //                             } else {
                   20267: //                                     EscapeCommFunction(hComm, CLRDTR);
                   20268: //                             }
                   20269: //                     }
1.1.1.25  root     20270:                        
                   20271:                        // get comm pins
                   20272:                        DWORD dwModemStat = 0;
                   20273:                        
                   20274:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   20275:                                EnterCriticalSection(&q->csModemStat);
                   20276:                                if(dwModemStat & MS_RLSD_ON) {
                   20277:                                        p->modem_stat |= 0x80;
                   20278:                                } else {
                   20279:                                        p->modem_stat &= ~0x80;
                   20280:                                }
                   20281:                                if(dwModemStat & MS_RING_ON) {
                   20282:                                        p->modem_stat |= 0x40;
                   20283:                                } else {
                   20284:                                        p->modem_stat &= ~0x40;
                   20285:                                }
1.1.1.26  root     20286: //                             if(dwModemStat & MS_DSR_ON) {
                   20287: //                                     p->modem_stat |= 0x20;
                   20288: //                             } else {
                   20289: //                                     p->modem_stat &= ~0x20;
                   20290: //                             }
                   20291: //                             if(dwModemStat & MS_CTS_ON) {
                   20292: //                                     p->modem_stat |= 0x10;
                   20293: //                             } else {
                   20294: //                                     p->modem_stat &= ~0x10;
                   20295: //                             }
1.1.1.25  root     20296:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   20297:                                        p->modem_stat |= 0x08;
                   20298:                                }
                   20299:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   20300:                                        p->modem_stat |= 0x04;
                   20301:                                }
1.1.1.26  root     20302: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   20303: //                                     p->modem_stat |= 0x02;
                   20304: //                             }
                   20305: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   20306: //                                     p->modem_stat |= 0x01;
                   20307: //                             }
1.1.1.25  root     20308:                                LeaveCriticalSection(&q->csModemStat);
                   20309:                        }
                   20310:                        
                   20311:                        // send data
                   20312:                        DWORD dwSend = 0;
                   20313:                        
                   20314:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     20315:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     20316:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   20317:                        }
                   20318:                        LeaveCriticalSection(&q->csSendData);
                   20319:                        
                   20320:                        if(dwSend != 0) {
                   20321:                                DWORD dwWritten = 0;
                   20322:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   20323:                        }
                   20324:                        
                   20325:                        // get line status and recv data
                   20326:                        DWORD dwLineStat = 0;
                   20327:                        COMSTAT comStat;
                   20328:                        
                   20329:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   20330:                                EnterCriticalSection(&q->csLineStat);
                   20331:                                if(dwLineStat & CE_BREAK) {
                   20332:                                        p->line_stat_err |= 0x10;
                   20333:                                }
                   20334:                                if(dwLineStat & CE_FRAME) {
                   20335:                                        p->line_stat_err |= 0x08;
                   20336:                                }
                   20337:                                if(dwLineStat & CE_RXPARITY) {
                   20338:                                        p->line_stat_err |= 0x04;
                   20339:                                }
                   20340:                                if(dwLineStat & CE_OVERRUN) {
                   20341:                                        p->line_stat_err |= 0x02;
                   20342:                                }
                   20343:                                LeaveCriticalSection(&q->csLineStat);
                   20344:                                
                   20345:                                if(comStat.cbInQue != 0) {
                   20346:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     20347:                                        DWORD dwRecv = 0;
                   20348:                                        if(p->recv_buffer != NULL) {
                   20349:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   20350:                                        }
1.1.1.25  root     20351:                                        LeaveCriticalSection(&q->csRecvData);
                   20352:                                        
                   20353:                                        if(dwRecv != 0) {
                   20354:                                                DWORD dwRead = 0;
                   20355:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   20356:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     20357:                                                        if(p->recv_buffer != NULL) {
                   20358:                                                                for(int i = 0; i < dwRead; i++) {
                   20359:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   20360:                                                                }
1.1.1.25  root     20361:                                                        }
                   20362:                                                        LeaveCriticalSection(&q->csRecvData);
                   20363:                                                }
                   20364:                                        }
                   20365:                                }
                   20366:                        }
                   20367:                        Sleep(10);
                   20368:                }
                   20369:                CloseHandle(hComm);
                   20370:        }
                   20371:        return 0;
                   20372: }
                   20373: 
1.1.1.8   root     20374: // cmos
                   20375: 
                   20376: void cmos_init()
                   20377: {
                   20378:        memset(cmos, 0, sizeof(cmos));
                   20379:        cmos_addr = 0;
1.1       root     20380:        
1.1.1.8   root     20381:        // from DOSBox
                   20382:        cmos_write(0x0a, 0x26);
                   20383:        cmos_write(0x0b, 0x02);
                   20384:        cmos_write(0x0d, 0x80);
1.1       root     20385: }
                   20386: 
1.1.1.8   root     20387: void cmos_write(int addr, UINT8 val)
1.1       root     20388: {
1.1.1.8   root     20389:        cmos[addr & 0x7f] = val;
                   20390: }
                   20391: 
                   20392: #define CMOS_GET_TIME() { \
                   20393:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   20394:        if(prev_sec != cur_sec) { \
                   20395:                GetLocalTime(&time); \
                   20396:                prev_sec = cur_sec; \
                   20397:        } \
1.1       root     20398: }
1.1.1.8   root     20399: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     20400: 
1.1.1.8   root     20401: UINT8 cmos_read(int addr)
1.1       root     20402: {
1.1.1.8   root     20403:        static SYSTEMTIME time;
                   20404:        static UINT32 prev_sec = 0;
1.1       root     20405:        
1.1.1.8   root     20406:        switch(addr & 0x7f) {
                   20407:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   20408:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   20409:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   20410:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   20411:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   20412:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   20413:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   20414: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   20415:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   20416:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   20417:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   20418:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20419:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20420:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20421:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20422:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     20423:        }
1.1.1.8   root     20424:        return(cmos[addr & 0x7f]);
1.1       root     20425: }
                   20426: 
1.1.1.7   root     20427: // kbd (a20)
                   20428: 
                   20429: void kbd_init()
                   20430: {
1.1.1.8   root     20431:        kbd_data = kbd_command = 0;
1.1.1.7   root     20432:        kbd_status = 0x18;
                   20433: }
                   20434: 
                   20435: UINT8 kbd_read_data()
                   20436: {
1.1.1.57  root     20437:        UINT8 data = kbd_data;
                   20438:        kbd_data = 0;
1.1.1.8   root     20439:        kbd_status &= ~1;
1.1.1.57  root     20440:        return(data);
1.1.1.7   root     20441: }
                   20442: 
                   20443: void kbd_write_data(UINT8 val)
                   20444: {
                   20445:        switch(kbd_command) {
                   20446:        case 0xd1:
                   20447:                i386_set_a20_line((val >> 1) & 1);
                   20448:                break;
                   20449:        }
                   20450:        kbd_command = 0;
1.1.1.8   root     20451:        kbd_status &= ~8;
1.1.1.7   root     20452: }
                   20453: 
                   20454: UINT8 kbd_read_status()
                   20455: {
                   20456:        return(kbd_status);
                   20457: }
                   20458: 
                   20459: void kbd_write_command(UINT8 val)
                   20460: {
                   20461:        switch(val) {
                   20462:        case 0xd0:
                   20463:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     20464:                kbd_status |= 1;
1.1.1.7   root     20465:                break;
                   20466:        case 0xdd:
                   20467:                i386_set_a20_line(0);
                   20468:                break;
                   20469:        case 0xdf:
                   20470:                i386_set_a20_line(1);
                   20471:                break;
1.1.1.26  root     20472:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   20473:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     20474:                if(!(val & 1)) {
1.1.1.8   root     20475:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     20476:                                // reset pic
                   20477:                                pic_init();
                   20478:                                pic[0].irr = pic[1].irr = 0x00;
                   20479:                                pic[0].imr = pic[1].imr = 0xff;
                   20480:                        }
                   20481:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     20482:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   20483:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   20484:                        i386_jmp_far(selector, address);
1.1.1.7   root     20485:                }
                   20486:                i386_set_a20_line((val >> 1) & 1);
                   20487:                break;
                   20488:        }
                   20489:        kbd_command = val;
1.1.1.8   root     20490:        kbd_status |= 8;
1.1.1.7   root     20491: }
                   20492: 
1.1.1.9   root     20493: // vga
                   20494: 
                   20495: UINT8 vga_read_status()
                   20496: {
                   20497:        // 60hz
                   20498:        static const int period[3] = {16, 17, 17};
                   20499:        static int index = 0;
                   20500:        UINT32 time = timeGetTime() % period[index];
                   20501:        
                   20502:        index = (index + 1) % 3;
1.1.1.14  root     20503:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     20504: }
                   20505: 
1.1       root     20506: // i/o bus
                   20507: 
1.1.1.29  root     20508: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   20509: //#define SW1US_PATCH
                   20510: 
1.1.1.25  root     20511: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     20512: #ifdef USE_DEBUGGER
1.1.1.25  root     20513: {
1.1.1.33  root     20514:        if(now_debugging) {
                   20515:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20516:                        if(in_break_point.table[i].status == 1) {
                   20517:                                if(addr == in_break_point.table[i].addr) {
                   20518:                                        in_break_point.hit = i + 1;
                   20519:                                        now_suspended = true;
                   20520:                                        break;
                   20521:                                }
                   20522:                        }
                   20523:                }
1.1.1.25  root     20524:        }
1.1.1.33  root     20525:        return(debugger_read_io_byte(addr));
1.1.1.25  root     20526: }
1.1.1.33  root     20527: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     20528: #endif
1.1       root     20529: {
1.1.1.33  root     20530:        UINT8 val = 0xff;
                   20531:        
1.1       root     20532:        switch(addr) {
1.1.1.29  root     20533: #ifdef SW1US_PATCH
                   20534:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20535:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     20536:                val = sio_read(0, addr - 1);
                   20537:                break;
1.1.1.29  root     20538: #else
1.1.1.25  root     20539:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20540:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     20541:                val = dma_read(0, addr);
                   20542:                break;
1.1.1.29  root     20543: #endif
1.1.1.25  root     20544:        case 0x20: case 0x21:
1.1.1.33  root     20545:                val = pic_read(0, addr);
                   20546:                break;
1.1.1.25  root     20547:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     20548:                val = pit_read(addr & 0x03);
                   20549:                break;
1.1.1.7   root     20550:        case 0x60:
1.1.1.33  root     20551:                val = kbd_read_data();
                   20552:                break;
1.1.1.9   root     20553:        case 0x61:
1.1.1.33  root     20554:                val = system_port;
                   20555:                break;
1.1.1.7   root     20556:        case 0x64:
1.1.1.33  root     20557:                val = kbd_read_status();
                   20558:                break;
1.1       root     20559:        case 0x71:
1.1.1.33  root     20560:                val = cmos_read(cmos_addr);
                   20561:                break;
1.1.1.25  root     20562:        case 0x81:
1.1.1.33  root     20563:                val = dma_page_read(0, 2);
                   20564:                break;
1.1.1.25  root     20565:        case 0x82:
1.1.1.33  root     20566:                val = dma_page_read(0, 3);
                   20567:                break;
1.1.1.25  root     20568:        case 0x83:
1.1.1.33  root     20569:                val = dma_page_read(0, 1);
                   20570:                break;
1.1.1.25  root     20571:        case 0x87:
1.1.1.33  root     20572:                val = dma_page_read(0, 0);
                   20573:                break;
1.1.1.25  root     20574:        case 0x89:
1.1.1.33  root     20575:                val = dma_page_read(1, 2);
                   20576:                break;
1.1.1.25  root     20577:        case 0x8a:
1.1.1.33  root     20578:                val = dma_page_read(1, 3);
                   20579:                break;
1.1.1.25  root     20580:        case 0x8b:
1.1.1.33  root     20581:                val = dma_page_read(1, 1);
                   20582:                break;
1.1.1.25  root     20583:        case 0x8f:
1.1.1.33  root     20584:                val = dma_page_read(1, 0);
                   20585:                break;
1.1       root     20586:        case 0x92:
1.1.1.33  root     20587:                val = (m_a20_mask >> 19) & 2;
                   20588:                break;
1.1.1.25  root     20589:        case 0xa0: case 0xa1:
1.1.1.33  root     20590:                val = pic_read(1, addr);
                   20591:                break;
1.1.1.25  root     20592:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20593:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     20594:                val = dma_read(1, (addr - 0xc0) >> 1);
                   20595:                break;
1.1.1.37  root     20596:        case 0x278: case 0x279: case 0x27a:
                   20597:                val = pio_read(1, addr);
                   20598:                break;
1.1.1.29  root     20599:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     20600:                val = sio_read(3, addr);
                   20601:                break;
1.1.1.25  root     20602:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     20603:                val = sio_read(1, addr);
                   20604:                break;
1.1.1.25  root     20605:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     20606:                val = pio_read(0, addr);
                   20607:                break;
1.1.1.25  root     20608:        case 0x3ba: case 0x3da:
1.1.1.33  root     20609:                val = vga_read_status();
                   20610:                break;
1.1.1.37  root     20611:        case 0x3bc: case 0x3bd: case 0x3be:
                   20612:                val = pio_read(2, addr);
                   20613:                break;
1.1.1.58  root     20614:        case 0x3d5:
                   20615:                if(crtc_addr < 16) {
                   20616:                        val = crtc_regs[crtc_addr];
                   20617:                }
                   20618:                break;
1.1.1.29  root     20619:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     20620:                val = sio_read(2, addr);
                   20621:                break;
1.1.1.25  root     20622:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     20623:                val = sio_read(0, addr);
                   20624:                break;
1.1       root     20625:        default:
1.1.1.33  root     20626: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     20627:                break;
                   20628:        }
1.1.1.33  root     20629: #ifdef ENABLE_DEBUG_IOPORT
                   20630:        if(fp_debug_log != NULL) {
                   20631:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   20632:        }
                   20633: #endif
                   20634:        return(val);
1.1       root     20635: }
                   20636: 
                   20637: UINT16 read_io_word(offs_t addr)
                   20638: {
                   20639:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   20640: }
                   20641: 
1.1.1.33  root     20642: #ifdef USE_DEBUGGER
                   20643: UINT16 debugger_read_io_word(offs_t addr)
                   20644: {
                   20645:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   20646: }
                   20647: #endif
                   20648: 
1.1       root     20649: UINT32 read_io_dword(offs_t addr)
                   20650: {
                   20651:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   20652: }
                   20653: 
1.1.1.33  root     20654: #ifdef USE_DEBUGGER
                   20655: UINT32 debugger_read_io_dword(offs_t addr)
                   20656: {
                   20657:        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));
                   20658: }
                   20659: #endif
                   20660: 
1.1       root     20661: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     20662: #ifdef USE_DEBUGGER
                   20663: {
                   20664:        if(now_debugging) {
                   20665:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20666:                        if(out_break_point.table[i].status == 1) {
                   20667:                                if(addr == out_break_point.table[i].addr) {
                   20668:                                        out_break_point.hit = i + 1;
                   20669:                                        now_suspended = true;
                   20670:                                        break;
                   20671:                                }
                   20672:                        }
                   20673:                }
                   20674:        }
                   20675:        debugger_write_io_byte(addr, val);
                   20676: }
                   20677: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   20678: #endif
1.1       root     20679: {
1.1.1.25  root     20680: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     20681:        if(fp_debug_log != NULL) {
1.1.1.43  root     20682: #ifdef USE_SERVICE_THREAD
                   20683:                if(addr != 0xf7)
                   20684: #endif
1.1.1.33  root     20685:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     20686:        }
                   20687: #endif
1.1       root     20688:        switch(addr) {
1.1.1.29  root     20689: #ifdef SW1US_PATCH
                   20690:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20691:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   20692:                sio_write(0, addr - 1, val);
                   20693:                break;
                   20694: #else
1.1.1.25  root     20695:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20696:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   20697:                dma_write(0, addr, val);
                   20698:                break;
1.1.1.29  root     20699: #endif
1.1.1.25  root     20700:        case 0x20: case 0x21:
1.1       root     20701:                pic_write(0, addr, val);
                   20702:                break;
1.1.1.25  root     20703:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     20704:                pit_write(addr & 0x03, val);
                   20705:                break;
1.1.1.7   root     20706:        case 0x60:
                   20707:                kbd_write_data(val);
                   20708:                break;
1.1.1.9   root     20709:        case 0x61:
                   20710:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   20711:                        // beep on
                   20712: //                     MessageBeep(-1);
                   20713:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   20714:                        // beep off
                   20715:                }
                   20716:                system_port = val;
                   20717:                break;
1.1       root     20718:        case 0x64:
1.1.1.7   root     20719:                kbd_write_command(val);
1.1       root     20720:                break;
                   20721:        case 0x70:
                   20722:                cmos_addr = val;
                   20723:                break;
                   20724:        case 0x71:
1.1.1.8   root     20725:                cmos_write(cmos_addr, val);
1.1       root     20726:                break;
1.1.1.25  root     20727:        case 0x81:
                   20728:                dma_page_write(0, 2, val);
                   20729:        case 0x82:
                   20730:                dma_page_write(0, 3, val);
                   20731:        case 0x83:
                   20732:                dma_page_write(0, 1, val);
                   20733:        case 0x87:
                   20734:                dma_page_write(0, 0, val);
                   20735:        case 0x89:
                   20736:                dma_page_write(1, 2, val);
                   20737:        case 0x8a:
                   20738:                dma_page_write(1, 3, val);
                   20739:        case 0x8b:
                   20740:                dma_page_write(1, 1, val);
                   20741:        case 0x8f:
                   20742:                dma_page_write(1, 0, val);
1.1       root     20743:        case 0x92:
1.1.1.7   root     20744:                i386_set_a20_line((val >> 1) & 1);
1.1       root     20745:                break;
1.1.1.25  root     20746:        case 0xa0: case 0xa1:
1.1       root     20747:                pic_write(1, addr, val);
                   20748:                break;
1.1.1.25  root     20749:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20750:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     20751:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     20752:                break;
1.1.1.35  root     20753: #ifdef USE_SERVICE_THREAD
                   20754:        case 0xf7:
                   20755:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     20756:                if(in_service && cursor_moved) {
                   20757:                        // update cursor position before service is done
                   20758:                        pcbios_update_cursor_position();
                   20759:                        cursor_moved = false;
                   20760:                }
1.1.1.35  root     20761:                finish_service_loop();
                   20762:                break;
                   20763: #endif
1.1.1.37  root     20764:        case 0x278: case 0x279: case 0x27a:
                   20765:                pio_write(1, addr, val);
                   20766:                break;
1.1.1.29  root     20767:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   20768:                sio_write(3, addr, val);
                   20769:                break;
1.1.1.25  root     20770:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   20771:                sio_write(1, addr, val);
                   20772:                break;
                   20773:        case 0x378: case 0x379: case 0x37a:
                   20774:                pio_write(0, addr, val);
                   20775:                break;
1.1.1.37  root     20776:        case 0x3bc: case 0x3bd: case 0x3be:
                   20777:                pio_write(2, addr, val);
                   20778:                break;
1.1.1.58  root     20779:        case 0x3d4:
                   20780:                crtc_addr = val;
                   20781:                break;
                   20782:        case 0x3d5:
                   20783:                if(crtc_addr < 16) {
                   20784:                        if(crtc_regs[crtc_addr] != val) {
                   20785:                                crtc_regs[crtc_addr] = val;
                   20786:                                crtc_changed[crtc_addr] = 1;
                   20787:                        }
                   20788:                }
                   20789:                break;
1.1.1.29  root     20790:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   20791:                sio_write(2, addr, val);
                   20792:                break;
1.1.1.25  root     20793:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   20794:                sio_write(0, addr, val);
                   20795:                break;
1.1       root     20796:        default:
1.1.1.33  root     20797: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     20798:                break;
                   20799:        }
                   20800: }
                   20801: 
                   20802: void write_io_word(offs_t addr, UINT16 val)
                   20803: {
                   20804:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20805:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20806: }
                   20807: 
1.1.1.33  root     20808: #ifdef USE_DEBUGGER
                   20809: void debugger_write_io_word(offs_t addr, UINT16 val)
                   20810: {
                   20811:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20812:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20813: }
                   20814: #endif
                   20815: 
1.1       root     20816: void write_io_dword(offs_t addr, UINT32 val)
                   20817: {
                   20818:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20819:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20820:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20821:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20822: }
1.1.1.33  root     20823: 
                   20824: #ifdef USE_DEBUGGER
                   20825: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   20826: {
                   20827:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20828:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20829:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20830:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20831: }
                   20832: #endif

unix.superglobalmegacorp.com

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