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

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.63! root     2874: #if defined(SUPPORT_VCPI)
        !          2875:                        "\t-x\tenable LIM EMS, VCPI, and XMS\n"
        !          2876: #elif defined(SUPPORT_XMS)
        !          2877:                        "\t-x\tenable LIM EMS and XMS\n"
1.1.1.19  root     2878: #else
1.1.1.28  root     2879:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2880: #endif
                   2881:                );
1.1.1.10  root     2882:                
                   2883:                if(!is_started_from_command_prompt()) {
                   2884:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2885:                        while(!_kbhit()) {
                   2886:                                Sleep(10);
                   2887:                        }
                   2888:                }
1.1.1.20  root     2889: #ifdef _DEBUG
                   2890:                _CrtDumpMemoryLeaks();
                   2891: #endif
1.1       root     2892:                return(EXIT_FAILURE);
                   2893:        }
1.1.1.27  root     2894:        if(convert_cmd_file) {
                   2895:                retval = EXIT_FAILURE;
1.1.1.28  root     2896:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2897:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2898:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2899:                        
1.1.1.28  root     2900:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2901:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2902:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2903:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2904:                        } else {
1.1.1.28  root     2905:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2906:                                if(offset != 0) {
                   2907:                                        UINT8 buffer[14];
                   2908:                                        fseek(fp, offset, SEEK_SET);
                   2909:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2910:                                        memset(path, 0, sizeof(path));
                   2911:                                        fread(path, buffer[9], 1, fp);
                   2912:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2913:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2914:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2915:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2916:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2917:                                } else {
                   2918:                                        // read pe header of msdos.exe
                   2919:                                        UINT8 header[0x400];
                   2920:                                        fseek(fp, 0, SEEK_SET);
                   2921:                                        fread(header, sizeof(header), 1, fp);
                   2922:                                        
                   2923:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2924:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2925:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2926:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2927:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2928:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2929:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2930:                                        
                   2931:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2932:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2933:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2934:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2935:                                        if(dwExtraLastSectionBytes != 0) {
                   2936:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2937:                                                dwLastSectionSize += dwRemain;
                   2938:                                        }
                   2939:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2940:                                        
                   2941:                                        // store msdos.exe
                   2942:                                        fseek(fp, 0, SEEK_SET);
                   2943:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2944:                                                if((data = fgetc(fp)) != EOF) {
                   2945:                                                        fputc(data, fo);
                   2946:                                                } else {
                   2947:                                                        // we should not reach here :-(
                   2948:                                                        fputc(0, fo);
                   2949:                                                }
                   2950:                                        }
                   2951:                                        
                   2952:                                        // store options
                   2953:                                        UINT8 flags = 0;
                   2954:                                        if(stay_busy) {
                   2955:                                                flags |= 0x01;
                   2956:                                        }
                   2957:                                        if(no_windows) {
                   2958:                                                flags |= 0x02;
                   2959:                                        }
                   2960:                                        if(standard_env) {
                   2961:                                                flags |= 0x04;
                   2962:                                        }
                   2963:                                        if(ignore_illegal_insn) {
                   2964:                                                flags |= 0x08;
                   2965:                                        }
                   2966:                                        if(limit_max_memory) {
                   2967:                                                flags |= 0x10;
                   2968:                                        }
1.1.1.29  root     2969:                                        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     2970:                                                flags |= 0x20;
                   2971:                                        }
                   2972:                                        if(support_ems) {
                   2973:                                                flags |= 0x40;
                   2974:                                        }
1.1.1.30  root     2975: #ifdef SUPPORT_XMS
                   2976:                                        if(support_xms) {
                   2977:                                                flags |= 0x80;
                   2978:                                        }
                   2979: #endif
1.1.1.28  root     2980:                                        
                   2981:                                        fputc(flags, fo);
                   2982:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2983:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2984:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2985:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2986:                                        fputc(dos_major_version, fo);
                   2987:                                        fputc(dos_minor_version, fo);
                   2988:                                        fputc(win_major_version, fo);
                   2989:                                        fputc(win_minor_version, fo);
1.1.1.28  root     2990:                                        fputc((code_page >> 0) & 0xff, fo);
                   2991:                                        fputc((code_page >> 8) & 0xff, fo);
                   2992:                                        
                   2993:                                        // store command file info
1.1.1.60  root     2994:                                        GetFullPathNameA(argv[arg_offset + 1], MAX_PATH, full, &name);
1.1.1.28  root     2995:                                        int name_len = strlen(name);
                   2996:                                        fseek(fs, 0, SEEK_END);
                   2997:                                        long file_size = ftell(fs);
                   2998:                                        
                   2999:                                        fputc(name_len, fo);
                   3000:                                        fputc((file_size >>  0) & 0xff, fo);
                   3001:                                        fputc((file_size >>  8) & 0xff, fo);
                   3002:                                        fputc((file_size >> 16) & 0xff, fo);
                   3003:                                        fputc((file_size >> 24) & 0xff, fo);
                   3004:                                        fwrite(name, name_len, 1, fo);
                   3005:                                        
                   3006:                                        // store command file
                   3007:                                        fseek(fs, 0, SEEK_SET);
                   3008:                                        for(int i = 0; i < file_size; i++) {
                   3009:                                                if((data = fgetc(fs)) != EOF) {
                   3010:                                                        fputc(data, fo);
                   3011:                                                } else {
                   3012:                                                        // we should not reach here :-(
                   3013:                                                        fputc(0, fo);
                   3014:                                                }
                   3015:                                        }
                   3016:                                        
                   3017:                                        // store padding data and update pe header
1.1.1.29  root     3018:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   3019:                                        coffHeader->NumberOfSections++;
                   3020:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   3021:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   3022:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   3023:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   3024:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     3025:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   3026:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   3027:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     3028:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     3029:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   3030:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     3031:                                                        if(i < 2) {
                   3032:                                                                fputc(padding[i & 15], fo);
                   3033:                                                        } else {
                   3034:                                                                fputc(padding[(i - 2) & 15], fo);
                   3035:                                                        }
1.1.1.28  root     3036:                                                }
                   3037:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   3038:                                        }
                   3039:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   3040:                                        
                   3041:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   3042:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   3043:                                        if(dwExtraNewSectionBytes != 0) {
                   3044:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   3045:                                                dwNewSectionSize += dwRemain;
                   3046:                                        }
                   3047:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   3048:                                        
                   3049:                                        fseek(fo, 0, SEEK_SET);
                   3050:                                        fwrite(header, sizeof(header), 1, fo);
                   3051:                                        
                   3052:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   3053:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     3054:                                }
                   3055:                        }
                   3056:                        if(fp != NULL) {
                   3057:                                fclose(fp);
                   3058:                        }
                   3059:                        if(fs != NULL) {
                   3060:                                fclose(fs);
                   3061:                        }
                   3062:                        if(fo != NULL) {
                   3063:                                fclose(fo);
                   3064:                        }
                   3065:                }
                   3066: #ifdef _DEBUG
                   3067:                _CrtDumpMemoryLeaks();
                   3068: #endif
                   3069:                return(retval);
                   3070:        }
1.1       root     3071:        
1.1.1.62  root     3072:        is_winxp_or_later = is_greater_windows_version(5, 1, 0, 0);
1.1.1.54  root     3073:        is_xp_64_or_later = is_greater_windows_version(5, 2, 0, 0);
1.1.1.14  root     3074:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   3075:        
1.1.1.23  root     3076:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     3077:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     3078:        CONSOLE_CURSOR_INFO ci;
1.1.1.61  root     3079:        UINT input_cp = GetConsoleCP();
                   3080:        UINT output_cp = GetConsoleOutputCP();
                   3081:        int multibyte_cp = _getmbcp();
1.1.1.23  root     3082:        
1.1.1.28  root     3083:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     3084:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.59  root     3085:        ci_old = ci_new = ci;
1.1.1.24  root     3086:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1.1.61  root     3087:        get_console_font_success = get_console_font_size(&font_width, &font_height);
1.1       root     3088:        
1.1.1.14  root     3089:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   3090:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   3091:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3092:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     3093:                }
                   3094:        }
1.1.1.28  root     3095:        if(get_console_info_success) {
1.1.1.12  root     3096:                scr_width = csbi.dwSize.X;
1.1.1.14  root     3097:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   3098:                
1.1.1.28  root     3099:                // v-text shadow buffer size must be lesser than 0x7fd0
                   3100:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     3101:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   3102:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   3103:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     3104:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     3105:                                scr_width = 80;
                   3106:                                scr_height = 25;
                   3107:                        }
1.1.1.28  root     3108:                        screen_size_changed = true;
1.1.1.14  root     3109:                }
1.1.1.12  root     3110:        } else {
                   3111:                // for a proof (not a console)
                   3112:                scr_width = 80;
                   3113:                scr_height = 25;
                   3114:        }
1.1.1.14  root     3115:        scr_buf_size.X = scr_width;
                   3116:        scr_buf_size.Y = scr_height;
                   3117:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   3118:        scr_top = csbi.srWindow.Top;
1.1       root     3119:        cursor_moved = false;
1.1.1.59  root     3120:        cursor_moved_by_crtc = false;
1.1       root     3121:        
1.1.1.54  root     3122:        SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
                   3123:        
1.1.1.35  root     3124: #ifdef USE_SERVICE_THREAD
                   3125:        InitializeCriticalSection(&input_crit_sect);
                   3126:        InitializeCriticalSection(&key_buf_crit_sect);
                   3127:        InitializeCriticalSection(&putch_crit_sect);
1.1.1.50  root     3128:        main_thread_id = GetCurrentThreadId();
1.1.1.35  root     3129: #endif
1.1.1.50  root     3130:        
1.1.1.25  root     3131:        key_buf_char = new FIFO(256);
                   3132:        key_buf_scan = new FIFO(256);
1.1.1.57  root     3133:        key_buf_data = new FIFO(256);
1.1       root     3134:        
                   3135:        hardware_init();
                   3136:        
1.1.1.33  root     3137: #ifdef USE_DEBUGGER
                   3138:        debugger_init();
                   3139: #endif
                   3140:        
1.1.1.9   root     3141:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     3142:                retval = EXIT_FAILURE;
                   3143:        } else {
1.1.1.27  root     3144: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     3145:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   3146: #endif
                   3147:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   3148:                
1.1.1.28  root     3149:                if(screen_size_changed) {
1.1.1.24  root     3150:                        change_console_size(scr_width, scr_height);
                   3151:                }
1.1.1.8   root     3152:                TIMECAPS caps;
                   3153:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   3154:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.35  root     3155: #ifdef USE_VRAM_THREAD
1.1.1.14  root     3156:                InitializeCriticalSection(&vram_crit_sect);
                   3157:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   3158: #endif
1.1.1.33  root     3159: #ifdef USE_DEBUGGER
                   3160:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   3161:                // wait until telnet client starts and connects to me
                   3162:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   3163:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   3164:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   3165:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   3166:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   3167:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   3168:                                Sleep(100);
                   3169:                        }
                   3170:                }
                   3171: #endif
1.1       root     3172:                hardware_run();
1.1.1.35  root     3173: #ifdef USE_VRAM_THREAD
1.1.1.14  root     3174:                vram_flush();
                   3175:                DeleteCriticalSection(&vram_crit_sect);
                   3176: #endif
1.1.1.24  root     3177:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     3178:                
1.1.1.24  root     3179:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.61  root     3180:                hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   3181:                
                   3182:                // restore console settings
                   3183:                _setmbcp(multibyte_cp);
                   3184:                SetConsoleCP(input_cp);
                   3185:                SetConsoleOutputCP(multibyte_cp);
                   3186:                
1.1.1.28  root     3187:                if(get_console_info_success) {
1.1.1.12  root     3188:                        if(restore_console_on_exit) {
1.1.1.14  root     3189:                                // window can't be bigger than buffer,
                   3190:                                // buffer can't be smaller than window,
                   3191:                                // so make a tiny window,
                   3192:                                // set the required buffer,
                   3193:                                // then set the required window
1.1.1.61  root     3194:                                CONSOLE_SCREEN_BUFFER_INFO cur_csbi;
1.1.1.14  root     3195:                                SMALL_RECT rect;
1.1.1.61  root     3196:                                GetConsoleScreenBufferInfo(hStdout, &cur_csbi);
                   3197:                                int min_width  = min(cur_csbi.srWindow.Right - cur_csbi.srWindow.Left + 1, csbi.srWindow.Right - csbi.srWindow.Left + 1);
                   3198:                                int min_height = min(cur_csbi.srWindow.Bottom - cur_csbi.srWindow.Top + 1, csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
                   3199:                                
                   3200:                                SET_RECT(rect, 0, cur_csbi.srWindow.Top, min_width - 1, cur_csbi.srWindow.Top + min_height - 1);
1.1.1.14  root     3201:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     3202:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     3203:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.61  root     3204:                                if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
                   3205:                                        SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
                   3206:                                        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3207:                                }
                   3208:                        }
                   3209:                }
                   3210:                if(get_console_font_success) {
                   3211:                        set_console_font_size(font_width, font_height);
                   3212:                }
                   3213:                if(get_console_info_success) {
                   3214:                        if(restore_console_on_exit) {
                   3215:                                SMALL_RECT rect;
                   3216:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
                   3217:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
                   3218:                                if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
                   3219:                                        SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
                   3220:                                        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3221:                                }
1.1.1.12  root     3222:                        }
1.1.1.14  root     3223:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   3224:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     3225:                }
1.1.1.24  root     3226:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   3227:                
1.1       root     3228:                msdos_finish();
1.1.1.14  root     3229:                
                   3230:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     3231:        }
1.1.1.35  root     3232:        if(temp_file_created) {
1.1.1.60  root     3233:                DeleteFileA(temp_file_path);
1.1.1.35  root     3234:                temp_file_created = false;
                   3235:        }
1.1.1.10  root     3236:        hardware_finish();
                   3237:        
1.1.1.28  root     3238:        if(key_buf_char != NULL) {
                   3239:                key_buf_char->release();
                   3240:                delete key_buf_char;
                   3241:                key_buf_char = NULL;
                   3242:        }
                   3243:        if(key_buf_scan != NULL) {
                   3244:                key_buf_scan->release();
                   3245:                delete key_buf_scan;
                   3246:                key_buf_scan = NULL;
                   3247:        }
1.1.1.57  root     3248:        if(key_buf_data != NULL) {
                   3249:                key_buf_data->release();
                   3250:                delete key_buf_data;
                   3251:                key_buf_data = NULL;
                   3252:        }
1.1.1.35  root     3253: #ifdef USE_SERVICE_THREAD
                   3254:        DeleteCriticalSection(&input_crit_sect);
                   3255:        DeleteCriticalSection(&key_buf_crit_sect);
                   3256:        DeleteCriticalSection(&putch_crit_sect);
                   3257: #endif
1.1.1.20  root     3258: #ifdef _DEBUG
                   3259:        _CrtDumpMemoryLeaks();
                   3260: #endif
1.1       root     3261:        return(retval);
                   3262: }
                   3263: 
1.1.1.20  root     3264: /* ----------------------------------------------------------------------------
                   3265:        console
                   3266: ---------------------------------------------------------------------------- */
                   3267: 
1.1.1.14  root     3268: void change_console_size(int width, int height)
1.1.1.12  root     3269: {
1.1.1.23  root     3270:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     3271:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   3272:        SMALL_RECT rect;
                   3273:        COORD co;
                   3274:        
                   3275:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     3276:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   3277:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
1.1.1.60  root     3278:                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
1.1.1.14  root     3279:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.60  root     3280:                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     3281:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   3282:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
1.1.1.60  root     3283:                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     3284:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.60  root     3285:                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     3286:                }
                   3287:        }
1.1.1.14  root     3288:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     3289:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     3290:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     3291:                SetConsoleCursorPosition(hStdout, co);
                   3292:                cursor_moved = true;
1.1.1.59  root     3293:                cursor_moved_by_crtc = false;
1.1.1.12  root     3294:        }
1.1.1.14  root     3295:        
                   3296:        // window can't be bigger than buffer,
                   3297:        // buffer can't be smaller than window,
                   3298:        // so make a tiny window,
                   3299:        // set the required buffer,
                   3300:        // then set the required window
1.1.1.61  root     3301:        int min_width  = min(csbi.srWindow.Right - csbi.srWindow.Left + 1, width);
                   3302:        int min_height = min(csbi.srWindow.Bottom - csbi.srWindow.Top + 1, height);
                   3303:        
                   3304:        SET_RECT(rect, 0, csbi.srWindow.Top, min_width - 1, csbi.srWindow.Top + min_height - 1);
1.1.1.12  root     3305:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     3306:        co.X = width;
                   3307:        co.Y = height;
1.1.1.12  root     3308:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     3309:        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.61  root     3310:        if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
                   3311:                SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
                   3312:                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3313:        }
1.1.1.14  root     3314:        
                   3315:        scr_width = scr_buf_size.X = width;
                   3316:        scr_height = scr_buf_size.Y = height;
                   3317:        scr_top = 0;
                   3318:        
                   3319:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   3320:        
                   3321:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     3322:        text_vram_end_address = text_vram_top_address + regen;
                   3323:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   3324:        
1.1.1.14  root     3325:        if(regen > 0x4000) {
                   3326:                regen = 0x8000;
                   3327:                vram_pages = 1;
                   3328:        } else if(regen > 0x2000) {
                   3329:                regen = 0x4000;
                   3330:                vram_pages = 2;
                   3331:        } else if(regen > 0x1000) {
                   3332:                regen = 0x2000;
                   3333:                vram_pages = 4;
                   3334:        } else {
                   3335:                regen = 0x1000;
                   3336:                vram_pages = 8;
                   3337:        }
1.1.1.15  root     3338:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   3339:        *(UINT16 *)(mem + 0x44c) = regen;
                   3340:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   3341:        
1.1.1.24  root     3342:        mouse.min_position.x = 0;
                   3343:        mouse.min_position.y = 0;
1.1.1.34  root     3344:        mouse.max_position.x = 8 * (scr_width  - 1);
                   3345:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     3346:        
1.1.1.15  root     3347:        restore_console_on_exit = true;
1.1.1.14  root     3348: }
                   3349: 
                   3350: void clear_scr_buffer(WORD attr)
                   3351: {
                   3352:        for(int y = 0; y < scr_height; y++) {
                   3353:                for(int x = 0; x < scr_width; x++) {
                   3354:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3355:                        SCR_BUF(y,x).Attributes = attr;
                   3356:                }
                   3357:        }
1.1.1.12  root     3358: }
                   3359: 
1.1.1.24  root     3360: bool update_console_input()
1.1       root     3361: {
1.1.1.35  root     3362: #ifdef USE_SERVICE_THREAD
                   3363:        EnterCriticalSection(&input_crit_sect);
                   3364: #endif
1.1.1.23  root     3365:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     3366:        DWORD dwNumberOfEvents = 0;
1.1       root     3367:        DWORD dwRead;
                   3368:        INPUT_RECORD ir[16];
1.1.1.24  root     3369:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   3370:        bool result = false;
1.1       root     3371:        
1.1.1.8   root     3372:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   3373:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   3374:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     3375:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.59  root     3376:                                        if(ir[i].Event.MouseEvent.dwEventFlags & MOUSE_MOVED) {
                   3377:                                                if(mouse.hidden == 0 || (mouse.call_addr_ps2.dw && mouse.enabled_ps2)) {
                   3378:                                                        // NOTE: if restore_console_on_exit, console is not scrolled
                   3379:                                                        if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   3380:                                                                GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   3381:                                                        }
                   3382:                                                        // FIXME: character size is always 8x8 ???
                   3383:                                                        int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   3384:                                                        int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
                   3385:                                                        
                   3386:                                                        if(mouse.position.x != x || mouse.position.y != y) {
                   3387:                                                                mouse.position.x = x;
                   3388:                                                                mouse.position.y = y;
                   3389:                                                                mouse.status |= 1;
                   3390:                                                                mouse.status_alt |= 1;
                   3391:                                                        }
1.1.1.34  root     3392:                                                }
1.1.1.59  root     3393:                                        } else if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   3394:                                                for(int j = 0; j < MAX_MOUSE_BUTTONS; j++) {
1.1.1.34  root     3395:                                                        static const DWORD bits[] = {
                   3396:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   3397:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
                   3398:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
                   3399:                                                        };
1.1.1.59  root     3400:                                                        bool prev_status = mouse.buttons[j].status;
                   3401:                                                        mouse.buttons[j].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[j]) != 0);
1.1.1.34  root     3402:                                                        
1.1.1.59  root     3403:                                                        if(!prev_status && mouse.buttons[j].status) {
                   3404:                                                                mouse.buttons[j].pressed_times++;
                   3405:                                                                mouse.buttons[j].pressed_position.x = mouse.position.x;
                   3406:                                                                mouse.buttons[j].pressed_position.y = mouse.position.x;
                   3407:                                                                if(j < 2) {
                   3408:                                                                        mouse.status_alt |= 2 << (j * 2);
1.1.1.43  root     3409:                                                                }
1.1.1.59  root     3410:                                                                mouse.status |= 2 << (j * 2);
                   3411:                                                        } else if(prev_status && !mouse.buttons[j].status) {
                   3412:                                                                mouse.buttons[j].released_times++;
                   3413:                                                                mouse.buttons[j].released_position.x = mouse.position.x;
                   3414:                                                                mouse.buttons[j].released_position.y = mouse.position.x;
                   3415:                                                                if(j < 2) {
                   3416:                                                                        mouse.status_alt |= 4 << (j * 2);
1.1.1.43  root     3417:                                                                }
1.1.1.59  root     3418:                                                                mouse.status |= 4 << (j * 2);
1.1.1.14  root     3419:                                                        }
                   3420:                                                }
                   3421:                                        }
1.1.1.24  root     3422:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3423:                                        // update keyboard flags in bios data area
1.1.1.35  root     3424:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
                   3425:                                                mem[0x417] |= 0x40;
1.1.1.33  root     3426:                                        } else {
1.1.1.35  root     3427:                                                mem[0x417] &= ~0x40;
1.1.1.33  root     3428:                                        }
1.1.1.35  root     3429:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
                   3430:                                                mem[0x417] |= 0x20;
1.1.1.33  root     3431:                                        } else {
1.1.1.35  root     3432:                                                mem[0x417] &= ~0x20;
                   3433:                                        }
                   3434:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
                   3435:                                                mem[0x417] |= 0x10;
                   3436:                                        } else {
                   3437:                                                mem[0x417] &= ~0x10;
1.1.1.33  root     3438:                                        }
                   3439:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43  root     3440:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3441:                                                        mouse.status_alt |= 0x80;
                   3442:                                                }
1.1.1.33  root     3443:                                                mem[0x417] |= 0x08;
                   3444:                                        } else {
                   3445:                                                mem[0x417] &= ~0x08;
                   3446:                                        }
1.1.1.35  root     3447:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43  root     3448:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3449:                                                        mouse.status_alt |= 0x40;
                   3450:                                                }
1.1.1.35  root     3451:                                                mem[0x417] |= 0x04;
1.1.1.33  root     3452:                                        } else {
1.1.1.35  root     3453:                                                mem[0x417] &= ~0x04;
1.1.1.33  root     3454:                                        }
                   3455:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43  root     3456:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3457:                                                        mouse.status_alt |= 0x20;
                   3458:                                                }
1.1.1.33  root     3459:                                                if(!(mem[0x417] & 0x03)) {
                   3460:                                                        mem[0x417] |= 0x02; // left shift
                   3461:                                                }
                   3462:                                        } else {
                   3463:                                                mem[0x417] &= ~0x03;
                   3464:                                        }
1.1.1.35  root     3465:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3466:                                                mem[0x418] |= 0x02;
                   3467:                                        } else {
                   3468:                                                mem[0x418] &= ~0x02;
                   3469:                                        }
                   3470:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3471:                                                mem[0x418] |= 0x01;
                   3472:                                        } else {
                   3473:                                                mem[0x418] &= ~0x01;
                   3474:                                        }
1.1.1.33  root     3475:                                        
1.1.1.28  root     3476:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.57  root     3477: //                                     kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
                   3478: //                                     kbd_status |= 1;
                   3479:                                        UINT8 tmp_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3480:                                        
                   3481:                                        // update dos key buffer
                   3482:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3483:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
1.1.1.51  root     3484:                                        UINT8 scn_old = scn;
1.1.1.33  root     3485:                                        
                   3486:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3487:                                                // make
1.1.1.57  root     3488:                                                tmp_data &= 0x7f;
1.1.1.24  root     3489:                                                
1.1.1.33  root     3490:                                                if(chr == 0x00) {
1.1.1.24  root     3491:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3492:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3493:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3494:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3495:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3496:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3497:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3498:                                                                } else if(scn == 0x35) {
                   3499:                                                                        scn = 0xa4;             // keypad /
                   3500:                                                                }
                   3501:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3502:                                                                if(scn == 0x07) {
                   3503:                                                                        chr = 0x1e;     // Ctrl+^
                   3504:                                                                } else if(scn == 0x0c) {
                   3505:                                                                        chr = 0x1f;     // Ctrl+_
                   3506:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3507:                                                                        static const UINT8 ctrl_map[] = {
                   3508:                                                                                0x95,   // keypad /
                   3509:                                                                                0,
                   3510:                                                                                0x96,   // keypad *
                   3511:                                                                                0, 0, 0,
                   3512:                                                                                0x5e,   // F1
                   3513:                                                                                0x5f,   // F2
                   3514:                                                                                0x60,   // F3
                   3515:                                                                                0x61,   // F4
                   3516:                                                                                0x62,   // F5
                   3517:                                                                                0x63,   // F6
                   3518:                                                                                0x64,   // F7
                   3519:                                                                                0x65,   // F8
                   3520:                                                                                0x66,   // F9
                   3521:                                                                                0x67,   // F10
                   3522:                                                                                0,
                   3523:                                                                                0,
                   3524:                                                                                0x77,   // Home
                   3525:                                                                                0x8d,   // Up
                   3526:                                                                                0x84,   // PgUp
                   3527:                                                                                0x8e,   // keypad -
                   3528:                                                                                0x73,   // Left
                   3529:                                                                                0x8f,   // keypad center
                   3530:                                                                                0x74,   // Right
                   3531:                                                                                0x90,   // keyapd +
                   3532:                                                                                0x75,   // End
                   3533:                                                                                0x91,   // Down
                   3534:                                                                                0x76,   // PgDn
                   3535:                                                                                0x92,   // Insert
                   3536:                                                                                0x93,   // Delete
                   3537:                                                                                0, 0, 0,
                   3538:                                                                                0x89,   // F11
                   3539:                                                                                0x8a,   // F12
                   3540:                                                                        };
                   3541:                                                                        scn = ctrl_map[scn - 0x35];
                   3542:                                                                }
                   3543:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3544:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3545:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3546:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3547:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3548:                                                                }
                   3549:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3550:                                                                scn += 0x85 - 0x57;
                   3551:                                                        }
                   3552:                                                        // ignore shift, ctrl, alt, win and menu keys
1.1.1.51  root     3553:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && !(scn >= 0x5b && scn <= 0x5d && scn == scn_old)) {
1.1.1.32  root     3554:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3555: #ifdef USE_SERVICE_THREAD
                   3556:                                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3557: #endif
1.1.1.32  root     3558:                                                                        if(chr == 0) {
1.1.1.51  root     3559:                                                                                pcbios_set_key_buffer(0x00, ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
1.1.1.32  root     3560:                                                                        }
1.1.1.51  root     3561:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3562: #ifdef USE_SERVICE_THREAD
                   3563:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3564: #endif
1.1.1.24  root     3565:                                                                }
                   3566:                                                        }
                   3567:                                                } else {
                   3568:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3569:                                                                chr = 0;
                   3570:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3571:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3572:                                                                }
                   3573:                                                        }
1.1.1.32  root     3574:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3575: #ifdef USE_SERVICE_THREAD
                   3576:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3577: #endif
1.1.1.51  root     3578:                                                                pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3579: #ifdef USE_SERVICE_THREAD
                   3580:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3581: #endif
1.1.1.32  root     3582:                                                        }
1.1.1.24  root     3583:                                                }
1.1.1.57  root     3584:                                        } else {
                   3585:                                                if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
                   3586:                                                        // ctrl-break, ctrl-c
                   3587:                                                        if(scn == 0x46) {
                   3588:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3589: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3590:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3591: #endif
1.1.1.57  root     3592:                                                                        pcbios_set_key_buffer(0x00, 0x00);
1.1.1.35  root     3593: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3594:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3595: #endif
1.1.1.57  root     3596:                                                                }
                   3597:                                                                ctrl_break_pressed = true;
                   3598:                                                                mem[0x471] = 0x80;
                   3599:                                                                raise_int_1bh = true;
                   3600:                                                        } else {
                   3601:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3602: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3603:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3604: #endif
1.1.1.57  root     3605:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3606: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3607:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3608: #endif
1.1.1.57  root     3609:                                                                }
                   3610:                                                                ctrl_c_pressed = (scn == 0x2e);
1.1.1.33  root     3611:                                                        }
                   3612:                                                }
                   3613:                                                // break
1.1.1.57  root     3614:                                                tmp_data |= 0x80;
                   3615:                                        }
                   3616:                                        if(!(kbd_status & 1)) {
                   3617:                                                kbd_data = tmp_data;
                   3618:                                                kbd_status |= 1;
                   3619:                                        } else {
                   3620:                                                if(key_buf_data != NULL) {
                   3621: #ifdef USE_SERVICE_THREAD
                   3622:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3623: #endif
                   3624:                                                        key_buf_data->write(tmp_data);
                   3625: #ifdef USE_SERVICE_THREAD
                   3626:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3627: #endif
                   3628:                                                }
1.1       root     3629:                                        }
1.1.1.24  root     3630:                                        result = key_changed = true;
1.1.1.36  root     3631:                                        // IME may be on and it may causes screen scroll up and cursor position change
                   3632:                                        cursor_moved = true;
1.1       root     3633:                                }
                   3634:                        }
                   3635:                }
                   3636:        }
1.1.1.35  root     3637: #ifdef USE_SERVICE_THREAD
                   3638:        LeaveCriticalSection(&input_crit_sect);
                   3639: #endif
1.1.1.24  root     3640:        return(result);
1.1.1.8   root     3641: }
                   3642: 
1.1.1.14  root     3643: bool update_key_buffer()
1.1.1.8   root     3644: {
1.1.1.35  root     3645:        if(update_console_input()) {
                   3646:                return(true);
                   3647:        }
                   3648:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3649: #ifdef USE_SERVICE_THREAD
                   3650:                EnterCriticalSection(&key_buf_crit_sect);
                   3651: #endif
1.1.1.55  root     3652:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     3653: #ifdef USE_SERVICE_THREAD
                   3654:                LeaveCriticalSection(&key_buf_crit_sect);
                   3655: #endif
                   3656:                if(!empty) return(true);
                   3657:        }
                   3658:        return(false);
1.1.1.8   root     3659: }
                   3660: 
1.1.1.20  root     3661: /* ----------------------------------------------------------------------------
                   3662:        MS-DOS virtual machine
                   3663: ---------------------------------------------------------------------------- */
                   3664: 
1.1.1.32  root     3665: static const struct {
1.1.1.33  root     3666:        char *name;
                   3667:        DWORD lcid;
                   3668:        char *std;
                   3669:        char *dlt;
                   3670: } tz_table[] = {
                   3671:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3672: //     0       GMT             Greenwich Mean Time             GMT0
                   3673:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3674:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3675:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3676:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3677: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3678:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3679:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3680: //     3       BST             Brazil Standard Time            BST3
                   3681:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3682:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3683:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3684: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3685:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3686: //     3       GST             Greenland Standard Time         GST3
                   3687:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3688: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3689:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3690: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3691:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3692: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3693:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3694:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3695: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3696:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3697:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3698:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3699: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3700:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3701: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3702:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3703: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3704:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3705: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3706:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3707:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3708:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3709: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3710:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3711: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3712:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3713:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3714:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3715: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3716:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3717:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3718: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3719: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3720:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3721: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3722:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3723:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3724: //     11      SST             Samoa Standard Time             SST11
                   3725:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3726: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3727:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3728: //     -10     GST             Guam Standard Time              GST-10
                   3729:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3730: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3731:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3732:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3733:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3734: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3735:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3736:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3737: //     -9      JST             Japan Standard Time             JST-9
                   3738:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3739: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3740:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3741:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3742: //     -8      HKT             Hong Kong Time                  HKT-8
                   3743:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3744: //     -8      CCT             China Coast Time                CCT-8
                   3745:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3746:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3747: //     -8      SST             Singapore Standard Time         SST-8
                   3748:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3749: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3750:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3751:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3752: //     -7:30   JT              Java Standard Time              JST-7:30
                   3753: //     -7      NST             North Sumatra Time              NST-7
                   3754:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3755: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3756:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3757: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3758:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3759: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3760:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3761:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3762: //     -2      EET             Eastern Europe Time             EET-2
                   3763:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3764:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3765:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3766:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3767: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3768:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3769: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3770: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3771: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3772: //     -1      CET     CES     Central European Time           CET-1CES
                   3773:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3774:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3775:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3776:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3777: //     -1      WAT             West African Time               WAT-1
                   3778:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3779:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3780: //     0       UTC             Universal Coordinated Time      UTC0
                   3781:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3782:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3783:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3784:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3785:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3786:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3787: };
                   3788: 
1.1.1.53  root     3789: // FIXME: consider to build on non-Japanese environment :-(
                   3790: // message_japanese string must be in shift-jis
                   3791: 
1.1.1.33  root     3792: static const struct {
1.1.1.32  root     3793:        UINT16 code;
                   3794:        char *message_english;
                   3795:        char *message_japanese;
                   3796: } standard_error_table[] = {
                   3797:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3798:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3799:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3800:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3801:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3802:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3803:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3804:        {0x08,  "Insufficient memory", "������������܂���."},
                   3805:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3806:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3807:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3808:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3809:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3810:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3811:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3812:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3813:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3814:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3815:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3816:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3817:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3818:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3819:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3820:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3821:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3822:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3823:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3824:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3825:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3826:        {0x1F,  "General failure", "�G���[�ł�."},
                   3827:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3828:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3829:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3830:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3831:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3832:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3833:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3834:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3835: /*
                   3836:        {0x32,  "Network request not supported", NULL},
                   3837:        {0x33,  "Remote computer not listening", NULL},
                   3838:        {0x34,  "Duplicate name on network", NULL},
                   3839:        {0x35,  "Network name not found", NULL},
                   3840:        {0x36,  "Network busy", NULL},
                   3841:        {0x37,  "Network device no longer exists", NULL},
                   3842:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3843:        {0x39,  "Network adapter hardware error", NULL},
                   3844:        {0x3A,  "Incorrect response from network", NULL},
                   3845:        {0x3B,  "Unexpected network error", NULL},
                   3846:        {0x3C,  "Incompatible remote adapter", NULL},
                   3847:        {0x3D,  "Print queue full", NULL},
                   3848:        {0x3E,  "Queue not full", NULL},
                   3849:        {0x3F,  "Not enough space to print file", NULL},
                   3850:        {0x40,  "Network name was deleted", NULL},
                   3851:        {0x41,  "Network: Access denied", NULL},
                   3852:        {0x42,  "Network device type incorrect", NULL},
                   3853:        {0x43,  "Network name not found", NULL},
                   3854:        {0x44,  "Network name limit exceeded", NULL},
                   3855:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3856:        {0x46,  "Temporarily paused", NULL},
                   3857:        {0x47,  "Network request not accepted", NULL},
                   3858:        {0x48,  "Network print/disk redirection paused", NULL},
                   3859:        {0x49,  "Network software not installed", NULL},
                   3860:        {0x4A,  "Unexpected adapter close", NULL},
                   3861: */
                   3862:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3863:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3864:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3865:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3866:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3867:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3868:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3869:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3870:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3871:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
1.1.1.53  root     3872: #ifdef SUPPORT_MSCDEX
1.1.1.32  root     3873:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3874:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3875:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3876:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3877:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
1.1.1.53  root     3878: #endif
1.1.1.32  root     3879:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3880:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3881:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3882:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3883:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3884:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3885: };
                   3886: 
                   3887: static const struct {
                   3888:        UINT16 code;
                   3889:        char *message_english;
                   3890:        char *message_japanese;
                   3891: } param_error_table[] = {
                   3892:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3893:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3894:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3895:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3896:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3897:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3898:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3899:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3900:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3901:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3902:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3903: };
                   3904: 
                   3905: static const struct {
                   3906:        UINT16 code;
                   3907:        char *message_english;
                   3908:        char *message_japanese;
                   3909: } critical_error_table[] = {
                   3910:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3911:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3912:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3913:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3914:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3915:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3916:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3917:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3918:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3919:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3920:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3921:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3922:        {0x0C,  "General failure", "�G���[�ł�."},
                   3923:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3924:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3925:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3926:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3927:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3928:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3929:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3930:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3931:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3932: };
                   3933: 
1.1.1.20  root     3934: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3935: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3936: void msdos_putch(UINT8 data);
1.1.1.50  root     3937: void msdos_putch_fast(UINT8 data);
1.1.1.35  root     3938: #ifdef USE_SERVICE_THREAD
                   3939: void msdos_putch_tmp(UINT8 data);
                   3940: #endif
1.1.1.45  root     3941: const char *msdos_short_path(const char *path);
1.1.1.44  root     3942: bool msdos_is_valid_drive(int drv);
                   3943: bool msdos_is_removable_drive(int drv);
                   3944: bool msdos_is_cdrom_drive(int drv);
                   3945: bool msdos_is_remote_drive(int drv);
                   3946: bool msdos_is_subst_drive(int drv);
1.1.1.20  root     3947: 
1.1       root     3948: // process info
                   3949: 
                   3950: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3951: {
                   3952:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3953:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3954:                        memset(&process[i], 0, sizeof(process_t));
                   3955:                        process[i].psp = psp_seg;
                   3956:                        return(&process[i]);
                   3957:                }
                   3958:        }
                   3959:        fatalerror("too many processes\n");
                   3960:        return(NULL);
                   3961: }
                   3962: 
1.1.1.52  root     3963: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error = true)
1.1       root     3964: {
                   3965:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3966:                if(process[i].psp == psp_seg) {
                   3967:                        return(&process[i]);
                   3968:                }
                   3969:        }
1.1.1.33  root     3970:        if(show_error) {
                   3971:                fatalerror("invalid psp address\n");
                   3972:        }
1.1       root     3973:        return(NULL);
                   3974: }
                   3975: 
1.1.1.23  root     3976: void msdos_sda_update(int psp_seg)
                   3977: {
                   3978:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3979:        
                   3980:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3981:                if(process[i].psp == psp_seg) {
                   3982:                        sda->switchar = process[i].switchar;
                   3983:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3984:                        sda->current_dta.w.h = process[i].dta.w.h;
                   3985:                        sda->current_psp = process[i].psp;
                   3986:                        break;
                   3987:                }
                   3988:        }
                   3989:        sda->malloc_strategy = malloc_strategy;
                   3990:        sda->return_code = retval;
                   3991:        sda->current_drive = _getdrive();
                   3992: }
                   3993: 
1.1.1.13  root     3994: // dta info
                   3995: 
                   3996: void msdos_dta_info_init()
                   3997: {
1.1.1.14  root     3998:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     3999:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   4000:        }
                   4001: }
                   4002: 
                   4003: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   4004: {
                   4005:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     4006:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   4007:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   4008:                        if(free_dta == NULL) {
1.1.1.13  root     4009:                                free_dta = &dtalist[i];
                   4010:                        }
1.1.1.14  root     4011:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     4012:                        return(&dtalist[i]);
                   4013:                }
                   4014:        }
1.1.1.14  root     4015:        if(free_dta) {
1.1.1.13  root     4016:                free_dta->psp = psp_seg;
                   4017:                free_dta->dta = dta_laddr;
                   4018:                return(free_dta);
                   4019:        }
                   4020:        fatalerror("too many dta\n");
                   4021:        return(NULL);
                   4022: }
                   4023: 
                   4024: void msdos_dta_info_free(UINT16 psp_seg)
                   4025: {
1.1.1.14  root     4026:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   4027:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     4028:                        FindClose(dtalist[i].find_handle);
                   4029:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   4030:                }
                   4031:        }
                   4032: }
                   4033: 
1.1       root     4034: void msdos_cds_update(int drv)
                   4035: {
1.1.1.44  root     4036:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1       root     4037:        
1.1.1.44  root     4038:        memset(cds, 0, 88);
                   4039:        
                   4040:        if(msdos_is_valid_drive(drv)) {
                   4041:                char path[MAX_PATH];
                   4042:                if(msdos_is_remote_drive(drv)) {
                   4043:                        cds->drive_attrib = 0xc000;     // network drive
                   4044:                } else if(msdos_is_subst_drive(drv)) {
                   4045:                        cds->drive_attrib = 0x5000;     // subst drive
                   4046:                } else {
                   4047:                        cds->drive_attrib = 0x4000;     // physical drive
                   4048:                }
                   4049:                if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
                   4050:                        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
                   4051:                }
                   4052:        }
                   4053:        if(cds->path_name[0] == '\0') {
                   4054:                sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   4055:        }
                   4056:        cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   4057:        cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
                   4058:        cds->word_1 = cds->word_2 = 0xffff;
                   4059:        cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
                   4060:        cds->bs_offset = 2;
                   4061: }
                   4062: 
1.1.1.45  root     4063: void msdos_cds_update(int drv, const char *path)
1.1.1.44  root     4064: {
                   4065:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
                   4066:        
                   4067:        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1       root     4068: }
                   4069: 
1.1.1.17  root     4070: // nls information tables
                   4071: 
                   4072: // uppercase table (func 6502h)
                   4073: void msdos_upper_table_update()
                   4074: {
                   4075:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     4076:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     4077:                UINT8 c[4];
1.1.1.33  root     4078:                *(UINT32 *)c = 0;               // reset internal conversion state
                   4079:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     4080:                c[0] = 0x80 + i;
                   4081:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   4082:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   4083:        }
                   4084: }
                   4085: 
1.1.1.23  root     4086: // lowercase table (func 6503h)
                   4087: void msdos_lower_table_update()
                   4088: {
                   4089:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   4090:        for(unsigned i = 0; i < 0x80; ++i) {
                   4091:                UINT8 c[4];
1.1.1.33  root     4092:                *(UINT32 *)c = 0;               // reset internal conversion state
                   4093:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     4094:                c[0] = 0x80 + i;
                   4095:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   4096:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   4097:        }
                   4098: }
                   4099: 
1.1.1.17  root     4100: // filename uppercase table (func 6504h)
                   4101: void msdos_filename_upper_table_init()
                   4102: {
                   4103:        // depended on (file)system, not on active codepage
                   4104:        // temporary solution: just filling data
                   4105:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     4106:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     4107:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   4108:        }
                   4109: }
                   4110: 
                   4111: // filaname terminator table (func 6505h)
                   4112: void msdos_filename_terminator_table_init()
                   4113: {
                   4114:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   4115:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   4116:        
                   4117:        data[2] = 1;            // marker? (permissible character value)
                   4118:        data[3] = 0x00;         // 00h...FFh
                   4119:        data[4] = 0xff;
                   4120:        data[5] = 0;            // marker? (excluded character)
                   4121:        data[6] = 0x00;         // 00h...20h
                   4122:        data[7] = 0x20;
                   4123:        data[8] = 2;            // marker? (illegal characters for filename)
                   4124:        data[9] = (UINT8)strlen(illegal_chars);
                   4125:        memcpy(data + 10, illegal_chars, data[9]);
                   4126:        
                   4127:        // total length
                   4128:        *(UINT16 *)data = (10 - 2) + data[9];
                   4129: }
                   4130: 
                   4131: // collating table (func 6506h)
                   4132: void msdos_collating_table_update()
                   4133: {
                   4134:        // temporary solution: just filling data
                   4135:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     4136:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     4137:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   4138:        }
                   4139: }
                   4140: 
1.1       root     4141: // dbcs
                   4142: 
                   4143: void msdos_dbcs_table_update()
                   4144: {
                   4145:        UINT8 dbcs_data[DBCS_SIZE];
                   4146:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   4147:        
                   4148:        CPINFO info;
                   4149:        GetCPInfo(active_code_page, &info);
                   4150:        
                   4151:        if(info.MaxCharSize != 1) {
                   4152:                for(int i = 0;; i += 2) {
                   4153:                        UINT8 lo = info.LeadByte[i + 0];
                   4154:                        UINT8 hi = info.LeadByte[i + 1];
                   4155:                        dbcs_data[2 + i + 0] = lo;
                   4156:                        dbcs_data[2 + i + 1] = hi;
                   4157:                        if(lo == 0 && hi == 0) {
                   4158:                                dbcs_data[0] = i + 2;
                   4159:                                break;
                   4160:                        }
                   4161:                }
                   4162:        } else {
                   4163:                dbcs_data[0] = 2;       // ???
                   4164:        }
                   4165:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   4166: }
                   4167: 
1.1.1.17  root     4168: void msdos_dbcs_table_finish()
                   4169: {
1.1.1.32  root     4170:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     4171:                _setmbcp(system_code_page);
                   4172:        }
1.1.1.32  root     4173:        if(console_code_page != GetConsoleCP()) {
                   4174:                SetConsoleCP(console_code_page);
                   4175:                SetConsoleOutputCP(console_code_page);
                   4176:        }
1.1.1.17  root     4177: }
                   4178: 
                   4179: void msdos_nls_tables_init()
1.1       root     4180: {
1.1.1.32  root     4181:        active_code_page = console_code_page = GetConsoleCP();
                   4182:        system_code_page = _getmbcp();
                   4183:        
                   4184:        if(active_code_page != system_code_page) {
                   4185:                if(_setmbcp(active_code_page) != 0) {
                   4186:                        active_code_page = system_code_page;
                   4187:                }
                   4188:        }
                   4189:        
1.1.1.17  root     4190:        msdos_upper_table_update();
1.1.1.23  root     4191:        msdos_lower_table_update();
1.1.1.17  root     4192:        msdos_filename_terminator_table_init();
                   4193:        msdos_filename_upper_table_init();
                   4194:        msdos_collating_table_update();
1.1       root     4195:        msdos_dbcs_table_update();
                   4196: }
                   4197: 
1.1.1.17  root     4198: void msdos_nls_tables_update()
1.1       root     4199: {
1.1.1.17  root     4200:        msdos_dbcs_table_update();
                   4201:        msdos_upper_table_update();
1.1.1.23  root     4202:        msdos_lower_table_update();
                   4203: //     msdos_collating_table_update();
1.1       root     4204: }
                   4205: 
                   4206: int msdos_lead_byte_check(UINT8 code)
                   4207: {
                   4208:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   4209:        
                   4210:        for(int i = 0;; i += 2) {
                   4211:                UINT8 lo = dbcs_table[i + 0];
                   4212:                UINT8 hi = dbcs_table[i + 1];
                   4213:                if(lo == 0 && hi == 0) {
                   4214:                        break;
                   4215:                }
                   4216:                if(lo <= code && code <= hi) {
                   4217:                        return(1);
                   4218:                }
                   4219:        }
                   4220:        return(0);
                   4221: }
                   4222: 
1.1.1.20  root     4223: int msdos_ctrl_code_check(UINT8 code)
                   4224: {
1.1.1.22  root     4225:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     4226: }
                   4227: 
1.1.1.36  root     4228: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
                   4229: {
                   4230:        int is_kanji_1st = 0;
                   4231:        int is_kanji_2nd = 0;
                   4232:        
                   4233:        for(int p = 0;; p++) {
                   4234:                if(is_kanji_1st) {
                   4235:                        is_kanji_1st = 0;
                   4236:                        is_kanji_2nd = 1;
                   4237:                } else if(msdos_lead_byte_check(buf[p])) {
                   4238:                        is_kanji_1st = 1;
                   4239:                }
                   4240:                if(p == n) {
                   4241:                        return(is_kanji_2nd);
                   4242:                }
                   4243:                is_kanji_2nd = 0;
                   4244:        }
                   4245: }
                   4246: 
1.1       root     4247: // file control
                   4248: 
1.1.1.45  root     4249: const char *msdos_remove_double_quote(const char *path)
1.1.1.14  root     4250: {
                   4251:        static char tmp[MAX_PATH];
                   4252:        
                   4253:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45  root     4254:                memset(tmp, 0, sizeof(tmp));
1.1.1.14  root     4255:                memcpy(tmp, path + 1, strlen(path) - 2);
                   4256:        } else {
                   4257:                strcpy(tmp, path);
                   4258:        }
                   4259:        return(tmp);
                   4260: }
                   4261: 
1.1.1.45  root     4262: const char *msdos_remove_end_separator(const char *path)
1.1.1.32  root     4263: {
                   4264:        static char tmp[MAX_PATH];
                   4265:        
                   4266:        strcpy(tmp, path);
1.1.1.45  root     4267:        
                   4268:        // for example "C:\" case, the end separator should not be removed
                   4269:        if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
                   4270:                tmp[strlen(tmp) - 1] = '\0';
1.1.1.32  root     4271:        }
                   4272:        return(tmp);
                   4273: }
                   4274: 
1.1.1.45  root     4275: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14  root     4276: {
                   4277:        static char tmp[MAX_PATH];
1.1.1.45  root     4278:        const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14  root     4279:        
                   4280:        if(strlen(tmp_dir) == 0) {
                   4281:                strcpy(tmp, file);
                   4282:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   4283:                sprintf(tmp, "%s%s", tmp_dir, file);
                   4284:        } else {
                   4285:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   4286:        }
                   4287:        return(tmp);
                   4288: }
                   4289: 
1.1.1.45  root     4290: const char *msdos_trimmed_path(const char *path, int lfn)
1.1       root     4291: {
                   4292:        static char tmp[MAX_PATH];
                   4293:        
                   4294:        if(lfn) {
                   4295:                strcpy(tmp, path);
                   4296:        } else {
                   4297:                // remove space in the path
1.1.1.45  root     4298:                const char *src = path;
                   4299:                char *dst = tmp;
1.1       root     4300:                
                   4301:                while(*src != '\0') {
                   4302:                        if(msdos_lead_byte_check(*src)) {
                   4303:                                *dst++ = *src++;
                   4304:                                *dst++ = *src++;
                   4305:                        } else if(*src != ' ') {
                   4306:                                *dst++ = *src++;
                   4307:                        } else {
                   4308:                                src++;  // skip space
                   4309:                        }
                   4310:                }
                   4311:                *dst = '\0';
                   4312:        }
1.1.1.14  root     4313:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   4314:                // redirect C:\COMMAND.COM to comspec_path
                   4315:                strcpy(tmp, comspec_path);
                   4316:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   4317:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   4318:                static int root_drive_protected = -1;
                   4319:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   4320:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   4321:                
1.1.1.60  root     4322:                if(GetFullPathNameA(tmp, MAX_PATH, temp, &name_temp) != 0 &&
1.1.1.14  root     4323:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   4324:                        strcpy(name, name_temp);
                   4325:                        name_temp[0] = '\0';
                   4326:                        
                   4327:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   4328:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   4329:                                if(root_drive_protected == -1) {
                   4330:                                        FILE *fp = NULL;
                   4331:                                        
                   4332:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   4333:                                        root_drive_protected = 1;
                   4334:                                        try {
                   4335:                                                if((fp = fopen(temp, "w")) != NULL) {
                   4336:                                                        if(fprintf(fp, "TEST") == 4) {
                   4337:                                                                root_drive_protected = 0;
                   4338:                                                        }
                   4339:                                                }
                   4340:                                        } catch(...) {
                   4341:                                        }
                   4342:                                        if(fp != NULL) {
                   4343:                                                fclose(fp);
                   4344:                                        }
                   4345:                                        if(_access(temp, 0) == 0) {
                   4346:                                                remove(temp);
                   4347:                                        }
                   4348:                                }
                   4349:                                if(root_drive_protected == 1) {
1.1.1.60  root     4350:                                        if(GetEnvironmentVariableA("TEMP", temp, MAX_PATH) != 0 ||
                   4351:                                           GetEnvironmentVariableA("TMP",  temp, MAX_PATH) != 0) {
1.1.1.14  root     4352:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   4353:                                        }
                   4354:                                }
                   4355:                        }
                   4356:                }
                   4357:        }
1.1       root     4358:        return(tmp);
                   4359: }
                   4360: 
1.1.1.45  root     4361: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28  root     4362: {
1.1.1.32  root     4363:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     4364:        static char env_path[ENV_SIZE];
                   4365:        char tmp[ENV_SIZE], *token;
                   4366:        
                   4367:        memset(env_path, 0, sizeof(env_path));
                   4368:        strcpy(tmp, src);
                   4369:        token = my_strtok(tmp, ";");
                   4370:        
                   4371:        while(token != NULL) {
                   4372:                if(token[0] != '\0') {
1.1.1.45  root     4373:                        const char *path = msdos_remove_double_quote(token);
                   4374:                        char short_path[MAX_PATH];
1.1.1.32  root     4375:                        if(path != NULL && strlen(path) != 0) {
                   4376:                                if(env_path[0] != '\0') {
                   4377:                                        strcat(env_path, ";");
                   4378:                                }
1.1.1.60  root     4379:                                if(GetShortPathNameA(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     4380:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     4381:                                } else {
                   4382:                                        my_strupr(short_path);
1.1.1.32  root     4383:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     4384:                                }
                   4385:                        }
                   4386:                }
                   4387:                token = my_strtok(NULL, ";");
                   4388:        }
                   4389:        return(env_path);
                   4390: }
                   4391: 
1.1.1.45  root     4392: bool match(const char *text, const char *pattern)
1.1       root     4393: {
1.1.1.24  root     4394:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     4395:        switch(*pattern) {
1.1       root     4396:        case '\0':
                   4397:                return !*text;
                   4398:        case '*':
1.1.1.14  root     4399:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     4400:        case '?':
                   4401:                return *text && match(text + 1, pattern + 1);
                   4402:        default:
                   4403:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   4404:        }
                   4405: }
                   4406: 
1.1.1.45  root     4407: bool msdos_match_volume_label(const char *path, const char *volume)
1.1       root     4408: {
1.1.1.45  root     4409:        const char *p = NULL;
1.1       root     4410:        
1.1.1.14  root     4411:        if(!*volume) {
                   4412:                return false;
                   4413:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     4414:                return msdos_match_volume_label(p + 1, volume);
                   4415:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   4416:                return msdos_match_volume_label(p + 1, volume);
                   4417:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     4418:                char tmp[MAX_PATH];
                   4419:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   4420:                return match(volume, tmp);
1.1       root     4421:        } else {
                   4422:                return match(volume, path);
                   4423:        }
                   4424: }
                   4425: 
1.1.1.45  root     4426: const char *msdos_fcb_path(fcb_t *fcb)
1.1       root     4427: {
                   4428:        static char tmp[MAX_PATH];
                   4429:        char name[9], ext[4];
                   4430:        
                   4431:        memset(name, 0, sizeof(name));
                   4432:        memcpy(name, fcb->file_name, 8);
                   4433:        strcpy(name, msdos_trimmed_path(name, 0));
                   4434:        
                   4435:        memset(ext, 0, sizeof(ext));
                   4436:        memcpy(ext, fcb->file_name + 8, 3);
                   4437:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4438:        
                   4439:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4440:                strcpy(name, "*");
                   4441:        }
                   4442:        if(ext[0] == '\0') {
                   4443:                strcpy(tmp, name);
                   4444:        } else {
                   4445:                if(strcmp(ext, "???") == 0) {
                   4446:                        strcpy(ext, "*");
                   4447:                }
                   4448:                sprintf(tmp, "%s.%s", name, ext);
                   4449:        }
                   4450:        return(tmp);
                   4451: }
                   4452: 
1.1.1.45  root     4453: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1       root     4454: {
1.1.1.60  root     4455:        char tmp[MAX_PATH];
                   4456:        strcpy(tmp, path);
                   4457:        char *ext = my_strchr(tmp, '.');
1.1       root     4458:        
                   4459:        memset(fcb->file_name, 0x20, 8 + 3);
1.1.1.60  root     4460:        if(ext != NULL && tmp[0] != '.') {
1.1       root     4461:                *ext = '\0';
                   4462:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4463:        }
1.1.1.60  root     4464:        memcpy(fcb->file_name, tmp, strlen(tmp));
1.1       root     4465: }
                   4466: 
1.1.1.45  root     4467: const char *msdos_short_path(const char *path)
1.1       root     4468: {
                   4469:        static char tmp[MAX_PATH];
                   4470:        
1.1.1.60  root     4471:        if(GetShortPathNameA(path, tmp, MAX_PATH) == 0) {
1.1.1.24  root     4472:                strcpy(tmp, path);
                   4473:        }
1.1       root     4474:        my_strupr(tmp);
                   4475:        return(tmp);
                   4476: }
                   4477: 
1.1.1.60  root     4478: const char *msdos_short_name(WIN32_FIND_DATAA *fd)
1.1.1.13  root     4479: {
                   4480:        static char tmp[MAX_PATH];
1.1.1.45  root     4481:        
1.1.1.14  root     4482:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4483:                strcpy(tmp, fd->cAlternateFileName);
                   4484:        } else {
                   4485:                strcpy(tmp, fd->cFileName);
                   4486:        }
                   4487:        my_strupr(tmp);
                   4488:        return(tmp);
                   4489: }
                   4490: 
1.1.1.45  root     4491: const char *msdos_short_full_path(const char *path)
1.1       root     4492: {
                   4493:        static char tmp[MAX_PATH];
                   4494:        char full[MAX_PATH], *name;
                   4495:        
1.1.1.14  root     4496:        // Full works with non-existent files, but Short does not
1.1.1.60  root     4497:        GetFullPathNameA(path, MAX_PATH, full, &name);
1.1.1.14  root     4498:        *tmp = '\0';
1.1.1.60  root     4499:        if(GetShortPathNameA(full, tmp, MAX_PATH) == 0 && name > path) {
1.1.1.14  root     4500:                name[-1] = '\0';
1.1.1.60  root     4501:                DWORD len = GetShortPathNameA(full, tmp, MAX_PATH);
1.1.1.14  root     4502:                if(len == 0) {
                   4503:                        strcpy(tmp, full);
                   4504:                } else {
                   4505:                        tmp[len++] = '\\';
                   4506:                        strcpy(tmp + len, name);
                   4507:                }
                   4508:        }
1.1       root     4509:        my_strupr(tmp);
                   4510:        return(tmp);
                   4511: }
                   4512: 
1.1.1.45  root     4513: const char *msdos_short_full_dir(const char *path)
1.1       root     4514: {
                   4515:        static char tmp[MAX_PATH];
                   4516:        char full[MAX_PATH], *name;
                   4517:        
1.1.1.60  root     4518:        GetFullPathNameA(path, MAX_PATH, full, &name);
1.1       root     4519:        name[-1] = '\0';
1.1.1.60  root     4520:        if(GetShortPathNameA(full, tmp, MAX_PATH) == 0) {
1.1.1.24  root     4521:                strcpy(tmp, full);
                   4522:        }
1.1       root     4523:        my_strupr(tmp);
                   4524:        return(tmp);
                   4525: }
                   4526: 
1.1.1.45  root     4527: const char *msdos_local_file_path(const char *path, int lfn)
1.1       root     4528: {
1.1.1.45  root     4529:        static char trimmed[MAX_PATH];
                   4530:        
                   4531:        strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14  root     4532: #if 0
                   4533:        // I have forgotten the reason of this routine... :-(
1.1       root     4534:        if(_access(trimmed, 0) != 0) {
                   4535:                process_t *process = msdos_process_info_get(current_psp);
                   4536:                static char tmp[MAX_PATH];
                   4537:                
                   4538:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4539:                if(_access(tmp, 0) == 0) {
                   4540:                        return(tmp);
                   4541:                }
                   4542:        }
1.1.1.14  root     4543: #endif
1.1       root     4544:        return(trimmed);
                   4545: }
                   4546: 
1.1.1.45  root     4547: bool msdos_is_device_path(const char *path)
1.1.1.11  root     4548: {
                   4549:        char full[MAX_PATH], *name;
                   4550:        
1.1.1.60  root     4551:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4552:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4553:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4554:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4555:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4556:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4557:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4558:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4559:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4560:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4561:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4562:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4563:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4564:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4565:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4566:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4567:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4568:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4569:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4570:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4571:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4572:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4573:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4574:                        return(true);
                   4575:                } else if(name != NULL) {
                   4576:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4577:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4578:                           _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43  root     4579: //                        _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30  root     4580:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4581:                                return(true);
                   4582:                        }
                   4583:                }
1.1.1.24  root     4584:        }
                   4585:        return(false);
1.1.1.11  root     4586: }
                   4587: 
1.1.1.45  root     4588: bool msdos_is_con_path(const char *path)
1.1.1.8   root     4589: {
1.1.1.14  root     4590:        char full[MAX_PATH], *name;
1.1.1.8   root     4591:        
1.1.1.60  root     4592:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4593:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4594:        }
                   4595:        return(false);
                   4596: }
                   4597: 
1.1.1.45  root     4598: int msdos_is_comm_path(const char *path)
1.1.1.24  root     4599: {
                   4600:        char full[MAX_PATH], *name;
                   4601:        
1.1.1.60  root     4602:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4603:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4604:                        return(1);
                   4605:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4606:                        return(2);
                   4607:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4608:                        return(3);
                   4609:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4610:                        return(4);
1.1.1.24  root     4611:                }
                   4612:        }
1.1.1.29  root     4613:        return(0);
                   4614: }
                   4615: 
1.1.1.45  root     4616: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37  root     4617: {
                   4618:        // 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     4619:        const char *p = NULL;
1.1.1.37  root     4620:        
                   4621:        if((p = strstr(path, ":")) != NULL) {
                   4622:                UINT8 selector = sio_read(sio_port - 1, 3);
                   4623:                
                   4624:                // baud rate
                   4625:                int baud = max(110, min(9600, atoi(p + 1)));
                   4626:                UINT16 divisor = 115200 / baud;
                   4627:                
                   4628:                if((p = strstr(p + 1, ",")) != NULL) {
                   4629:                        // parity
                   4630:                        if(p[1] == 'N' || p[1] == 'n') {
                   4631:                                selector = (selector & ~0x38) | 0x00;
                   4632:                        } else if(p[1] == 'O' || p[1] == 'o') {
                   4633:                                selector = (selector & ~0x38) | 0x08;
                   4634:                        } else if(p[1] == 'E' || p[1] == 'e') {
                   4635:                                selector = (selector & ~0x38) | 0x18;
                   4636:                        } else if(p[1] == 'M' || p[1] == 'm') {
                   4637:                                selector = (selector & ~0x38) | 0x28;
                   4638:                        } else if(p[1] == 'S' || p[1] == 's') {
                   4639:                                selector = (selector & ~0x38) | 0x38;
                   4640:                        }
                   4641:                        if((p = strstr(p + 1, ",")) != NULL) {
                   4642:                                // word length
                   4643:                                if(p[1] == '8') {
                   4644:                                        selector = (selector & ~0x03) | 0x03;
                   4645:                                } else if(p[1] == '7') {
                   4646:                                        selector = (selector & ~0x03) | 0x02;
                   4647:                                } else if(p[1] == '6') {
                   4648:                                        selector = (selector & ~0x03) | 0x01;
                   4649:                                } else if(p[1] == '5') {
                   4650:                                        selector = (selector & ~0x03) | 0x00;
                   4651:                                }
                   4652:                                if((p = strstr(p + 1, ",")) != NULL) {
                   4653:                                        // stop bits
                   4654:                                        float bits = atof(p + 1);
                   4655:                                        if(bits > 1.0F) {
                   4656:                                                selector |= 0x04;
                   4657:                                        } else {
                   4658:                                                selector &= ~0x04;
                   4659:                                        }
                   4660:                                }
                   4661:                        }
                   4662:                }
                   4663:                sio_write(sio_port - 1, 3, selector | 0x80);
                   4664:                sio_write(sio_port - 1, 0, divisor & 0xff);
                   4665:                sio_write(sio_port - 1, 1, divisor >> 8);
                   4666:                sio_write(sio_port - 1, 3, selector);
                   4667:        }
                   4668: }
                   4669: 
1.1.1.45  root     4670: int msdos_is_prn_path(const char *path)
1.1.1.30  root     4671: {
                   4672:        char full[MAX_PATH], *name;
                   4673:        
1.1.1.60  root     4674:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.30  root     4675:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4676:                        return(1);
                   4677:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4678:                        return(1);
                   4679:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4680:                        return(2);
                   4681:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4682:                        return(3);
                   4683:                }
                   4684:        }
                   4685:        return(0);
                   4686: }
                   4687: 
1.1.1.44  root     4688: bool msdos_is_valid_drive(int drv)
                   4689: {
                   4690:        return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
                   4691: }
                   4692: 
                   4693: bool msdos_is_removable_drive(int drv)
                   4694: {
                   4695:        char volume[] = "A:\\";
                   4696:        
                   4697:        volume[0] = 'A' + drv;
                   4698:        
1.1.1.60  root     4699:        return(GetDriveTypeA(volume) == DRIVE_REMOVABLE);
1.1.1.44  root     4700: }
                   4701: 
                   4702: bool msdos_is_cdrom_drive(int drv)
                   4703: {
                   4704:        char volume[] = "A:\\";
                   4705:        
                   4706:        volume[0] = 'A' + drv;
                   4707:        
1.1.1.60  root     4708:        return(GetDriveTypeA(volume) == DRIVE_CDROM);
1.1.1.44  root     4709: }
                   4710: 
                   4711: bool msdos_is_remote_drive(int drv)
                   4712: {
                   4713:        char volume[] = "A:\\";
                   4714:        
                   4715:        volume[0] = 'A' + drv;
                   4716:        
1.1.1.60  root     4717:        return(GetDriveTypeA(volume) == DRIVE_REMOTE);
1.1.1.44  root     4718: }
                   4719: 
                   4720: bool msdos_is_subst_drive(int drv)
                   4721: {
                   4722:        char device[] = "A:", path[MAX_PATH];
                   4723:        
                   4724:        device[0] = 'A' + drv;
                   4725:        
1.1.1.60  root     4726:        if(QueryDosDeviceA(device, path, MAX_PATH)) {
1.1.1.44  root     4727:                if(strncmp(path, "\\??\\", 4) == 0) {
                   4728:                        return(true);
                   4729:                }
                   4730:        }
                   4731:        return(false);
                   4732: }
                   4733: 
1.1.1.45  root     4734: bool msdos_is_existing_file(const char *path)
1.1.1.24  root     4735: {
                   4736:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
1.1.1.60  root     4737:        WIN32_FIND_DATAA fd;
1.1.1.24  root     4738:        HANDLE hFind;
                   4739:        
1.1.1.60  root     4740:        if((hFind = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.24  root     4741:                FindClose(hFind);
1.1.1.63! root     4742:                return((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0);
        !          4743:        }
        !          4744:        return(false);
        !          4745: }
        !          4746: 
        !          4747: bool msdos_is_existing_dir(const char *path)
        !          4748: {
        !          4749:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
        !          4750:        WIN32_FIND_DATAA fd;
        !          4751:        HANDLE hFind;
        !          4752:        
        !          4753:        if((hFind = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
        !          4754:                FindClose(hFind);
        !          4755:                return((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
1.1.1.24  root     4756:        }
                   4757:        return(false);
1.1.1.8   root     4758: }
                   4759: 
1.1.1.45  root     4760: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9   root     4761: {
                   4762:        static char tmp[MAX_PATH];
1.1.1.28  root     4763:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4764:        
1.1.1.28  root     4765:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.60  root     4766:        if(GetFullPathNameA(command_path, MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4767:                sprintf(file_name, "COMMAND.COM");
                   4768:                if(_access(tmp, 0) == 0) {
                   4769:                        return(tmp);
                   4770:                }
                   4771:        }
1.1.1.28  root     4772:        
                   4773:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.60  root     4774:        if(GetModuleFileNameA(NULL, path, MAX_PATH) != 0 && GetFullPathNameA(path, MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4775:                sprintf(file_name, "COMMAND.COM");
                   4776:                if(_access(tmp, 0) == 0) {
                   4777:                        return(tmp);
                   4778:                }
                   4779:        }
1.1.1.28  root     4780:        
                   4781:        // check if COMMAND.COM is in the current directory
1.1.1.60  root     4782:        if(GetFullPathNameA("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4783:                if(_access(tmp, 0) == 0) {
                   4784:                        return(tmp);
                   4785:                }
                   4786:        }
1.1.1.28  root     4787:        
                   4788:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4789:        strcpy(path, env_path);
                   4790:        char *token = my_strtok(path, ";");
1.1.1.9   root     4791:        while(token != NULL) {
1.1.1.14  root     4792:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4793:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4794:                        if(_access(tmp, 0) == 0) {
                   4795:                                return(tmp);
                   4796:                        }
                   4797:                }
                   4798:                token = my_strtok(NULL, ";");
                   4799:        }
                   4800:        return(NULL);
                   4801: }
                   4802: 
1.1.1.14  root     4803: int msdos_drive_number(const char *path)
1.1       root     4804: {
                   4805:        char tmp[MAX_PATH], *name;
                   4806:        
1.1.1.60  root     4807:        if(GetFullPathNameA(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
1.1.1.45  root     4808:                if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4809:                        return(tmp[0] - 'a');
                   4810:                } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
                   4811:                        return(tmp[0] - 'A');
                   4812:                }
1.1       root     4813:        }
1.1.1.45  root     4814: //     return(msdos_drive_number("."));
                   4815:        return(_getdrive() - 1);
1.1       root     4816: }
                   4817: 
1.1.1.45  root     4818: const char *msdos_volume_label(const char *path)
1.1       root     4819: {
                   4820:        static char tmp[MAX_PATH];
                   4821:        char volume[] = "A:\\";
                   4822:        
                   4823:        if(path[1] == ':') {
                   4824:                volume[0] = path[0];
                   4825:        } else {
                   4826:                volume[0] = 'A' + _getdrive() - 1;
                   4827:        }
1.1.1.60  root     4828:        if(!GetVolumeInformationA(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
1.1       root     4829:                memset(tmp, 0, sizeof(tmp));
                   4830:        }
                   4831:        return(tmp);
                   4832: }
                   4833: 
1.1.1.45  root     4834: const char *msdos_short_volume_label(const char *label)
1.1       root     4835: {
                   4836:        static char tmp[(8 + 1 + 3) + 1];
1.1.1.45  root     4837:        const char *src = label;
1.1       root     4838:        int remain = strlen(label);
                   4839:        char *dst_n = tmp;
                   4840:        char *dst_e = tmp + 9;
                   4841:        
                   4842:        strcpy(tmp, "        .   ");
                   4843:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4844:                if(msdos_lead_byte_check(*src)) {
                   4845:                        if(++i == 8) {
                   4846:                                break;
                   4847:                        }
                   4848:                        *dst_n++ = *src++;
                   4849:                        remain--;
                   4850:                }
                   4851:                *dst_n++ = *src++;
                   4852:                remain--;
                   4853:        }
                   4854:        if(remain > 0) {
                   4855:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4856:                        if(msdos_lead_byte_check(*src)) {
                   4857:                                if(++i == 3) {
                   4858:                                        break;
                   4859:                                }
                   4860:                                *dst_e++ = *src++;
                   4861:                                remain--;
                   4862:                        }
                   4863:                        *dst_e++ = *src++;
                   4864:                        remain--;
                   4865:                }
                   4866:                *dst_e = '\0';
                   4867:        } else {
                   4868:                *dst_n = '\0';
                   4869:        }
                   4870:        my_strupr(tmp);
                   4871:        return(tmp);
                   4872: }
                   4873: 
1.1.1.13  root     4874: errno_t msdos_maperr(unsigned long oserrno)
                   4875: {
                   4876:        _doserrno = oserrno;
1.1.1.14  root     4877:        switch(oserrno) {
1.1.1.13  root     4878:        case ERROR_FILE_NOT_FOUND:         // 2
                   4879:        case ERROR_PATH_NOT_FOUND:         // 3
                   4880:        case ERROR_INVALID_DRIVE:          // 15
                   4881:        case ERROR_NO_MORE_FILES:          // 18
                   4882:        case ERROR_BAD_NETPATH:            // 53
                   4883:        case ERROR_BAD_NET_NAME:           // 67
                   4884:        case ERROR_BAD_PATHNAME:           // 161
                   4885:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4886:                return ENOENT;
                   4887:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4888:                return EMFILE;
                   4889:        case ERROR_ACCESS_DENIED:          // 5
                   4890:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4891:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4892:        case ERROR_CANNOT_MAKE:            // 82
                   4893:        case ERROR_FAIL_I24:               // 83
                   4894:        case ERROR_DRIVE_LOCKED:           // 108
                   4895:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4896:        case ERROR_NOT_LOCKED:             // 158
                   4897:        case ERROR_LOCK_FAILED:            // 167
                   4898:                return EACCES;
                   4899:        case ERROR_INVALID_HANDLE:         // 6
                   4900:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4901:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4902:                return EBADF;
                   4903:        case ERROR_ARENA_TRASHED:          // 7
                   4904:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4905:        case ERROR_INVALID_BLOCK:          // 9
                   4906:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4907:                return ENOMEM;
                   4908:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4909:                return E2BIG;
                   4910:        case ERROR_BAD_FORMAT:             // 11
                   4911:                return ENOEXEC;
                   4912:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4913:                return EXDEV;
                   4914:        case ERROR_FILE_EXISTS:            // 80
                   4915:        case ERROR_ALREADY_EXISTS:         // 183
                   4916:                return EEXIST;
                   4917:        case ERROR_NO_PROC_SLOTS:          // 89
                   4918:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4919:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4920:                return EAGAIN;
                   4921:        case ERROR_BROKEN_PIPE:            // 109
                   4922:                return EPIPE;
                   4923:        case ERROR_DISK_FULL:              // 112
                   4924:                return ENOSPC;
                   4925:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4926:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4927:                return ECHILD;
                   4928:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4929:                return ENOTEMPTY;
                   4930:        }
1.1.1.14  root     4931:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4932:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4933:                return EACCES;
                   4934:        }
1.1.1.14  root     4935:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4936:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4937:                return ENOEXEC;
                   4938:        }
                   4939:        return EINVAL;
                   4940: }
                   4941: 
1.1.1.45  root     4942: int msdos_open(const char *path, int oflag)
1.1.1.13  root     4943: {
1.1.1.14  root     4944:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45  root     4945:                return(_open(path, oflag));
1.1.1.13  root     4946:        }
1.1.1.14  root     4947:        
                   4948:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4949:        DWORD disposition;
1.1.1.14  root     4950:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4951:        default:
1.1.1.13  root     4952:        case _O_EXCL:
                   4953:                disposition = OPEN_EXISTING;
                   4954:                break;
                   4955:        case _O_CREAT:
                   4956:                disposition = OPEN_ALWAYS;
                   4957:                break;
                   4958:        case _O_CREAT | _O_EXCL:
                   4959:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4960:                disposition = CREATE_NEW;
                   4961:                break;
                   4962:        case _O_TRUNC:
                   4963:        case _O_TRUNC | _O_EXCL:
                   4964:                disposition = TRUNCATE_EXISTING;
                   4965:                break;
                   4966:        case _O_CREAT | _O_TRUNC:
                   4967:                disposition = CREATE_ALWAYS;
                   4968:                break;
                   4969:        }
1.1.1.14  root     4970:        
1.1.1.60  root     4971:        HANDLE h = CreateFileA(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
1.1.1.13  root     4972:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4973:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4974:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4975:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4976:                // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.60  root     4977:                h = CreateFileA(path, GENERIC_READ,
1.1.1.13  root     4978:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4979:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4980:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4981:                        errno = msdos_maperr(GetLastError());
1.1.1.45  root     4982:                        return(-1);
1.1.1.13  root     4983:                }
                   4984:        }
1.1.1.14  root     4985:        
1.1.1.13  root     4986:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     4987:        if(fd == -1) {
1.1.1.13  root     4988:                CloseHandle(h);
                   4989:        }
1.1.1.45  root     4990:        return(fd);
                   4991: }
                   4992: 
                   4993: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
                   4994: {
                   4995:        int fd = -1;
                   4996:        
                   4997:        *sio_port = *lpt_port = 0;
                   4998:        
                   4999:        if(msdos_is_con_path(path)) {
                   5000:                // MODE.COM opens CON device with read/write mode :-(
                   5001:                if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
                   5002:                        oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
                   5003:                        oflag |= _O_RDONLY;
                   5004:                }
                   5005:                if((fd = msdos_open("CON", oflag)) == -1) {
                   5006: //                     fd = msdos_open("NUL", oflag);
                   5007:                }
                   5008:        } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
                   5009:                fd = msdos_open("NUL", oflag);
                   5010:                msdos_set_comm_params(*sio_port, path);
                   5011:        } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
                   5012:                fd = msdos_open("NUL", oflag);
                   5013:        } else if(msdos_is_device_path(path)) {
                   5014:                fd = msdos_open("NUL", oflag);
                   5015: //     } else if(oflag & _O_CREAT) {
                   5016: //             fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
                   5017: //     } else {
                   5018: //             fd = _open(path, oflag);
                   5019:        }
                   5020:        return(fd);
                   5021: }
                   5022: 
                   5023: UINT16 msdos_device_info(const char *path)
                   5024: {
                   5025:        if(msdos_is_con_path(path)) {
                   5026:                return(0x80d3);
                   5027:        } else if(msdos_is_comm_path(path)) {
                   5028:                return(0x80a0);
                   5029:        } else if(msdos_is_prn_path(path)) {
                   5030: //             return(0xa8c0);
                   5031:                return(0x80a0);
                   5032:        } else if(msdos_is_device_path(path)) {
                   5033:                if(strstr(path, "EMMXXXX0") != NULL) {
                   5034:                        return(0xc0c0);
                   5035:                } else if(strstr(path, "MSCD001") != NULL) {
                   5036:                        return(0xc880);
                   5037:                } else {
                   5038:                        return(0x8084);
                   5039:                }
                   5040:        } else {
                   5041:                return(msdos_drive_number(path));
                   5042:        }
1.1.1.13  root     5043: }
                   5044: 
1.1.1.52  root     5045: 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     5046: {
                   5047:        static int id = 0;
                   5048:        char full[MAX_PATH], *name;
                   5049:        
1.1.1.60  root     5050:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1       root     5051:                strcpy(file_handler[fd].path, full);
                   5052:        } else {
                   5053:                strcpy(file_handler[fd].path, path);
                   5054:        }
1.1.1.14  root     5055:        // isatty makes no distinction between CON & NUL
                   5056:        // GetFileSize fails on CON, succeeds on NUL
                   5057:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45  root     5058:                if(info == 0x80d3) {
                   5059:                        info = 0x8084;
                   5060:                }
1.1.1.14  root     5061:                atty = 0;
                   5062:        } else if(!atty && info == 0x80d3) {
1.1.1.45  root     5063: //             info = msdos_drive_number(".");
                   5064:                info = msdos_drive_number(path);
1.1.1.14  root     5065:        }
1.1       root     5066:        file_handler[fd].valid = 1;
                   5067:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37  root     5068:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     5069:        file_handler[fd].mode = mode;
                   5070:        file_handler[fd].info = info;
                   5071:        file_handler[fd].psp = psp_seg;
1.1.1.37  root     5072:        file_handler[fd].sio_port = sio_port;
                   5073:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     5074:        
                   5075:        // init system file table
                   5076:        if(fd < 20) {
                   5077:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   5078:                
                   5079:                memset(sft, 0, 0x3b);
                   5080:                
                   5081:                *(UINT16 *)(sft + 0x00) = 1;
                   5082:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
1.1.1.60  root     5083:                *(UINT8  *)(sft + 0x04) = GetFileAttributesA(file_handler[fd].path) & 0xff;
1.1.1.21  root     5084:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   5085:                
                   5086:                if(!(file_handler[fd].info & 0x80)) {
                   5087:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   5088:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   5089:                        
                   5090:                        FILETIME time, local;
                   5091:                        HANDLE hHandle;
                   5092:                        WORD dos_date = 0, dos_time = 0;
                   5093:                        DWORD file_size = 0;
                   5094:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   5095:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   5096:                                        FileTimeToLocalFileTime(&time, &local);
                   5097:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   5098:                                }
                   5099:                                file_size = GetFileSize(hHandle, NULL);
                   5100:                        }
                   5101:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   5102:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   5103:                        *(UINT32 *)(sft + 0x11) = file_size;
                   5104:                }
                   5105:                
                   5106:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   5107:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   5108:                my_strupr(fname);
                   5109:                my_strupr(ext);
                   5110:                memset(sft + 0x20, 0x20, 11);
                   5111:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   5112:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   5113:                
                   5114:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   5115:        }
1.1       root     5116: }
                   5117: 
                   5118: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   5119: {
                   5120:        strcpy(file_handler[dst].path, file_handler[src].path);
                   5121:        file_handler[dst].valid = 1;
                   5122:        file_handler[dst].id = file_handler[src].id;
                   5123:        file_handler[dst].atty = file_handler[src].atty;
                   5124:        file_handler[dst].mode = file_handler[src].mode;
                   5125:        file_handler[dst].info = file_handler[src].info;
                   5126:        file_handler[dst].psp = psp_seg;
1.1.1.37  root     5127:        file_handler[dst].sio_port = file_handler[src].sio_port;
                   5128:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     5129: }
                   5130: 
1.1.1.20  root     5131: void msdos_file_handler_close(int fd)
1.1       root     5132: {
                   5133:        file_handler[fd].valid = 0;
1.1.1.21  root     5134:        
                   5135:        if(fd < 20) {
                   5136:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   5137:        }
1.1       root     5138: }
                   5139: 
1.1.1.14  root     5140: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     5141: {
1.1.1.14  root     5142:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   5143:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   5144:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     5145: }
                   5146: 
                   5147: // find file
                   5148: 
                   5149: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   5150: {
                   5151:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   5152:                return(0);      // search directory only !!!
                   5153:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   5154:                return(0);
                   5155:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   5156:                return(0);
                   5157:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   5158:                return(0);
                   5159:        } else if((attribute & required_mask) != required_mask) {
                   5160:                return(0);
                   5161:        } else {
                   5162:                return(1);
                   5163:        }
                   5164: }
                   5165: 
1.1.1.60  root     5166: int msdos_find_file_has_8dot3name(WIN32_FIND_DATAA *fd)
1.1.1.13  root     5167: {
1.1.1.14  root     5168:        if(fd->cAlternateFileName[0]) {
1.1.1.42  root     5169:                return(1);
1.1.1.13  root     5170:        }
                   5171:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     5172:        if(len > 12) {
1.1.1.42  root     5173:                return(0);
1.1.1.13  root     5174:        }
                   5175:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     5176:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42  root     5177:                return(0);
1.1.1.13  root     5178:        }
1.1.1.42  root     5179:        return(1);
1.1.1.13  root     5180: }
                   5181: 
1.1.1.60  root     5182: void msdos_find_file_conv_local_time(WIN32_FIND_DATAA *fd)
1.1       root     5183: {
                   5184:        FILETIME local;
                   5185:        
                   5186:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   5187:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   5188:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   5189:        
                   5190:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   5191:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   5192:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   5193:        
                   5194:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   5195:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   5196:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   5197: }
                   5198: 
                   5199: // i/o
                   5200: 
                   5201: void msdos_stdio_reopen()
                   5202: {
                   5203:        if(!file_handler[0].valid) {
                   5204:                _dup2(DUP_STDIN, 0);
                   5205:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   5206:        }
                   5207:        if(!file_handler[1].valid) {
                   5208:                _dup2(DUP_STDOUT, 1);
                   5209:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   5210:        }
                   5211:        if(!file_handler[2].valid) {
                   5212:                _dup2(DUP_STDERR, 2);
                   5213:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   5214:        }
1.1.1.21  root     5215:        if(!file_handler[3].valid) {
                   5216:                _dup2(DUP_STDAUX, 3);
                   5217:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   5218:        }
                   5219:        if(!file_handler[4].valid) {
                   5220:                _dup2(DUP_STDPRN, 4);
1.1.1.45  root     5221: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   5222:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     5223:        }
                   5224:        for(int i = 0; i < 5; i++) {
                   5225:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   5226:                        msdos_psp_set_file_table(i, i, current_psp);
                   5227:                }
                   5228:        }
1.1       root     5229: }
                   5230: 
1.1.1.37  root     5231: int msdos_read(int fd, void *buffer, unsigned int count)
                   5232: {
                   5233:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5234:                // read from serial port
                   5235:                int read = 0;
                   5236:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5237:                        UINT8 *buf = (UINT8 *)buffer;
                   5238:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5239:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5240:                        DWORD timeout = timeGetTime() + 1000;
                   5241:                        while(read < count) {
                   5242:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
                   5243:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
                   5244:                                        timeout = timeGetTime() + 1000;
                   5245:                                } else {
                   5246:                                        if(timeGetTime() > timeout) {
                   5247:                                                break;
                   5248:                                        }
                   5249:                                        Sleep(10);
1.1.1.37  root     5250:                                }
                   5251:                        }
                   5252:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5253:                }
                   5254:                return(read);
                   5255:        }
                   5256:        return(_read(fd, buffer, count));
                   5257: }
                   5258: 
1.1       root     5259: int msdos_kbhit()
                   5260: {
                   5261:        msdos_stdio_reopen();
                   5262:        
1.1.1.20  root     5263:        process_t *process = msdos_process_info_get(current_psp);
                   5264:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5265:        
                   5266:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5267:                // stdin is redirected to file
1.1.1.20  root     5268:                return(eof(fd) == 0);
1.1       root     5269:        }
                   5270:        
                   5271:        // check keyboard status
1.1.1.35  root     5272:        if(key_recv != 0) {
1.1       root     5273:                return(1);
                   5274:        }
1.1.1.35  root     5275:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5276: #ifdef USE_SERVICE_THREAD
                   5277:                EnterCriticalSection(&key_buf_crit_sect);
                   5278: #endif
1.1.1.55  root     5279:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5280: #ifdef USE_SERVICE_THREAD
                   5281:                LeaveCriticalSection(&key_buf_crit_sect);
                   5282: #endif
                   5283:                if(!empty) return(1);
                   5284:        }
                   5285:        return(_kbhit());
1.1       root     5286: }
                   5287: 
                   5288: int msdos_getch_ex(int echo)
                   5289: {
                   5290:        static char prev = 0;
                   5291:        
                   5292:        msdos_stdio_reopen();
                   5293:        
1.1.1.20  root     5294:        process_t *process = msdos_process_info_get(current_psp);
                   5295:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5296:        
                   5297:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5298:                // stdin is redirected to file
                   5299: retry:
                   5300:                char data;
1.1.1.37  root     5301:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     5302:                        char tmp = data;
                   5303:                        if(data == 0x0a) {
                   5304:                                if(prev == 0x0d) {
                   5305:                                        goto retry; // CRLF -> skip LF
                   5306:                                } else {
                   5307:                                        data = 0x0d; // LF only -> CR
                   5308:                                }
                   5309:                        }
                   5310:                        prev = tmp;
                   5311:                        return(data);
                   5312:                }
                   5313:                return(EOF);
                   5314:        }
                   5315:        
                   5316:        // input from console
1.1.1.5   root     5317:        int key_char, key_scan;
1.1.1.33  root     5318:        if(key_recv != 0) {
1.1.1.5   root     5319:                key_char = (key_code >> 0) & 0xff;
                   5320:                key_scan = (key_code >> 8) & 0xff;
                   5321:                key_code >>= 16;
1.1.1.33  root     5322:                key_recv >>= 16;
1.1.1.5   root     5323:        } else {
1.1.1.54  root     5324:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_exit) {
1.1.1.35  root     5325:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5326: #ifdef USE_SERVICE_THREAD
                   5327:                                EnterCriticalSection(&key_buf_crit_sect);
                   5328: #endif
1.1.1.55  root     5329:                                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5330: #ifdef USE_SERVICE_THREAD
                   5331:                                LeaveCriticalSection(&key_buf_crit_sect);
                   5332: #endif
                   5333:                                if(!empty) break;
                   5334:                        }
1.1.1.23  root     5335:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   5336:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   5337:                                if(_kbhit()) {
1.1.1.32  root     5338:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5339: #ifdef USE_SERVICE_THREAD
                   5340:                                                EnterCriticalSection(&key_buf_crit_sect);
                   5341: #endif
1.1.1.51  root     5342:                                                pcbios_set_key_buffer(_getch(), 0x00);
1.1.1.35  root     5343: #ifdef USE_SERVICE_THREAD
                   5344:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   5345: #endif
1.1.1.32  root     5346:                                        }
1.1.1.23  root     5347:                                } else {
                   5348:                                        Sleep(10);
                   5349:                                }
                   5350:                        } else {
                   5351:                                if(!update_key_buffer()) {
                   5352:                                        Sleep(10);
                   5353:                                }
1.1.1.14  root     5354:                        }
                   5355:                }
1.1.1.54  root     5356:                if(m_exit) {
1.1.1.33  root     5357:                        // insert CR to terminate input loops
1.1.1.14  root     5358:                        key_char = 0x0d;
                   5359:                        key_scan = 0;
1.1.1.32  root     5360:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5361: #ifdef USE_SERVICE_THREAD
                   5362:                        EnterCriticalSection(&key_buf_crit_sect);
                   5363: #endif
1.1.1.51  root     5364:                        pcbios_get_key_buffer(&key_char, &key_scan);
1.1.1.35  root     5365: #ifdef USE_SERVICE_THREAD
                   5366:                        LeaveCriticalSection(&key_buf_crit_sect);
                   5367: #endif
1.1.1.5   root     5368:                }
1.1       root     5369:        }
                   5370:        if(echo && key_char) {
                   5371:                msdos_putch(key_char);
                   5372:        }
                   5373:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   5374: }
                   5375: 
                   5376: inline int msdos_getch()
                   5377: {
                   5378:        return(msdos_getch_ex(0));
                   5379: }
                   5380: 
                   5381: inline int msdos_getche()
                   5382: {
                   5383:        return(msdos_getch_ex(1));
                   5384: }
                   5385: 
                   5386: int msdos_write(int fd, const void *buffer, unsigned int count)
                   5387: {
1.1.1.37  root     5388:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5389:                // write to serial port
1.1.1.38  root     5390:                int written = 0;
1.1.1.37  root     5391:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5392:                        UINT8 *buf = (UINT8 *)buffer;
                   5393:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5394:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5395:                        DWORD timeout = timeGetTime() + 1000;
                   5396:                        while(written < count) {
                   5397:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
                   5398:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
                   5399:                                        timeout = timeGetTime() + 1000;
                   5400:                                } else {
                   5401:                                        if(timeGetTime() > timeout) {
                   5402:                                                break;
                   5403:                                        }
                   5404:                                        Sleep(10);
                   5405:                                }
1.1.1.37  root     5406:                        }
                   5407:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5408:                }
1.1.1.38  root     5409:                return(written);
1.1.1.37  root     5410:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   5411:                // write to printer port
                   5412:                UINT8 *buf = (UINT8 *)buffer;
                   5413:                for(unsigned int i = 0; i < count; i++) {
                   5414: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5415:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5416:                }
                   5417:                return(count);
                   5418:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     5419:                // CR+LF -> LF
1.1.1.37  root     5420:                static int is_cr = 0;
1.1       root     5421:                UINT8 *buf = (UINT8 *)buffer;
                   5422:                for(unsigned int i = 0; i < count; i++) {
                   5423:                        UINT8 data = buf[i];
                   5424:                        if(is_cr) {
                   5425:                                if(data != 0x0a) {
                   5426:                                        UINT8 tmp = 0x0d;
                   5427:                                        _write(1, &tmp, 1);
                   5428:                                }
                   5429:                                _write(1, &data, 1);
                   5430:                                is_cr = 0;
                   5431:                        } else if(data == 0x0d) {
                   5432:                                is_cr = 1;
                   5433:                        } else {
                   5434:                                _write(1, &data, 1);
                   5435:                        }
                   5436:                }
                   5437:                return(count);
                   5438:        }
1.1.1.14  root     5439:        vram_flush();
1.1       root     5440:        return(_write(fd, buffer, count));
                   5441: }
                   5442: 
                   5443: void msdos_putch(UINT8 data)
1.1.1.50  root     5444: {
                   5445:        msdos_stdio_reopen();
                   5446:        
                   5447:        process_t *process = msdos_process_info_get(current_psp);
                   5448:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5449:        
                   5450:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
                   5451:                // stdout is redirected to file
                   5452:                msdos_write(fd, &data, 1);
                   5453:                return;
                   5454:        }
                   5455:        
                   5456:        // call int 29h ?
1.1.1.58  root     5457:        if(*(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     5458:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   5459:                // int 29h is not hooked, no need to call int 29h
                   5460:                msdos_putch_fast(data);
                   5461: #ifdef USE_SERVICE_THREAD
                   5462:        } else if(in_service && main_thread_id != GetCurrentThreadId()) {
                   5463:                // XXX: in usually we should not reach here
                   5464:                // this is called from service thread to echo the input
                   5465:                // we can not call int 29h because it causes a critial issue to control cpu running in main thread :-(
                   5466:                msdos_putch_fast(data);
                   5467: #endif
1.1.1.51  root     5468:        } else if(in_service_29h) {
1.1.1.50  root     5469:                // disallow reentering call int 29h routine to prevent an infinite loop :-(
                   5470:                msdos_putch_fast(data);
                   5471:        } else {
                   5472:                // this is called from main thread, so we can call int 29h :-)
1.1.1.51  root     5473:                in_service_29h = true;
1.1.1.50  root     5474:                try {
                   5475:                        UINT32 tmp_pc = m_pc;
                   5476:                        UINT16 tmp_ax = REG16(AX);
                   5477:                        UINT32 tmp_bx = REG16(BX); // BX may be destroyed by some versions of DOS 3.3
                   5478:                        
                   5479:                        // call int 29h routine is at fffc:0027
                   5480:                        i386_call_far(DUMMY_TOP >> 4, 0x0027);
                   5481:                        REG8(AL) = data;
                   5482:                        
                   5483:                        // run cpu until call int 29h routine is done
1.1.1.54  root     5484:                        while(!m_exit && tmp_pc != m_pc) {
1.1.1.50  root     5485:                                try {
                   5486:                                        hardware_run_cpu();
                   5487:                                } catch(...) {
                   5488:                                }
                   5489:                        }
                   5490:                        REG16(AX) = tmp_ax;
                   5491:                        REG16(BX) = tmp_bx;
                   5492:                } catch(...) {
                   5493:                }
1.1.1.51  root     5494:                in_service_29h = false;
1.1.1.50  root     5495:        }
                   5496: }
                   5497: 
                   5498: void msdos_putch_fast(UINT8 data)
1.1.1.35  root     5499: #ifdef USE_SERVICE_THREAD
                   5500: {
                   5501:        EnterCriticalSection(&putch_crit_sect);
                   5502:        msdos_putch_tmp(data);
                   5503:        LeaveCriticalSection(&putch_crit_sect);
                   5504: }
                   5505: void msdos_putch_tmp(UINT8 data)
                   5506: #endif
1.1       root     5507: {
1.1.1.34  root     5508:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5509:        SMALL_RECT rect;
                   5510:        COORD co;
1.1       root     5511:        static int p = 0;
                   5512:        static int is_kanji = 0;
                   5513:        static int is_esc = 0;
                   5514:        static int stored_x;
                   5515:        static int stored_y;
                   5516:        static WORD stored_a;
1.1.1.20  root     5517:        static char tmp[64], out[64];
1.1       root     5518:        
1.1.1.23  root     5519:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     5520:        
                   5521:        // output to console
                   5522:        tmp[p++] = data;
                   5523:        
1.1.1.14  root     5524:        vram_flush();
                   5525:        
1.1       root     5526:        if(is_kanji) {
                   5527:                // kanji character
                   5528:                is_kanji = 0;
                   5529:        } else if(is_esc) {
                   5530:                // escape sequense
                   5531:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   5532:                        p = is_esc = 0;
                   5533:                } else if(tmp[1] == '=' && p == 4) {
                   5534:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     5535:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     5536:                        SetConsoleCursorPosition(hStdout, co);
                   5537:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5538:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     5539:                        cursor_moved = false;
1.1.1.59  root     5540:                        cursor_moved_by_crtc = false;
1.1       root     5541:                        p = is_esc = 0;
                   5542:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
1.1.1.59  root     5543:                        if(cursor_moved_by_crtc) {
                   5544:                                if(!restore_console_on_exit) {
                   5545:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5546:                                        scr_top = csbi.srWindow.Top;
                   5547:                                }
                   5548:                                co.X = mem[0x450 + REG8(BH) * 2];
                   5549:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   5550:                                SetConsoleCursorPosition(hStdout, co);
                   5551:                                cursor_moved_by_crtc = false;
                   5552:                        }
1.1       root     5553:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5554:                        co.X = csbi.dwCursorPosition.X;
                   5555:                        co.Y = csbi.dwCursorPosition.Y;
                   5556:                        WORD wAttributes = csbi.wAttributes;
                   5557:                        
                   5558:                        if(tmp[1] == 'D') {
                   5559:                                co.Y++;
                   5560:                        } else if(tmp[1] == 'E') {
                   5561:                                co.X = 0;
                   5562:                                co.Y++;
                   5563:                        } else if(tmp[1] == 'M') {
                   5564:                                co.Y--;
                   5565:                        } else if(tmp[1] == '*') {
1.1.1.14  root     5566:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5567:                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5568:                                co.X = 0;
                   5569:                                co.Y = csbi.srWindow.Top;
1.1       root     5570:                        } else if(tmp[1] == '[') {
                   5571:                                int param[256], params = 0;
                   5572:                                memset(param, 0, sizeof(param));
                   5573:                                for(int i = 2; i < p; i++) {
                   5574:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   5575:                                                param[params] *= 10;
                   5576:                                                param[params] += tmp[i] - '0';
                   5577:                                        } else {
                   5578:                                                params++;
                   5579:                                        }
                   5580:                                }
                   5581:                                if(data == 'A') {
1.1.1.14  root     5582:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     5583:                                } else if(data == 'B') {
1.1.1.14  root     5584:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     5585:                                } else if(data == 'C') {
1.1.1.14  root     5586:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     5587:                                } else if(data == 'D') {
1.1.1.14  root     5588:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     5589:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     5590:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   5591:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     5592:                                } else if(data == 'J') {
1.1.1.14  root     5593:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5594:                                        if(param[0] == 0) {
                   5595:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5596:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5597:                                                if(co.Y < csbi.srWindow.Bottom) {
                   5598:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5599:                                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5600:                                                }
                   5601:                                        } else if(param[0] == 1) {
1.1.1.14  root     5602:                                                if(co.Y > csbi.srWindow.Top) {
                   5603:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
1.1.1.60  root     5604:                                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5605:                                                }
                   5606:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.60  root     5607:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5608:                                        } else if(param[0] == 2) {
1.1.1.14  root     5609:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5610:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5611:                                                co.X = co.Y = 0;
                   5612:                                        }
                   5613:                                } else if(data == 'K') {
1.1.1.14  root     5614:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5615:                                        if(param[0] == 0) {
                   5616:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5617:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5618:                                        } else if(param[0] == 1) {
                   5619:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.60  root     5620:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5621:                                        } else if(param[0] == 2) {
                   5622:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5623:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5624:                                        }
                   5625:                                } else if(data == 'L') {
1.1.1.14  root     5626:                                        if(params == 0) {
                   5627:                                                param[0] = 1;
1.1       root     5628:                                        }
1.1.1.14  root     5629:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5630:                                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
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:                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5633:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5634:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.60  root     5635:                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5636:                                        co.X = 0;
                   5637:                                } else if(data == 'M') {
1.1.1.14  root     5638:                                        if(params == 0) {
                   5639:                                                param[0] = 1;
                   5640:                                        }
                   5641:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5642:                                                clear_scr_buffer(csbi.wAttributes);
                   5643:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5644:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5645:                                        } else {
1.1.1.14  root     5646:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5647:                                                ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5648:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5649:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5650:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5651:                                        }
                   5652:                                        co.X = 0;
                   5653:                                } else if(data == 'h') {
                   5654:                                        if(tmp[2] == '>' && tmp[3] == '5') {
1.1.1.60  root     5655:                                                ci_new.bVisible = FALSE;
1.1       root     5656:                                        }
                   5657:                                } else if(data == 'l') {
                   5658:                                        if(tmp[2] == '>' && tmp[3] == '5') {
1.1.1.60  root     5659:                                                ci_new.bVisible = TRUE;
1.1       root     5660:                                        }
                   5661:                                } else if(data == 'm') {
                   5662:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5663:                                        int reverse = 0, hidden = 0;
                   5664:                                        for(int i = 0; i < params; i++) {
                   5665:                                                if(param[i] == 1) {
                   5666:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5667:                                                } else if(param[i] == 4) {
                   5668:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5669:                                                } else if(param[i] == 7) {
                   5670:                                                        reverse = 1;
                   5671:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5672:                                                        hidden = 1;
                   5673:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5674:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5675:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5676:                                                                param[i] -= 16;
                   5677:                                                        } else {
                   5678:                                                                param[i] -= 30;
                   5679:                                                        }
                   5680:                                                        if(param[i] & 1) {
                   5681:                                                                wAttributes |= FOREGROUND_RED;
                   5682:                                                        }
                   5683:                                                        if(param[i] & 2) {
                   5684:                                                                wAttributes |= FOREGROUND_GREEN;
                   5685:                                                        }
                   5686:                                                        if(param[i] & 4) {
                   5687:                                                                wAttributes |= FOREGROUND_BLUE;
                   5688:                                                        }
                   5689:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5690:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5691:                                                        if((param[i] - 40) & 1) {
                   5692:                                                                wAttributes |= BACKGROUND_RED;
                   5693:                                                        }
                   5694:                                                        if((param[i] - 40) & 2) {
                   5695:                                                                wAttributes |= BACKGROUND_GREEN;
                   5696:                                                        }
                   5697:                                                        if((param[i] - 40) & 4) {
                   5698:                                                                wAttributes |= BACKGROUND_BLUE;
                   5699:                                                        }
                   5700:                                                }
                   5701:                                        }
                   5702:                                        if(reverse) {
                   5703:                                                wAttributes &= ~0xff;
                   5704:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5705:                                        }
                   5706:                                        if(hidden) {
                   5707:                                                wAttributes &= ~0x0f;
                   5708:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5709:                                        }
                   5710:                                } else if(data == 'n') {
                   5711:                                        if(param[0] == 6) {
                   5712:                                                char tmp[16];
                   5713:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5714:                                                int len = strlen(tmp);
1.1.1.32  root     5715:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5716: #ifdef USE_SERVICE_THREAD
                   5717:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5718: #endif
1.1.1.32  root     5719:                                                        for(int i = 0; i < len; i++) {
1.1.1.51  root     5720:                                                                pcbios_set_key_buffer(tmp[i], 0x00);
1.1.1.32  root     5721:                                                        }
1.1.1.35  root     5722: #ifdef USE_SERVICE_THREAD
                   5723:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5724: #endif
1.1       root     5725:                                                }
                   5726:                                        }
                   5727:                                } else if(data == 's') {
                   5728:                                        stored_x = co.X;
                   5729:                                        stored_y = co.Y;
                   5730:                                        stored_a = wAttributes;
                   5731:                                } else if(data == 'u') {
                   5732:                                        co.X = stored_x;
                   5733:                                        co.Y = stored_y;
                   5734:                                        wAttributes = stored_a;
                   5735:                                }
                   5736:                        }
                   5737:                        if(co.X < 0) {
                   5738:                                co.X = 0;
                   5739:                        } else if(co.X >= csbi.dwSize.X) {
                   5740:                                co.X = csbi.dwSize.X - 1;
                   5741:                        }
1.1.1.14  root     5742:                        if(co.Y < csbi.srWindow.Top) {
                   5743:                                co.Y = csbi.srWindow.Top;
                   5744:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5745:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5746:                        }
                   5747:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5748:                                SetConsoleCursorPosition(hStdout, co);
                   5749:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5750:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5751:                                cursor_moved = false;
                   5752:                        }
                   5753:                        if(wAttributes != csbi.wAttributes) {
                   5754:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5755:                        }
                   5756:                        p = is_esc = 0;
                   5757:                }
                   5758:                return;
                   5759:        } else {
                   5760:                if(msdos_lead_byte_check(data)) {
                   5761:                        is_kanji = 1;
                   5762:                        return;
                   5763:                } else if(data == 0x1b) {
                   5764:                        is_esc = 1;
                   5765:                        return;
                   5766:                }
                   5767:        }
1.1.1.20  root     5768:        
                   5769:        DWORD q = 0, num;
                   5770:        is_kanji = 0;
                   5771:        for(int i = 0; i < p; i++) {
                   5772:                UINT8 c = tmp[i];
                   5773:                if(is_kanji) {
                   5774:                        is_kanji = 0;
                   5775:                } else if(msdos_lead_byte_check(data)) {
                   5776:                        is_kanji = 1;
                   5777:                } else if(msdos_ctrl_code_check(data)) {
                   5778:                        out[q++] = '^';
                   5779:                        c += 'A' - 1;
                   5780:                }
                   5781:                out[q++] = c;
                   5782:        }
1.1.1.59  root     5783:        if(cursor_moved_by_crtc) {
                   5784:                if(!restore_console_on_exit) {
                   5785:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5786:                        scr_top = csbi.srWindow.Top;
                   5787:                }
                   5788:                co.X = mem[0x450 + REG8(BH) * 2];
                   5789:                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   5790:                SetConsoleCursorPosition(hStdout, co);
                   5791:                cursor_moved_by_crtc = false;
                   5792:        }
1.1.1.34  root     5793:        if(q == 1 && out[0] == 0x08) {
                   5794:                // back space
                   5795:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5796:                if(csbi.dwCursorPosition.X > 0) {
                   5797:                        co.X = csbi.dwCursorPosition.X - 1;
                   5798:                        co.Y = csbi.dwCursorPosition.Y;
                   5799:                        SetConsoleCursorPosition(hStdout, co);
                   5800:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5801:                        co.X = csbi.dwSize.X - 1;
                   5802:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5803:                        SetConsoleCursorPosition(hStdout, co);
                   5804:                } else {
1.1.1.60  root     5805:                        WriteConsoleA(hStdout, out, q, &num, NULL); // to make sure
1.1.1.34  root     5806:                }
                   5807:        } else {
1.1.1.60  root     5808:                WriteConsoleA(hStdout, out, q, &num, NULL);
1.1.1.34  root     5809:        }
1.1       root     5810:        p = 0;
1.1.1.14  root     5811:        
1.1.1.15  root     5812:        if(!restore_console_on_exit) {
                   5813:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5814:                scr_top = csbi.srWindow.Top;
                   5815:        }
1.1       root     5816:        cursor_moved = true;
                   5817: }
                   5818: 
                   5819: int msdos_aux_in()
                   5820: {
1.1.1.21  root     5821:        msdos_stdio_reopen();
                   5822:        
1.1.1.20  root     5823:        process_t *process = msdos_process_info_get(current_psp);
                   5824:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5825:        
                   5826:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5827:                char data = 0;
1.1.1.37  root     5828:                msdos_read(fd, &data, 1);
1.1       root     5829:                return(data);
                   5830:        } else {
                   5831:                return(EOF);
                   5832:        }
                   5833: }
                   5834: 
                   5835: void msdos_aux_out(char data)
                   5836: {
1.1.1.21  root     5837:        msdos_stdio_reopen();
                   5838:        
1.1.1.20  root     5839:        process_t *process = msdos_process_info_get(current_psp);
                   5840:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5841:        
                   5842:        if(fd < process->max_files && file_handler[fd].valid) {
                   5843:                msdos_write(fd, &data, 1);
1.1       root     5844:        }
                   5845: }
                   5846: 
                   5847: void msdos_prn_out(char data)
                   5848: {
1.1.1.21  root     5849:        msdos_stdio_reopen();
                   5850:        
1.1.1.20  root     5851:        process_t *process = msdos_process_info_get(current_psp);
                   5852:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5853:        
                   5854:        if(fd < process->max_files && file_handler[fd].valid) {
                   5855:                msdos_write(fd, &data, 1);
1.1       root     5856:        }
                   5857: }
                   5858: 
                   5859: // memory control
                   5860: 
1.1.1.52  root     5861: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name = NULL)
1.1       root     5862: {
                   5863:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5864:        
                   5865:        mcb->mz = mz;
                   5866:        mcb->psp = psp;
1.1.1.30  root     5867:        mcb->paragraphs = paragraphs;
1.1.1.39  root     5868:        
                   5869:        if(prog_name != NULL) {
                   5870:                memset(mcb->prog_name, 0, 8);
                   5871:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
                   5872:        }
1.1       root     5873:        return(mcb);
                   5874: }
                   5875: 
                   5876: void msdos_mcb_check(mcb_t *mcb)
                   5877: {
                   5878:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5879:                #if 0
                   5880:                        // shutdown now !!!
                   5881:                        fatalerror("broken memory control block\n");
                   5882:                #else
                   5883:                        // return error code and continue
                   5884:                        throw(0x07); // broken memory control block
                   5885:                #endif
1.1       root     5886:        }
                   5887: }
                   5888: 
1.1.1.39  root     5889: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5890: {
                   5891:        int mcb_seg = seg - 1;
                   5892:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5893:        msdos_mcb_check(mcb);
                   5894:        
1.1.1.30  root     5895:        if(mcb->paragraphs > paragraphs) {
1.1       root     5896:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5897:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5898:                
                   5899:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5900:                mcb->mz = 'M';
1.1.1.30  root     5901:                mcb->paragraphs = paragraphs;
1.1       root     5902:        }
                   5903: }
                   5904: 
                   5905: void msdos_mem_merge(int seg)
                   5906: {
                   5907:        int mcb_seg = seg - 1;
                   5908:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5909:        msdos_mcb_check(mcb);
                   5910:        
                   5911:        while(1) {
                   5912:                if(mcb->mz == 'Z') {
                   5913:                        break;
                   5914:                }
1.1.1.30  root     5915:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5916:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5917:                msdos_mcb_check(next_mcb);
                   5918:                
                   5919:                if(next_mcb->psp != 0) {
                   5920:                        break;
                   5921:                }
                   5922:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5923:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5924:        }
                   5925: }
                   5926: 
1.1.1.8   root     5927: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5928: {
                   5929:        while(1) {
                   5930:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5931:                bool last_block;
1.1       root     5932:                
1.1.1.14  root     5933:                if(mcb->psp == 0) {
                   5934:                        msdos_mem_merge(mcb_seg + 1);
                   5935:                } else {
                   5936:                        msdos_mcb_check(mcb);
                   5937:                }
1.1.1.33  root     5938:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5939:                        // check if the next is dummy mcb to link to umb
                   5940:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5941:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5942:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5943:                }
                   5944:                if(!(new_process && !last_block)) {
1.1.1.30  root     5945:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5946:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5947:                                mcb->psp = current_psp;
                   5948:                                return(mcb_seg + 1);
                   5949:                        }
                   5950:                }
                   5951:                if(mcb->mz == 'Z') {
                   5952:                        break;
                   5953:                }
1.1.1.30  root     5954:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5955:        }
                   5956:        return(-1);
                   5957: }
                   5958: 
                   5959: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5960: {
                   5961:        int mcb_seg = seg - 1;
                   5962:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5963:        msdos_mcb_check(mcb);
1.1.1.30  root     5964:        int current_paragraphs = mcb->paragraphs;
1.1       root     5965:        
                   5966:        msdos_mem_merge(seg);
1.1.1.30  root     5967:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5968:                if(max_paragraphs) {
1.1.1.30  root     5969:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5970:                }
1.1       root     5971:                msdos_mem_split(seg, current_paragraphs);
                   5972:                return(-1);
                   5973:        }
                   5974:        msdos_mem_split(seg, paragraphs);
                   5975:        return(0);
                   5976: }
                   5977: 
                   5978: void msdos_mem_free(int seg)
                   5979: {
                   5980:        int mcb_seg = seg - 1;
                   5981:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5982:        msdos_mcb_check(mcb);
                   5983:        
                   5984:        mcb->psp = 0;
                   5985:        msdos_mem_merge(seg);
                   5986: }
                   5987: 
1.1.1.8   root     5988: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5989: {
                   5990:        int max_paragraphs = 0;
                   5991:        
                   5992:        while(1) {
                   5993:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5994:                bool last_block;
                   5995:                
1.1       root     5996:                msdos_mcb_check(mcb);
                   5997:                
1.1.1.33  root     5998:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5999:                        // check if the next is dummy mcb to link to umb
                   6000:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   6001:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   6002:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   6003:                }
                   6004:                if(!(new_process && !last_block)) {
1.1.1.30  root     6005:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   6006:                                max_paragraphs = mcb->paragraphs;
1.1       root     6007:                        }
                   6008:                }
                   6009:                if(mcb->mz == 'Z') {
                   6010:                        break;
                   6011:                }
1.1.1.30  root     6012:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     6013:        }
1.1.1.14  root     6014:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     6015: }
                   6016: 
1.1.1.8   root     6017: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   6018: {
                   6019:        int last_seg = -1;
                   6020:        
                   6021:        while(1) {
                   6022:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   6023:                msdos_mcb_check(mcb);
                   6024:                
1.1.1.14  root     6025:                if(mcb->psp == psp) {
1.1.1.8   root     6026:                        last_seg = mcb_seg;
                   6027:                }
1.1.1.14  root     6028:                if(mcb->mz == 'Z') {
                   6029:                        break;
                   6030:                }
1.1.1.30  root     6031:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     6032:        }
                   6033:        return(last_seg);
                   6034: }
                   6035: 
1.1.1.19  root     6036: int msdos_mem_get_umb_linked()
                   6037: {
1.1.1.33  root     6038:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   6039:        msdos_mcb_check(mcb);
1.1.1.19  root     6040:        
1.1.1.33  root     6041:        if(mcb->mz == 'M') {
                   6042:                return(-1);
1.1.1.19  root     6043:        }
                   6044:        return(0);
                   6045: }
                   6046: 
1.1.1.33  root     6047: void msdos_mem_link_umb()
1.1.1.19  root     6048: {
1.1.1.33  root     6049:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   6050:        msdos_mcb_check(mcb);
1.1.1.19  root     6051:        
1.1.1.33  root     6052:        mcb->mz = 'M';
                   6053:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39  root     6054:        
                   6055:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     6056: }
                   6057: 
1.1.1.33  root     6058: void msdos_mem_unlink_umb()
1.1.1.19  root     6059: {
1.1.1.33  root     6060:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   6061:        msdos_mcb_check(mcb);
1.1.1.19  root     6062:        
1.1.1.33  root     6063:        mcb->mz = 'Z';
                   6064:        mcb->paragraphs = 0;
1.1.1.39  root     6065:        
                   6066:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     6067: }
                   6068: 
1.1.1.29  root     6069: #ifdef SUPPORT_HMA
                   6070: 
                   6071: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   6072: {
                   6073:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6074:        
                   6075:        mcb->ms[0] = 'M';
                   6076:        mcb->ms[1] = 'S';
                   6077:        mcb->owner = owner;
                   6078:        mcb->size = size;
                   6079:        mcb->next = next;
                   6080:        return(mcb);
                   6081: }
                   6082: 
                   6083: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   6084: {
                   6085:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   6086: }
                   6087: 
                   6088: int msdos_hma_mem_split(int offset, int size)
                   6089: {
                   6090:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6091:        
                   6092:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6093:                return(-1);
                   6094:        }
                   6095:        if(mcb->size >= size + 0x10) {
                   6096:                int new_offset = offset + 0x10 + size;
                   6097:                int new_size = mcb->size - 0x10 - size;
                   6098:                
                   6099:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   6100:                mcb->size = size;
                   6101:                mcb->next = new_offset;
                   6102:                return(0);
                   6103:        }
                   6104:        return(-1);
                   6105: }
                   6106: 
                   6107: void msdos_hma_mem_merge(int offset)
                   6108: {
                   6109:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6110:        
                   6111:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6112:                return;
                   6113:        }
                   6114:        while(1) {
                   6115:                if(mcb->next == 0) {
                   6116:                        break;
                   6117:                }
                   6118:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   6119:                
                   6120:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   6121:                        return;
                   6122:                }
                   6123:                if(next_mcb->owner != 0) {
                   6124:                        break;
                   6125:                }
                   6126:                mcb->size += 0x10 + next_mcb->size;
                   6127:                mcb->next = next_mcb->next;
                   6128:        }
                   6129: }
                   6130: 
                   6131: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   6132: {
                   6133:        int offset = 0x10; // first mcb in HMA
                   6134:        
                   6135:        while(1) {
                   6136:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6137:                
                   6138:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   6139:                        return(-1);
                   6140:                }
                   6141:                if(mcb->owner == 0) {
                   6142:                        msdos_hma_mem_merge(offset);
                   6143:                }
                   6144:                if(mcb->owner == 0 && mcb->size >= size) {
                   6145:                        msdos_hma_mem_split(offset, size);
                   6146:                        mcb->owner = owner;
                   6147:                        return(offset);
                   6148:                }
                   6149:                if(mcb->next == 0) {
                   6150:                        break;
                   6151:                }
                   6152:                offset = mcb->next;
                   6153:        }
                   6154:        return(-1);
                   6155: }
                   6156: 
                   6157: int msdos_hma_mem_realloc(int offset, int size)
                   6158: {
                   6159:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6160:        
                   6161:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6162:                return(-1);
                   6163:        }
                   6164:        if(mcb->size < size) {
                   6165:                return(-1);
                   6166:        }
                   6167:        msdos_hma_mem_split(offset, size);
                   6168:        return(0);
                   6169: }
                   6170: 
                   6171: void msdos_hma_mem_free(int offset)
                   6172: {
                   6173:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6174:        
                   6175:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6176:                return;
                   6177:        }
                   6178:        mcb->owner = 0;
                   6179:        msdos_hma_mem_merge(offset);
                   6180: }
                   6181: 
                   6182: int msdos_hma_mem_get_free(int *available_offset)
                   6183: {
                   6184:        int offset = 0x10; // first mcb in HMA
                   6185:        int size = 0;
                   6186:        
                   6187:        while(1) {
                   6188:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6189:                
                   6190:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   6191:                        return(0);
                   6192:                }
                   6193:                if(mcb->owner == 0 && size < mcb->size) {
                   6194:                        if(available_offset != NULL) {
                   6195:                                *available_offset = offset;
                   6196:                        }
                   6197:                        size = mcb->size;
                   6198:                }
                   6199:                if(mcb->next == 0) {
                   6200:                        break;
                   6201:                }
                   6202:                offset = mcb->next;
                   6203:        }
                   6204:        return(size);
                   6205: }
                   6206: 
                   6207: #endif
                   6208: 
1.1       root     6209: // environment
                   6210: 
1.1.1.45  root     6211: void msdos_env_set_argv(int env_seg, const char *argv)
1.1       root     6212: {
                   6213:        char *dst = (char *)(mem + (env_seg << 4));
                   6214:        
                   6215:        while(1) {
                   6216:                if(dst[0] == 0) {
                   6217:                        break;
                   6218:                }
                   6219:                dst += strlen(dst) + 1;
                   6220:        }
                   6221:        *dst++ = 0; // end of environment
                   6222:        *dst++ = 1; // top of argv[0]
                   6223:        *dst++ = 0;
                   6224:        memcpy(dst, argv, strlen(argv));
                   6225:        dst += strlen(argv);
                   6226:        *dst++ = 0;
                   6227:        *dst++ = 0;
                   6228: }
                   6229: 
1.1.1.45  root     6230: const char *msdos_env_get_argv(int env_seg)
1.1       root     6231: {
                   6232:        static char env[ENV_SIZE];
                   6233:        char *src = env;
                   6234:        
                   6235:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   6236:        while(1) {
                   6237:                if(src[0] == 0) {
                   6238:                        if(src[1] == 1) {
                   6239:                                return(src + 3);
                   6240:                        }
                   6241:                        break;
                   6242:                }
                   6243:                src += strlen(src) + 1;
                   6244:        }
                   6245:        return(NULL);
                   6246: }
                   6247: 
1.1.1.45  root     6248: const char *msdos_env_get(int env_seg, const char *name)
1.1       root     6249: {
                   6250:        static char env[ENV_SIZE];
                   6251:        char *src = env;
                   6252:        
                   6253:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   6254:        while(1) {
                   6255:                if(src[0] == 0) {
                   6256:                        break;
                   6257:                }
                   6258:                int len = strlen(src);
                   6259:                char *n = my_strtok(src, "=");
                   6260:                char *v = src + strlen(n) + 1;
                   6261:                
                   6262:                if(_stricmp(name, n) == 0) {
                   6263:                        return(v);
                   6264:                }
                   6265:                src += len + 1;
                   6266:        }
                   6267:        return(NULL);
                   6268: }
                   6269: 
1.1.1.45  root     6270: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1       root     6271: {
                   6272:        char env[ENV_SIZE];
                   6273:        char *src = env;
                   6274:        char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45  root     6275:        const char *argv = msdos_env_get_argv(env_seg);
1.1       root     6276:        int done = 0;
                   6277:        
                   6278:        memcpy(src, dst, ENV_SIZE);
                   6279:        memset(dst, 0, ENV_SIZE);
                   6280:        while(1) {
                   6281:                if(src[0] == 0) {
                   6282:                        break;
                   6283:                }
                   6284:                int len = strlen(src);
                   6285:                char *n = my_strtok(src, "=");
                   6286:                char *v = src + strlen(n) + 1;
                   6287:                char tmp[1024];
                   6288:                
                   6289:                if(_stricmp(name, n) == 0) {
                   6290:                        sprintf(tmp, "%s=%s", n, value);
                   6291:                        done = 1;
                   6292:                } else {
                   6293:                        sprintf(tmp, "%s=%s", n, v);
                   6294:                }
                   6295:                memcpy(dst, tmp, strlen(tmp));
                   6296:                dst += strlen(tmp) + 1;
                   6297:                src += len + 1;
                   6298:        }
                   6299:        if(!done) {
                   6300:                char tmp[1024];
                   6301:                
                   6302:                sprintf(tmp, "%s=%s", name, value);
                   6303:                memcpy(dst, tmp, strlen(tmp));
                   6304:                dst += strlen(tmp) + 1;
                   6305:        }
                   6306:        if(argv) {
                   6307:                *dst++ = 0; // end of environment
                   6308:                *dst++ = 1; // top of argv[0]
                   6309:                *dst++ = 0;
                   6310:                memcpy(dst, argv, strlen(argv));
                   6311:                dst += strlen(argv);
                   6312:                *dst++ = 0;
                   6313:                *dst++ = 0;
                   6314:        }
                   6315: }
                   6316: 
                   6317: // process
                   6318: 
1.1.1.8   root     6319: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     6320: {
                   6321:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6322:        
                   6323:        memset(psp, 0, PSP_SIZE);
                   6324:        psp->exit[0] = 0xcd;
                   6325:        psp->exit[1] = 0x20;
1.1.1.8   root     6326:        psp->first_mcb = mcb_seg;
1.1.1.46  root     6327: #if 1
1.1.1.49  root     6328:        psp->call5[0] = 0xcd;   // int 30h
                   6329:        psp->call5[1] = 0x30;
1.1.1.46  root     6330:        psp->call5[2] = 0xc3;   // ret
                   6331: #else
                   6332:        psp->call5[0] = 0x8a;   // mov ah, cl
                   6333:        psp->call5[1] = 0xe1;
                   6334:        psp->call5[2] = 0xcd;   // int 21h
                   6335:        psp->call5[3] = 0x21;
                   6336:        psp->call5[4] = 0xc3;   // ret
                   6337: #endif
1.1       root     6338:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   6339:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   6340:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   6341:        psp->parent_psp = parent_psp;
1.1.1.20  root     6342:        if(parent_psp == (UINT16)-1) {
                   6343:                for(int i = 0; i < 20; i++) {
                   6344:                        if(file_handler[i].valid) {
                   6345:                                psp->file_table[i] = i;
                   6346:                        } else {
                   6347:                                psp->file_table[i] = 0xff;
                   6348:                        }
1.1       root     6349:                }
1.1.1.20  root     6350:        } else {
                   6351:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     6352:        }
                   6353:        psp->env_seg = env_seg;
                   6354:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     6355:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     6356:        psp->file_table_size = 20;
                   6357:        psp->file_table_ptr.w.l = 0x18;
                   6358:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     6359:        psp->service[0] = 0xcd;
                   6360:        psp->service[1] = 0x21;
                   6361:        psp->service[2] = 0xcb;
                   6362:        return(psp);
                   6363: }
                   6364: 
1.1.1.20  root     6365: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   6366: {
                   6367:        if(psp_seg && fd < 20) {
                   6368:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6369:                psp->file_table[fd] = value;
                   6370:        }
                   6371: }
                   6372: 
                   6373: int msdos_psp_get_file_table(int fd, int psp_seg)
                   6374: {
                   6375:        if(psp_seg && fd < 20) {
                   6376:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6377:                fd = psp->file_table[fd];
                   6378:        }
                   6379:        return fd;
                   6380: }
                   6381: 
1.1.1.52  root     6382: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al, bool first_process = false)
1.1       root     6383: {
                   6384:        // load command file
                   6385:        int fd = -1;
1.1.1.45  root     6386:        int sio_port = 0;
                   6387:        int lpt_port = 0;
1.1       root     6388:        int dos_command = 0;
1.1.1.24  root     6389:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     6390:        char pipe_stdin_path[MAX_PATH] = {0};
                   6391:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39  root     6392:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     6393:        
                   6394:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6395:        int opt_len = mem[opt_ofs];
                   6396:        memset(opt, 0, sizeof(opt));
                   6397:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6398:        
1.1.1.14  root     6399:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   6400:                // this is a batch file, run command.com
                   6401:                char tmp[MAX_PATH];
                   6402:                if(opt_len != 0) {
                   6403:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   6404:                } else {
                   6405:                        sprintf(tmp, "/C %s", cmd);
                   6406:                }
                   6407:                strcpy(opt, tmp);
                   6408:                opt_len = strlen(opt);
                   6409:                mem[opt_ofs] = opt_len;
                   6410:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6411:                strcpy(command, comspec_path);
                   6412:                strcpy(name_tmp, "COMMAND.COM");
                   6413:        } else {
                   6414:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   6415:                        // redirect C:\COMMAND.COM to comspec_path
                   6416:                        strcpy(command, comspec_path);
                   6417:                } else {
                   6418:                        strcpy(command, cmd);
                   6419:                }
1.1.1.60  root     6420:                if(GetFullPathNameA(command, MAX_PATH, path, &name) == 0) {
1.1.1.24  root     6421:                        return(-1);
                   6422:                }
1.1.1.14  root     6423:                memset(name_tmp, 0, sizeof(name_tmp));
                   6424:                strcpy(name_tmp, name);
                   6425:                
                   6426:                // check command.com
1.1.1.38  root     6427:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   6428:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     6429:                        if(opt_len == 0) {
                   6430: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   6431:                                process_t *current_process = NULL;
                   6432:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   6433:                                        if(process[i].psp == current_psp) {
                   6434:                                                current_process = &process[i];
                   6435:                                                break;
                   6436:                                        }
                   6437:                                }
                   6438:                                if(current_process != NULL) {
                   6439:                                        param->cmd_line.dw = current_process->dta.dw;
                   6440:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6441:                                        opt_len = mem[opt_ofs];
                   6442:                                        memset(opt, 0, sizeof(opt));
                   6443:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6444:                                }
                   6445:                        }
                   6446:                        for(int i = 0; i < opt_len; i++) {
                   6447:                                if(opt[i] == ' ') {
                   6448:                                        continue;
                   6449:                                }
                   6450:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   6451:                                        for(int j = i + 3; j < opt_len; j++) {
                   6452:                                                if(opt[j] == ' ') {
                   6453:                                                        continue;
                   6454:                                                }
                   6455:                                                char *token = my_strtok(opt + j, " ");
                   6456:                                                
1.1.1.38  root     6457:                                                strcpy(command, token);
                   6458:                                                char tmp[MAX_PATH];
                   6459:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39  root     6460:                                                strcpy(opt, "");
                   6461:                                                for(int i = 0; i < strlen(tmp); i++) {
                   6462:                                                        if(tmp[i] != ' ') {
                   6463:                                                                strcpy(opt, tmp + i);
                   6464:                                                                break;
                   6465:                                                        }
                   6466:                                                }
                   6467:                                                strcpy(tmp, opt);
1.1.1.38  root     6468:                                                
                   6469:                                                if(al == 0x00) {
1.1.1.39  root     6470:                                                        #define GET_FILE_PATH() { \
                   6471:                                                                if(token[0] != '>' && token[0] != '<') { \
                   6472:                                                                        token++; \
                   6473:                                                                } \
                   6474:                                                                token++; \
                   6475:                                                                while(*token == ' ') { \
                   6476:                                                                        token++; \
                   6477:                                                                } \
                   6478:                                                                char *ptr = token; \
                   6479:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
                   6480:                                                                        ptr++; \
                   6481:                                                                } \
                   6482:                                                                *ptr = '\0'; \
                   6483:                                                        }
                   6484:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
                   6485:                                                                GET_FILE_PATH();
1.1.1.38  root     6486:                                                                strcpy(pipe_stdin_path, token);
                   6487:                                                                strcpy(opt, tmp);
                   6488:                                                        }
1.1.1.39  root     6489:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
                   6490:                                                                GET_FILE_PATH();
1.1.1.38  root     6491:                                                                strcpy(pipe_stdout_path, token);
                   6492:                                                                strcpy(opt, tmp);
                   6493:                                                        }
1.1.1.39  root     6494:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6495:                                                                GET_FILE_PATH();
                   6496:                                                                strcpy(pipe_stderr_path, token);
                   6497:                                                                strcpy(opt, tmp);
                   6498:                                                        }
                   6499:                                                        #undef GET_FILE_PATH
                   6500:                                                        
                   6501:                                                        if((token = strstr(opt, "0<")) != NULL) {
                   6502:                                                                *token = '\0';
                   6503:                                                        }
                   6504:                                                        if((token = strstr(opt, "1>")) != NULL) {
                   6505:                                                                *token = '\0';
                   6506:                                                        }
                   6507:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6508:                                                                *token = '\0';
                   6509:                                                        }
1.1.1.38  root     6510:                                                        if((token = strstr(opt, "<")) != NULL) {
                   6511:                                                                *token = '\0';
                   6512:                                                        }
                   6513:                                                        if((token = strstr(opt, ">")) != NULL) {
                   6514:                                                                *token = '\0';
                   6515:                                                        }
1.1.1.14  root     6516:                                                }
1.1.1.39  root     6517:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
                   6518:                                                        opt[i] = '\0';
                   6519:                                                }
1.1.1.38  root     6520:                                                opt_len = strlen(opt);
                   6521:                                                mem[opt_ofs] = opt_len;
                   6522:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6523:                                                dos_command = 1;
1.1.1.14  root     6524:                                                break;
1.1       root     6525:                                        }
                   6526:                                }
1.1.1.14  root     6527:                                break;
1.1       root     6528:                        }
                   6529:                }
                   6530:        }
                   6531:        
                   6532:        // load command file
                   6533:        strcpy(path, command);
                   6534:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6535:                sprintf(path, "%s.COM", command);
                   6536:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6537:                        sprintf(path, "%s.EXE", command);
                   6538:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     6539:                                sprintf(path, "%s.BAT", command);
                   6540:                                if(_access(path, 0) == 0) {
                   6541:                                        // this is a batch file, run command.com
                   6542:                                        char tmp[MAX_PATH];
                   6543:                                        if(opt_len != 0) {
                   6544:                                                sprintf(tmp, "/C %s %s", path, opt);
                   6545:                                        } else {
                   6546:                                                sprintf(tmp, "/C %s", path);
                   6547:                                        }
                   6548:                                        strcpy(opt, tmp);
                   6549:                                        opt_len = strlen(opt);
                   6550:                                        mem[opt_ofs] = opt_len;
                   6551:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6552:                                        strcpy(path, comspec_path);
                   6553:                                        strcpy(name_tmp, "COMMAND.COM");
                   6554:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6555:                                } else {
                   6556:                                        // search path in parent environments
                   6557:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45  root     6558:                                        const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14  root     6559:                                        if(env != NULL) {
                   6560:                                                char env_path[4096];
                   6561:                                                strcpy(env_path, env);
                   6562:                                                char *token = my_strtok(env_path, ";");
                   6563:                                                
                   6564:                                                while(token != NULL) {
                   6565:                                                        if(strlen(token) != 0) {
                   6566:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   6567:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6568:                                                                        break;
                   6569:                                                                }
                   6570:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   6571:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6572:                                                                        break;
                   6573:                                                                }
                   6574:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   6575:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6576:                                                                        break;
                   6577:                                                                }
                   6578:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   6579:                                                                if(_access(path, 0) == 0) {
                   6580:                                                                        // this is a batch file, run command.com
                   6581:                                                                        char tmp[MAX_PATH];
                   6582:                                                                        if(opt_len != 0) {
                   6583:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   6584:                                                                        } else {
                   6585:                                                                                sprintf(tmp, "/C %s", path);
                   6586:                                                                        }
                   6587:                                                                        strcpy(opt, tmp);
                   6588:                                                                        opt_len = strlen(opt);
                   6589:                                                                        mem[opt_ofs] = opt_len;
                   6590:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6591:                                                                        strcpy(path, comspec_path);
                   6592:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   6593:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6594:                                                                        break;
                   6595:                                                                }
1.1.1.8   root     6596:                                                        }
1.1.1.14  root     6597:                                                        token = my_strtok(NULL, ";");
1.1       root     6598:                                                }
                   6599:                                        }
                   6600:                                }
                   6601:                        }
                   6602:                }
                   6603:        }
                   6604:        if(fd == -1) {
1.1.1.38  root     6605:                // we can not find command.com in the path, so open comspec_path
                   6606:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6607:                        strcpy(command, comspec_path);
                   6608:                        strcpy(path, command);
                   6609:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6610:                }
                   6611:        }
                   6612:        if(fd == -1) {
1.1.1.52  root     6613:                if(!first_process && al == 0 && dos_command) {
1.1       root     6614:                        // may be dos command
                   6615:                        char tmp[MAX_PATH];
1.1.1.52  root     6616:                        if(opt_len != 0) {
                   6617:                                sprintf(tmp, "%s %s", command, opt);
                   6618:                        } else {
                   6619:                                sprintf(tmp, "%s", command);
                   6620:                        }
                   6621:                        retval = system(tmp);
1.1       root     6622:                        return(0);
                   6623:                } else {
                   6624:                        return(-1);
                   6625:                }
                   6626:        }
1.1.1.52  root     6627:        memset(file_buffer, 0, sizeof(file_buffer));
1.1       root     6628:        _read(fd, file_buffer, sizeof(file_buffer));
                   6629:        _close(fd);
                   6630:        
1.1.1.52  root     6631:        // check if this is win32 program
                   6632:        if(!first_process && al == 0) {
                   6633:                UINT16 sign_dos = *(UINT16 *)(file_buffer + 0x00);
                   6634:                UINT32 e_lfanew = *(UINT32 *)(file_buffer + 0x3c);
                   6635:                if(sign_dos == IMAGE_DOS_SIGNATURE && e_lfanew >= 0x40 && e_lfanew < 0x400) {
                   6636:                        UINT32 sign_nt = *(UINT32 *)(file_buffer + e_lfanew + 0x00);
                   6637:                        UINT16 machine = *(UINT16 *)(file_buffer + e_lfanew + 0x04);
                   6638:                        if(sign_nt == IMAGE_NT_SIGNATURE && (machine == IMAGE_FILE_MACHINE_I386 || machine == IMAGE_FILE_MACHINE_AMD64)) {
                   6639:                                char tmp[MAX_PATH];
                   6640:                                if(opt_len != 0) {
                   6641:                                        sprintf(tmp, "\"%s\" %s", path, opt);
                   6642:                                } else {
                   6643:                                        sprintf(tmp, "\"%s\"", path);
                   6644:                                }
                   6645:                                retval = system(tmp);
                   6646:                                return(0);
                   6647:                        }
                   6648:                }
                   6649:        }
                   6650:        
1.1       root     6651:        // copy environment
1.1.1.29  root     6652:        int umb_linked, env_seg, psp_seg;
1.1       root     6653:        
1.1.1.29  root     6654:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6655:                msdos_mem_unlink_umb();
                   6656:        }
1.1.1.8   root     6657:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6658:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6659:                        if(umb_linked != 0) {
                   6660:                                msdos_mem_link_umb();
                   6661:                        }
                   6662:                        return(-1);
                   6663:                }
1.1       root     6664:        }
                   6665:        if(param->env_seg == 0) {
                   6666:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6667:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6668:        } else {
                   6669:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6670:        }
                   6671:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6672:        
                   6673:        // check exe header
                   6674:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6675:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6676:        UINT16 cs, ss, ip, sp;
                   6677:        
                   6678:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6679:                // memory allocation
                   6680:                int header_size = header->header_size * 16;
                   6681:                int load_size = header->pages * 512 - header_size;
                   6682:                if(header_size + load_size < 512) {
                   6683:                        load_size = 512 - header_size;
                   6684:                }
                   6685:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6686:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6687:                        msdos_mem_free(env_seg);
                   6688:                        return(-1);
                   6689:                }
                   6690:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6691:                if(paragraphs > free_paragraphs) {
                   6692:                        paragraphs = free_paragraphs;
                   6693:                }
1.1.1.8   root     6694:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6695:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6696:                                if(umb_linked != 0) {
                   6697:                                        msdos_mem_link_umb();
                   6698:                                }
                   6699:                                msdos_mem_free(env_seg);
                   6700:                                return(-1);
                   6701:                        }
1.1       root     6702:                }
                   6703:                // relocation
                   6704:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6705:                for(int i = 0; i < header->relocations; i++) {
                   6706:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6707:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6708:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6709:                }
                   6710:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6711:                // segments
                   6712:                cs = header->init_cs + start_seg;
                   6713:                ss = header->init_ss + start_seg;
                   6714:                ip = header->init_ip;
                   6715:                sp = header->init_sp - 2; // for symdeb
                   6716:        } else {
                   6717:                // memory allocation
                   6718:                paragraphs = free_paragraphs;
1.1.1.8   root     6719:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6720:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6721:                                if(umb_linked != 0) {
                   6722:                                        msdos_mem_link_umb();
                   6723:                                }
                   6724:                                msdos_mem_free(env_seg);
                   6725:                                return(-1);
                   6726:                        }
1.1       root     6727:                }
                   6728:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6729:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6730:                // segments
                   6731:                cs = ss = psp_seg;
                   6732:                ip = 0x100;
                   6733:                sp = 0xfffe;
                   6734:        }
1.1.1.29  root     6735:        if(umb_linked != 0) {
                   6736:                msdos_mem_link_umb();
                   6737:        }
1.1       root     6738:        
                   6739:        // create psp
1.1.1.3   root     6740:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6741:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6742:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6743:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6744:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6745:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6746:        
                   6747:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6748:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6749:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6750:        
1.1.1.4   root     6751:        for(int i = 0; i < 8; i++) {
                   6752:                if(name_tmp[i] == '.') {
                   6753:                        mcb_psp->prog_name[i] = '\0';
                   6754:                        break;
                   6755:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6756:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6757:                        i++;
                   6758:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6759:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6760:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6761:                } else {
                   6762:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6763:                }
                   6764:        }
                   6765:        
1.1       root     6766:        // process info
                   6767:        process_t *process = msdos_process_info_create(psp_seg);
                   6768:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6769: #ifdef USE_DEBUGGER
                   6770:        strcpy(process->module_path, path);
                   6771: #endif
1.1       root     6772:        process->dta.w.l = 0x80;
                   6773:        process->dta.w.h = psp_seg;
                   6774:        process->switchar = '/';
                   6775:        process->max_files = 20;
                   6776:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6777:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6778:        process->parent_ds = SREG(DS);
1.1.1.31  root     6779:        process->parent_es = SREG(ES);
1.1       root     6780:        
                   6781:        current_psp = psp_seg;
1.1.1.23  root     6782:        msdos_sda_update(current_psp);
1.1       root     6783:        
                   6784:        if(al == 0x00) {
                   6785:                int_10h_feh_called = int_10h_ffh_called = false;
                   6786:                
1.1.1.38  root     6787:                // pipe
                   6788:                if(pipe_stdin_path[0] != '\0') {
1.1.1.45  root     6789: //                     if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
                   6790:                        if(msdos_is_device_path(pipe_stdin_path)) {
                   6791:                                fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
                   6792:                        } else {
                   6793:                                fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
                   6794:                        }
                   6795:                        if(fd != -1) {
                   6796:                                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     6797:                                psp->file_table[0] = fd;
                   6798:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6799:                        }
                   6800:                }
                   6801:                if(pipe_stdout_path[0] != '\0') {
                   6802:                        if(_access(pipe_stdout_path, 0) == 0) {
1.1.1.60  root     6803:                                SetFileAttributesA(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6804:                                DeleteFileA(pipe_stdout_path);
1.1.1.38  root     6805:                        }
1.1.1.45  root     6806: //                     if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6807:                        if(msdos_is_device_path(pipe_stdout_path)) {
                   6808:                                fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6809:                        } else {
                   6810:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6811:                        }
                   6812:                        if(fd != -1) {
                   6813:                                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     6814:                                psp->file_table[1] = fd;
                   6815:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6816:                        }
                   6817:                }
1.1.1.39  root     6818:                if(pipe_stderr_path[0] != '\0') {
                   6819:                        if(_access(pipe_stderr_path, 0) == 0) {
1.1.1.60  root     6820:                                SetFileAttributesA(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
                   6821:                                DeleteFileA(pipe_stderr_path);
1.1.1.39  root     6822:                        }
1.1.1.45  root     6823: //                     if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6824:                        if(msdos_is_device_path(pipe_stderr_path)) {
                   6825:                                fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6826:                        } else {
                   6827:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6828:                        }
                   6829:                        if(fd != -1) {
                   6830:                                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     6831:                                psp->file_table[2] = fd;
                   6832:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6833:                        }
                   6834:                }
1.1.1.38  root     6835:                
1.1       root     6836:                // registers and segments
                   6837:                REG16(AX) = REG16(BX) = 0x00;
                   6838:                REG16(CX) = 0xff;
                   6839:                REG16(DX) = psp_seg;
                   6840:                REG16(SI) = ip;
                   6841:                REG16(DI) = sp;
                   6842:                REG16(SP) = sp;
1.1.1.3   root     6843:                SREG(DS) = SREG(ES) = psp_seg;
                   6844:                SREG(SS) = ss;
                   6845:                i386_load_segment_descriptor(DS);
                   6846:                i386_load_segment_descriptor(ES);
                   6847:                i386_load_segment_descriptor(SS);
1.1       root     6848:                
                   6849:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6850:                i386_jmp_far(cs, ip);
                   6851:        } else if(al == 0x01) {
                   6852:                // copy ss:sp and cs:ip to param block
                   6853:                param->sp = sp;
                   6854:                param->ss = ss;
                   6855:                param->ip = ip;
                   6856:                param->cs = cs;
1.1.1.31  root     6857:                
                   6858:                // the AX value to be passed to the child program is put on top of the child's stack
                   6859:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6860:        }
                   6861:        return(0);
                   6862: }
                   6863: 
                   6864: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6865: {
                   6866:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6867:        
                   6868:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6869:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6870:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6871:        
1.1.1.3   root     6872:        SREG(SS) = psp->stack.w.h;
                   6873:        i386_load_segment_descriptor(SS);
1.1       root     6874:        REG16(SP) = psp->stack.w.l;
                   6875:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6876:        
1.1.1.28  root     6877: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6878:        process_t *current_process = NULL;
                   6879:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6880:                if(process[i].psp == psp_seg) {
                   6881:                        current_process = &process[i];
                   6882:                        break;
                   6883:                }
                   6884:        }
                   6885:        if(current_process == NULL) {
                   6886:                throw(0x1f); // general failure
                   6887:        }
                   6888:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6889:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6890:        if(current_process->called_by_int2eh) {
                   6891:                REG16(AX) = ret;
                   6892:        }
                   6893:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6894:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6895:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6896:        i386_load_segment_descriptor(ES);
1.1       root     6897:        
                   6898:        if(mem_free) {
1.1.1.8   root     6899:                int mcb_seg;
                   6900:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6901:                        msdos_mem_free(mcb_seg + 1);
                   6902:                }
                   6903:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6904:                        msdos_mem_free(mcb_seg + 1);
                   6905:                }
1.1       root     6906:                
                   6907:                for(int i = 0; i < MAX_FILES; i++) {
                   6908:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6909:                                _close(i);
1.1.1.20  root     6910:                                msdos_file_handler_close(i);
                   6911:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6912:                        }
                   6913:                }
1.1.1.13  root     6914:                msdos_dta_info_free(psp_seg);
1.1       root     6915:        }
1.1.1.14  root     6916:        msdos_stdio_reopen();
1.1       root     6917:        
1.1.1.28  root     6918:        memset(current_process, 0, sizeof(process_t));
1.1       root     6919:        
                   6920:        current_psp = psp->parent_psp;
                   6921:        retval = ret;
1.1.1.23  root     6922:        msdos_sda_update(current_psp);
1.1       root     6923: }
                   6924: 
                   6925: // drive
                   6926: 
1.1.1.42  root     6927: int pcbios_update_drive_param(int drive_num, int force_update);
                   6928: 
1.1       root     6929: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6930: {
1.1.1.41  root     6931:        if(!(drive_num >= 0 && drive_num < 26)) {
                   6932:                return(0);
                   6933:        }
1.1.1.42  root     6934:        pcbios_update_drive_param(drive_num, force_update);
                   6935:        
                   6936:        drive_param_t *drive_param = &drive_params[drive_num];
1.1       root     6937:        *seg = DPB_TOP >> 4;
                   6938:        *ofs = sizeof(dpb_t) * drive_num;
                   6939:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6940:        
                   6941:        memset(dpb, 0, sizeof(dpb_t));
                   6942:        
1.1.1.41  root     6943:        dpb->drive_num = drive_num;
                   6944:        dpb->unit_num = drive_num;
1.1.1.42  root     6945:        
                   6946:        if(drive_param->valid) {
                   6947:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   6948:                
                   6949:                dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
                   6950:                dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
                   6951:                dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6952:                dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6953:                switch(geo->MediaType) {
                   6954:                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6955:                        dpb->media_type = 0xff;
                   6956:                        break;
                   6957:                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6958:                        dpb->media_type = 0xfe;
                   6959:                        break;
                   6960:                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6961:                        dpb->media_type = 0xfd;
                   6962:                        break;
                   6963:                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6964:                        dpb->media_type = 0xfc;
                   6965:                        break;
                   6966:                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6967:                case F3_1Pt2_512:
                   6968:                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6969:                case F5_720_512:
                   6970:                        dpb->media_type = 0xf9;
                   6971:                        break;
                   6972:                case FixedMedia:        // hard disk
                   6973:                case RemovableMedia:
                   6974:                case Unknown:
                   6975:                        dpb->media_type = 0xf8;
                   6976:                        break;
                   6977:                default:
                   6978:                        dpb->media_type = 0xf0;
                   6979:                        break;
                   6980:                }
                   6981:        }
1.1.1.41  root     6982:        dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
                   6983:        dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42  root     6984:        dpb->info_sector = 0xffff;
                   6985:        dpb->backup_boot_sector = 0xffff;
                   6986:        dpb->free_clusters = 0xffff;
                   6987:        dpb->free_search_cluster = 0xffffffff;
                   6988:        
                   6989:        return(drive_param->valid);
1.1       root     6990: }
                   6991: 
                   6992: // pc bios
                   6993: 
1.1.1.35  root     6994: #ifdef USE_SERVICE_THREAD
                   6995: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   6996: {
                   6997: #if defined(HAS_I386)
                   6998:        if(m_SF != 0) {
                   6999:                m_SF = 0;
1.1.1.49  root     7000:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     7001:        } else {
                   7002:                m_SF = 1;
1.1.1.49  root     7003:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     7004:        }
                   7005: #else
                   7006:        if(m_SignVal < 0) {
                   7007:                m_SignVal = 0;
1.1.1.49  root     7008:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     7009:        } else {
                   7010:                m_SignVal = -1;
1.1.1.49  root     7011:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     7012:        }
                   7013: #endif
1.1.1.59  root     7014:        // dummy loop to wait BIOS/DOS service is done is at fffc:0013
1.1.1.49  root     7015:        i386_call_far(DUMMY_TOP >> 4, 0x0013);
1.1.1.35  root     7016:        in_service = true;
                   7017:        service_exit = false;
                   7018:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   7019: }
                   7020: 
                   7021: void finish_service_loop()
                   7022: {
                   7023:        if(in_service && service_exit) {
                   7024: #if defined(HAS_I386)
                   7025:                if(m_SF != 0) {
                   7026:                        m_SF = 0;
                   7027:                } else {
                   7028:                        m_SF = 1;
                   7029:                }
                   7030: #else
                   7031:                if(m_SignVal < 0) {
                   7032:                        m_SignVal = 0;
                   7033:                } else {
                   7034:                        m_SignVal = -1;
                   7035:                }
                   7036: #endif
                   7037:                in_service = false;
                   7038:        }
                   7039: }
                   7040: #endif
                   7041: 
1.1.1.19  root     7042: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   7043: {
                   7044:        static unsigned __int64 start_msec_since_midnight = 0;
                   7045:        static unsigned __int64 start_msec_since_hostboot = 0;
                   7046:        
                   7047:        if(start_msec_since_midnight == 0) {
                   7048:                SYSTEMTIME time;
                   7049:                GetLocalTime(&time);
                   7050:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   7051:                start_msec_since_hostboot = cur_msec;
                   7052:        }
                   7053:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   7054:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   7055:        return (UINT32)tick;
                   7056: }
                   7057: 
                   7058: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   7059: {
                   7060:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   7061:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   7062:        
                   7063:        if(prev_tick > next_tick) {
                   7064:                mem[0x470] = 1;
                   7065:        }
                   7066:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   7067: }
                   7068: 
1.1.1.14  root     7069: inline void pcbios_irq0()
                   7070: {
                   7071:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     7072:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     7073: }
                   7074: 
1.1.1.16  root     7075: int pcbios_get_text_vram_address(int page)
1.1       root     7076: {
                   7077:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7078:                return TEXT_VRAM_TOP;
1.1       root     7079:        } else {
1.1.1.14  root     7080:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     7081:        }
                   7082: }
                   7083: 
1.1.1.16  root     7084: int pcbios_get_shadow_buffer_address(int page)
1.1       root     7085: {
1.1.1.14  root     7086:        if(!int_10h_feh_called) {
1.1.1.16  root     7087:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     7088:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7089:                return SHADOW_BUF_TOP;
                   7090:        } else {
1.1.1.14  root     7091:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     7092:        }
                   7093: }
                   7094: 
1.1.1.16  root     7095: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     7096: {
1.1.1.16  root     7097:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     7098: }
                   7099: 
1.1.1.56  root     7100: bool pcbios_set_font_size(int width, int height)
                   7101: {
1.1.1.61  root     7102:        if(set_console_font_size(width, height)) {
                   7103:                *(UINT16 *)(mem + 0x485) = height;
                   7104:                return(true);
                   7105:        }
                   7106:        return(false);
1.1.1.56  root     7107: }
                   7108: 
1.1.1.16  root     7109: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     7110: {
1.1.1.14  root     7111:        // clear the existing screen, not just the new one
                   7112:        int clr_height = max(height, scr_height);
                   7113:        
1.1.1.16  root     7114:        if(scr_width != width || scr_height != height) {
                   7115:                change_console_size(width, height);
1.1.1.14  root     7116:        }
                   7117:        mem[0x462] = 0;
                   7118:        *(UINT16 *)(mem + 0x44e) = 0;
                   7119:        
1.1.1.16  root     7120:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   7121:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   7122:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   7123:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1.1.51  root     7124:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     7125:        
1.1.1.23  root     7126:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     7127:        if(clr_screen) {
1.1.1.14  root     7128:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   7129:                        mem[ofs++] = 0x20;
                   7130:                        mem[ofs++] = 0x07;
                   7131:                }
                   7132:                
1.1.1.35  root     7133: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7134:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7135: #endif
1.1.1.14  root     7136:                for(int y = 0; y < clr_height; y++) {
                   7137:                        for(int x = 0; x < scr_width; x++) {
                   7138:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7139:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     7140:                        }
                   7141:                }
                   7142:                SMALL_RECT rect;
1.1.1.14  root     7143:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
1.1.1.60  root     7144:                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7145:                vram_length_char = vram_last_length_char = 0;
                   7146:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     7147: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7148:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7149: #endif
1.1       root     7150:        }
1.1.1.14  root     7151:        COORD co;
                   7152:        co.X = 0;
                   7153:        co.Y = scr_top;
                   7154:        SetConsoleCursorPosition(hStdout, co);
                   7155:        cursor_moved = true;
1.1.1.59  root     7156:        cursor_moved_by_crtc = false;
1.1.1.14  root     7157:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     7158: }
                   7159: 
1.1.1.36  root     7160: void pcbios_update_cursor_position()
                   7161: {
                   7162:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7163:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   7164:        if(!restore_console_on_exit) {
                   7165:                scr_top = csbi.srWindow.Top;
                   7166:        }
                   7167:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   7168:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   7169: }
                   7170: 
1.1.1.16  root     7171: inline void pcbios_int_10h_00h()
                   7172: {
                   7173:        switch(REG8(AL) & 0x7f) {
                   7174:        case 0x70: // v-text mode
                   7175:        case 0x71: // extended cga v-text mode
                   7176:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   7177:                break;
1.1.1.61  root     7178:        case 0x73:
                   7179:        case 0x03:
                   7180:                change_console_size(80, 25); // for Windows10
                   7181:                pcbios_set_font_size(font_width, font_height);
1.1.1.16  root     7182:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   7183:                break;
                   7184:        }
                   7185:        if(REG8(AL) & 0x80) {
                   7186:                mem[0x487] |= 0x80;
                   7187:        } else {
                   7188:                mem[0x487] &= ~0x80;
                   7189:        }
                   7190:        mem[0x449] = REG8(AL) & 0x7f;
                   7191: }
                   7192: 
1.1       root     7193: inline void pcbios_int_10h_01h()
                   7194: {
1.1.1.13  root     7195:        mem[0x460] = REG8(CL);
                   7196:        mem[0x461] = REG8(CH);
1.1.1.14  root     7197:        
1.1.1.60  root     7198:        int size = (int)(REG8(CL) & 7) - (int)(REG8(CH) & 7) + 1;
                   7199:        
                   7200:        if(!((REG8(CH) & 0x20) != 0 || size < 0)) {
                   7201:                ci_new.bVisible = TRUE;
                   7202:                ci_new.dwSize = (size + 2) * 100 / (8 + 2);
                   7203:        } else {
                   7204:                ci_new.bVisible = FALSE;
                   7205:        }
1.1       root     7206: }
                   7207: 
                   7208: inline void pcbios_int_10h_02h()
                   7209: {
1.1.1.14  root     7210:        // continuously setting the cursor effectively stops it blinking
1.1.1.59  root     7211:        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     7212:                COORD co;
                   7213:                co.X = REG8(DL);
1.1.1.14  root     7214:                co.Y = REG8(DH) + scr_top;
                   7215:                
                   7216:                // some programs hide the cursor by moving it off screen
                   7217:                static bool hidden = false;
1.1.1.23  root     7218:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7219:                
                   7220:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
1.1.1.59  root     7221:                        if(ci_new.bVisible) {
                   7222:                                ci_new.bVisible = FALSE;
1.1.1.14  root     7223:                                hidden = true;
                   7224:                        }
                   7225:                } else if(hidden) {
1.1.1.59  root     7226:                        if(!ci_new.bVisible) {
                   7227:                                ci_new.bVisible = TRUE;
1.1.1.14  root     7228:                        }
                   7229:                        hidden = false;
                   7230:                }
1.1.1.59  root     7231:                cursor_moved_by_crtc = false;
1.1       root     7232:        }
1.1.1.14  root     7233:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   7234:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     7235: }
                   7236: 
                   7237: inline void pcbios_int_10h_03h()
                   7238: {
1.1.1.14  root     7239:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7240:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7241:        REG8(CL) = mem[0x460];
                   7242:        REG8(CH) = mem[0x461];
                   7243: }
                   7244: 
                   7245: inline void pcbios_int_10h_05h()
                   7246: {
1.1.1.14  root     7247:        if(REG8(AL) >= vram_pages) {
                   7248:                return;
                   7249:        }
                   7250:        if(mem[0x462] != REG8(AL)) {
                   7251:                vram_flush();
                   7252:                
1.1.1.23  root     7253:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7254:                SMALL_RECT rect;
1.1.1.14  root     7255:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7256:                ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7257:                
1.1.1.16  root     7258:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     7259:                        for(int x = 0; x < scr_width; x++) {
                   7260:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7261:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7262:                        }
                   7263:                }
1.1.1.16  root     7264:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     7265:                        for(int x = 0; x < scr_width; x++) {
                   7266:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   7267:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     7268:                        }
                   7269:                }
1.1.1.60  root     7270:                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7271:                
                   7272:                COORD co;
1.1.1.14  root     7273:                co.X = mem[0x450 + REG8(AL) * 2];
                   7274:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   7275:                if(co.Y < scr_top + scr_height) {
                   7276:                        SetConsoleCursorPosition(hStdout, co);
                   7277:                }
1.1.1.59  root     7278:                cursor_moved_by_crtc = false;
1.1       root     7279:        }
1.1.1.14  root     7280:        mem[0x462] = REG8(AL);
                   7281:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   7282:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     7283:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     7284:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     7285:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     7286:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     7287:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     7288: }
                   7289: 
                   7290: inline void pcbios_int_10h_06h()
                   7291: {
1.1.1.14  root     7292:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7293:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7294:                return;
                   7295:        }
                   7296:        vram_flush();
                   7297:        
1.1.1.23  root     7298:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7299:        SMALL_RECT rect;
1.1.1.14  root     7300:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7301:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7302:        
                   7303:        int right = min(REG8(DL), scr_width - 1);
                   7304:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7305:        
                   7306:        if(REG8(AL) == 0) {
1.1.1.14  root     7307:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7308:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7309:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7310:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7311:                        }
                   7312:                }
                   7313:        } else {
1.1.1.14  root     7314:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     7315:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7316:                                if(y2 <= bottom) {
                   7317:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7318:                                } else {
1.1.1.14  root     7319:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7320:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7321:                                }
1.1.1.14  root     7322:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7323:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7324:                        }
                   7325:                }
                   7326:        }
1.1.1.60  root     7327:        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7328: }
                   7329: 
                   7330: inline void pcbios_int_10h_07h()
                   7331: {
1.1.1.14  root     7332:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7333:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7334:                return;
                   7335:        }
                   7336:        vram_flush();
                   7337:        
1.1.1.23  root     7338:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7339:        SMALL_RECT rect;
1.1.1.14  root     7340:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7341:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7342:        
                   7343:        int right = min(REG8(DL), scr_width - 1);
                   7344:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7345:        
                   7346:        if(REG8(AL) == 0) {
1.1.1.14  root     7347:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7348:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7349:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7350:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7351:                        }
                   7352:                }
                   7353:        } else {
1.1.1.14  root     7354:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     7355:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7356:                                if(y2 >= REG8(CH)) {
                   7357:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7358:                                } else {
1.1.1.14  root     7359:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7360:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7361:                                }
1.1.1.14  root     7362:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7363:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7364:                        }
                   7365:                }
                   7366:        }
1.1.1.60  root     7367:        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7368: }
                   7369: 
                   7370: inline void pcbios_int_10h_08h()
                   7371: {
                   7372:        COORD co;
                   7373:        DWORD num;
                   7374:        
1.1.1.14  root     7375:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7376:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7377:        
                   7378:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7379:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7380:                co.Y += scr_top;
                   7381:                vram_flush();
1.1.1.60  root     7382:                ReadConsoleOutputCharacterA(hStdout, scr_char, 1, co, &num);
1.1       root     7383:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   7384:                REG8(AL) = scr_char[0];
                   7385:                REG8(AH) = scr_attr[0];
                   7386:        } else {
1.1.1.16  root     7387:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     7388:        }
                   7389: }
                   7390: 
                   7391: inline void pcbios_int_10h_09h()
                   7392: {
                   7393:        COORD co;
                   7394:        
1.1.1.14  root     7395:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7396:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7397:        
1.1.1.16  root     7398:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7399:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7400:        
                   7401:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7402: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7403:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7404: #endif
1.1.1.16  root     7405:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7406:                while(dest < end) {
                   7407:                        write_text_vram_char(dest - vram, REG8(AL));
                   7408:                        mem[dest++] = REG8(AL);
                   7409:                        write_text_vram_attr(dest - vram, REG8(BL));
                   7410:                        mem[dest++] = REG8(BL);
1.1       root     7411:                }
1.1.1.35  root     7412: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7413:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7414: #endif
1.1       root     7415:        } else {
1.1.1.14  root     7416:                while(dest < end) {
1.1       root     7417:                        mem[dest++] = REG8(AL);
                   7418:                        mem[dest++] = REG8(BL);
                   7419:                }
                   7420:        }
                   7421: }
                   7422: 
                   7423: inline void pcbios_int_10h_0ah()
                   7424: {
                   7425:        COORD co;
                   7426:        
1.1.1.14  root     7427:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7428:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7429:        
1.1.1.16  root     7430:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7431:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7432:        
                   7433:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7434: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7435:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7436: #endif
1.1.1.16  root     7437:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7438:                while(dest < end) {
                   7439:                        write_text_vram_char(dest - vram, REG8(AL));
                   7440:                        mem[dest++] = REG8(AL);
                   7441:                        dest++;
1.1       root     7442:                }
1.1.1.35  root     7443: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7444:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7445: #endif
1.1       root     7446:        } else {
1.1.1.14  root     7447:                while(dest < end) {
1.1       root     7448:                        mem[dest++] = REG8(AL);
                   7449:                        dest++;
                   7450:                }
                   7451:        }
                   7452: }
                   7453: 
1.1.1.40  root     7454: inline void pcbios_int_10h_0ch()
                   7455: {
                   7456:        HDC hdc = get_console_window_device_context();
                   7457:        
                   7458:        if(hdc != NULL) {
                   7459:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
                   7460:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
                   7461:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
                   7462:                
                   7463:                if(REG8(AL) & 0x80) {
                   7464:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7465:                        if(color != CLR_INVALID) {
                   7466:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7467:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7468:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
                   7469:                        }
                   7470:                }
                   7471:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
                   7472:        }
                   7473: }
                   7474: 
                   7475: inline void pcbios_int_10h_0dh()
                   7476: {
                   7477:        HDC hdc = get_console_window_device_context();
                   7478:        BYTE r = 0;
                   7479:        BYTE g = 0;
                   7480:        BYTE b = 0;
                   7481:        
                   7482:        if(hdc != NULL) {
                   7483:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7484:                if(color != CLR_INVALID) {
                   7485:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7486:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7487:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
                   7488:                }
                   7489:        }
                   7490:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
                   7491: }
                   7492: 
1.1       root     7493: inline void pcbios_int_10h_0eh()
                   7494: {
1.1.1.59  root     7495:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   7496:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     7497:        DWORD num;
                   7498:        COORD co;
                   7499:        
1.1.1.59  root     7500:        if(cursor_moved_by_crtc) {
                   7501:                if(!restore_console_on_exit) {
                   7502:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7503:                        scr_top = csbi.srWindow.Top;
                   7504:                }
                   7505:                co.X = mem[0x450 + REG8(BH) * 2];
                   7506:                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   7507:                SetConsoleCursorPosition(hStdout, co);
                   7508:                cursor_moved_by_crtc = false;
                   7509:        }
1.1.1.54  root     7510:        co.X = mem[0x450 + mem[0x462] * 2];
                   7511:        co.Y = mem[0x451 + mem[0x462] * 2];
1.1.1.14  root     7512:        
                   7513:        if(REG8(AL) == 7) {
                   7514:                //MessageBeep(-1);
                   7515:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   7516:                if(REG8(AL) == 10) {
                   7517:                        vram_flush();
                   7518:                }
1.1.1.60  root     7519:                WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     7520:                cursor_moved = true;
                   7521:        } else {
1.1.1.54  root     7522:                int dest = pcbios_get_shadow_buffer_address(mem[0x462], co.X, co.Y);
1.1.1.35  root     7523: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7524:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7525: #endif
1.1.1.54  root     7526:                int vram = pcbios_get_shadow_buffer_address(mem[0x462]);
                   7527:                write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     7528: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7529:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7530: #endif
1.1.1.54  root     7531:                
                   7532:                if(++co.X == scr_width) {
                   7533:                        co.X = 0;
                   7534:                        if(++co.Y == scr_height) {
                   7535:                                vram_flush();
1.1.1.60  root     7536:                                WriteConsoleA(hStdout, "\n", 1, &num, NULL);
1.1.1.14  root     7537:                                cursor_moved = true;
                   7538:                        }
                   7539:                }
1.1.1.54  root     7540:                if(!cursor_moved) {
                   7541:                        co.Y += scr_top;
                   7542:                        SetConsoleCursorPosition(hStdout, co);
                   7543:                        cursor_moved = true;
                   7544:                }
1.1.1.14  root     7545:                mem[dest] = REG8(AL);
                   7546:        }
1.1       root     7547: }
                   7548: 
                   7549: inline void pcbios_int_10h_0fh()
                   7550: {
                   7551:        REG8(AL) = mem[0x449];
                   7552:        REG8(AH) = mem[0x44a];
                   7553:        REG8(BH) = mem[0x462];
                   7554: }
                   7555: 
1.1.1.14  root     7556: inline void pcbios_int_10h_11h()
                   7557: {
                   7558:        switch(REG8(AL)) {
1.1.1.58  root     7559:        case 0x00:
                   7560:        case 0x10:
1.1.1.61  root     7561:                if(REG8(BH)) {
                   7562:                        change_console_size(80, 25); // for Windows10
                   7563:                        if(!pcbios_set_font_size(font_width, (int)REG8(BH))) {
                   7564:                                for(int h = min(font_height, (int)REG8(BH)); h <= max(font_height, (int)REG8(BH)); h++) {
                   7565:                                        if(h != (int)REG8(BH)) {
                   7566:                                                if(pcbios_set_font_size(font_width, h)) {
                   7567:                                                        break;
                   7568:                                                }
                   7569:                                        }
                   7570:                                }
                   7571:                        }
1.1.1.58  root     7572:                        pcbios_set_console_size(80, (25 * 16) / REG8(BH), true);
                   7573:                }
                   7574:                break;
1.1.1.16  root     7575:        case 0x01:
1.1.1.14  root     7576:        case 0x11:
1.1.1.61  root     7577:                change_console_size(80, 28); // for Windows10
                   7578:                if(!pcbios_set_font_size(font_width, 14)) {
                   7579:                        for(int h = min(font_height, 14); h <= max(font_height, 14); h++) {
                   7580:                                if(h != 14) {
                   7581:                                        if(pcbios_set_font_size(font_width, h)) {
                   7582:                                                break;
                   7583:                                        }
                   7584:                                }
                   7585:                        }
1.1.1.56  root     7586:                }
1.1.1.61  root     7587:                pcbios_set_console_size(80, 28, true); // 28 = 25 * 16 / 14
1.1.1.14  root     7588:                break;
1.1.1.16  root     7589:        case 0x02:
1.1.1.14  root     7590:        case 0x12:
1.1.1.61  root     7591:                change_console_size(80, 25); // for Windows10
                   7592:                if(!pcbios_set_font_size(8, 8)) {
                   7593:                        bool success = false;
                   7594:                        for(int y = 8; y <= 14; y++) {
                   7595:                                for(int x = min(font_width, 6); x <= max(font_width, 8); x++) {
                   7596:                                        if(pcbios_set_font_size(x, y)) {
                   7597:                                                success = true;
                   7598:                                                break;
                   7599:                                        }
                   7600:                                }
                   7601:                        }
                   7602:                        if(!success) {
                   7603:                                pcbios_set_font_size(font_width, font_height);
                   7604:                        }
1.1.1.56  root     7605:                }
1.1.1.61  root     7606:                pcbios_set_console_size(80, 50, true); // 50 = 25 * 16 / 8
1.1.1.14  root     7607:                break;
1.1.1.16  root     7608:        case 0x04:
1.1.1.14  root     7609:        case 0x14:
1.1.1.61  root     7610:                change_console_size(80, 25); // for Windows10
                   7611:                pcbios_set_font_size(font_width, font_height);
                   7612:                pcbios_set_console_size(80, 25, true);
1.1.1.58  root     7613:                break;
                   7614:        case 0x18:
1.1.1.61  root     7615:                change_console_size(80, 25); // for Windows10
                   7616:                pcbios_set_font_size(font_width, font_height);
                   7617:                pcbios_set_console_size(80, 25, true);
1.1.1.14  root     7618:                break;
                   7619:        case 0x30:
                   7620:                SREG(ES) = 0;
                   7621:                i386_load_segment_descriptor(ES);
                   7622:                REG16(BP) = 0;
                   7623:                REG16(CX) = mem[0x485];
                   7624:                REG8(DL) = mem[0x484];
                   7625:                break;
1.1.1.54  root     7626:        default:
                   7627:                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));
                   7628:                m_CF = 1;
                   7629:                break;
1.1.1.14  root     7630:        }
                   7631: }
                   7632: 
                   7633: inline void pcbios_int_10h_12h()
                   7634: {
1.1.1.16  root     7635:        switch(REG8(BL)) {
                   7636:        case 0x10:
1.1.1.14  root     7637:                REG16(BX) = 0x0003;
                   7638:                REG16(CX) = 0x0009;
1.1.1.16  root     7639:                break;
1.1.1.14  root     7640:        }
                   7641: }
                   7642: 
1.1       root     7643: inline void pcbios_int_10h_13h()
                   7644: {
1.1.1.3   root     7645:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     7646:        COORD co;
                   7647:        DWORD num;
                   7648:        
                   7649:        co.X = REG8(DL);
1.1.1.14  root     7650:        co.Y = REG8(DH) + scr_top;
                   7651:        
                   7652:        vram_flush();
1.1       root     7653:        
                   7654:        switch(REG8(AL)) {
                   7655:        case 0x00:
                   7656:        case 0x01:
                   7657:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7658:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7659:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7660:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7661:                        SetConsoleCursorPosition(hStdout, co);
                   7662:                        
                   7663:                        if(csbi.wAttributes != REG8(BL)) {
                   7664:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   7665:                        }
1.1.1.60  root     7666:                        WriteConsoleA(hStdout, &mem[ofs], REG16(CX), &num, NULL);
1.1.1.14  root     7667:                        
1.1       root     7668:                        if(csbi.wAttributes != REG8(BL)) {
                   7669:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7670:                        }
                   7671:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     7672:                                if(!restore_console_on_exit) {
                   7673:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7674:                                        scr_top = csbi.srWindow.Top;
                   7675:                                }
1.1.1.14  root     7676:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7677:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7678:                                SetConsoleCursorPosition(hStdout, co);
                   7679:                        } else {
                   7680:                                cursor_moved = true;
                   7681:                        }
1.1.1.59  root     7682:                        cursor_moved_by_crtc = false;
1.1       root     7683:                } else {
1.1.1.3   root     7684:                        m_CF = 1;
1.1       root     7685:                }
                   7686:                break;
                   7687:        case 0x02:
                   7688:        case 0x03:
                   7689:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7690:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7691:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7692:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7693:                        SetConsoleCursorPosition(hStdout, co);
                   7694:                        
                   7695:                        WORD wAttributes = csbi.wAttributes;
                   7696:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7697:                                if(wAttributes != mem[ofs + 1]) {
                   7698:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7699:                                        wAttributes = mem[ofs + 1];
                   7700:                                }
1.1.1.60  root     7701:                                WriteConsoleA(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7702:                        }
                   7703:                        if(csbi.wAttributes != wAttributes) {
                   7704:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7705:                        }
                   7706:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7707:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7708:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7709:                                SetConsoleCursorPosition(hStdout, co);
                   7710:                        } else {
                   7711:                                cursor_moved = true;
                   7712:                        }
1.1.1.59  root     7713:                        cursor_moved_by_crtc = false;
1.1       root     7714:                } else {
1.1.1.3   root     7715:                        m_CF = 1;
1.1       root     7716:                }
                   7717:                break;
                   7718:        case 0x10:
                   7719:        case 0x11:
                   7720:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7721:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.60  root     7722:                        ReadConsoleOutputCharacterA(hStdout, scr_char, REG16(CX), co, &num);
1.1       root     7723:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7724:                        for(int i = 0; i < num; i++) {
                   7725:                                mem[ofs++] = scr_char[i];
                   7726:                                mem[ofs++] = scr_attr[i];
1.1.1.45  root     7727:                                if(REG8(AL) & 0x01) {
1.1       root     7728:                                        mem[ofs++] = 0;
                   7729:                                        mem[ofs++] = 0;
                   7730:                                }
                   7731:                        }
                   7732:                } else {
1.1.1.16  root     7733:                        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     7734:                                mem[ofs++] = mem[src++];
                   7735:                                mem[ofs++] = mem[src++];
1.1.1.45  root     7736:                                if(REG8(AL) & 0x01) {
1.1       root     7737:                                        mem[ofs++] = 0;
                   7738:                                        mem[ofs++] = 0;
                   7739:                                }
1.1.1.14  root     7740:                                if(++co.X == scr_width) {
                   7741:                                        if(++co.Y == scr_height) {
1.1       root     7742:                                                break;
                   7743:                                        }
                   7744:                                        co.X = 0;
                   7745:                                }
                   7746:                        }
                   7747:                }
                   7748:                break;
1.1.1.45  root     7749:        case 0x12: // ???
                   7750:        case 0x13: // ???
1.1       root     7751:        case 0x20:
                   7752:        case 0x21:
                   7753:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7754:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7755:                        int len = min(REG16(CX), scr_width * scr_height);
                   7756:                        for(int i = 0; i < len; i++) {
1.1       root     7757:                                scr_char[i] = mem[ofs++];
                   7758:                                scr_attr[i] = mem[ofs++];
1.1.1.45  root     7759:                                if(REG8(AL) & 0x01) {
1.1       root     7760:                                        ofs += 2;
                   7761:                                }
                   7762:                        }
1.1.1.60  root     7763:                        WriteConsoleOutputCharacterA(hStdout, scr_char, len, co, &num);
1.1.1.14  root     7764:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7765:                } else {
1.1.1.16  root     7766:                        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     7767:                                mem[dest++] = mem[ofs++];
                   7768:                                mem[dest++] = mem[ofs++];
1.1.1.45  root     7769:                                if(REG8(AL) & 0x01) {
1.1       root     7770:                                        ofs += 2;
                   7771:                                }
1.1.1.14  root     7772:                                if(++co.X == scr_width) {
                   7773:                                        if(++co.Y == scr_height) {
1.1       root     7774:                                                break;
                   7775:                                        }
                   7776:                                        co.X = 0;
                   7777:                                }
                   7778:                        }
                   7779:                }
                   7780:                break;
                   7781:        default:
1.1.1.22  root     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));
1.1.1.3   root     7783:                m_CF = 1;
1.1       root     7784:                break;
                   7785:        }
                   7786: }
                   7787: 
1.1.1.30  root     7788: inline void pcbios_int_10h_18h()
                   7789: {
                   7790:        switch(REG8(AL)) {
                   7791:        case 0x00:
                   7792:        case 0x01:
                   7793: //             REG8(AL) = 0x86;
                   7794:                REG8(AL) = 0x00;
                   7795:                break;
                   7796:        default:
                   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));
                   7798:                m_CF = 1;
                   7799:                break;
                   7800:        }
                   7801: }
                   7802: 
1.1.1.14  root     7803: inline void pcbios_int_10h_1ah()
                   7804: {
                   7805:        switch(REG8(AL)) {
                   7806:        case 0x00:
                   7807:                REG8(AL) = 0x1a;
                   7808:                REG8(BL) = 0x08;
                   7809:                REG8(BH) = 0x00;
                   7810:                break;
                   7811:        default:
1.1.1.22  root     7812:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.14  root     7813:                m_CF = 1;
                   7814:                break;
                   7815:        }
                   7816: }
                   7817: 
1.1       root     7818: inline void pcbios_int_10h_1dh()
                   7819: {
                   7820:        switch(REG8(AL)) {
1.1.1.43  root     7821:        case 0x00:
                   7822:                // DOS/V Shift Status Line Control is not supported
                   7823:                m_CF = 1;
                   7824:                break;
1.1       root     7825:        case 0x01:
                   7826:                break;
                   7827:        case 0x02:
                   7828:                REG16(BX) = 0;
                   7829:                break;
                   7830:        default:
1.1.1.22  root     7831:                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));
                   7832:                m_CF = 1;
                   7833:                break;
                   7834:        }
                   7835: }
                   7836: 
                   7837: inline void pcbios_int_10h_4fh()
                   7838: {
                   7839:        switch(REG8(AL)) {
                   7840:        case 0x00:
                   7841:                REG8(AH) = 0x02; // not supported
                   7842:                break;
                   7843:        case 0x01:
                   7844:        case 0x02:
                   7845:        case 0x03:
                   7846:        case 0x04:
                   7847:        case 0x05:
                   7848:        case 0x06:
                   7849:        case 0x07:
                   7850:        case 0x08:
                   7851:        case 0x09:
                   7852:        case 0x0a:
                   7853:        case 0x0b:
                   7854:        case 0x0c:
                   7855:                REG8(AH) = 0x01; // failed
                   7856:                break;
                   7857:        default:
                   7858:                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     7859:                m_CF = 1;
1.1       root     7860:                break;
                   7861:        }
                   7862: }
                   7863: 
                   7864: inline void pcbios_int_10h_82h()
                   7865: {
                   7866:        static UINT8 mode = 0;
                   7867:        
                   7868:        switch(REG8(AL)) {
1.1.1.22  root     7869:        case 0x00:
1.1       root     7870:                if(REG8(BL) != 0xff) {
                   7871:                        mode = REG8(BL);
                   7872:                }
                   7873:                REG8(AL) = mode;
                   7874:                break;
                   7875:        default:
1.1.1.22  root     7876:                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     7877:                m_CF = 1;
1.1       root     7878:                break;
                   7879:        }
                   7880: }
                   7881: 
1.1.1.22  root     7882: inline void pcbios_int_10h_83h()
                   7883: {
                   7884:        static UINT8 mode = 0;
                   7885:        
                   7886:        switch(REG8(AL)) {
                   7887:        case 0x00:
                   7888:                REG16(AX) = 0; // offset???
                   7889:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7890:                i386_load_segment_descriptor(ES);
                   7891:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7892:                break;
                   7893:        default:
                   7894:                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));
                   7895:                m_CF = 1;
                   7896:                break;
                   7897:        }
                   7898: }
                   7899: 
                   7900: inline void pcbios_int_10h_90h()
                   7901: {
                   7902:        REG8(AL) = mem[0x449];
                   7903: }
                   7904: 
                   7905: inline void pcbios_int_10h_91h()
                   7906: {
                   7907:        REG8(AL) = 0x04; // VGA
                   7908: }
                   7909: 
                   7910: inline void pcbios_int_10h_efh()
                   7911: {
                   7912:        REG16(DX) = 0xffff;
                   7913: }
                   7914: 
1.1       root     7915: inline void pcbios_int_10h_feh()
                   7916: {
                   7917:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7918:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7919:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7920:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7921:        }
                   7922:        int_10h_feh_called = true;
                   7923: }
                   7924: 
                   7925: inline void pcbios_int_10h_ffh()
                   7926: {
                   7927:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7928:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7929:                COORD co;
                   7930:                DWORD num;
                   7931:                
1.1.1.14  root     7932:                vram_flush();
                   7933:                
                   7934:                co.X = (REG16(DI) >> 1) % scr_width;
                   7935:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7936:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7937:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7938:                int len;
                   7939:                for(len = 0; ofs < end; len++) {
                   7940:                        scr_char[len] = mem[ofs++];
                   7941:                        scr_attr[len] = mem[ofs++];
                   7942:                }
                   7943:                co.Y += scr_top;
1.1.1.60  root     7944:                WriteConsoleOutputCharacterA(hStdout, scr_char, len, co, &num);
1.1.1.14  root     7945:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7946:        }
                   7947:        int_10h_ffh_called = true;
                   7948: }
                   7949: 
1.1.1.42  root     7950: int pcbios_update_drive_param(int drive_num, int force_update)
                   7951: {
                   7952:        if(drive_num >= 0 && drive_num < 26) {
                   7953:                drive_param_t *drive_param = &drive_params[drive_num];
                   7954:                
                   7955:                if(force_update || !drive_param->initialized) {
                   7956:                        drive_param->valid = 0;
                   7957:                        char dev[64];
                   7958:                        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7959:                        
1.1.1.60  root     7960:                        HANDLE hFile = CreateFileA(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.42  root     7961:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7962:                                DWORD dwSize;
                   7963:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
                   7964:                                        drive_param->valid = 1;
                   7965:                                }
                   7966:                                CloseHandle(hFile);
                   7967:                        }
                   7968:                        drive_param->initialized = 1;
                   7969:                }
                   7970:                return(drive_param->valid);
                   7971:        }
                   7972:        return(0);
                   7973: }
                   7974: 
                   7975: inline void pcbios_int_13h_00h()
                   7976: {
                   7977:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7978:        
                   7979:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7980:                REG8(AH) = 0x00; // successful completion
                   7981:        } else {
                   7982:                if(REG8(DL) & 0x80) {
                   7983:                        REG8(AH) = 0x05; // reset failed (hard disk)
                   7984:                } else {
                   7985:                        REG8(AH) = 0x80; // timeout (not ready)
                   7986:                }
                   7987:                m_CF = 1;
                   7988:        }
                   7989: }
                   7990: 
                   7991: inline void pcbios_int_13h_02h()
                   7992: {
                   7993:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7994:        
                   7995:        if(REG8(AL) == 0) {
                   7996:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7997:                m_CF = 1;
                   7998:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7999:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8000:                m_CF = 1;
                   8001:        } else {
                   8002:                drive_param_t *drive_param = &drive_params[drive_num];
                   8003:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8004:                char dev[64];
                   8005:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   8006:                
1.1.1.60  root     8007:                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     8008:                if(hFile == INVALID_HANDLE_VALUE) {
                   8009:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8010:                        m_CF = 1;
                   8011:                } else {
                   8012:                        UINT32 sector_num = REG8(AL);
                   8013:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   8014:                        UINT32 head = REG8(DH);
                   8015:                        UINT32 sector = REG8(CL) & 0x3f;
                   8016:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   8017:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   8018:                        DWORD dwSize;
                   8019:                        
                   8020: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8021: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8022: //                             m_CF = 1;
                   8023: //                     } else 
                   8024:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8025:                                REG8(AH) = 0x04; // sector not found/read error
                   8026:                                m_CF = 1;
                   8027:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   8028:                                REG8(AH) = 0x04; // sector not found/read error
                   8029:                                m_CF = 1;
                   8030:                        } else {
                   8031:                                REG8(AH) = 0x00; // successful completion
                   8032:                        }
                   8033:                        CloseHandle(hFile);
                   8034:                }
                   8035:        }
                   8036: }
                   8037: 
                   8038: inline void pcbios_int_13h_03h()
                   8039: {
                   8040:        // this operation may cause serious damage for drives, so support only floppy disk...
                   8041:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8042:        
                   8043:        if(REG8(AL) == 0) {
                   8044:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   8045:                m_CF = 1;
                   8046:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   8047:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8048:                m_CF = 1;
                   8049:        } else if(!drive_params[drive_num].is_fdd()) {
                   8050:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8051:                m_CF = 1;
                   8052:        } else {
                   8053:                drive_param_t *drive_param = &drive_params[drive_num];
                   8054:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8055:                char dev[64];
                   8056:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   8057:                
1.1.1.60  root     8058:                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     8059:                if(hFile == INVALID_HANDLE_VALUE) {
                   8060:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8061:                        m_CF = 1;
                   8062:                } else {
                   8063:                        UINT32 sector_num = REG8(AL);
                   8064:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   8065:                        UINT32 head = REG8(DH);
                   8066:                        UINT32 sector = REG8(CL) & 0x3f;
                   8067:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   8068:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   8069:                        DWORD dwSize;
                   8070:                        
                   8071: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8072: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8073: //                             m_CF = 1;
                   8074: //                     } else 
                   8075:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8076:                                REG8(AH) = 0x04; // sector not found/read error
                   8077:                                m_CF = 1;
                   8078:                        } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   8079:                                REG8(AH) = 0x04; // sector not found/read error
                   8080:                                m_CF = 1;
                   8081:                        } else {
                   8082:                                REG8(AH) = 0x00; // successful completion
                   8083:                        }
                   8084:                        CloseHandle(hFile);
                   8085:                }
                   8086:        }
                   8087: }
                   8088: 
                   8089: inline void pcbios_int_13h_04h()
                   8090: {
                   8091:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8092:        
                   8093:        if(REG8(AL) == 0) {
                   8094:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   8095:                m_CF = 1;
                   8096:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   8097:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8098:                m_CF = 1;
                   8099:        } else {
                   8100:                drive_param_t *drive_param = &drive_params[drive_num];
                   8101:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8102:                char dev[64];
                   8103:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   8104:                
1.1.1.60  root     8105:                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     8106:                if(hFile == INVALID_HANDLE_VALUE) {
                   8107:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8108:                        m_CF = 1;
                   8109:                } else {
                   8110:                        UINT32 sector_num = REG8(AL);
                   8111:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   8112:                        UINT32 head = REG8(DH);
                   8113:                        UINT32 sector = REG8(CL) & 0x3f;
                   8114:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   8115:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   8116:                        DWORD dwSize;
                   8117:                        UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
                   8118:                        
                   8119: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8120: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8121: //                             m_CF = 1;
                   8122: //                     } else 
                   8123:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8124:                                REG8(AH) = 0x04; // sector not found/read error
                   8125:                                m_CF = 1;
                   8126:                        } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   8127:                                REG8(AH) = 0x04; // sector not found/read error
                   8128:                                m_CF = 1;
                   8129:                        } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
                   8130:                                REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
                   8131:                                m_CF = 1;
                   8132:                        } else {
                   8133:                                REG8(AH) = 0x00; // successful completion
                   8134:                        }
                   8135:                        free(tmp_buffer);
                   8136:                        CloseHandle(hFile);
                   8137:                }
                   8138:        }
                   8139: }
                   8140: 
                   8141: inline void pcbios_int_13h_08h()
                   8142: {
                   8143:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8144:        
                   8145:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8146:                drive_param_t *drive_param = &drive_params[drive_num];
                   8147:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8148:                
                   8149:                REG16(AX) = 0x0000;
                   8150:                switch(geo->MediaType) {
                   8151:                case F5_360_512:
                   8152:                case F5_320_512:
                   8153:                case F5_320_1024:
                   8154:                case F5_180_512:
                   8155:                case F5_160_512:
                   8156:                        REG8(BL) = 0x01; // 320K/360K disk
                   8157:                        break;
                   8158:                case F5_1Pt2_512:
                   8159:                case F3_1Pt2_512:
                   8160:                case F3_1Pt23_1024:
                   8161:                case F5_1Pt23_1024:
                   8162:                        REG8(BL) = 0x02; // 1.2M disk
                   8163:                        break;
                   8164:                case F3_720_512:
                   8165:                case F3_640_512:
                   8166:                case F5_640_512:
                   8167:                case F5_720_512:
                   8168:                        REG8(BL) = 0x03; // 720K disk
                   8169:                        break;
                   8170:                case F3_1Pt44_512:
                   8171:                        REG8(BL) = 0x04; // 1.44M disk
                   8172:                        break;
                   8173:                case F3_2Pt88_512:
                   8174:                        REG8(BL) = 0x06; // 2.88M disk
                   8175:                        break;
                   8176:                case RemovableMedia:
                   8177:                        REG8(BL) = 0x10; // ATAPI Removable Media Device
                   8178:                        break;
                   8179:                default:
                   8180:                        REG8(BL) = 0x00; // unknown
                   8181:                        break;
                   8182:                }
                   8183:                if(REG8(DL) & 0x80) {
                   8184:                        switch(GetLogicalDrives() & 0x0c) {
                   8185:                        case 0x00: REG8(DL) = 0x00; break;
                   8186:                        case 0x04:
                   8187:                        case 0x08: REG8(DL) = 0x01; break;
                   8188:                        case 0x0c: REG8(DL) = 0x02; break;
                   8189:                        }
                   8190:                } else {
                   8191:                        switch(GetLogicalDrives() & 0x03) {
                   8192:                        case 0x00: REG8(DL) = 0x00; break;
                   8193:                        case 0x01:
                   8194:                        case 0x02: REG8(DL) = 0x01; break;
                   8195:                        case 0x03: REG8(DL) = 0x02; break;
                   8196:                        }
                   8197:                }
                   8198:                REG8(DH) = drive_param->head_num();
                   8199:                int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
                   8200:                int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
                   8201:                REG8(CH) = cyl & 0xff;
                   8202:                REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
                   8203:        } else {
                   8204:                REG8(AH) = 0x07;
                   8205:                m_CF = 1;
                   8206:        }
                   8207: }
                   8208: 
                   8209: inline void pcbios_int_13h_10h()
                   8210: {
                   8211:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8212:        
                   8213:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8214:                REG8(AH) = 0x00; // successful completion
                   8215:        } else {
                   8216:                if(REG8(DL) & 0x80) {
                   8217:                        REG8(AH) = 0xaa; // drive not ready (hard disk)
                   8218:                } else {
                   8219:                        REG8(AH) = 0x80; // timeout (not ready)
                   8220:                }
                   8221:                m_CF = 1;
                   8222:        }
                   8223: }
                   8224: 
                   8225: inline void pcbios_int_13h_15h()
                   8226: {
                   8227:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8228:        
                   8229:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8230:                if(REG8(DL) & 0x80) {
                   8231:                        REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
                   8232:                } else {
                   8233:                        REG8(AH) = 0x03; // hard disk
                   8234:                }
                   8235:        } else {
                   8236:                REG8(AH) = 0x00; // no such drive
                   8237:        }
                   8238: }
                   8239: 
1.1.1.43  root     8240: inline void pcbios_int_13h_41h()
                   8241: {
                   8242:        if(REG16(BX) == 0x55aa) {
                   8243:                // IBM/MS INT 13 Extensions is not installed
                   8244:                REG8(AH) = 0x01;
                   8245:                m_CF = 1;
                   8246:        } else {
                   8247:                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));
                   8248:                REG8(AH) = 0x01;
                   8249:                m_CF = 1;
                   8250:        }
                   8251: }
                   8252: 
1.1.1.25  root     8253: inline void pcbios_int_14h_00h()
                   8254: {
1.1.1.29  root     8255:        if(REG16(DX) < 4) {
1.1.1.25  root     8256:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   8257:                UINT8 selector = sio_read(REG16(DX), 3);
                   8258:                selector &= ~0x3f;
                   8259:                selector |= REG8(AL) & 0x1f;
                   8260:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   8261:                sio_write(REG16(DX), 3, selector | 0x80);
                   8262:                sio_write(REG16(DX), 0, divisor & 0xff);
                   8263:                sio_write(REG16(DX), 1, divisor >> 8);
                   8264:                sio_write(REG16(DX), 3, selector);
                   8265:                REG8(AH) = sio_read(REG16(DX), 5);
                   8266:                REG8(AL) = sio_read(REG16(DX), 6);
                   8267:        } else {
                   8268:                REG8(AH) = 0x80;
                   8269:        }
                   8270: }
                   8271: 
                   8272: inline void pcbios_int_14h_01h()
                   8273: {
1.1.1.29  root     8274:        if(REG16(DX) < 4) {
1.1.1.25  root     8275:                UINT8 selector = sio_read(REG16(DX), 3);
                   8276:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8277:                sio_write(REG16(DX), 0, REG8(AL));
                   8278:                sio_write(REG16(DX), 3, selector);
                   8279:                REG8(AH) = sio_read(REG16(DX), 5);
                   8280:        } else {
                   8281:                REG8(AH) = 0x80;
                   8282:        }
                   8283: }
                   8284: 
                   8285: inline void pcbios_int_14h_02h()
                   8286: {
1.1.1.29  root     8287:        if(REG16(DX) < 4) {
1.1.1.25  root     8288:                UINT8 selector = sio_read(REG16(DX), 3);
                   8289:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8290:                REG8(AL) = sio_read(REG16(DX), 0);
                   8291:                sio_write(REG16(DX), 3, selector);
                   8292:                REG8(AH) = sio_read(REG16(DX), 5);
                   8293:        } else {
                   8294:                REG8(AH) = 0x80;
                   8295:        }
                   8296: }
                   8297: 
                   8298: inline void pcbios_int_14h_03h()
                   8299: {
1.1.1.29  root     8300:        if(REG16(DX) < 4) {
1.1.1.25  root     8301:                REG8(AH) = sio_read(REG16(DX), 5);
                   8302:                REG8(AL) = sio_read(REG16(DX), 6);
                   8303:        } else {
                   8304:                REG8(AH) = 0x80;
                   8305:        }
                   8306: }
                   8307: 
                   8308: inline void pcbios_int_14h_04h()
                   8309: {
1.1.1.29  root     8310:        if(REG16(DX) < 4) {
1.1.1.25  root     8311:                UINT8 selector = sio_read(REG16(DX), 3);
                   8312:                if(REG8(CH) <= 0x03) {
                   8313:                        selector = (selector & ~0x03) | REG8(CH);
                   8314:                }
                   8315:                if(REG8(BL) == 0x00) {
                   8316:                        selector &= ~0x04;
                   8317:                } else if(REG8(BL) == 0x01) {
                   8318:                        selector |= 0x04;
                   8319:                }
                   8320:                if(REG8(BH) == 0x00) {
                   8321:                        selector = (selector & ~0x38) | 0x00;
                   8322:                } else if(REG8(BH) == 0x01) {
                   8323:                        selector = (selector & ~0x38) | 0x08;
                   8324:                } else if(REG8(BH) == 0x02) {
                   8325:                        selector = (selector & ~0x38) | 0x18;
                   8326:                } else if(REG8(BH) == 0x03) {
                   8327:                        selector = (selector & ~0x38) | 0x28;
                   8328:                } else if(REG8(BH) == 0x04) {
                   8329:                        selector = (selector & ~0x38) | 0x38;
                   8330:                }
                   8331:                if(REG8(AL) == 0x00) {
                   8332:                        selector |= 0x40;
                   8333:                } else if(REG8(AL) == 0x01) {
                   8334:                        selector &= ~0x40;
                   8335:                }
                   8336:                if(REG8(CL) <= 0x0b) {
                   8337:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   8338:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   8339:                        sio_write(REG16(DX), 3, selector | 0x80);
                   8340:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   8341:                        sio_write(REG16(DX), 1, divisor >> 8);
                   8342:                }
                   8343:                sio_write(REG16(DX), 3, selector);
                   8344:                REG8(AH) = sio_read(REG16(DX), 5);
                   8345:                REG8(AL) = sio_read(REG16(DX), 6);
                   8346:        } else {
                   8347:                REG8(AH) = 0x80;
                   8348:        }
                   8349: }
                   8350: 
                   8351: inline void pcbios_int_14h_05h()
                   8352: {
1.1.1.29  root     8353:        if(REG16(DX) < 4) {
1.1.1.25  root     8354:                if(REG8(AL) == 0x00) {
                   8355:                        REG8(BL) = sio_read(REG16(DX), 4);
                   8356:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8357:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8358:                } else if(REG8(AL) == 0x01) {
                   8359:                        sio_write(REG16(DX), 4, REG8(BL));
                   8360:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8361:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8362:                } else {
                   8363:                        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));
                   8364:                }
                   8365:        } else {
                   8366:                REG8(AH) = 0x80;
                   8367:        }
                   8368: }
                   8369: 
1.1.1.14  root     8370: inline void pcbios_int_15h_10h()
                   8371: {
1.1.1.22  root     8372:        switch(REG8(AL)) {
                   8373:        case 0x00:
1.1.1.14  root     8374:                Sleep(10);
1.1.1.35  root     8375:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     8376:                break;
                   8377:        default:
                   8378:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.14  root     8379:                REG8(AH) = 0x86;
                   8380:                m_CF = 1;
                   8381:        }
                   8382: }
                   8383: 
1.1       root     8384: inline void pcbios_int_15h_23h()
                   8385: {
                   8386:        switch(REG8(AL)) {
1.1.1.22  root     8387:        case 0x00:
1.1.1.8   root     8388:                REG8(CL) = cmos_read(0x2d);
                   8389:                REG8(CH) = cmos_read(0x2e);
1.1       root     8390:                break;
1.1.1.22  root     8391:        case 0x01:
1.1.1.8   root     8392:                cmos_write(0x2d, REG8(CL));
                   8393:                cmos_write(0x2e, REG8(CH));
1.1       root     8394:                break;
                   8395:        default:
1.1.1.22  root     8396:                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     8397:                REG8(AH) = 0x86;
1.1.1.3   root     8398:                m_CF = 1;
1.1       root     8399:                break;
                   8400:        }
                   8401: }
                   8402: 
                   8403: inline void pcbios_int_15h_24h()
                   8404: {
                   8405:        switch(REG8(AL)) {
1.1.1.22  root     8406:        case 0x00:
1.1.1.3   root     8407:                i386_set_a20_line(0);
1.1       root     8408:                REG8(AH) = 0;
                   8409:                break;
1.1.1.22  root     8410:        case 0x01:
1.1.1.3   root     8411:                i386_set_a20_line(1);
1.1       root     8412:                REG8(AH) = 0;
                   8413:                break;
1.1.1.22  root     8414:        case 0x02:
1.1       root     8415:                REG8(AH) = 0;
1.1.1.3   root     8416:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     8417:                REG16(CX) = 0;
                   8418:                break;
1.1.1.22  root     8419:        case 0x03:
1.1       root     8420:                REG16(AX) = 0;
                   8421:                REG16(BX) = 0;
                   8422:                break;
1.1.1.22  root     8423:        default:
                   8424:                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));
                   8425:                REG8(AH) = 0x86;
                   8426:                m_CF = 1;
                   8427:                break;
1.1       root     8428:        }
                   8429: }
                   8430: 
                   8431: inline void pcbios_int_15h_49h()
                   8432: {
1.1.1.27  root     8433:        REG8(AH) = 0x00;
                   8434:        REG8(BL) = 0x00; // DOS/V
1.1       root     8435: }
                   8436: 
1.1.1.22  root     8437: inline void pcbios_int_15h_50h()
                   8438: {
                   8439:        switch(REG8(AL)) {
                   8440:        case 0x00:
                   8441:        case 0x01:
                   8442:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   8443:                        REG8(AH) = 0x01; // invalid font type in bh
                   8444:                        m_CF = 1;
1.1.1.27  root     8445:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     8446:                        REG8(AH) = 0x02; // bl not zero
                   8447:                        m_CF = 1;
                   8448:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   8449:                        REG8(AH) = 0x04; // invalid code page
                   8450:                        m_CF = 1;
1.1.1.27  root     8451:                } else if(REG8(AL) == 0x01) {
                   8452:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     8453:                        m_CF = 1;
1.1.1.27  root     8454:                } else {
1.1.1.49  root     8455:                        // dummy font read routine is at fffc:000d
                   8456:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.27  root     8457:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     8458:                        REG16(BX) = 0x000d;
1.1.1.27  root     8459:                        REG8(AH) = 0x00; // success
1.1.1.22  root     8460:                }
                   8461:                break;
                   8462:        default:
                   8463:                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));
                   8464:                REG8(AH) = 0x86;
                   8465:                m_CF = 1;
                   8466:                break;
                   8467:        }
                   8468: }
                   8469: 
1.1.1.30  root     8470: inline void pcbios_int_15h_53h()
                   8471: {
                   8472:        switch(REG8(AL)) {
                   8473:        case 0x00:
                   8474:                // APM is not installed
                   8475:                REG8(AH) = 0x86;
                   8476:                m_CF = 1;
                   8477:                break;
                   8478:        default:
                   8479:                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));
                   8480:                REG8(AH) = 0x86;
                   8481:                m_CF = 1;
                   8482:                break;
                   8483:        }
                   8484: }
                   8485: 
1.1.1.43  root     8486: inline void pcbios_int_15h_84h()
                   8487: {
                   8488:        // joystick support (from DOSBox)
                   8489:        switch(REG16(DX)) {
                   8490:        case 0x00:
                   8491:                REG16(AX) = 0x00f0;
                   8492:                REG16(DX) = 0x0201;
                   8493:                m_CF = 1;
                   8494:                break;
                   8495:        case 0x01:
                   8496:                REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   8497:                m_CF = 1;
                   8498:                break;
                   8499:        default:
                   8500:                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));
                   8501:                REG8(AH) = 0x86;
                   8502:                m_CF = 1;
                   8503:                break;
                   8504:        }
                   8505: }
1.1.1.35  root     8506: 
                   8507: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     8508: {
                   8509:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     8510:        UINT32 msec = usec / 1000;
                   8511:        
1.1.1.54  root     8512:        while(msec && !m_exit) {
1.1.1.14  root     8513:                UINT32 tmp = min(msec, 100);
                   8514:                if(msec - tmp < 10) {
                   8515:                        tmp = msec;
                   8516:                }
                   8517:                Sleep(tmp);
                   8518:                msec -= tmp;
                   8519:        }
1.1.1.35  root     8520:        
                   8521: #ifdef USE_SERVICE_THREAD
                   8522:        service_exit = true;
                   8523: #endif
                   8524:        return(0);
                   8525: }
                   8526: 
                   8527: inline void pcbios_int_15h_86h()
                   8528: {
                   8529:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   8530: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8531:                if(!in_service && !in_service_29h) {
                   8532:                        start_service_loop(pcbios_int_15h_86h_thread);
                   8533:                } else {
                   8534: #endif
                   8535:                        pcbios_int_15h_86h_thread(NULL);
                   8536:                        REQUEST_HARDWRE_UPDATE();
                   8537: #ifdef USE_SERVICE_THREAD
                   8538:                }
1.1.1.35  root     8539: #endif
                   8540:        }
1.1       root     8541: }
                   8542: 
                   8543: inline void pcbios_int_15h_87h()
                   8544: {
                   8545:        // copy extended memory (from DOSBox)
                   8546:        int len = REG16(CX) * 2;
1.1.1.3   root     8547:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     8548:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   8549:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   8550:        memcpy(mem + dst, mem + src, len);
                   8551:        REG16(AX) = 0x00;
                   8552: }
                   8553: 
                   8554: inline void pcbios_int_15h_88h()
                   8555: {
1.1.1.17  root     8556:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     8557: }
                   8558: 
                   8559: inline void pcbios_int_15h_89h()
                   8560: {
1.1.1.21  root     8561: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     8562:        // switch to protected mode (from DOSBox)
                   8563:        write_io_byte(0x20, 0x10);
                   8564:        write_io_byte(0x21, REG8(BH));
                   8565:        write_io_byte(0x21, 0x00);
                   8566:        write_io_byte(0xa0, 0x10);
                   8567:        write_io_byte(0xa1, REG8(BL));
                   8568:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     8569:        i386_set_a20_line(1);
                   8570:        int ofs = SREG_BASE(ES) + REG16(SI);
                   8571:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   8572:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   8573:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   8574:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   8575: #if defined(HAS_I386)
                   8576:        m_cr[0] |= 1;
                   8577: #else
                   8578:        m_msw |= 1;
                   8579: #endif
                   8580:        SREG(DS) = 0x18;
                   8581:        SREG(ES) = 0x20;
                   8582:        SREG(SS) = 0x28;
                   8583:        i386_load_segment_descriptor(DS);
                   8584:        i386_load_segment_descriptor(ES);
                   8585:        i386_load_segment_descriptor(SS);
1.1.1.21  root     8586:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     8587:        REG16(SP) += 6;
1.1.1.3   root     8588: #if defined(HAS_I386)
1.1.1.21  root     8589:        UINT32 flags = get_flags();
                   8590:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8591:        set_flags(flags);
1.1.1.3   root     8592: #else
1.1.1.21  root     8593:        UINT32 flags = CompressFlags();
                   8594:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8595:        ExpandFlags(flags);
1.1.1.3   root     8596: #endif
1.1       root     8597:        REG16(AX) = 0x00;
1.1.1.21  root     8598:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     8599: #else
1.1.1.21  root     8600:        // i86/i186/v30: protected mode is not supported
1.1       root     8601:        REG8(AH) = 0x86;
1.1.1.3   root     8602:        m_CF = 1;
1.1       root     8603: #endif
                   8604: }
                   8605: 
1.1.1.21  root     8606: inline void pcbios_int_15h_8ah()
                   8607: {
                   8608:        UINT32 size = MAX_MEM - 0x100000;
                   8609:        REG16(AX) = size & 0xffff;
                   8610:        REG16(DX) = size >> 16;
                   8611: }
                   8612: 
1.1.1.54  root     8613: #ifdef EXT_BIOS_TOP
                   8614: inline void pcbios_int_15h_c1h()
                   8615: {
                   8616:        SREG(ES) = EXT_BIOS_TOP >> 4;
                   8617:        i386_load_segment_descriptor(ES);
                   8618: }
                   8619: #endif
                   8620: 
                   8621: void pcbios_read_from_ps2_mouse(UINT16 *data_1st, UINT16 *data_2nd, UINT16 *data_3rd)
                   8622: {
                   8623:        // from DOSBox DoPS2Callback()
                   8624:        UINT16 mdat = 0x08;
                   8625:        INT16 xdiff = mouse.position.x - mouse.prev_position.x;
                   8626:        INT16 ydiff = mouse.prev_position.y - mouse.position.y;
                   8627:        
1.1.1.59  root     8628: #if 1
                   8629:        if(xdiff > +16) xdiff = +16;
                   8630:        if(xdiff < -16) xdiff = -16;
                   8631:        if(ydiff > +16) ydiff = +16;
                   8632:        if(ydiff < -16) ydiff = -16;
                   8633: #endif
                   8634:        
1.1.1.54  root     8635:        if(mouse.buttons[0].status) {
                   8636:                mdat |= 0x01;
                   8637:        }
                   8638:        if(mouse.buttons[1].status) {
                   8639:                mdat |= 0x02;
                   8640:        }
                   8641:        mouse.prev_position.x = mouse.position.x;
                   8642:        mouse.prev_position.y = mouse.position.y;
1.1.1.59  root     8643:        
1.1.1.54  root     8644:        if((xdiff > 0xff) || (xdiff < -0xff)) {
                   8645:                mdat |= 0x40;   // x overflow
                   8646:        }
                   8647:        if((ydiff > 0xff) || (ydiff < -0xff)) {
                   8648:                mdat |= 0x80;   // y overflow
                   8649:        }
                   8650:        xdiff %= 256;
                   8651:        ydiff %= 256;
                   8652:        if(xdiff < 0) {
                   8653:                xdiff = (0x100 + xdiff);
                   8654:                mdat |= 0x10;
                   8655:        }
                   8656:        if(ydiff < 0) {
                   8657:                ydiff = (0x100 + ydiff);
                   8658:                mdat |= 0x20;
                   8659:        }
                   8660:        *data_1st = (UINT16)mdat;
                   8661:        *data_2nd = (UINT16)(xdiff % 256);
                   8662:        *data_3rd = (UINT16)(ydiff % 256);
                   8663: }
                   8664: 
                   8665: inline void pcbios_int_15h_c2h()
                   8666: {
                   8667:        static UINT8 sampling_rate = 5;
                   8668:        static UINT8 resolution = 2;
                   8669:        static UINT8 scaling = 1;
                   8670:        
                   8671:        switch(REG8(AL)) {
                   8672:        case 0x00:
1.1.1.59  root     8673:                if(REG8(BH) == 0x00) {
1.1.1.61  root     8674:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
1.1.1.59  root     8675:                        pic[1].imr |= 0x10; // disable irq12
                   8676:                        mouse.enabled_ps2 = false;
                   8677:                        REG8(AH) = 0x00; // successful
                   8678:                } else if(REG8(BH) == 0x01) {
1.1.1.61  root     8679:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), (dwConsoleMode | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS) & ~(ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE));
1.1.1.59  root     8680:                        pic[1].imr &= ~0x10; // enable irq12
                   8681:                        mouse.enabled_ps2 = true;
1.1.1.54  root     8682:                        REG8(AH) = 0x00; // successful
                   8683:                } else {
                   8684:                        REG8(AH) = 0x01; // invalid function
                   8685:                        m_CF = 1;
                   8686:                }
                   8687:                break;
                   8688:        case 0x01:
                   8689:                REG8(BH) = 0x00; // device id
                   8690:                REG8(BL) = 0xaa; // mouse
                   8691:        case 0x05:
1.1.1.61  root     8692:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
1.1.1.59  root     8693:                pic[1].imr |= 0x10; // disable irq12
                   8694:                mouse.enabled_ps2 = false;
1.1.1.54  root     8695:                sampling_rate = 5;
                   8696:                resolution = 2;
                   8697:                scaling = 1;
                   8698:                REG8(AH) = 0x00; // successful
                   8699:                break;
                   8700:        case 0x02:
                   8701:                sampling_rate = REG8(BH);
                   8702:                REG8(AH) = 0x00; // successful
                   8703:                break;
                   8704:        case 0x03:
                   8705:                resolution = REG8(BH);
                   8706:                REG8(AH) = 0x00; // successful
                   8707:                break;
                   8708:        case 0x04:
                   8709:                REG8(BH) = 0x00; // device id
                   8710:                REG8(AH) = 0x00; // successful
                   8711:                break;
                   8712:        case 0x06:
                   8713:                switch(REG8(BH)) {
                   8714:                case 0x00:
                   8715:                        REG8(BL) = 0x00;
                   8716:                        if(mouse.buttons[1].status) {
                   8717:                                REG8(BL) |= 0x01;
                   8718:                        }
                   8719:                        if(mouse.buttons[0].status) {
                   8720:                                REG8(BL) |= 0x04;
                   8721:                        }
                   8722:                        if(scaling == 2) {
                   8723:                                REG8(BL) |= 0x10;
                   8724:                        }
                   8725:                        REG8(CL) = resolution;
                   8726:                        switch(sampling_rate) {
                   8727:                        case 0:  REG8(DL) =  10; break;
                   8728:                        case 1:  REG8(DL) =  20; break;
                   8729:                        case 2:  REG8(DL) =  40; break;
                   8730:                        case 3:  REG8(DL) =  60; break;
                   8731:                        case 4:  REG8(DL) =  80; break;
                   8732: //                     case 5:  REG8(DL) = 100; break;
                   8733:                        case 6:  REG8(DL) = 200; break;
                   8734:                        default: REG8(DL) = 100; break;
                   8735:                        }
                   8736:                        REG8(AH) = 0x00; // successful
                   8737:                        break;
                   8738:                case 0x01:
                   8739:                case 0x02:
                   8740:                        scaling = REG8(BH);
                   8741:                        REG8(AH) = 0x00; // successful
                   8742:                        break;
                   8743:                default:
                   8744:                        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));
                   8745:                        REG8(AH) = 0x01; // invalid function
                   8746:                        m_CF = 1;
                   8747:                        break;
                   8748:                }
                   8749:                break;
                   8750:        case 0x07: // set device handler addr
                   8751:                mouse.call_addr_ps2.w.l = REG16(BX);
                   8752:                mouse.call_addr_ps2.w.h = SREG(ES);
                   8753:                REG8(AH) = 0x00; // successful
                   8754:                break;
                   8755:        case 0x08:
                   8756:                REG8(AH) = 0x00; // successful
                   8757:                break;
                   8758:        case 0x09:
                   8759:                {
                   8760:                        UINT16 data_1st, data_2nd, data_3rd;
                   8761:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   8762:                        REG8(BL) = (UINT8)(data_1st & 0xff);
                   8763:                        REG8(CL) = (UINT8)(data_2nd & 0xff);
                   8764:                        REG8(DL) = (UINT8)(data_3rd & 0xff);
                   8765:                }
                   8766:                REG8(AH) = 0x00; // successful
                   8767:                break;
                   8768:        default:
                   8769:                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));
                   8770: //             REG8(AH) = 0x86;
                   8771:                REG8(AH) = 0x01; // invalid function
                   8772:                m_CF = 1;
                   8773:                break;
                   8774:        }
                   8775: }
                   8776: 
1.1.1.3   root     8777: #if defined(HAS_I386)
1.1       root     8778: inline void pcbios_int_15h_c9h()
                   8779: {
                   8780:        REG8(AH) = 0x00;
                   8781:        REG8(CH) = cpu_type;
                   8782:        REG8(CL) = cpu_step;
                   8783: }
1.1.1.3   root     8784: #endif
1.1       root     8785: 
                   8786: inline void pcbios_int_15h_cah()
                   8787: {
                   8788:        switch(REG8(AL)) {
1.1.1.22  root     8789:        case 0x00:
1.1       root     8790:                if(REG8(BL) > 0x3f) {
                   8791:                        REG8(AH) = 0x03;
1.1.1.3   root     8792:                        m_CF = 1;
1.1       root     8793:                } else if(REG8(BL) < 0x0e) {
                   8794:                        REG8(AH) = 0x04;
1.1.1.3   root     8795:                        m_CF = 1;
1.1       root     8796:                } else {
1.1.1.8   root     8797:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     8798:                }
                   8799:                break;
1.1.1.22  root     8800:        case 0x01:
1.1       root     8801:                if(REG8(BL) > 0x3f) {
                   8802:                        REG8(AH) = 0x03;
1.1.1.3   root     8803:                        m_CF = 1;
1.1       root     8804:                } else if(REG8(BL) < 0x0e) {
                   8805:                        REG8(AH) = 0x04;
1.1.1.3   root     8806:                        m_CF = 1;
1.1       root     8807:                } else {
1.1.1.8   root     8808:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     8809:                }
                   8810:                break;
                   8811:        default:
1.1.1.22  root     8812:                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     8813:                REG8(AH) = 0x86;
1.1.1.3   root     8814:                m_CF = 1;
1.1       root     8815:                break;
                   8816:        }
                   8817: }
                   8818: 
1.1.1.22  root     8819: inline void pcbios_int_15h_e8h()
1.1.1.17  root     8820: {
1.1.1.22  root     8821:        switch(REG8(AL)) {
                   8822: #if defined(HAS_I386)
                   8823:        case 0x01:
                   8824:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   8825:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8826:                break;
1.1.1.17  root     8827: #endif
1.1.1.22  root     8828:        default:
                   8829:                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));
                   8830:                REG8(AH) = 0x86;
                   8831:                m_CF = 1;
                   8832:                break;
                   8833:        }
                   8834: }
1.1.1.17  root     8835: 
1.1.1.55  root     8836: bool pcbios_is_key_buffer_empty()
                   8837: {
                   8838:        return(*(UINT16 *)(mem + 0x41a) == *(UINT16 *)(mem + 0x41c));
                   8839: }
                   8840: 
1.1.1.51  root     8841: void pcbios_clear_key_buffer()
                   8842: {
                   8843:        key_buf_char->clear();
                   8844:        key_buf_scan->clear();
                   8845:        
                   8846:        // update key buffer
                   8847:        *(UINT16 *)(mem + 0x41a) = *(UINT16 *)(mem + 0x41c); // head = tail
                   8848: }
                   8849: 
                   8850: void pcbios_set_key_buffer(int key_char, int key_scan)
                   8851: {
                   8852:        // update key buffer
                   8853:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8854:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8855:        UINT16 next = tail + 2;
                   8856:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8857:                next = *(UINT16 *)(mem + 0x480);
                   8858:        }
                   8859:        if(next != head) {
                   8860:                *(UINT16 *)(mem + 0x41c) = next;
                   8861:                mem[0x400 + (tail++)] = key_char;
                   8862:                mem[0x400 + (tail++)] = key_scan;
1.1.1.55  root     8863:        } else {
                   8864:                // store to extra key buffer
                   8865:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8866:                        key_buf_char->write(key_char);
                   8867:                        key_buf_scan->write(key_scan);
                   8868:                }
1.1.1.51  root     8869:        }
                   8870: }
                   8871: 
                   8872: bool pcbios_get_key_buffer(int *key_char, int *key_scan)
                   8873: {
                   8874:        // update key buffer
                   8875:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8876:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8877:        UINT16 next = head + 2;
                   8878:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8879:                next = *(UINT16 *)(mem + 0x480);
                   8880:        }
                   8881:        if(head != tail) {
                   8882:                *(UINT16 *)(mem + 0x41a) = next;
1.1.1.55  root     8883:                *key_char = mem[0x400 + (head++)];
                   8884:                *key_scan = mem[0x400 + (head++)];
                   8885:                
                   8886:                // restore from extra key buffer
                   8887:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8888:                        if(!key_buf_char->empty()) {
                   8889:                                pcbios_set_key_buffer(key_buf_char->read(), key_buf_scan->read());
                   8890:                        }
                   8891:                }
                   8892:                return(true);
                   8893:        } else {
                   8894:                *key_char = 0x00;
                   8895:                *key_scan = 0x00;
                   8896:                return(false);
1.1.1.51  root     8897:        }
                   8898: }
                   8899: 
1.1.1.60  root     8900: bool pcbios_check_key_buffer(int *key_char, int *key_scan)
                   8901: {
                   8902:        // do not remove from key buffer
                   8903:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8904:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8905:        if(head != tail) {
                   8906:                *key_char = mem[0x400 + (head++)];
                   8907:                *key_scan = mem[0x400 + (head++)];
                   8908:                return(true);
                   8909:        } else {
                   8910:                *key_char = 0x00;
                   8911:                *key_scan = 0x00;
                   8912:                return(false);
                   8913:        }
                   8914: }
                   8915: 
1.1.1.33  root     8916: void pcbios_update_key_code(bool wait)
1.1       root     8917: {
1.1.1.32  root     8918:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8919: #ifdef USE_SERVICE_THREAD
                   8920:                EnterCriticalSection(&key_buf_crit_sect);
                   8921: #endif
1.1.1.55  root     8922:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     8923: #ifdef USE_SERVICE_THREAD
                   8924:                LeaveCriticalSection(&key_buf_crit_sect);
                   8925: #endif
                   8926:                if(empty) {
1.1.1.32  root     8927:                        if(!update_key_buffer()) {
1.1.1.33  root     8928:                                if(wait) {
1.1.1.32  root     8929:                                        Sleep(10);
                   8930:                                } else {
                   8931:                                        maybe_idle();
                   8932:                                }
1.1.1.14  root     8933:                        }
                   8934:                }
1.1.1.34  root     8935:        }
                   8936:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8937: #ifdef USE_SERVICE_THREAD
                   8938:                EnterCriticalSection(&key_buf_crit_sect);
                   8939: #endif
1.1.1.51  root     8940:                int key_char, key_scan;
                   8941:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8942:                        key_code  = key_char << 0;
                   8943:                        key_code |= key_scan << 8;
1.1.1.35  root     8944:                        key_recv  = 0x0000ffff;
1.1.1.51  root     8945:                }
                   8946:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8947:                        key_code |= key_char << 16;
                   8948:                        key_code |= key_scan << 24;
1.1.1.33  root     8949:                        key_recv |= 0xffff0000;
1.1.1.32  root     8950:                }
1.1.1.35  root     8951: #ifdef USE_SERVICE_THREAD
                   8952:                LeaveCriticalSection(&key_buf_crit_sect);
                   8953: #endif
1.1       root     8954:        }
                   8955: }
                   8956: 
1.1.1.35  root     8957: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     8958: {
1.1.1.54  root     8959:        while(key_recv == 0 && !m_exit) {
1.1.1.33  root     8960:                pcbios_update_key_code(true);
1.1       root     8961:        }
1.1.1.33  root     8962:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8963:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   8964:                        if(REG8(AH) == 0x10) {
                   8965:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   8966:                        } else {
                   8967:                                key_code = ((key_code >> 16) & 0xff00);
                   8968:                        }
                   8969:                        key_recv >>= 16;
1.1       root     8970:                }
                   8971:        }
                   8972:        REG16(AX) = key_code & 0xffff;
                   8973:        key_code >>= 16;
1.1.1.33  root     8974:        key_recv >>= 16;
1.1.1.35  root     8975:        
                   8976: #ifdef USE_SERVICE_THREAD
                   8977:        service_exit = true;
                   8978: #endif
                   8979:        return(0);
                   8980: }
                   8981: 
                   8982: inline void pcbios_int_16h_00h()
                   8983: {
                   8984: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8985:        if(!in_service && !in_service_29h) {
                   8986:                start_service_loop(pcbios_int_16h_00h_thread);
                   8987:        } else {
                   8988: #endif
                   8989:                pcbios_int_16h_00h_thread(NULL);
                   8990:                REQUEST_HARDWRE_UPDATE();
                   8991: #ifdef USE_SERVICE_THREAD
                   8992:        }
1.1.1.35  root     8993: #endif
1.1       root     8994: }
                   8995: 
                   8996: inline void pcbios_int_16h_01h()
                   8997: {
1.1.1.33  root     8998:        if(key_recv == 0) {
                   8999:                pcbios_update_key_code(false);
1.1.1.5   root     9000:        }
1.1.1.33  root     9001:        if(key_recv != 0) {
                   9002:                UINT32 key_code_tmp = key_code;
                   9003:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   9004:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   9005:                                if(REG8(AH) == 0x11) {
                   9006:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   9007:                                } else {
                   9008:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   9009:                                }
                   9010:                        }
1.1       root     9011:                }
1.1.1.5   root     9012:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     9013: #if defined(HAS_I386)
1.1.1.33  root     9014:                m_ZF = 0;
                   9015: #else
                   9016:                m_ZeroVal = 1;
                   9017: #endif
                   9018:        } else {
                   9019: #if defined(HAS_I386)
                   9020:                m_ZF = 1;
1.1.1.3   root     9021: #else
1.1.1.33  root     9022:                m_ZeroVal = 0;
1.1.1.3   root     9023: #endif
1.1.1.33  root     9024:        }
1.1       root     9025: }
                   9026: 
                   9027: inline void pcbios_int_16h_02h()
                   9028: {
                   9029:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   9030:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   9031:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   9032:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   9033:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   9034:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   9035:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   9036:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   9037: }
                   9038: 
                   9039: inline void pcbios_int_16h_03h()
                   9040: {
                   9041:        static UINT16 status = 0;
                   9042:        
                   9043:        switch(REG8(AL)) {
                   9044:        case 0x05:
                   9045:                status = REG16(BX);
                   9046:                break;
                   9047:        case 0x06:
                   9048:                REG16(BX) = status;
                   9049:                break;
                   9050:        default:
1.1.1.3   root     9051:                m_CF = 1;
1.1       root     9052:                break;
                   9053:        }
                   9054: }
                   9055: 
                   9056: inline void pcbios_int_16h_05h()
                   9057: {
1.1.1.32  root     9058:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     9059: #ifdef USE_SERVICE_THREAD
                   9060:                EnterCriticalSection(&key_buf_crit_sect);
                   9061: #endif
1.1.1.51  root     9062:                pcbios_set_key_buffer(REG8(CL), REG8(CH));
1.1.1.35  root     9063: #ifdef USE_SERVICE_THREAD
                   9064:                LeaveCriticalSection(&key_buf_crit_sect);
                   9065: #endif
1.1.1.32  root     9066:        }
1.1       root     9067:        REG8(AL) = 0x00;
                   9068: }
                   9069: 
1.1.1.60  root     9070: inline void pcbios_int_16h_09h()
                   9071: {
                   9072:        REG8(AL)  = 0x00;
                   9073: //     REG8(AL) |= 0x01;       // INT 16/AX=0300h supported    (set default delay and rate (PCjr and some PS/2))
                   9074: //     REG8(AL) |= 0x02;       // INT 16/AX=0304h supported    (turn off typematic repeat (PCjr and some PS/2))
                   9075:        REG8(AL) |= 0x04;       // INT 16/AX=0305h supported    (set repeat rate and delay (AT,PS))
                   9076:        REG8(AL) |= 0x08;       // INT 16/AX=0306h supported    (get current typematic rate and delay (newer PS/2s))
                   9077:        REG8(AL) |= 0x10;       // INT 16/AH=0Ah supported      (get keyboard id)
                   9078:        REG8(AL) |= 0x20;       // INT 16/AH=10h-12h supported  (enhanced keyboard support)
                   9079: //     REG8(AL) |= 0x40;       // INT 16/AH=20h-22h supported  (122-key keyboard support)
                   9080: //     REG8(AL) |= 0x80;       // reserved
                   9081: }
                   9082: 
                   9083: inline void pcbios_int_16h_0ah()
                   9084: {
                   9085: //     REG16(BX) = 0x41ab;     // MF2 Keyboard (usually in translate mode)
                   9086:        REG16(BX) = 0x83ab;     // MF2 Keyboard (pass-through mode)
                   9087: }
                   9088: 
                   9089: inline void pcbios_int_16h_11h()
                   9090: {
                   9091:        int key_char, key_scan;
                   9092:        
                   9093: #ifdef USE_SERVICE_THREAD
                   9094:        EnterCriticalSection(&key_buf_crit_sect);
                   9095: #endif
                   9096:        if(pcbios_check_key_buffer(&key_char, &key_scan)) {
                   9097:                REG8(AL) = key_char;
                   9098:                REG8(AH) = key_scan;
                   9099: #if defined(HAS_I386)
                   9100:                m_ZF = 0;
                   9101: #else
                   9102:                m_ZeroVal = 1;
                   9103: #endif
                   9104:        } else {
                   9105: #if defined(HAS_I386)
                   9106:                m_ZF = 1;
                   9107: #else
                   9108:                m_ZeroVal = 0;
                   9109: #endif
                   9110:        }
                   9111: #ifdef USE_SERVICE_THREAD
                   9112:        LeaveCriticalSection(&key_buf_crit_sect);
                   9113: #endif
                   9114: }
                   9115: 
1.1       root     9116: inline void pcbios_int_16h_12h()
                   9117: {
                   9118:        pcbios_int_16h_02h();
                   9119:        
                   9120:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   9121:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   9122:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   9123:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   9124:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   9125:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   9126:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   9127:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   9128: }
                   9129: 
                   9130: inline void pcbios_int_16h_13h()
                   9131: {
                   9132:        static UINT16 status = 0;
                   9133:        
                   9134:        switch(REG8(AL)) {
                   9135:        case 0x00:
                   9136:                status = REG16(DX);
                   9137:                break;
                   9138:        case 0x01:
                   9139:                REG16(DX) = status;
                   9140:                break;
                   9141:        default:
1.1.1.22  root     9142:                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     9143:                m_CF = 1;
1.1       root     9144:                break;
                   9145:        }
                   9146: }
                   9147: 
                   9148: inline void pcbios_int_16h_14h()
                   9149: {
                   9150:        static UINT8 status = 0;
                   9151:        
                   9152:        switch(REG8(AL)) {
                   9153:        case 0x00:
                   9154:        case 0x01:
                   9155:                status = REG8(AL);
                   9156:                break;
                   9157:        case 0x02:
                   9158:                REG8(AL) = status;
                   9159:                break;
                   9160:        default:
1.1.1.22  root     9161:                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     9162:                m_CF = 1;
1.1       root     9163:                break;
                   9164:        }
                   9165: }
                   9166: 
1.1.1.24  root     9167: inline void pcbios_int_16h_55h()
                   9168: {
                   9169:        switch(REG8(AL)) {
                   9170:        case 0x00:
                   9171:                // keyboard tsr is not present
                   9172:                break;
                   9173:        case 0xfe:
                   9174:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   9175:                break;
                   9176:        case 0xff:
                   9177:                break;
                   9178:        default:
                   9179:                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));
                   9180:                m_CF = 1;
                   9181:                break;
                   9182:        }
                   9183: }
                   9184: 
1.1.1.30  root     9185: inline void pcbios_int_16h_6fh()
                   9186: {
                   9187:        switch(REG8(AL)) {
                   9188:        case 0x00:
                   9189:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   9190:                break;
                   9191:        default:
                   9192:                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));
                   9193:                m_CF = 1;
                   9194:                break;
                   9195:        }
                   9196: }
                   9197: 
1.1.1.37  root     9198: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   9199: {
                   9200:        UINT8 hi = jis >> 8;
                   9201:        UINT8 lo = jis & 0xff;
                   9202:        
                   9203:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   9204:        hi = (hi - 0x21) / 2 + 0x81;
                   9205:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   9206:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   9207:        
                   9208:        return((hi << 8) + lo);
                   9209: }
                   9210: 
                   9211: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   9212: {
                   9213:        UINT8 hi = sjis >> 8;
                   9214:        UINT8 lo = sjis & 0xff;
                   9215:        
                   9216:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   9217:                return(0x2121);
                   9218:        }
                   9219:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   9220:                return(0x2121);
                   9221:        }
                   9222:        if(hi >= 0xf0 && hi <= 0xf3) {
                   9223:                // gaiji
                   9224:                if(lo >= 0x40 && lo <= 0x7e) {
                   9225:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   9226:                }
                   9227:                if(lo >= 0x80 && lo <= 0x9e) {
                   9228:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   9229:                }
                   9230:                if(lo >= 0x9f && lo <= 0xfc) {
                   9231:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   9232:                }
                   9233:        }
                   9234:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   9235:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   9236:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   9237:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   9238:        
                   9239:        return((hi << 8) + lo);
                   9240: }
                   9241: 
1.1.1.38  root     9242: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   9243: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   9244: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     9245: 
                   9246: void pcbios_printer_out(int c, UINT8 data)
                   9247: {
                   9248:        if(pio[c].conv_mode) {
                   9249:                if(pio[c].sjis_hi != 0) {
                   9250:                        if(!pio[c].jis_mode) {
                   9251:                                printer_out(c, 0x1c);
                   9252:                                printer_out(c, 0x26);
                   9253:                                pio[c].jis_mode = true;
                   9254:                        }
                   9255:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   9256:                        printer_out(c, jis >> 8);
                   9257:                        printer_out(c, jis & 0xff);
                   9258:                        pio[c].sjis_hi = 0;
                   9259:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   9260:                        printer_out(c, data);
                   9261:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   9262:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   9263:                        }
                   9264:                        pio[c].esc_len++;
                   9265:                        
                   9266:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     9267:                        case 0x33: // 1Bh 33h XX
                   9268:                        case 0x4a: // 1Bh 4Ah XX
                   9269:                        case 0x4e: // 1Bh 4Eh XX
                   9270:                        case 0x51: // 1Bh 51h XX
                   9271:                        case 0x55: // 1Bh 55h XX
                   9272:                        case 0x6c: // 1Bh 6Ch XX
                   9273:                        case 0x71: // 1Bh 71h XX
                   9274:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     9275:                                if(pio[c].esc_len == 3) {
                   9276:                                        pio[c].esc_buf[0] = 0x00;
                   9277:                                }
                   9278:                                break;
1.1.1.38  root     9279:                        case 0x24: // 1Bh 24h XX XX
                   9280:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     9281:                                if(pio[c].esc_len == 4) {
                   9282:                                        pio[c].esc_buf[0] = 0x00;
                   9283:                                }
                   9284:                                break;
1.1.1.38  root     9285:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     9286:                                if(pio[c].esc_len >= 3) {
                   9287:                                        switch(pio[c].esc_buf[2]) {
                   9288:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   9289:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   9290:                                                        pio[c].esc_buf[0] = 0x00;
                   9291:                                                }
                   9292:                                                break;
                   9293:                                        case 32: case 33: case 38: case 39: case 40:
                   9294:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   9295:                                                        pio[c].esc_buf[0] = 0x00;
                   9296:                                                }
                   9297:                                                break;
1.1.1.38  root     9298:                                        case 71: case 72: case 73:
                   9299:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   9300:                                                        pio[c].esc_buf[0] = 0x00;
                   9301:                                                }
                   9302:                                                break;
1.1.1.37  root     9303:                                        default:
                   9304:                                                pio[c].esc_buf[0] = 0x00;
                   9305:                                                break;
                   9306:                                        }
                   9307:                                }
                   9308:                                break;
1.1.1.38  root     9309:                        case 0x40: // 1Bh 40h
1.1.1.37  root     9310:                                if(pio[c].jis_mode) {
                   9311:                                        printer_out(c, 0x1c);
                   9312:                                        printer_out(c, 0x2e);
                   9313:                                        pio[c].jis_mode = false;
                   9314:                                }
                   9315:                                pio[c].esc_buf[0] = 0x00;
                   9316:                                break;
1.1.1.38  root     9317:                        case 0x42: // 1Bh 42h data 00h
                   9318:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     9319:                                if(pio[c].esc_len >= 3 && data == 0) {
                   9320:                                        pio[c].esc_buf[0] = 0x00;
                   9321:                                }
                   9322:                                break;
1.1.1.38  root     9323:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     9324:                                if(pio[c].esc_len >= 3 && data != 0) {
                   9325:                                        pio[c].esc_buf[0] = 0x00;
                   9326:                                }
                   9327:                                break;
1.1.1.38  root     9328:                        default: // 1Bh XX
1.1.1.37  root     9329:                                pio[c].esc_buf[0] = 0x00;
                   9330:                                break;
                   9331:                        }
                   9332:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   9333:                        printer_out(c, data);
                   9334:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   9335:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   9336:                        }
                   9337:                        pio[c].esc_len++;
                   9338:                        
                   9339:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     9340:                        case 0x21: // 1Ch 21h XX
                   9341:                        case 0x2d: // 1Ch 2Dh XX
                   9342:                        case 0x57: // 1Ch 57h XX
                   9343:                        case 0x6b: // 1Ch 6Bh XX
                   9344:                        case 0x72: // 1Ch 72h XX
                   9345:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     9346:                                if(pio[c].esc_len == 3) {
                   9347:                                        pio[c].esc_buf[0] = 0x00;
                   9348:                                }
                   9349:                                break;
1.1.1.38  root     9350:                        case 0x26: // 1Ch 26h
1.1.1.37  root     9351:                                pio[c].jis_mode = true;
                   9352:                                pio[c].esc_buf[0] = 0x00;
                   9353:                                break;
1.1.1.38  root     9354:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     9355:                                pio[c].jis_mode = false;
                   9356:                                pio[c].esc_buf[0] = 0x00;
                   9357:                                break;
1.1.1.38  root     9358:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     9359:                                if(pio[c].esc_len == 76) {
                   9360:                                        pio[c].esc_buf[0] = 0x00;
                   9361:                                }
                   9362:                                break;
1.1.1.38  root     9363:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     9364:                                if(pio[c].esc_len == 6) {
                   9365:                                        pio[c].esc_buf[0] = 0x00;
                   9366:                                }
                   9367:                                break;
1.1.1.38  root     9368:                        case 0x53: // 1Ch 53h XX XX
                   9369:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     9370:                                if(pio[c].esc_len == 4) {
                   9371:                                        pio[c].esc_buf[0] = 0x00;
                   9372:                                }
                   9373:                                break;
1.1.1.38  root     9374:                        default: // 1Ch XX
1.1.1.37  root     9375:                                pio[c].esc_buf[0] = 0x00;
                   9376:                                break;
                   9377:                        }
                   9378:                } else if(data == 0x1b || data == 0x1c) {
                   9379:                        printer_out(c, data);
                   9380:                        pio[c].esc_buf[0] = data;
                   9381:                        pio[c].esc_len = 1;
                   9382:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   9383:                        pio[c].sjis_hi = data;
                   9384:                } else {
                   9385:                        if(pio[c].jis_mode) {
                   9386:                                printer_out(c, 0x1c);
                   9387:                                printer_out(c, 0x2e);
                   9388:                                pio[c].jis_mode = false;
                   9389:                        }
                   9390:                        printer_out(c, data);
                   9391:                }
                   9392:        } else {
                   9393:                if(pio[c].jis_mode) {
                   9394:                        printer_out(c, 0x1c);
                   9395:                        printer_out(c, 0x2e);
                   9396:                        pio[c].jis_mode = false;
                   9397:                }
                   9398:                printer_out(c, data);
                   9399:        }
                   9400: }
                   9401: 
                   9402: inline void pcbios_int_17h_00h()
                   9403: {
                   9404:        if(REG16(DX) < 3) {
                   9405:                pcbios_printer_out(REG16(DX), REG8(AL));
                   9406:                REG8(AH) = 0xd0;
                   9407:        }
                   9408: }
                   9409: 
                   9410: inline void pcbios_int_17h_01h()
                   9411: {
                   9412:        if(REG16(DX) < 3) {
                   9413:                REG8(AH) = 0xd0;
                   9414:        }
                   9415: }
                   9416: 
                   9417: inline void pcbios_int_17h_02h()
                   9418: {
                   9419:        if(REG16(DX) < 3) {
                   9420:                REG8(AH) = 0xd0;
                   9421:        }
                   9422: }
                   9423: 
                   9424: inline void pcbios_int_17h_03h()
                   9425: {
                   9426:        switch(REG8(AL)) {
                   9427:        case 0x00:
                   9428:                if(REG16(DX) < 3) {
                   9429:                        if(pio[REG16(DX)].jis_mode) {
                   9430:                                printer_out(REG16(DX), 0x1c);
                   9431:                                printer_out(REG16(DX), 0x2e);
                   9432:                                pio[REG16(DX)].jis_mode = false;
                   9433:                        }
                   9434:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   9435:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   9436:                        }
                   9437:                        REG16(CX) = 0x0000;
                   9438:                        REG8(AH) = 0xd0;
                   9439:                }
                   9440:                break;
                   9441:        default:
                   9442:                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));
                   9443:                break;
                   9444:        }
                   9445: }
                   9446: 
                   9447: inline void pcbios_int_17h_50h()
                   9448: {
                   9449:        switch(REG8(AL)) {
                   9450:        case 0x00:
                   9451:                if(REG16(DX) < 3) {
                   9452:                        if(REG16(BX) = 0x0001) {
                   9453:                                pio[REG16(DX)].conv_mode = false;
                   9454:                                REG8(AL) = 0x00;
                   9455:                        } else if(REG16(BX) = 0x0051) {
                   9456:                                pio[REG16(DX)].conv_mode = true;
                   9457:                                REG8(AL) = 0x00;
                   9458:                        } else {
                   9459:                                REG8(AL) = 0x01;
                   9460:                        }
                   9461:                } else {
                   9462:                        REG8(AL) = 0x02;
                   9463:                }
                   9464:                break;
                   9465:        case 0x01:
                   9466:                if(REG16(DX) < 3) {
                   9467:                        if(pio[REG16(DX)].conv_mode) {
                   9468:                                REG16(BX) = 0x0051;
                   9469:                        } else {
                   9470:                                REG16(BX) = 0x0001;
                   9471:                        }
                   9472:                        REG8(AL) = 0x00;
                   9473:                } else {
                   9474:                        REG8(AL) = 0x02;
                   9475:                }
                   9476:                break;
                   9477:        default:
                   9478:                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));
                   9479:                break;
                   9480:        }
                   9481: }
                   9482: 
                   9483: inline void pcbios_int_17h_51h()
                   9484: {
                   9485:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   9486:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   9487:        } else {
                   9488:                REG16(DX) = 0x0000;
                   9489:        }
                   9490: }
                   9491: 
                   9492: inline void pcbios_int_17h_52h()
                   9493: {
                   9494:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   9495:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   9496:        } else {
                   9497:                REG16(DX) = 0x0000;
                   9498:        }
                   9499: }
                   9500: 
                   9501: inline void pcbios_int_17h_84h()
                   9502: {
                   9503:        if(REG16(DX) < 3) {
                   9504:                if(pio[REG16(DX)].jis_mode) {
                   9505:                        printer_out(REG16(DX), 0x1c);
                   9506:                        printer_out(REG16(DX), 0x2e);
                   9507:                        pio[REG16(DX)].jis_mode = false;
                   9508:                }
                   9509:                printer_out(REG16(DX), REG8(AL));
                   9510:                REG8(AH) = 0xd0;
                   9511:        }
                   9512: }
                   9513: 
                   9514: inline void pcbios_int_17h_85h()
                   9515: {
                   9516:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   9517: }
                   9518: 
1.1       root     9519: inline void pcbios_int_1ah_00h()
                   9520: {
1.1.1.19  root     9521:        pcbios_update_daily_timer_counter(timeGetTime());
                   9522:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   9523:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   9524:        REG8(AL) = mem[0x470];
                   9525:        mem[0x470] = 0;
1.1       root     9526: }
                   9527: 
                   9528: inline int to_bcd(int t)
                   9529: {
                   9530:        int u = (t % 100) / 10;
                   9531:        return (u << 4) | (t % 10);
                   9532: }
                   9533: 
                   9534: inline void pcbios_int_1ah_02h()
                   9535: {
                   9536:        SYSTEMTIME time;
                   9537:        
                   9538:        GetLocalTime(&time);
                   9539:        REG8(CH) = to_bcd(time.wHour);
                   9540:        REG8(CL) = to_bcd(time.wMinute);
                   9541:        REG8(DH) = to_bcd(time.wSecond);
                   9542:        REG8(DL) = 0x00;
                   9543: }
                   9544: 
                   9545: inline void pcbios_int_1ah_04h()
                   9546: {
                   9547:        SYSTEMTIME time;
                   9548:        
                   9549:        GetLocalTime(&time);
                   9550:        REG8(CH) = to_bcd(time.wYear / 100);
                   9551:        REG8(CL) = to_bcd(time.wYear);
                   9552:        REG8(DH) = to_bcd(time.wMonth);
                   9553:        REG8(DL) = to_bcd(time.wDay);
                   9554: }
                   9555: 
                   9556: inline void pcbios_int_1ah_0ah()
                   9557: {
                   9558:        SYSTEMTIME time;
                   9559:        FILETIME file_time;
                   9560:        WORD dos_date, dos_time;
                   9561:        
                   9562:        GetLocalTime(&time);
                   9563:        SystemTimeToFileTime(&time, &file_time);
                   9564:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   9565:        REG16(CX) = dos_date;
                   9566: }
                   9567: 
                   9568: // msdos system call
                   9569: 
1.1.1.43  root     9570: inline void msdos_int_21h_56h(int lfn);
                   9571: 
1.1       root     9572: inline void msdos_int_21h_00h()
                   9573: {
1.1.1.3   root     9574:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     9575: }
                   9576: 
1.1.1.35  root     9577: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     9578: {
                   9579:        REG8(AL) = msdos_getche();
1.1.1.33  root     9580:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9581:        
1.1.1.35  root     9582: #ifdef USE_SERVICE_THREAD
                   9583:        service_exit = true;
                   9584: #endif
                   9585:        return(0);
                   9586: }
                   9587: 
                   9588: inline void msdos_int_21h_01h()
                   9589: {
                   9590: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9591:        if(!in_service && !in_service_29h &&
1.1.1.58  root     9592:           *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     9593:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9594:                // msdos_putch() will be used in this service
                   9595:                // if int 29h is hooked, run this service in main thread to call int 29h
                   9596:                start_service_loop(msdos_int_21h_01h_thread);
                   9597:        } else {
                   9598: #endif
                   9599:                msdos_int_21h_01h_thread(NULL);
                   9600:                REQUEST_HARDWRE_UPDATE();
                   9601: #ifdef USE_SERVICE_THREAD
                   9602:        }
1.1.1.35  root     9603: #endif
1.1       root     9604: }
                   9605: 
                   9606: inline void msdos_int_21h_02h()
                   9607: {
1.1.1.33  root     9608:        UINT8 data = REG8(DL);
                   9609:        msdos_putch(data);
                   9610:        REG8(AL) = data;
                   9611:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9612: }
                   9613: 
                   9614: inline void msdos_int_21h_03h()
                   9615: {
                   9616:        REG8(AL) = msdos_aux_in();
                   9617: }
                   9618: 
                   9619: inline void msdos_int_21h_04h()
                   9620: {
                   9621:        msdos_aux_out(REG8(DL));
                   9622: }
                   9623: 
                   9624: inline void msdos_int_21h_05h()
                   9625: {
                   9626:        msdos_prn_out(REG8(DL));
                   9627: }
                   9628: 
                   9629: inline void msdos_int_21h_06h()
                   9630: {
                   9631:        if(REG8(DL) == 0xff) {
                   9632:                if(msdos_kbhit()) {
                   9633:                        REG8(AL) = msdos_getch();
1.1.1.3   root     9634: #if defined(HAS_I386)
                   9635:                        m_ZF = 0;
                   9636: #else
                   9637:                        m_ZeroVal = 1;
                   9638: #endif
1.1       root     9639:                } else {
                   9640:                        REG8(AL) = 0;
1.1.1.3   root     9641: #if defined(HAS_I386)
                   9642:                        m_ZF = 1;
                   9643: #else
                   9644:                        m_ZeroVal = 0;
                   9645: #endif
1.1.1.14  root     9646:                        maybe_idle();
1.1       root     9647:                }
                   9648:        } else {
1.1.1.33  root     9649:                UINT8 data = REG8(DL);
                   9650:                msdos_putch(data);
                   9651:                REG8(AL) = data;
1.1       root     9652:        }
                   9653: }
                   9654: 
1.1.1.35  root     9655: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     9656: {
                   9657:        REG8(AL) = msdos_getch();
1.1.1.26  root     9658:        
1.1.1.35  root     9659: #ifdef USE_SERVICE_THREAD
                   9660:        service_exit = true;
                   9661: #endif
                   9662:        return(0);
1.1       root     9663: }
                   9664: 
1.1.1.35  root     9665: inline void msdos_int_21h_07h()
                   9666: {
                   9667: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9668:        if(!in_service && !in_service_29h) {
                   9669:                start_service_loop(msdos_int_21h_07h_thread);
                   9670:        } else {
                   9671: #endif
                   9672:                msdos_int_21h_07h_thread(NULL);
                   9673:                REQUEST_HARDWRE_UPDATE();
                   9674: #ifdef USE_SERVICE_THREAD
                   9675:        }
1.1.1.35  root     9676: #endif
                   9677: }
                   9678: 
                   9679: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     9680: {
                   9681:        REG8(AL) = msdos_getch();
1.1.1.33  root     9682:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9683:        
1.1.1.35  root     9684: #ifdef USE_SERVICE_THREAD
                   9685:        service_exit = true;
                   9686: #endif
                   9687:        return(0);
                   9688: }
                   9689: 
                   9690: inline void msdos_int_21h_08h()
                   9691: {
                   9692: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9693:        if(!in_service && !in_service_29h) {
                   9694:                start_service_loop(msdos_int_21h_08h_thread);
                   9695:        } else {
                   9696: #endif
                   9697:                msdos_int_21h_08h_thread(NULL);
                   9698:                REQUEST_HARDWRE_UPDATE();
                   9699: #ifdef USE_SERVICE_THREAD
                   9700:        }
1.1.1.35  root     9701: #endif
1.1       root     9702: }
                   9703: 
                   9704: inline void msdos_int_21h_09h()
                   9705: {
1.1.1.21  root     9706:        msdos_stdio_reopen();
                   9707:        
1.1.1.20  root     9708:        process_t *process = msdos_process_info_get(current_psp);
                   9709:        int fd = msdos_psp_get_file_table(1, current_psp);
                   9710:        
1.1.1.14  root     9711:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   9712:        int len = 0;
1.1       root     9713:        
1.1.1.14  root     9714:        while(str[len] != '$' && len < 0x10000) {
                   9715:                len++;
                   9716:        }
1.1.1.20  root     9717:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9718:                // stdout is redirected to file
1.1.1.20  root     9719:                msdos_write(fd, str, len);
1.1       root     9720:        } else {
                   9721:                for(int i = 0; i < len; i++) {
1.1.1.14  root     9722:                        msdos_putch(str[i]);
1.1       root     9723:                }
                   9724:        }
1.1.1.33  root     9725:        REG8(AL) = '$';
                   9726:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9727: }
                   9728: 
1.1.1.35  root     9729: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     9730: {
1.1.1.3   root     9731:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     9732:        int max = mem[ofs] - 1;
                   9733:        UINT8 *buf = mem + ofs + 2;
                   9734:        int chr, p = 0;
                   9735:        
                   9736:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     9737:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     9738:                        p = 0;
1.1.1.33  root     9739:                        msdos_putch(0x03);
                   9740:                        msdos_putch(0x0d);
                   9741:                        msdos_putch(0x0a);
1.1.1.26  root     9742:                        break;
1.1.1.33  root     9743:                } else if(ctrl_break_pressed) {
                   9744:                        // skip this byte
1.1.1.26  root     9745:                } else if(chr == 0x00) {
1.1       root     9746:                        // skip 2nd byte
                   9747:                        msdos_getch();
                   9748:                } else if(chr == 0x08) {
                   9749:                        // back space
                   9750:                        if(p > 0) {
                   9751:                                p--;
1.1.1.20  root     9752:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     9753:                                        msdos_putch(0x08);
                   9754:                                        msdos_putch(0x08);
                   9755:                                        msdos_putch(0x20);
                   9756:                                        msdos_putch(0x20);
                   9757:                                        msdos_putch(0x08);
                   9758:                                        msdos_putch(0x08);
1.1.1.36  root     9759:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9760:                                        p--;
                   9761:                                        msdos_putch(0x08);
                   9762:                                        msdos_putch(0x08);
                   9763:                                        msdos_putch(0x20);
                   9764:                                        msdos_putch(0x20);
                   9765:                                        msdos_putch(0x08);
                   9766:                                        msdos_putch(0x08);
1.1.1.34  root     9767:                                } else {
                   9768:                                        msdos_putch(0x08);
                   9769:                                        msdos_putch(0x20);
                   9770:                                        msdos_putch(0x08);
                   9771:                                }
                   9772:                        }
                   9773:                } else if(chr == 0x1b) {
                   9774:                        // escape
                   9775:                        while(p > 0) {
                   9776:                                p--;
                   9777:                                if(msdos_ctrl_code_check(buf[p])) {
                   9778:                                        msdos_putch(0x08);
                   9779:                                        msdos_putch(0x08);
                   9780:                                        msdos_putch(0x20);
                   9781:                                        msdos_putch(0x20);
                   9782:                                        msdos_putch(0x08);
                   9783:                                        msdos_putch(0x08);
1.1.1.20  root     9784:                                } else {
1.1.1.34  root     9785:                                        msdos_putch(0x08);
                   9786:                                        msdos_putch(0x20);
                   9787:                                        msdos_putch(0x08);
1.1.1.20  root     9788:                                }
1.1       root     9789:                        }
                   9790:                } else if(p < max) {
                   9791:                        buf[p++] = chr;
                   9792:                        msdos_putch(chr);
                   9793:                }
                   9794:        }
                   9795:        buf[p] = 0x0d;
                   9796:        mem[ofs + 1] = p;
1.1.1.33  root     9797:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9798:        
1.1.1.35  root     9799: #ifdef USE_SERVICE_THREAD
                   9800:        service_exit = true;
                   9801: #endif
                   9802:        return(0);
                   9803: }
                   9804: 
                   9805: inline void msdos_int_21h_0ah()
                   9806: {
                   9807:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   9808: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9809:                if(!in_service && !in_service_29h &&
1.1.1.58  root     9810:                   *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     9811:                   *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9812:                        // msdos_putch() will be used in this service
                   9813:                        // if int 29h is hooked, run this service in main thread to call int 29h
                   9814:                        start_service_loop(msdos_int_21h_0ah_thread);
                   9815:                } else {
                   9816: #endif
                   9817:                        msdos_int_21h_0ah_thread(NULL);
                   9818:                        REQUEST_HARDWRE_UPDATE();
                   9819: #ifdef USE_SERVICE_THREAD
                   9820:                }
1.1.1.35  root     9821: #endif
                   9822:        }
1.1       root     9823: }
                   9824: 
                   9825: inline void msdos_int_21h_0bh()
                   9826: {
                   9827:        if(msdos_kbhit()) {
                   9828:                REG8(AL) = 0xff;
                   9829:        } else {
                   9830:                REG8(AL) = 0x00;
1.1.1.14  root     9831:                maybe_idle();
1.1       root     9832:        }
1.1.1.33  root     9833:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9834: }
                   9835: 
                   9836: inline void msdos_int_21h_0ch()
                   9837: {
                   9838:        // clear key buffer
1.1.1.21  root     9839:        msdos_stdio_reopen();
                   9840:        
1.1.1.20  root     9841:        process_t *process = msdos_process_info_get(current_psp);
                   9842:        int fd = msdos_psp_get_file_table(0, current_psp);
                   9843:        
                   9844:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9845:                // stdin is redirected to file
                   9846:        } else {
                   9847:                while(msdos_kbhit()) {
                   9848:                        msdos_getch();
                   9849:                }
                   9850:        }
                   9851:        
                   9852:        switch(REG8(AL)) {
                   9853:        case 0x01:
                   9854:                msdos_int_21h_01h();
                   9855:                break;
                   9856:        case 0x06:
                   9857:                msdos_int_21h_06h();
                   9858:                break;
                   9859:        case 0x07:
                   9860:                msdos_int_21h_07h();
                   9861:                break;
                   9862:        case 0x08:
                   9863:                msdos_int_21h_08h();
                   9864:                break;
                   9865:        case 0x0a:
                   9866:                msdos_int_21h_0ah();
                   9867:                break;
                   9868:        default:
1.1.1.48  root     9869:                // the buffer is flushed but no input is attempted
1.1       root     9870:                break;
                   9871:        }
                   9872: }
                   9873: 
                   9874: inline void msdos_int_21h_0dh()
                   9875: {
                   9876: }
                   9877: 
                   9878: inline void msdos_int_21h_0eh()
                   9879: {
                   9880:        if(REG8(DL) < 26) {
                   9881:                _chdrive(REG8(DL) + 1);
                   9882:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9883:                msdos_sda_update(current_psp);
1.1       root     9884:        }
                   9885:        REG8(AL) = 26; // zdrive
                   9886: }
                   9887: 
1.1.1.14  root     9888: inline void msdos_int_21h_0fh()
                   9889: {
                   9890:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9891:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9892:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     9893:        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     9894:        
1.1.1.14  root     9895:        if(hFile == INVALID_HANDLE_VALUE) {
                   9896:                REG8(AL) = 0xff;
                   9897:        } else {
                   9898:                REG8(AL) = 0;
                   9899:                fcb->current_block = 0;
                   9900:                fcb->record_size = 128;
                   9901:                fcb->file_size = GetFileSize(hFile, NULL);
                   9902:                fcb->handle = hFile;
                   9903:                fcb->cur_record = 0;
                   9904:        }
                   9905: }
                   9906: 
                   9907: inline void msdos_int_21h_10h()
                   9908: {
                   9909:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9910:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9911:        
                   9912:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9913: }
                   9914: 
1.1       root     9915: inline void msdos_int_21h_11h()
                   9916: {
1.1.1.3   root     9917:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9918:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9919:        
                   9920:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9921:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9922:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9923:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45  root     9924:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     9925:        WIN32_FIND_DATAA fd;
1.1       root     9926:        
1.1.1.13  root     9927:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9928:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9929:                FindClose(dtainfo->find_handle);
                   9930:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9931:        }
                   9932:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9933:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9934:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9935:        
1.1.1.14  root     9936:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9937:                dtainfo->allowable_mask &= ~8;
1.1       root     9938:        }
1.1.1.60  root     9939:        if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14  root     9940:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9941:                      !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     9942:                        if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     9943:                                FindClose(dtainfo->find_handle);
                   9944:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9945:                                break;
                   9946:                        }
                   9947:                }
                   9948:        }
1.1.1.13  root     9949:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9950:                if(ext_fcb->flag == 0xff) {
                   9951:                        ext_find->flag = 0xff;
                   9952:                        memset(ext_find->reserved, 0, 5);
                   9953:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9954:                }
                   9955:                find->drive = _getdrive();
1.1.1.13  root     9956:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9957:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9958:                find->nt_res = 0;
                   9959:                msdos_find_file_conv_local_time(&fd);
                   9960:                find->create_time_ms = 0;
                   9961:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9962:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9963:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9964:                find->cluster_hi = find->cluster_lo = 0;
                   9965:                find->file_size = fd.nFileSizeLow;
                   9966:                REG8(AL) = 0x00;
1.1.1.14  root     9967:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9968:                if(ext_fcb->flag == 0xff) {
                   9969:                        ext_find->flag = 0xff;
                   9970:                        memset(ext_find->reserved, 0, 5);
                   9971:                        ext_find->attribute = 8;
                   9972:                }
                   9973:                find->drive = _getdrive();
                   9974:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9975:                find->attribute = 8;
                   9976:                find->nt_res = 0;
                   9977:                msdos_find_file_conv_local_time(&fd);
                   9978:                find->create_time_ms = 0;
                   9979:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9980:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9981:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9982:                find->cluster_hi = find->cluster_lo = 0;
                   9983:                find->file_size = 0;
1.1.1.14  root     9984:                dtainfo->allowable_mask &= ~8;
1.1       root     9985:                REG8(AL) = 0x00;
                   9986:        } else {
                   9987:                REG8(AL) = 0xff;
                   9988:        }
                   9989: }
                   9990: 
                   9991: inline void msdos_int_21h_12h()
                   9992: {
1.1.1.3   root     9993:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     9994: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9995:        
                   9996:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9997:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9998:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9999:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.60  root     10000:        WIN32_FIND_DATAA fd;
1.1       root     10001:        
1.1.1.13  root     10002:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   10003:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60  root     10004:                if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14  root     10005:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     10006:                              !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     10007:                                if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     10008:                                        FindClose(dtainfo->find_handle);
                   10009:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10010:                                        break;
                   10011:                                }
                   10012:                        }
                   10013:                } else {
1.1.1.13  root     10014:                        FindClose(dtainfo->find_handle);
                   10015:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10016:                }
                   10017:        }
1.1.1.13  root     10018:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     10019:                if(ext_fcb->flag == 0xff) {
                   10020:                        ext_find->flag = 0xff;
                   10021:                        memset(ext_find->reserved, 0, 5);
                   10022:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   10023:                }
                   10024:                find->drive = _getdrive();
1.1.1.13  root     10025:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     10026:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   10027:                find->nt_res = 0;
                   10028:                msdos_find_file_conv_local_time(&fd);
                   10029:                find->create_time_ms = 0;
                   10030:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   10031:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   10032:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   10033:                find->cluster_hi = find->cluster_lo = 0;
                   10034:                find->file_size = fd.nFileSizeLow;
                   10035:                REG8(AL) = 0x00;
1.1.1.14  root     10036:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     10037:                if(ext_fcb->flag == 0xff) {
                   10038:                        ext_find->flag = 0xff;
                   10039:                        memset(ext_find->reserved, 0, 5);
                   10040:                        ext_find->attribute = 8;
                   10041:                }
                   10042:                find->drive = _getdrive();
                   10043:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   10044:                find->attribute = 8;
                   10045:                find->nt_res = 0;
                   10046:                msdos_find_file_conv_local_time(&fd);
                   10047:                find->create_time_ms = 0;
                   10048:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   10049:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   10050:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   10051:                find->cluster_hi = find->cluster_lo = 0;
                   10052:                find->file_size = 0;
1.1.1.14  root     10053:                dtainfo->allowable_mask &= ~8;
1.1       root     10054:                REG8(AL) = 0x00;
                   10055:        } else {
                   10056:                REG8(AL) = 0xff;
                   10057:        }
                   10058: }
                   10059: 
                   10060: inline void msdos_int_21h_13h()
                   10061: {
1.1.1.3   root     10062:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     10063:                REG8(AL) = 0xff;
                   10064:        } else {
                   10065:                REG8(AL) = 0x00;
                   10066:        }
                   10067: }
                   10068: 
1.1.1.16  root     10069: inline void msdos_int_21h_14h()
                   10070: {
                   10071:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10072:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10073:        process_t *process = msdos_process_info_get(current_psp);
                   10074:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10075:        DWORD num = 0;
                   10076:        
                   10077:        memset(mem + dta_laddr, 0, fcb->record_size);
                   10078:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   10079:                REG8(AL) = 1;
                   10080:        } else {
                   10081:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   10082:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   10083:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   10084:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   10085:        }
                   10086: }
                   10087: 
                   10088: inline void msdos_int_21h_15h()
1.1.1.14  root     10089: {
                   10090:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10091:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     10092:        process_t *process = msdos_process_info_get(current_psp);
                   10093:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10094:        DWORD num = 0;
1.1.1.14  root     10095:        
1.1.1.16  root     10096:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   10097:                REG8(AL) = 1;
                   10098:        } else {
                   10099:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10100:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   10101:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   10102:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   10103:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   10104:        }
                   10105: }
                   10106: 
                   10107: inline void msdos_int_21h_16h()
                   10108: {
                   10109:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10110:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     10111:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     10112:        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     10113:        
1.1.1.14  root     10114:        if(hFile == INVALID_HANDLE_VALUE) {
                   10115:                REG8(AL) = 0xff;
                   10116:        } else {
                   10117:                REG8(AL) = 0;
                   10118:                fcb->current_block = 0;
                   10119:                fcb->record_size = 128;
                   10120:                fcb->file_size = 0;
                   10121:                fcb->handle = hFile;
                   10122:                fcb->cur_record = 0;
                   10123:        }
                   10124: }
                   10125: 
1.1.1.16  root     10126: inline void msdos_int_21h_17h()
                   10127: {
                   10128:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10129:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45  root     10130: //     const char *path_src = msdos_fcb_path(fcb_src);
                   10131:        char path_src[MAX_PATH];
                   10132:        strcpy(path_src, msdos_fcb_path(fcb_src));
                   10133:        
1.1.1.16  root     10134:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   10135:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45  root     10136: //     const char *path_dst = msdos_fcb_path(fcb_dst);
                   10137:        char path_dst[MAX_PATH];
                   10138:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     10139:        
                   10140:        if(rename(path_src, path_dst)) {
                   10141:                REG8(AL) = 0xff;
                   10142:        } else {
                   10143:                REG8(AL) = 0;
                   10144:        }
                   10145: }
                   10146: 
1.1       root     10147: inline void msdos_int_21h_18h()
                   10148: {
                   10149:        REG8(AL) = 0x00;
                   10150: }
                   10151: 
                   10152: inline void msdos_int_21h_19h()
                   10153: {
                   10154:        REG8(AL) = _getdrive() - 1;
                   10155: }
                   10156: 
                   10157: inline void msdos_int_21h_1ah()
                   10158: {
                   10159:        process_t *process = msdos_process_info_get(current_psp);
                   10160:        
                   10161:        process->dta.w.l = REG16(DX);
1.1.1.3   root     10162:        process->dta.w.h = SREG(DS);
1.1.1.23  root     10163:        msdos_sda_update(current_psp);
1.1       root     10164: }
                   10165: 
                   10166: inline void msdos_int_21h_1bh()
                   10167: {
                   10168:        int drive_num = _getdrive() - 1;
                   10169:        UINT16 seg, ofs;
                   10170:        
                   10171:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10172:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   10173:                REG8(AL) = dpb->highest_sector_num + 1;
                   10174:                REG16(CX) = dpb->bytes_per_sector;
                   10175:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     10176:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     10177:        } else {
                   10178:                REG8(AL) = 0xff;
1.1.1.3   root     10179:                m_CF = 1;
1.1       root     10180:        }
                   10181: 
                   10182: }
                   10183: 
                   10184: inline void msdos_int_21h_1ch()
                   10185: {
1.1.1.41  root     10186:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     10187:        UINT16 seg, ofs;
                   10188:        
                   10189:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10190:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   10191:                REG8(AL) = dpb->highest_sector_num + 1;
                   10192:                REG16(CX) = dpb->bytes_per_sector;
                   10193:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     10194:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     10195:        } else {
                   10196:                REG8(AL) = 0xff;
1.1.1.3   root     10197:                m_CF = 1;
1.1       root     10198:        }
                   10199: 
                   10200: }
                   10201: 
                   10202: inline void msdos_int_21h_1dh()
                   10203: {
                   10204:        REG8(AL) = 0;
                   10205: }
                   10206: 
                   10207: inline void msdos_int_21h_1eh()
                   10208: {
                   10209:        REG8(AL) = 0;
                   10210: }
                   10211: 
                   10212: inline void msdos_int_21h_1fh()
                   10213: {
                   10214:        int drive_num = _getdrive() - 1;
                   10215:        UINT16 seg, ofs;
                   10216:        
                   10217:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10218:                REG8(AL) = 0;
1.1.1.3   root     10219:                SREG(DS) = seg;
                   10220:                i386_load_segment_descriptor(DS);
1.1       root     10221:                REG16(BX) = ofs;
                   10222:        } else {
                   10223:                REG8(AL) = 0xff;
1.1.1.3   root     10224:                m_CF = 1;
1.1       root     10225:        }
                   10226: }
                   10227: 
                   10228: inline void msdos_int_21h_20h()
                   10229: {
                   10230:        REG8(AL) = 0;
                   10231: }
                   10232: 
1.1.1.14  root     10233: inline void msdos_int_21h_21h()
                   10234: {
                   10235:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10236:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10237:        
                   10238:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10239:                REG8(AL) = 1;
                   10240:        } else {
                   10241:                process_t *process = msdos_process_info_get(current_psp);
                   10242:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10243:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     10244:                DWORD num = 0;
1.1.1.14  root     10245:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   10246:                        REG8(AL) = 1;
                   10247:                } else {
                   10248:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10249:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     10250:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     10251:                }
                   10252:        }
                   10253: }
                   10254: 
                   10255: inline void msdos_int_21h_22h()
                   10256: {
                   10257:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10258:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10259:        
                   10260:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10261:                REG8(AL) = 0xff;
                   10262:        } else {
                   10263:                process_t *process = msdos_process_info_get(current_psp);
                   10264:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     10265:                DWORD num = 0;
1.1.1.14  root     10266:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   10267:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10268:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10269:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     10270:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     10271:        }
                   10272: }
                   10273: 
1.1.1.16  root     10274: inline void msdos_int_21h_23h()
                   10275: {
                   10276:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10277:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     10278:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     10279:        HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16  root     10280:        
                   10281:        if(hFile == INVALID_HANDLE_VALUE) {
                   10282:                REG8(AL) = 0xff;
                   10283:        } else {
                   10284:                UINT32 size = GetFileSize(hFile, NULL);
                   10285:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   10286:                REG8(AL) = 0;
                   10287:        }
                   10288: }
                   10289: 
                   10290: inline void msdos_int_21h_24h()
                   10291: {
                   10292:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10293:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10294:        
                   10295:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   10296: }
                   10297: 
1.1       root     10298: inline void msdos_int_21h_25h()
                   10299: {
                   10300:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     10301:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     10302: }
                   10303: 
                   10304: inline void msdos_int_21h_26h()
                   10305: {
                   10306:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   10307:        
                   10308:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
1.1.1.48  root     10309:        psp->first_mcb = REG16(DX) + (PSP_SIZE >> 4);
1.1       root     10310:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   10311:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   10312:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   10313:        psp->parent_psp = 0;
                   10314: }
                   10315: 
1.1.1.16  root     10316: inline void msdos_int_21h_27h()
                   10317: {
                   10318:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10319:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10320:        
                   10321:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10322:                REG8(AL) = 1;
                   10323:        } else {
                   10324:                process_t *process = msdos_process_info_get(current_psp);
                   10325:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10326:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   10327:                DWORD num = 0;
                   10328:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   10329:                        REG8(AL) = 1;
                   10330:                } else {
                   10331:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10332:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   10333:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   10334:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   10335:                }
                   10336:        }
                   10337: }
                   10338: 
                   10339: inline void msdos_int_21h_28h()
                   10340: {
                   10341:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10342:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10343:        
                   10344:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10345:                REG8(AL) = 0xff;
                   10346:        } else {
                   10347:                process_t *process = msdos_process_info_get(current_psp);
                   10348:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10349:                DWORD num = 0;
                   10350:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   10351:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10352:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10353:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   10354:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   10355:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   10356:        }
                   10357: }
                   10358: 
1.1       root     10359: inline void msdos_int_21h_29h()
                   10360: {
1.1.1.20  root     10361:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   10362:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     10363:        UINT8 drv = 0;
                   10364:        char sep_chars[] = ":.;,=+";
                   10365:        char end_chars[] = "\\<>|/\"[]";
                   10366:        char spc_chars[] = " \t";
                   10367:        
1.1.1.20  root     10368:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   10369:        buffer[1023] = 0;
                   10370:        memset(name, 0x20, sizeof(name));
                   10371:        memset(ext, 0x20, sizeof(ext));
                   10372:        
1.1       root     10373:        if(REG8(AL) & 1) {
1.1.1.20  root     10374:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   10375:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     10376:                        ofs++;
                   10377:                }
                   10378:        }
1.1.1.20  root     10379:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     10380:        
1.1.1.24  root     10381:        if(buffer[ofs + 1] == ':') {
                   10382:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   10383:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     10384:                        ofs += 2;
1.1.1.24  root     10385:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10386:                                ofs++;
                   10387:                        }
                   10388:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   10389:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     10390:                        ofs += 2;
1.1.1.24  root     10391:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10392:                                ofs++;
                   10393:                        }
1.1       root     10394:                }
                   10395:        }
1.1.1.20  root     10396:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10397:                UINT8 c = buffer[ofs];
                   10398:                if(is_kanji) {
                   10399:                        is_kanji = 0;
                   10400:                } else if(msdos_lead_byte_check(c)) {
                   10401:                        is_kanji = 1;
                   10402:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10403:                        break;
                   10404:                } else if(c >= 'a' && c <= 'z') {
                   10405:                        c -= 0x20;
                   10406:                }
                   10407:                ofs++;
                   10408:                name[i] = c;
                   10409:        }
1.1.1.20  root     10410:        if(buffer[ofs] == '.') {
1.1       root     10411:                ofs++;
1.1.1.20  root     10412:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10413:                        UINT8 c = buffer[ofs];
                   10414:                        if(is_kanji) {
                   10415:                                is_kanji = 0;
                   10416:                        } else if(msdos_lead_byte_check(c)) {
                   10417:                                is_kanji = 1;
                   10418:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10419:                                break;
                   10420:                        } else if(c >= 'a' && c <= 'z') {
                   10421:                                c -= 0x20;
                   10422:                        }
                   10423:                        ofs++;
                   10424:                        ext[i] = c;
                   10425:                }
                   10426:        }
1.1.1.20  root     10427:        int si = REG16(SI) + ofs;
1.1.1.3   root     10428:        int ds = SREG(DS);
1.1       root     10429:        while(si > 0xffff) {
                   10430:                si -= 0x10;
                   10431:                ds++;
                   10432:        }
                   10433:        REG16(SI) = si;
1.1.1.3   root     10434:        SREG(DS) = ds;
                   10435:        i386_load_segment_descriptor(DS);
1.1       root     10436:        
1.1.1.3   root     10437:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     10438:        if(!(REG8(AL) & 2) || drv != 0) {
                   10439:                fcb[0] = drv;
                   10440:        }
                   10441:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   10442:                memcpy(fcb + 1, name, 8);
                   10443:        }
                   10444:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   10445:                memcpy(fcb + 9, ext, 3);
                   10446:        }
                   10447:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     10448:                if(fcb[i] == '*') {
                   10449:                        found_star = 1;
                   10450:                }
                   10451:                if(found_star) {
                   10452:                        fcb[i] = '?';
                   10453:                }
                   10454:        }
1.1.1.20  root     10455:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     10456:                if(fcb[i] == '*') {
                   10457:                        found_star = 1;
                   10458:                }
                   10459:                if(found_star) {
                   10460:                        fcb[i] = '?';
                   10461:                }
                   10462:        }
                   10463:        
1.1.1.44  root     10464:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     10465:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   10466:                        REG8(AL) = 0x01;
1.1.1.20  root     10467:                } else {
                   10468:                        REG8(AL) = 0x00;
1.1       root     10469:                }
                   10470:        } else {
                   10471:                REG8(AL) = 0xff;
                   10472:        }
                   10473: }
                   10474: 
                   10475: inline void msdos_int_21h_2ah()
                   10476: {
                   10477:        SYSTEMTIME sTime;
                   10478:        
                   10479:        GetLocalTime(&sTime);
                   10480:        REG16(CX) = sTime.wYear;
                   10481:        REG8(DH) = (UINT8)sTime.wMonth;
                   10482:        REG8(DL) = (UINT8)sTime.wDay;
                   10483:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   10484: }
                   10485: 
                   10486: inline void msdos_int_21h_2bh()
                   10487: {
1.1.1.14  root     10488:        REG8(AL) = 0xff;
1.1       root     10489: }
                   10490: 
                   10491: inline void msdos_int_21h_2ch()
                   10492: {
                   10493:        SYSTEMTIME sTime;
                   10494:        
                   10495:        GetLocalTime(&sTime);
                   10496:        REG8(CH) = (UINT8)sTime.wHour;
                   10497:        REG8(CL) = (UINT8)sTime.wMinute;
                   10498:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     10499:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     10500: }
                   10501: 
                   10502: inline void msdos_int_21h_2dh()
                   10503: {
                   10504:        REG8(AL) = 0x00;
                   10505: }
                   10506: 
                   10507: inline void msdos_int_21h_2eh()
                   10508: {
                   10509:        process_t *process = msdos_process_info_get(current_psp);
                   10510:        
                   10511:        process->verify = REG8(AL);
                   10512: }
                   10513: 
                   10514: inline void msdos_int_21h_2fh()
                   10515: {
                   10516:        process_t *process = msdos_process_info_get(current_psp);
                   10517:        
                   10518:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     10519:        SREG(ES) = process->dta.w.h;
                   10520:        i386_load_segment_descriptor(ES);
1.1       root     10521: }
                   10522: 
                   10523: inline void msdos_int_21h_30h()
                   10524: {
                   10525:        // Version Flag / OEM
1.1.1.27  root     10526:        if(REG8(AL) == 0x01) {
1.1.1.29  root     10527: #ifdef SUPPORT_HMA
                   10528:                REG16(BX) = 0x0000;
                   10529: #else
                   10530:                REG16(BX) = 0x1000; // DOS is in HMA
                   10531: #endif
1.1       root     10532:        } else {
1.1.1.27  root     10533:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   10534:                // but this is not correct on Windows 98 SE
                   10535: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   10536:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     10537:        }
1.1.1.27  root     10538:        REG16(CX) = 0x0000;
1.1.1.30  root     10539:        REG8(AL) = dos_major_version;   // 7
                   10540:        REG8(AH) = dos_minor_version;   // 10
1.1       root     10541: }
                   10542: 
                   10543: inline void msdos_int_21h_31h()
                   10544: {
1.1.1.29  root     10545:        try {
                   10546:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10547:        } catch(...) {
                   10548:                // recover the broken mcb
                   10549:                int mcb_seg = current_psp - 1;
                   10550:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     10551:                
1.1.1.29  root     10552:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     10553:                        mcb->mz = 'M';
                   10554:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   10555:                        
1.1.1.29  root     10556:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     10557:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     10558:                        } else {
1.1.1.39  root     10559:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     10560:                        }
                   10561:                } else {
                   10562:                        mcb->mz = 'Z';
1.1.1.30  root     10563:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     10564:                }
                   10565:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10566:        }
1.1       root     10567:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   10568: }
                   10569: 
                   10570: inline void msdos_int_21h_32h()
                   10571: {
                   10572:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   10573:        UINT16 seg, ofs;
                   10574:        
                   10575:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10576:                REG8(AL) = 0;
1.1.1.3   root     10577:                SREG(DS) = seg;
                   10578:                i386_load_segment_descriptor(DS);
1.1       root     10579:                REG16(BX) = ofs;
                   10580:        } else {
                   10581:                REG8(AL) = 0xff;
1.1.1.3   root     10582:                m_CF = 1;
1.1       root     10583:        }
                   10584: }
                   10585: 
                   10586: inline void msdos_int_21h_33h()
                   10587: {
                   10588:        char path[MAX_PATH];
1.1.1.48  root     10589:        char drive = 3; // C:
1.1       root     10590:        
                   10591:        switch(REG8(AL)) {
                   10592:        case 0x00:
1.1.1.33  root     10593:                REG8(DL) = ctrl_break_checking;
1.1       root     10594:                break;
                   10595:        case 0x01:
1.1.1.33  root     10596:                ctrl_break_checking = REG8(DL);
                   10597:                break;
                   10598:        case 0x02:
                   10599:                {
                   10600:                        UINT8 old = ctrl_break_checking;
                   10601:                        ctrl_break_checking = REG8(DL);
                   10602:                        REG8(DL) = old;
                   10603:                }
                   10604:                break;
                   10605:        case 0x03:
                   10606:        case 0x04:
                   10607:                // DOS 4.0+ - Unused
1.1       root     10608:                break;
                   10609:        case 0x05:
1.1.1.60  root     10610:                if(GetSystemDirectoryA(path, MAX_PATH) != 0) {
1.1.1.48  root     10611:                        if(path[0] >= 'a' && path[0] <= 'z') {
                   10612:                                drive = path[0] - 'a' + 1;
                   10613:                        } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   10614:                                drive = path[0] - 'A' + 1;
                   10615:                        }
1.1       root     10616:                }
1.1.1.48  root     10617:                REG8(DL) = (UINT8)drive;
1.1       root     10618:                break;
                   10619:        case 0x06:
1.1.1.2   root     10620:                // MS-DOS version (7.10)
1.1       root     10621:                REG8(BL) = 7;
1.1.1.2   root     10622:                REG8(BH) = 10;
1.1       root     10623:                REG8(DL) = 0;
1.1.1.29  root     10624: #ifdef SUPPORT_HMA
                   10625:                REG8(DH) = 0x00;
                   10626: #else
                   10627:                REG8(DH) = 0x10; // DOS is in HMA
                   10628: #endif
1.1       root     10629:                break;
1.1.1.6   root     10630:        case 0x07:
                   10631:                if(REG8(DL) == 0) {
                   10632:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   10633:                } else if(REG8(DL) == 1) {
                   10634:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   10635:                }
                   10636:                break;
1.1       root     10637:        default:
1.1.1.22  root     10638:                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     10639: //             REG16(AX) = 0x01;
                   10640: //             m_CF = 1;
                   10641:                REG8(AL) = 0xff;
1.1       root     10642:                break;
                   10643:        }
                   10644: }
                   10645: 
1.1.1.23  root     10646: inline void msdos_int_21h_34h()
                   10647: {
                   10648:        SREG(ES) = SDA_TOP >> 4;
                   10649:        i386_load_segment_descriptor(ES);
                   10650:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   10651: }
                   10652: 
1.1       root     10653: inline void msdos_int_21h_35h()
                   10654: {
                   10655:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     10656:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   10657:        i386_load_segment_descriptor(ES);
1.1       root     10658: }
                   10659: 
                   10660: inline void msdos_int_21h_36h()
                   10661: {
                   10662:        struct _diskfree_t df = {0};
                   10663:        
                   10664:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   10665:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   10666:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     10667:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   10668:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     10669:        } else {
                   10670:                REG16(AX) = 0xffff;
                   10671:        }
                   10672: }
                   10673: 
                   10674: inline void msdos_int_21h_37h()
                   10675: {
1.1.1.22  root     10676:        static UINT8 dev_flag = 0xff;
1.1       root     10677:        
                   10678:        switch(REG8(AL)) {
                   10679:        case 0x00:
1.1.1.22  root     10680:                {
                   10681:                        process_t *process = msdos_process_info_get(current_psp);
                   10682:                        REG8(AL) = 0x00;
                   10683:                        REG8(DL) = process->switchar;
                   10684:                }
1.1       root     10685:                break;
                   10686:        case 0x01:
1.1.1.22  root     10687:                {
                   10688:                        process_t *process = msdos_process_info_get(current_psp);
                   10689:                        REG8(AL) = 0x00;
                   10690:                        process->switchar = REG8(DL);
1.1.1.23  root     10691:                        msdos_sda_update(current_psp);
1.1.1.22  root     10692:                }
                   10693:                break;
                   10694:        case 0x02:
                   10695:                REG8(DL) = dev_flag;
                   10696:                break;
                   10697:        case 0x03:
                   10698:                dev_flag = REG8(DL);
                   10699:                break;
                   10700:        case 0xd0:
                   10701:        case 0xd1:
                   10702:        case 0xd2:
                   10703:        case 0xd3:
                   10704:        case 0xd4:
                   10705:        case 0xd5:
                   10706:        case 0xd6:
                   10707:        case 0xd7:
                   10708:        case 0xdc:
                   10709:        case 0xdd:
                   10710:        case 0xde:
                   10711:        case 0xdf:
1.1.1.48  root     10712:                // DIET v1.43e
                   10713: //             REG16(AX) = 1;
                   10714:                REG8(AL) = 0xff;
1.1       root     10715:                break;
                   10716:        default:
1.1.1.22  root     10717:                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     10718: //             REG16(AX) = 1;
                   10719:                REG8(AL) = 0xff;
1.1       root     10720:                break;
                   10721:        }
                   10722: }
                   10723: 
1.1.1.52  root     10724: int get_country_info(country_info_t *ci, LCID locale = LOCALE_USER_DEFAULT)
1.1.1.17  root     10725: {
                   10726:        char LCdata[80];
                   10727:        
1.1.1.19  root     10728:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.60  root     10729:        GetLocaleInfoA(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     10730:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.60  root     10731:        GetLocaleInfoA(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10732:        ci->currency_format = *LCdata - '0';
1.1.1.60  root     10733:        GetLocaleInfoA(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10734:        ci->date_format = *LCdata - '0';
1.1.1.60  root     10735:        GetLocaleInfoA(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10736:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.60  root     10737:        GetLocaleInfoA(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10738:        *ci->date_sep = *LCdata;
1.1.1.60  root     10739:        GetLocaleInfoA(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     10740:        *ci->dec_sep = *LCdata;
1.1.1.60  root     10741:        GetLocaleInfoA(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     10742:        *ci->list_sep = *LCdata;
1.1.1.60  root     10743:        GetLocaleInfoA(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     10744:        *ci->thou_sep = *LCdata;
1.1.1.60  root     10745:        GetLocaleInfoA(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     10746:        *ci->time_sep = *LCdata;
1.1.1.60  root     10747:        GetLocaleInfoA(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     10748:        if(strchr(LCdata, 'H') != NULL) {
                   10749:                ci->time_format = 1;
                   10750:        }
1.1.1.49  root     10751:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffc:000a
                   10752:        ci->case_map.w.h = DUMMY_TOP >> 4;
1.1.1.60  root     10753:        GetLocaleInfoA(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     10754:        return atoi(LCdata);
                   10755: }
                   10756: 
1.1.1.42  root     10757: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   10758: {
                   10759:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   10760: }
                   10761: 
1.1.1.43  root     10762: void set_country_info(country_info_t *ci, int size)
                   10763: {
                   10764:        char LCdata[80];
                   10765:        
                   10766:        if(size >= 0x00 + 2) {
                   10767:                memset(LCdata, 0, sizeof(LCdata));
                   10768:                *LCdata = '0' + ci->date_format;
1.1.1.60  root     10769:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
1.1.1.43  root     10770:        }
                   10771:        if(size >= 0x02 + 5) {
                   10772:                memset(LCdata, 0, sizeof(LCdata));
                   10773:                memcpy(LCdata, &ci->currency_symbol, 4);
1.1.1.60  root     10774:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
1.1.1.43  root     10775:        }
                   10776:        if(size >= 0x07 + 2) {
                   10777:                memset(LCdata, 0, sizeof(LCdata));
                   10778:                *LCdata = *ci->thou_sep;
1.1.1.60  root     10779:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
1.1.1.43  root     10780:        }
                   10781:        if(size >= 0x09 + 2) {
                   10782:                memset(LCdata, 0, sizeof(LCdata));
                   10783:                *LCdata = *ci->dec_sep;
1.1.1.60  root     10784:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
1.1.1.43  root     10785:        }
                   10786:        if(size >= 0x0b + 2) {
                   10787:                memset(LCdata, 0, sizeof(LCdata));
                   10788:                *LCdata = *ci->date_sep;
1.1.1.60  root     10789:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
1.1.1.43  root     10790:        }
                   10791:        if(size >= 0x0d + 2) {
                   10792:                memset(LCdata, 0, sizeof(LCdata));
                   10793:                *LCdata = *ci->time_sep;
1.1.1.60  root     10794:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
1.1.1.43  root     10795:        }
                   10796:        if(size >= 0x0f + 1) {
                   10797:                memset(LCdata, 0, sizeof(LCdata));
                   10798:                *LCdata = '0' + ci->currency_format;
1.1.1.60  root     10799:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
1.1.1.43  root     10800:        }
                   10801:        if(size >= 0x10 + 1) {
                   10802:                sprintf(LCdata, "%d", ci->currency_dec_digits);
1.1.1.60  root     10803:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
1.1.1.43  root     10804:        }
                   10805:        if(size >= 0x11 + 1) {
                   10806:                // FIXME: is time format always H/h:mm:ss ???
                   10807:                if(ci->time_format & 1) {
                   10808:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   10809:                } else {
                   10810:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   10811:                }
1.1.1.60  root     10812:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
1.1.1.43  root     10813:        }
                   10814:        if(size >= 0x12 + 4) {
                   10815:                // 12h  DWORD   address of case map routine
                   10816:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   10817:        }
                   10818:        if(size >= 0x16 + 2) {
                   10819:                memset(LCdata, 0, sizeof(LCdata));
                   10820:                *LCdata = *ci->list_sep;
1.1.1.60  root     10821:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
1.1.1.43  root     10822:        }
                   10823: }
                   10824: 
1.1.1.42  root     10825: #ifndef SUBLANG_SWAHILI
                   10826:        #define SUBLANG_SWAHILI 0x01
                   10827: #endif
                   10828: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10829:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10830: #endif
                   10831: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10832:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10833: #endif
                   10834: #ifndef LANG_BANGLA
                   10835:        #define LANG_BANGLA 0x45
                   10836: #endif
                   10837: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10838:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10839: #endif
                   10840: 
                   10841: static const struct {
                   10842:        int code;
                   10843:        USHORT usPrimaryLanguage;
                   10844:        USHORT usSubLanguage;
                   10845: } country_table[] = {
                   10846:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10847:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10848:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10849:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10850:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10851:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10852: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10853: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10854: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10855: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10856: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10857:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10858:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10859: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10860:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10861: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10862:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10863:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10864:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10865:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10866:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10867:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10868:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10869: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10870: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10871:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10872:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10873:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10874:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10875:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10876: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10877: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10878: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10879:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10880: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10881: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10882: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10883: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10884:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10885:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10886:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10887: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10888:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10889:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10890:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10891:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10892:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10893:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10894:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10895:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10896:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10897:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10898:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10899:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10900:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10901: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10902:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10903:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10904:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10905:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10906:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10907:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10908:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10909:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10910:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10911:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10912: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10913:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10914: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10915:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10916:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10917:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10918:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10919:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10920:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10921:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10922:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10923: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10924:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10925: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10926: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10927:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   10928: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   10929:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   10930:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   10931:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   10932:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   10933:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   10934:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   10935:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   10936: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   10937: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   10938:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   10939: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   10940:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   10941:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   10942:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   10943:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   10944: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   10945: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   10946: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   10947:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   10948:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   10949:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   10950:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   10951:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   10952:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   10953:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   10954:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   10955:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   10956:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   10957:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   10958:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   10959:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   10960:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   10961:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   10962:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   10963:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   10964:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   10965:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   10966:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   10967:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     10968: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     10969:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   10970: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   10971:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   10972:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   10973:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   10974:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   10975:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   10976:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   10977:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   10978:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   10979:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   10980:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   10981:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   10982:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   10983:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   10984:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   10985:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   10986:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   10987:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   10988:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   10989:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   10990:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   10991:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   10992:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   10993:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   10994:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   10995:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   10996: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   10997:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   10998:        {-1, 0, 0},
                   10999: };
                   11000: 
1.1.1.14  root     11001: inline void msdos_int_21h_38h()
                   11002: {
                   11003:        switch(REG8(AL)) {
                   11004:        case 0x00:
1.1.1.19  root     11005:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     11006:                break;
                   11007:        default:
1.1.1.42  root     11008:                for(int i = 0;; i++) {
                   11009:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   11010:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   11011:                                break;
                   11012:                        } else if(country_table[i].code == -1) {
                   11013: //                             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));
                   11014: //                             REG16(AX) = 2;
                   11015: //                             m_CF = 1;
1.1.1.48  root     11016:                                // get current coutry info
1.1.1.42  root     11017:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   11018:                                break;
                   11019:                        }
                   11020:                }
1.1.1.14  root     11021:                break;
                   11022:        }
                   11023: }
                   11024: 
1.1       root     11025: inline void msdos_int_21h_39h(int lfn)
                   11026: {
1.1.1.3   root     11027:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     11028:                REG16(AX) = errno;
1.1.1.3   root     11029:                m_CF = 1;
1.1       root     11030:        }
                   11031: }
                   11032: 
                   11033: inline void msdos_int_21h_3ah(int lfn)
                   11034: {
1.1.1.3   root     11035:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     11036:                REG16(AX) = errno;
1.1.1.3   root     11037:                m_CF = 1;
1.1       root     11038:        }
                   11039: }
                   11040: 
                   11041: inline void msdos_int_21h_3bh(int lfn)
                   11042: {
1.1.1.45  root     11043:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     11044:        
                   11045:        if(_chdir(path)) {
1.1.1.17  root     11046:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     11047:                m_CF = 1;
1.1.1.44  root     11048:        } else {
                   11049:                int drv = _getdrive() - 1;
                   11050:                if(path[1] == ':') {
                   11051:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   11052:                                drv = path[0] - 'A';
                   11053:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   11054:                                drv = path[0] - 'a';
                   11055:                        }
                   11056:                }
                   11057:                msdos_cds_update(drv, path);
1.1       root     11058:        }
                   11059: }
                   11060: 
                   11061: inline void msdos_int_21h_3ch()
                   11062: {
1.1.1.45  root     11063:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1.1.60  root     11064:        int attr = GetFileAttributesA(path);
1.1.1.37  root     11065:        int fd = -1;
                   11066:        int sio_port = 0;
                   11067:        int lpt_port = 0;
1.1       root     11068:        
1.1.1.45  root     11069:        if(msdos_is_device_path(path)) {
                   11070:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     11071:        } else {
                   11072:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11073:        }
                   11074:        if(fd != -1) {
                   11075:                if(attr == -1) {
                   11076:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   11077:                }
1.1.1.60  root     11078:                SetFileAttributesA(path, attr);
1.1       root     11079:                REG16(AX) = fd;
1.1.1.45  root     11080:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     11081:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11082:        } else {
                   11083:                REG16(AX) = errno;
1.1.1.3   root     11084:                m_CF = 1;
1.1       root     11085:        }
                   11086: }
                   11087: 
                   11088: inline void msdos_int_21h_3dh()
                   11089: {
1.1.1.45  root     11090:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11091:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     11092:        int fd = -1;
                   11093:        int sio_port = 0;
                   11094:        int lpt_port = 0;
1.1       root     11095:        
                   11096:        if(mode < 0x03) {
1.1.1.45  root     11097:                if(msdos_is_device_path(path)) {
                   11098:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     11099:                } else {
1.1.1.13  root     11100:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     11101:                }
1.1       root     11102:                if(fd != -1) {
                   11103:                        REG16(AX) = fd;
1.1.1.45  root     11104:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     11105:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11106:                } else {
                   11107:                        REG16(AX) = errno;
1.1.1.3   root     11108:                        m_CF = 1;
1.1       root     11109:                }
                   11110:        } else {
                   11111:                REG16(AX) = 0x0c;
1.1.1.3   root     11112:                m_CF = 1;
1.1       root     11113:        }
                   11114: }
                   11115: 
                   11116: inline void msdos_int_21h_3eh()
                   11117: {
                   11118:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11119:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11120:        
1.1.1.20  root     11121:        if(fd < process->max_files && file_handler[fd].valid) {
                   11122:                _close(fd);
                   11123:                msdos_file_handler_close(fd);
                   11124:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     11125:        } else {
                   11126:                REG16(AX) = 0x06;
1.1.1.3   root     11127:                m_CF = 1;
1.1       root     11128:        }
                   11129: }
                   11130: 
1.1.1.35  root     11131: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   11132: {
                   11133:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   11134:        int max = REG16(CX);
                   11135:        int p = 0;
                   11136:        
                   11137:        while(max > p) {
                   11138:                int chr = msdos_getch();
                   11139:                
                   11140:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   11141:                        p = 0;
                   11142:                        buf[p++] = 0x0d;
                   11143:                        if(max > p) {
                   11144:                                buf[p++] = 0x0a;
                   11145:                        }
                   11146:                        msdos_putch(0x03);
                   11147:                        msdos_putch(0x0d);
                   11148:                        msdos_putch(0x0a);
                   11149:                        break;
                   11150:                } else if(ctrl_break_pressed) {
                   11151:                        // skip this byte
                   11152:                } else if(chr == 0x00) {
                   11153:                        // skip 2nd byte
                   11154:                        msdos_getch();
                   11155:                } else if(chr == 0x0d) {
                   11156:                        // carriage return
                   11157:                        buf[p++] = 0x0d;
                   11158:                        if(max > p) {
                   11159:                                buf[p++] = 0x0a;
                   11160:                        }
                   11161:                        msdos_putch('\n');
                   11162:                        break;
                   11163:                } else if(chr == 0x08) {
                   11164:                        // back space
                   11165:                        if(p > 0) {
                   11166:                                p--;
                   11167:                                if(msdos_ctrl_code_check(buf[p])) {
                   11168:                                        msdos_putch(0x08);
                   11169:                                        msdos_putch(0x08);
                   11170:                                        msdos_putch(0x20);
                   11171:                                        msdos_putch(0x20);
                   11172:                                        msdos_putch(0x08);
                   11173:                                        msdos_putch(0x08);
1.1.1.36  root     11174:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   11175:                                        p--;
                   11176:                                        msdos_putch(0x08);
                   11177:                                        msdos_putch(0x08);
                   11178:                                        msdos_putch(0x20);
                   11179:                                        msdos_putch(0x20);
                   11180:                                        msdos_putch(0x08);
                   11181:                                        msdos_putch(0x08);
1.1.1.35  root     11182:                                } else {
                   11183:                                        msdos_putch(0x08);
                   11184:                                        msdos_putch(0x20);
                   11185:                                        msdos_putch(0x08);
                   11186:                                }
                   11187:                        }
                   11188:                } else if(chr == 0x1b) {
                   11189:                        // escape
                   11190:                        while(p > 0) {
                   11191:                                p--;
                   11192:                                if(msdos_ctrl_code_check(buf[p])) {
                   11193:                                        msdos_putch(0x08);
                   11194:                                        msdos_putch(0x08);
                   11195:                                        msdos_putch(0x20);
                   11196:                                        msdos_putch(0x20);
                   11197:                                        msdos_putch(0x08);
                   11198:                                        msdos_putch(0x08);
                   11199:                                } else {
                   11200:                                        msdos_putch(0x08);
                   11201:                                        msdos_putch(0x20);
                   11202:                                        msdos_putch(0x08);
                   11203:                                }
                   11204:                        }
                   11205:                } else {
                   11206:                        buf[p++] = chr;
                   11207:                        msdos_putch(chr);
                   11208:                }
                   11209:        }
                   11210:        REG16(AX) = p;
                   11211:        
                   11212: #ifdef USE_SERVICE_THREAD
                   11213:        service_exit = true;
                   11214: #endif
                   11215:        return(0);
                   11216: }
                   11217: 
1.1       root     11218: inline void msdos_int_21h_3fh()
                   11219: {
                   11220:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11221:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11222:        
1.1.1.20  root     11223:        if(fd < process->max_files && file_handler[fd].valid) {
                   11224:                if(file_mode[file_handler[fd].mode].in) {
                   11225:                        if(file_handler[fd].atty) {
1.1       root     11226:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     11227:                                if(REG16(CX) != 0) {
                   11228: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     11229:                                        if(!in_service && !in_service_29h &&
1.1.1.58  root     11230:                                           *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     11231:                                           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   11232:                                                // msdos_putch() will be used in this service
                   11233:                                                // if int 29h is hooked, run this service in main thread to call int 29h
                   11234:                                                start_service_loop(msdos_int_21h_3fh_thread);
                   11235:                                        } else {
                   11236: #endif
                   11237:                                                msdos_int_21h_3fh_thread(NULL);
                   11238:                                                REQUEST_HARDWRE_UPDATE();
                   11239: #ifdef USE_SERVICE_THREAD
                   11240:                                        }
1.1.1.35  root     11241: #endif
                   11242:                                } else {
                   11243:                                        REG16(AX) = 0;
1.1       root     11244:                                }
                   11245:                        } else {
1.1.1.37  root     11246:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     11247:                        }
                   11248:                } else {
                   11249:                        REG16(AX) = 0x05;
1.1.1.3   root     11250:                        m_CF = 1;
1.1       root     11251:                }
                   11252:        } else {
                   11253:                REG16(AX) = 0x06;
1.1.1.3   root     11254:                m_CF = 1;
1.1       root     11255:        }
                   11256: }
                   11257: 
                   11258: inline void msdos_int_21h_40h()
                   11259: {
                   11260:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11261:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11262:        
1.1.1.20  root     11263:        if(fd < process->max_files && file_handler[fd].valid) {
                   11264:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     11265:                        if(REG16(CX)) {
1.1.1.20  root     11266:                                if(file_handler[fd].atty) {
1.1       root     11267:                                        // BX is stdout/stderr or is redirected to stdout
                   11268:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     11269:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     11270:                                        }
                   11271:                                        REG16(AX) = REG16(CX);
                   11272:                                } else {
1.1.1.20  root     11273:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     11274:                                }
                   11275:                        } else {
1.1.1.20  root     11276:                                UINT32 pos = _tell(fd);
                   11277:                                _lseek(fd, 0, SEEK_END);
                   11278:                                UINT32 size = _tell(fd);
1.1.1.12  root     11279:                                if(pos < size) {
1.1.1.20  root     11280:                                        _lseek(fd, pos, SEEK_SET);
                   11281:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     11282:                                } else {
                   11283:                                        for(UINT32 i = size; i < pos; i++) {
                   11284:                                                UINT8 tmp = 0;
1.1.1.23  root     11285:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     11286:                                        }
1.1.1.20  root     11287:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     11288:                                }
1.1.1.23  root     11289:                                REG16(AX) = 0;
1.1       root     11290:                        }
                   11291:                } else {
                   11292:                        REG16(AX) = 0x05;
1.1.1.3   root     11293:                        m_CF = 1;
1.1       root     11294:                }
                   11295:        } else {
                   11296:                REG16(AX) = 0x06;
1.1.1.3   root     11297:                m_CF = 1;
1.1       root     11298:        }
                   11299: }
                   11300: 
                   11301: inline void msdos_int_21h_41h(int lfn)
                   11302: {
1.1.1.3   root     11303:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     11304:                REG16(AX) = errno;
1.1.1.3   root     11305:                m_CF = 1;
1.1       root     11306:        }
                   11307: }
                   11308: 
                   11309: inline void msdos_int_21h_42h()
                   11310: {
                   11311:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11312:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11313:        
1.1.1.20  root     11314:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     11315:                if(REG8(AL) < 0x03) {
1.1.1.35  root     11316:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     11317:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   11318:                        UINT32 pos = _tell(fd);
1.1       root     11319:                        REG16(AX) = pos & 0xffff;
                   11320:                        REG16(DX) = (pos >> 16);
                   11321:                } else {
                   11322:                        REG16(AX) = 0x01;
1.1.1.3   root     11323:                        m_CF = 1;
1.1       root     11324:                }
                   11325:        } else {
                   11326:                REG16(AX) = 0x06;
1.1.1.3   root     11327:                m_CF = 1;
1.1       root     11328:        }
                   11329: }
                   11330: 
                   11331: inline void msdos_int_21h_43h(int lfn)
                   11332: {
1.1.1.45  root     11333:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     11334:        int attr;
                   11335:        
1.1.1.14  root     11336:        if(!lfn && REG8(AL) > 2) {
                   11337:                REG16(AX) = 0x01;
                   11338:                m_CF = 1;
                   11339:                return;
                   11340:        }
                   11341:        switch(REG8(lfn ? BL : AL)) {
1.1       root     11342:        case 0x00:
1.1.1.60  root     11343:                if((attr = GetFileAttributesA(path)) != -1) {
1.1.1.14  root     11344:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   11345:                } else {
                   11346:                        REG16(AX) = (UINT16)GetLastError();
                   11347:                        m_CF = 1;
                   11348:                }
                   11349:                break;
                   11350:        case 0x01:
1.1.1.60  root     11351:                if(!SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)))) {
1.1.1.14  root     11352:                        REG16(AX) = (UINT16)GetLastError();
                   11353:                        m_CF = 1;
                   11354:                }
                   11355:                break;
                   11356:        case 0x02:
                   11357:                {
1.1.1.60  root     11358:                        DWORD compressed_size = GetCompressedFileSizeA(path, NULL), file_size = 0;
1.1.1.45  root     11359:                        if(compressed_size != INVALID_FILE_SIZE) {
                   11360:                                if(compressed_size != 0) {
1.1.1.60  root     11361:                                        HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.45  root     11362:                                        if(hFile != INVALID_HANDLE_VALUE) {
                   11363:                                                file_size = GetFileSize(hFile, NULL);
                   11364:                                                CloseHandle(hFile);
                   11365:                                        }
                   11366:                                        if(compressed_size == file_size) {
                   11367:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   11368:                                                // this isn't correct if the file is in the NTFS MFT
1.1.1.60  root     11369:                                                if(GetDiskFreeSpaceA(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
1.1.1.45  root     11370:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   11371:                                                }
1.1.1.14  root     11372:                                        }
                   11373:                                }
1.1.1.45  root     11374:                                REG16(AX) = LOWORD(compressed_size);
                   11375:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     11376:                        } else {
                   11377:                                REG16(AX) = (UINT16)GetLastError();
                   11378:                                m_CF = 1;
1.1       root     11379:                        }
1.1.1.14  root     11380:                }
                   11381:                break;
                   11382:        case 0x03:
                   11383:        case 0x05:
                   11384:        case 0x07:
1.1.1.48  root     11385:                if(lfn) {
1.1.1.60  root     11386:                        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     11387:                        if(hFile != INVALID_HANDLE_VALUE) {
                   11388:                                FILETIME local, time;
                   11389:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   11390:                                if(REG8(BL) == 7) {
                   11391:                                        ULARGE_INTEGER hund;
                   11392:                                        hund.LowPart = local.dwLowDateTime;
                   11393:                                        hund.HighPart = local.dwHighDateTime;
                   11394:                                        hund.QuadPart += REG16(SI) * 100000;
                   11395:                                        local.dwLowDateTime = hund.LowPart;
                   11396:                                        local.dwHighDateTime = hund.HighPart;
                   11397:                                }
                   11398:                                LocalFileTimeToFileTime(&local, &time);
                   11399:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   11400:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   11401:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   11402:                                        REG16(AX) = (UINT16)GetLastError();
                   11403:                                        m_CF = 1;
                   11404:                                }
                   11405:                                CloseHandle(hFile);
                   11406:                        } else {
                   11407:                                REG16(AX) = (UINT16)GetLastError();
                   11408:                                m_CF = 1;
1.1       root     11409:                        }
1.1.1.48  root     11410:                } else {
                   11411:                        // 214303 DR DOS 3.41+ internal - Set Access Rights And Password
                   11412:                        // 214305 DR DOS 5.0-6.0 internal - Set Extended File Attributes
                   11413:                        // 214307 DR DOS 6.0 - Set File Owner
                   11414: //                     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));
                   11415:                        REG16(AX) = 0x01;
                   11416:                        m_CF = 1;
1.1.1.14  root     11417:                }
                   11418:                break;
                   11419:        case 0x04:
                   11420:        case 0x06:
                   11421:        case 0x08:
1.1.1.48  root     11422:                if(lfn) {
1.1.1.14  root     11423:                        WIN32_FILE_ATTRIBUTE_DATA fad;
1.1.1.60  root     11424:                        if(GetFileAttributesExA(path, GetFileExInfoStandard, (LPVOID)&fad)) {
1.1.1.14  root     11425:                                FILETIME *time, local;
                   11426:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   11427:                                                   0x06 ? &fad.ftLastAccessTime :
                   11428:                                                          &fad.ftCreationTime;
                   11429:                                FileTimeToLocalFileTime(time, &local);
                   11430:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   11431:                                if(REG8(BL) == 0x08) {
                   11432:                                        ULARGE_INTEGER hund;
                   11433:                                        hund.LowPart = local.dwLowDateTime;
                   11434:                                        hund.HighPart = local.dwHighDateTime;
                   11435:                                        hund.QuadPart /= 100000;
                   11436:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   11437:                                }
                   11438:                        } else {
                   11439:                                REG16(AX) = (UINT16)GetLastError();
                   11440:                                m_CF = 1;
1.1       root     11441:                        }
1.1.1.48  root     11442:                } else {
                   11443:                        // 214304 DR DOS 5.0-6.0 internal - Get Encrypted Password
                   11444:                        // 214306 DR DOS 6.0 - Get File Owner
                   11445: //                     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));
                   11446:                        REG16(AX) = 0x01;
                   11447:                        m_CF = 1;
1.1.1.14  root     11448:                }
                   11449:                break;
1.1.1.43  root     11450:        case 0xff:
1.1.1.48  root     11451:                if(!lfn && REG16(BP) == 0x5053) {
1.1.1.43  root     11452:                        if(REG8(CL) == 0x39) {
                   11453:                                msdos_int_21h_39h(1);
                   11454:                                break;
                   11455:                        } else if(REG8(CL) == 0x56) {
                   11456:                                msdos_int_21h_56h(1);
                   11457:                                break;
                   11458:                        }
                   11459:                }
1.1.1.14  root     11460:        default:
1.1.1.22  root     11461:                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     11462:                REG16(AX) = lfn ? 0x7100 : 0x01;
1.1.1.14  root     11463:                m_CF = 1;
                   11464:                break;
                   11465:        }
                   11466: }
                   11467: 
                   11468: inline void msdos_int_21h_44h()
                   11469: {
1.1.1.22  root     11470:        static UINT16 iteration_count = 0;
                   11471:        
1.1.1.44  root     11472:        process_t *process;
                   11473:        int fd, drv;
1.1.1.14  root     11474:        
                   11475:        switch(REG8(AL)) {
                   11476:        case 0x00:
                   11477:        case 0x01:
                   11478:        case 0x02:
                   11479:        case 0x03:
                   11480:        case 0x04:
                   11481:        case 0x05:
                   11482:        case 0x06:
                   11483:        case 0x07:
1.1.1.44  root     11484:                process = msdos_process_info_get(current_psp);
                   11485:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     11486:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   11487:                        REG16(AX) = 0x06;
                   11488:                        m_CF = 1;
                   11489:                        return;
1.1.1.14  root     11490:                }
                   11491:                break;
                   11492:        case 0x08:
                   11493:        case 0x09:
1.1.1.44  root     11494:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11495:                if(!msdos_is_valid_drive(drv)) {
                   11496:                        // invalid drive
1.1.1.14  root     11497:                        REG16(AX) = 0x0f;
                   11498:                        m_CF = 1;
                   11499:                        return;
1.1       root     11500:                }
                   11501:                break;
                   11502:        }
                   11503:        switch(REG8(AL)) {
1.1.1.48  root     11504:        case 0x00: // Get Device Information
1.1.1.20  root     11505:                REG16(DX) = file_handler[fd].info;
1.1       root     11506:                break;
1.1.1.48  root     11507:        case 0x01: // Set Device Information
1.1.1.45  root     11508:                if(REG8(DH) != 0) {
                   11509: //                     REG16(AX) = 0x0d; // data invalid
                   11510: //                     m_CF = 1;
                   11511:                        file_handler[fd].info = REG16(DX);
                   11512:                } else {
                   11513:                        file_handler[fd].info &= 0xff00;
                   11514:                        file_handler[fd].info |= REG8(DL);
                   11515:                }
1.1       root     11516:                break;
1.1.1.48  root     11517:        case 0x02: // Read From Character Device Control Channel
1.1.1.45  root     11518:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
                   11519:                        // from DOSBox
                   11520:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
                   11521:                        case 0x00:
                   11522:                                if(REG16(CX) >= 6) {
                   11523:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
                   11524:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
                   11525:                                        REG16(AX) = 6; // number of bytes actually read
                   11526:                                } else {
                   11527:                                        REG16(AX) = 0x0d; // data invalid
                   11528:                                        m_CF = 1;
                   11529:                                }
                   11530:                                break;
                   11531:                        case 0x01:
                   11532:                                if(REG16(CX) >= 6) {
                   11533:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
                   11534:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
                   11535:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
                   11536:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
                   11537:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
                   11538:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
                   11539:                                                        int page = (addr - EMS_TOP) / 0x4000;
                   11540:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
                   11541:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11542:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
                   11543:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
                   11544:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
                   11545:                                                } else {
                   11546:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
                   11547:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11548:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
                   11549:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
                   11550:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
                   11551:                                                }
                   11552:                                        }
                   11553:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
                   11554:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
                   11555:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
                   11556:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
                   11557:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
                   11558:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
                   11559:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
                   11560:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
                   11561:                                        
                   11562:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
                   11563:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
                   11564:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
                   11565:                                        REG16(AX) = 6; // number of bytes actually read
                   11566:                                } else {
                   11567:                                        REG16(AX) = 0x0d; // data invalid
                   11568:                                        m_CF = 1;
                   11569:                                }
                   11570:                                break;
                   11571:                        case 0x02:
                   11572:                                if(REG16(CX) >= 2) {
                   11573:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
                   11574:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
                   11575:                                        REG16(AX) = 2; // number of bytes actually read
                   11576:                                } else {
                   11577:                                        REG16(AX) = 0x0d; // data invalid
                   11578:                                        m_CF = 1;
                   11579:                                }
                   11580:                                break;
                   11581:                        case 0x03:
                   11582:                                if(REG16(CX) >= 4) {
                   11583:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
                   11584:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
                   11585:                                        REG16(AX) = 4; // number of bytes actually read
                   11586:                                } else {
                   11587:                                        REG16(AX) = 0x0d; // data invalid
                   11588:                                        m_CF = 1;
                   11589:                                }
                   11590:                                break;
                   11591:                        default:
                   11592:                                REG16(AX) = 0x01; // function number invalid
                   11593:                                m_CF = 1;
                   11594:                        }
                   11595:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
                   11596:                        if(REG16(CX) >= 5) {
                   11597:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
                   11598:                                REG16(AX) = 5; // number of bytes actually read
                   11599:                        } else {
                   11600:                                REG16(AX) = 0x0d; // data invalid
                   11601:                                m_CF = 1;
                   11602:                        }
                   11603:                } else {
                   11604: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
                   11605: //                     REG16(AX) = REG16(CX);
                   11606:                        REG16(AX) = 0x05; // access denied
                   11607:                        m_CF = 1;
                   11608:                }
                   11609:                break;
1.1.1.48  root     11610:        case 0x03: // Write To Character Device Control Channel
1.1.1.45  root     11611: //             REG16(AX) = 0x05;
                   11612: //             m_CF = 1;
                   11613:                REG16(AX) = 0x00; // success
                   11614:                break;
1.1.1.48  root     11615:        case 0x04: // Read From Block Device Control Channel
                   11616:        case 0x05: // Write To Block Device Control Channel
1.1       root     11617:                REG16(AX) = 0x05;
1.1.1.3   root     11618:                m_CF = 1;
1.1       root     11619:                break;
1.1.1.48  root     11620:        case 0x06: // Get Input Status
1.1.1.20  root     11621:                if(file_mode[file_handler[fd].mode].in) {
                   11622:                        if(file_handler[fd].atty) {
1.1.1.14  root     11623:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     11624:                        } else {
1.1.1.20  root     11625:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     11626:                        }
1.1.1.14  root     11627:                } else {
                   11628:                        REG8(AL) = 0x00;
1.1       root     11629:                }
                   11630:                break;
1.1.1.48  root     11631:        case 0x07: // Get Output Status
1.1.1.20  root     11632:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     11633:                        REG8(AL) = 0xff;
                   11634:                } else {
                   11635:                        REG8(AL) = 0x00;
1.1       root     11636:                }
                   11637:                break;
1.1.1.48  root     11638:        case 0x08: // Check If Block Device Removable
1.1.1.44  root     11639:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     11640:                        // removable drive
                   11641:                        REG16(AX) = 0x00;
1.1       root     11642:                } else {
1.1.1.14  root     11643:                        // fixed drive
                   11644:                        REG16(AX) = 0x01;
1.1       root     11645:                }
                   11646:                break;
1.1.1.48  root     11647:        case 0x09: // Check If Block Device Remote
1.1.1.44  root     11648:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     11649:                        // remote drive
                   11650:                        REG16(DX) = 0x1000;
1.1.1.44  root     11651:                } else if(msdos_is_subst_drive(drv)) {
                   11652:                        // subst drive
                   11653:                        REG16(DX) = 0x8000;
1.1       root     11654:                } else {
1.1.1.14  root     11655:                        // local drive
1.1.1.44  root     11656:                        REG16(DX) = 0x0000;
1.1       root     11657:                }
                   11658:                break;
1.1.1.48  root     11659:        case 0x0a: // Check If Handle Is Remote
1.1.1.45  root     11660:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
                   11661:                        REG16(DX) = 0x8000;
                   11662:                } else {
                   11663:                        REG16(DX) = 0x0000;
                   11664:                }
1.1.1.21  root     11665:                break;
1.1.1.48  root     11666:        case 0x0b: // Set Sharing Retry Count
1.1       root     11667:                break;
1.1.1.48  root     11668:        case 0x0c: // Generic Character Device Request
1.1.1.22  root     11669:                if(REG8(CL) == 0x45) {
                   11670:                        // set iteration (retry) count
                   11671:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   11672:                } else if(REG8(CL) == 0x4a) {
                   11673:                        // select code page
                   11674:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   11675:                        msdos_nls_tables_update();
1.1.1.44  root     11676:                } else if(REG8(CL) == 0x4c) {
                   11677:                        // start code-page preparation
                   11678:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11679:                        int count = 1, offset = 0;
                   11680:                        if(active_code_page != 437) {
                   11681:                                ids[count++] = active_code_page;
                   11682:                        }
                   11683:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11684:                                ids[count++] = system_code_page;
                   11685:                        }
                   11686:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   11687:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   11688:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11689:                        for(int i = 0; i < count; i++) {
                   11690:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11691:                        }
                   11692:                } else if(REG8(CL) == 0x4d) {
                   11693:                        // end code-page preparation
                   11694:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   11695:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.50  root     11696:                } else if(REG8(CL) == 0x5f) {
                   11697:                        // set display information
                   11698:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   11699:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   11700:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   11701:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   11702:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   11703:                                
                   11704:                                if(cur_width != new_width || cur_height != new_height) {
                   11705:                                        pcbios_set_console_size(new_width, new_height, true);
                   11706:                                }
                   11707:                        }
1.1.1.22  root     11708:                } else if(REG8(CL) == 0x65) {
                   11709:                        // get iteration (retry) count
                   11710:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   11711:                } else if(REG8(CL) == 0x6a) {
                   11712:                        // query selected code page
                   11713:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   11714:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   11715:                        
                   11716:                        CPINFO info;
                   11717:                        GetCPInfo(active_code_page, &info);
                   11718:                        
                   11719:                        if(info.MaxCharSize != 1) {
                   11720:                                for(int i = 0;; i++) {
                   11721:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   11722:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   11723:                                        
                   11724:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   11725:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   11726:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   11727:                                        
                   11728:                                        if(lo == 0 && hi == 0) {
                   11729:                                                break;
                   11730:                                        }
                   11731:                                }
                   11732:                        }
1.1.1.44  root     11733:                } else if(REG8(CL) == 0x6b) {
                   11734:                        // query prepare list
                   11735:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11736:                        int count = 1, offset = 0;
                   11737:                        if(active_code_page != 437) {
                   11738:                                ids[count++] = active_code_page;
                   11739:                        }
                   11740:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11741:                                ids[count++] = system_code_page;
                   11742:                        }
                   11743:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   11744:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11745:                        for(int i = 0; i < count; i++) {
                   11746:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11747:                        }
                   11748:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11749:                        for(int i = 0; i < count; i++) {
                   11750:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11751:                        }
1.1.1.22  root     11752:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     11753:                        // get display information
1.1.1.50  root     11754:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   11755:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   11756:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   11757:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   11758:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   11759:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   11760:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   11761:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   11762:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   11763:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   11764:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.22  root     11765:                } else {
                   11766:                        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));
                   11767:                        REG16(AX) = 0x01; // invalid function
                   11768:                        m_CF = 1;
                   11769:                }
                   11770:                break;
1.1.1.48  root     11771:        case 0x0d: // Generic Block Device Request
1.1.1.22  root     11772:                if(REG8(CL) == 0x40) {
                   11773:                        // set device parameters
1.1.1.48  root     11774: //             } else if(REG8(CL) == 0x41) {
                   11775: //                     // write logical device track
                   11776: //             } else if(REG8(CL) == 0x42) {
                   11777: //                     // format and verify logical device track
1.1.1.22  root     11778:                } else if(REG8(CL) == 0x46) {
                   11779:                        // set volume serial number
1.1.1.48  root     11780:                } else if(REG8(CL) == 0x47) {
                   11781:                        // set access flag
                   11782: //             } else if(REG8(CL) == 0x48) {
                   11783: //                     // set media lock state
                   11784: //             } else if(REG8(CL) == 0x49) {
                   11785: //                     // eject media in drive
1.1.1.22  root     11786:                } else if(REG8(CL) == 0x4a) {
                   11787:                        // lock logical volume
                   11788:                } else if(REG8(CL) == 0x4b) {
                   11789:                        // lock physical volume
                   11790:                } else if(REG8(CL) == 0x60) {
                   11791:                        // get device parameters
1.1.1.42  root     11792:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11793:                        
1.1.1.42  root     11794:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11795:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11796:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11797:                                
                   11798:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   11799:                                switch(geo->MediaType) {
                   11800:                                case F5_360_512:
                   11801:                                case F5_320_512:
                   11802:                                case F5_320_1024:
                   11803:                                case F5_180_512:
                   11804:                                case F5_160_512:
                   11805:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   11806:                                        break;
                   11807:                                case F5_1Pt2_512:
                   11808:                                case F3_1Pt2_512:
                   11809:                                case F3_1Pt23_1024:
                   11810:                                case F5_1Pt23_1024:
                   11811:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   11812:                                        break;
                   11813:                                case F3_720_512:
                   11814:                                case F3_640_512:
                   11815:                                case F5_640_512:
                   11816:                                case F5_720_512:
                   11817:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   11818:                                        break;
                   11819:                                case F8_256_128:
                   11820:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   11821:                                        break;
                   11822:                                case FixedMedia:
                   11823:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11824:                                        break;
                   11825:                                case F3_1Pt44_512:
                   11826:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11827:                                        break;
                   11828:                                case F3_2Pt88_512:
                   11829:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   11830:                                        break;
                   11831:                                default:
                   11832:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11833: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11834:                                        break;
1.1.1.22  root     11835:                                }
1.1.1.42  root     11836:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   11837:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   11838:                                switch(geo->MediaType) {
                   11839:                                case F5_360_512:
                   11840:                                case F5_320_512:
                   11841:                                case F5_320_1024:
                   11842:                                case F5_180_512:
                   11843:                                case F5_160_512:
                   11844:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   11845:                                        break;
                   11846:                                default:
                   11847:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   11848:                                        break;
                   11849:                                }
                   11850:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   11851:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   11852:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   11853:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   11854:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   11855:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   11856:                                switch(geo->MediaType) {
                   11857:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   11858:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   11859:                                        break;
                   11860:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   11861:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   11862:                                        break;
                   11863:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   11864:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11865:                                        break;
                   11866:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11867:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11868:                                        break;
                   11869:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11870:                                case F3_1Pt2_512:
                   11871:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11872:                                case F5_720_512:
                   11873:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11874:                                        break;
                   11875:                                case FixedMedia:        // hard disk
                   11876:                                case RemovableMedia:
                   11877:                                case Unknown:
                   11878:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11879:                                        break;
                   11880:                                default:
                   11881:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11882:                                        break;
                   11883:                                }
                   11884:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11885:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11886:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11887:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11888:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11889:                                // 21h  BYTE    device type
                   11890:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11891:                        } else {
                   11892:                                REG16(AX) = 0x0f; // invalid drive
                   11893:                                m_CF = 1;
                   11894:                        }
1.1.1.48  root     11895: //             } else if(REG8(CL) == 0x61) {
                   11896: //                     // read logical device track
                   11897: //             } else if(REG8(CL) == 0x62) {
                   11898: //                     // verify logical device track
1.1.1.22  root     11899:                } else if(REG8(CL) == 0x66) {
                   11900:                        // get volume serial number
                   11901:                        char path[] = "A:\\";
                   11902:                        char volume_label[MAX_PATH];
                   11903:                        DWORD serial_number = 0;
                   11904:                        char file_system[MAX_PATH];
                   11905:                        
                   11906:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11907:                        
1.1.1.60  root     11908:                        if(GetVolumeInformationA(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
1.1.1.22  root     11909:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11910:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11911:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11912:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11913:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11914:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11915:                        } else {
                   11916:                                REG16(AX) = 0x0f; // invalid drive
                   11917:                                m_CF = 1;
                   11918:                        }
                   11919:                } else if(REG8(CL) == 0x67) {
                   11920:                        // get access flag
                   11921:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11922:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11923:                } else if(REG8(CL) == 0x68) {
                   11924:                        // sense media type
1.1.1.42  root     11925:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11926:                        
1.1.1.42  root     11927:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11928:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11929:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11930:                                
                   11931:                                switch(geo->MediaType) {
                   11932:                                case F3_720_512:
                   11933:                                case F5_720_512:
                   11934:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11935:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   11936:                                        break;
                   11937:                                case F3_1Pt44_512:
                   11938:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11939:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   11940:                                        break;
                   11941:                                case F3_2Pt88_512:
                   11942:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11943:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   11944:                                        break;
                   11945:                                default:
                   11946:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   11947:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   11948:                                        break;
1.1.1.22  root     11949:                                }
                   11950:                        } else {
                   11951:                                REG16(AX) = 0x0f; // invalid drive
                   11952:                                m_CF = 1;
                   11953:                        }
                   11954:                } else if(REG8(CL) == 0x6a) {
                   11955:                        // unlock logical volume
                   11956:                } else if(REG8(CL) == 0x6b) {
                   11957:                        // unlock physical volume
1.1.1.48  root     11958: //             } else if(REG8(CL) == 0x6c) {
                   11959: //                     // get lock flag
                   11960: //             } else if(REG8(CL) == 0x6d) {
                   11961: //                     // enumerate open files
                   11962: //             } else if(REG8(CL) == 0x6e) {
                   11963: //                     // find swap file
                   11964: //             } else if(REG8(CL) == 0x6f) {
                   11965: //                     // get drive map information
                   11966: //             } else if(REG8(CL) == 0x70) {
                   11967: //                     // get current lock state
                   11968: //             } else if(REG8(CL) == 0x71) {
                   11969: //                     // get first cluster
1.1.1.22  root     11970:                } else {
                   11971:                        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));
                   11972:                        REG16(AX) = 0x01; // invalid function
                   11973:                        m_CF = 1;
                   11974:                }
                   11975:                break;
1.1.1.48  root     11976:        case 0x0e: // Get Lgical Drive Map
1.1.1.44  root     11977:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11978:                        REG16(AX) = 0x0f; // invalid drive
                   11979:                        m_CF = 1;
                   11980:                } else {
                   11981:                        REG8(AL) = 0;
1.1.1.22  root     11982:                }
                   11983:                break;
1.1.1.48  root     11984:        case 0x0f: // Set Logical Drive Map
1.1.1.44  root     11985:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11986:                        REG16(AX) = 0x0f; // invalid drive
                   11987:                        m_CF = 1;
1.1.1.22  root     11988:                }
                   11989:                break;
1.1.1.48  root     11990:        case 0x10: // Query Generic IOCTRL Capability (Handle)
1.1.1.22  root     11991:                switch(REG8(CL)) {
                   11992:                case 0x45:
                   11993:                case 0x4a:
1.1.1.48  root     11994:                case 0x4c:
                   11995:                case 0x4d:
1.1.1.22  root     11996:                case 0x65:
                   11997:                case 0x6a:
1.1.1.48  root     11998:                case 0x6b:
1.1.1.22  root     11999:                case 0x7f:
                   12000:                        REG16(AX) = 0x0000; // supported
                   12001:                        break;
                   12002:                default:
                   12003:                        REG8(AL) = 0x01; // ioctl capability not available
                   12004:                        m_CF = 1;
                   12005:                        break;
                   12006:                }
                   12007:                break;
1.1.1.48  root     12008:        case 0x11: // Query Generic IOCTRL Capability (Drive)
1.1.1.22  root     12009:                switch(REG8(CL)) {
                   12010:                case 0x40:
                   12011:                case 0x46:
                   12012:                case 0x4a:
                   12013:                case 0x4b:
                   12014:                case 0x60:
                   12015:                case 0x66:
                   12016:                case 0x67:
                   12017:                case 0x68:
                   12018:                case 0x6a:
                   12019:                case 0x6b:
1.1.1.48  root     12020:                        if(REG8(CH) == 0x00 || REG8(CH) == 0x01 || REG8(CH) == 0x03 || REG8(CH) == 0x05) {
                   12021:                                // CH = 00h     Unknown
                   12022:                                // CH = 01h     COMn:
                   12023:                                // CH = 03h     CON
                   12024:                                // CH = 05h     LPTn:
                   12025:                                REG16(AX) = 0x0000; // supported
                   12026:                                break;
                   12027:                        }
1.1.1.22  root     12028:                default:
                   12029:                        REG8(AL) = 0x01; // ioctl capability not available
                   12030:                        m_CF = 1;
                   12031:                        break;
                   12032:                }
                   12033:                break;
1.1.1.48  root     12034:        case 0x12: // DR DOS 5.0-6.0 - Determine DOS Type
                   12035:        case 0x14: // DR DOS 5.0-6.0 - Set Global Password
                   12036:        case 0x16: // DR DOS 5.0-6.0 - History Buffer, Share, And Hiload Control
                   12037:        case 0x51: // Concurrent DOS v3.2+ - Installation Check
                   12038:        case 0x52: // DR DOS 3.41+ - Determine DOS tTpe/Get DR DOS Version
                   12039:        case 0x54: // DR DOS 3.41+ - Set Global Password
                   12040:        case 0x56: // DR DOS 5.0+ - History Buffer Control
                   12041:        case 0x57: // DR DOS 5.0-6.0 - Share/Hiload Control
                   12042:        case 0x58: // DR DOS 5.0+ internal - Get Pointer To Internal Variable Table
                   12043:        case 0x59: // DR Multiuser DOS 5.0 - API
                   12044:                REG16(AX) = 0x01; // this is not DR-DOS
1.1.1.22  root     12045:                m_CF = 1;
                   12046:                break;
1.1       root     12047:        default:
1.1.1.22  root     12048:                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     12049:                REG16(AX) = 0x01;
1.1.1.3   root     12050:                m_CF = 1;
1.1       root     12051:                break;
                   12052:        }
                   12053: }
                   12054: 
                   12055: inline void msdos_int_21h_45h()
                   12056: {
                   12057:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12058:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12059:        
1.1.1.20  root     12060:        if(fd < process->max_files && file_handler[fd].valid) {
                   12061:                int dup_fd = _dup(fd);
                   12062:                if(dup_fd != -1) {
                   12063:                        REG16(AX) = dup_fd;
                   12064:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   12065: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   12066:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     12067:                } else {
                   12068:                        REG16(AX) = errno;
1.1.1.3   root     12069:                        m_CF = 1;
1.1       root     12070:                }
                   12071:        } else {
                   12072:                REG16(AX) = 0x06;
1.1.1.3   root     12073:                m_CF = 1;
1.1       root     12074:        }
                   12075: }
                   12076: 
                   12077: inline void msdos_int_21h_46h()
                   12078: {
                   12079:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12080:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   12081:        int dup_fd = REG16(CX);
                   12082:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     12083:        
1.1.1.20  root     12084:        if(REG16(BX) == REG16(CX)) {
                   12085:                REG16(AX) = 0x06;
                   12086:                m_CF = 1;
                   12087:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   12088:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   12089:                        _close(tmp_fd);
                   12090:                        msdos_file_handler_close(tmp_fd);
                   12091:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   12092:                }
                   12093:                if(_dup2(fd, dup_fd) != -1) {
                   12094:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   12095: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   12096:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     12097:                } else {
                   12098:                        REG16(AX) = errno;
1.1.1.3   root     12099:                        m_CF = 1;
1.1       root     12100:                }
                   12101:        } else {
                   12102:                REG16(AX) = 0x06;
1.1.1.3   root     12103:                m_CF = 1;
1.1       root     12104:        }
                   12105: }
                   12106: 
                   12107: inline void msdos_int_21h_47h(int lfn)
                   12108: {
                   12109:        char path[MAX_PATH];
                   12110:        
                   12111:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45  root     12112:                if(!lfn) {
                   12113:                        strcpy(path, msdos_short_path(path));
                   12114:                }
1.1       root     12115:                if(path[1] == ':') {
                   12116:                        // the returned path does not include a drive or the initial backslash
1.1.1.45  root     12117:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     12118:                } else {
1.1.1.45  root     12119:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     12120:                }
                   12121:        } else {
                   12122:                REG16(AX) = errno;
1.1.1.3   root     12123:                m_CF = 1;
1.1       root     12124:        }
                   12125: }
                   12126: 
                   12127: inline void msdos_int_21h_48h()
                   12128: {
1.1.1.19  root     12129:        int seg, umb_linked;
1.1       root     12130:        
1.1.1.8   root     12131:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     12132:                // unlink umb not to allocate memory in umb
                   12133:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   12134:                        msdos_mem_unlink_umb();
                   12135:                }
1.1.1.8   root     12136:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   12137:                        REG16(AX) = seg;
                   12138:                } else {
                   12139:                        REG16(AX) = 0x08;
                   12140:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   12141:                        m_CF = 1;
                   12142:                }
1.1.1.19  root     12143:                if(umb_linked != 0) {
                   12144:                        msdos_mem_link_umb();
                   12145:                }
1.1.1.8   root     12146:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   12147:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   12148:                        REG16(AX) = seg;
                   12149:                } else {
                   12150:                        REG16(AX) = 0x08;
                   12151:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   12152:                        m_CF = 1;
                   12153:                }
                   12154:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   12155:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   12156:                        REG16(AX) = seg;
                   12157:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   12158:                        REG16(AX) = seg;
                   12159:                } else {
                   12160:                        REG16(AX) = 0x08;
                   12161:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   12162:                        m_CF = 1;
                   12163:                }
1.1       root     12164:        }
                   12165: }
                   12166: 
                   12167: inline void msdos_int_21h_49h()
                   12168: {
1.1.1.14  root     12169:        int mcb_seg = SREG(ES) - 1;
                   12170:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   12171:        
                   12172:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   12173:                msdos_mem_free(SREG(ES));
                   12174:        } else {
1.1.1.33  root     12175:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     12176:                m_CF = 1;
                   12177:        }
1.1       root     12178: }
                   12179: 
                   12180: inline void msdos_int_21h_4ah()
                   12181: {
1.1.1.14  root     12182:        int mcb_seg = SREG(ES) - 1;
                   12183:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     12184:        int max_paragraphs;
                   12185:        
1.1.1.14  root     12186:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   12187:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   12188:                        REG16(AX) = 0x08;
                   12189:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   12190:                        m_CF = 1;
                   12191:                }
                   12192:        } else {
1.1.1.33  root     12193:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     12194:                m_CF = 1;
1.1       root     12195:        }
                   12196: }
                   12197: 
                   12198: inline void msdos_int_21h_4bh()
                   12199: {
1.1.1.3   root     12200:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12201:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     12202:        
                   12203:        switch(REG8(AL)) {
                   12204:        case 0x00:
                   12205:        case 0x01:
                   12206:                if(msdos_process_exec(command, param, REG8(AL))) {
                   12207:                        REG16(AX) = 0x02;
1.1.1.3   root     12208:                        m_CF = 1;
1.1       root     12209:                }
                   12210:                break;
1.1.1.14  root     12211:        case 0x03:
                   12212:                {
                   12213:                        int fd;
                   12214:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   12215:                                REG16(AX) = 0x02;
                   12216:                                m_CF = 1;
                   12217:                                break;
                   12218:                        }
                   12219:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   12220:                        _close(fd);
                   12221:                        
                   12222:                        UINT16 *overlay = (UINT16 *)param;
                   12223:                        
                   12224:                        // check exe header
                   12225:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   12226:                        int header_size = 0;
                   12227:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   12228:                                header_size = header->header_size * 16;
                   12229:                                // relocation
                   12230:                                int start_seg = overlay[1];
                   12231:                                for(int i = 0; i < header->relocations; i++) {
                   12232:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   12233:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   12234:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   12235:                                }
                   12236:                        }
                   12237:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   12238:                }
                   12239:                break;
1.1.1.48  root     12240:        case 0x04:
                   12241:                // Load And Execute In Background (European MS-DOS 4.0 only)
                   12242: //     case 0x05:
                   12243: //             // DOS 5+ - Set Execution State
                   12244:        case 0x80:
                   12245:                // DR DOS v3.41 - Run Already-Loaded Kernel File
                   12246:        case 0xf0:
                   12247:        case 0xf1:
                   12248:                // DIET v1.10+
1.1.1.43  root     12249:        case 0xfd:
                   12250:        case 0xfe:
                   12251:                // unknown function called in FreeCOM
                   12252:                REG16(AX) = 0x01;
                   12253:                m_CF = 1;
                   12254:                break;
1.1       root     12255:        default:
1.1.1.22  root     12256:                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     12257:                REG16(AX) = 0x01;
1.1.1.3   root     12258:                m_CF = 1;
1.1       root     12259:                break;
                   12260:        }
                   12261: }
                   12262: 
                   12263: inline void msdos_int_21h_4ch()
                   12264: {
                   12265:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   12266: }
                   12267: 
                   12268: inline void msdos_int_21h_4dh()
                   12269: {
                   12270:        REG16(AX) = retval;
                   12271: }
                   12272: 
                   12273: inline void msdos_int_21h_4eh()
                   12274: {
                   12275:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     12276:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   12277:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45  root     12278:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1.1.60  root     12279:        WIN32_FIND_DATAA fd;
1.1       root     12280:        
1.1.1.14  root     12281:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12282:        find->find_magic = FIND_MAGIC;
                   12283:        find->dta_index = dtainfo - dtalist;
1.1       root     12284:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12285:        dtainfo->allowable_mask = REG8(CL);
1.1.1.58  root     12286:        // note: SO1 dir command sets 0x3f, but only directories and volue label are found if bit3 is set :-(
                   12287:        if((dtainfo->allowable_mask & 0x3f) == 0x3f) {
                   12288:                dtainfo->allowable_mask &= ~0x08;
                   12289:        }
1.1.1.14  root     12290:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12291:        
1.1.1.14  root     12292:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12293:                dtainfo->allowable_mask &= ~8;
1.1       root     12294:        }
1.1.1.60  root     12295:        if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14  root     12296:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     12297:                      !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     12298:                        if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     12299:                                FindClose(dtainfo->find_handle);
                   12300:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12301:                                break;
                   12302:                        }
                   12303:                }
                   12304:        }
1.1.1.13  root     12305:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12306:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   12307:                msdos_find_file_conv_local_time(&fd);
                   12308:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   12309:                find->size = fd.nFileSizeLow;
1.1.1.13  root     12310:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     12311:                REG16(AX) = 0;
1.1.1.14  root     12312:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12313:                find->attrib = 8;
                   12314:                find->size = 0;
                   12315:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12316:                dtainfo->allowable_mask &= ~8;
1.1       root     12317:                REG16(AX) = 0;
                   12318:        } else {
                   12319:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12320:                m_CF = 1;
1.1       root     12321:        }
                   12322: }
                   12323: 
                   12324: inline void msdos_int_21h_4fh()
                   12325: {
                   12326:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     12327:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   12328:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.60  root     12329:        WIN32_FIND_DATAA fd;
1.1       root     12330:        
1.1.1.14  root     12331:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   12332:                REG16(AX) = 0x12;
                   12333:                m_CF = 1;
                   12334:                return;
                   12335:        }
                   12336:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     12337:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60  root     12338:                if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14  root     12339:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     12340:                              !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     12341:                                if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     12342:                                        FindClose(dtainfo->find_handle);
                   12343:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12344:                                        break;
                   12345:                                }
                   12346:                        }
                   12347:                } else {
1.1.1.13  root     12348:                        FindClose(dtainfo->find_handle);
                   12349:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12350:                }
                   12351:        }
1.1.1.13  root     12352:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12353:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   12354:                msdos_find_file_conv_local_time(&fd);
                   12355:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   12356:                find->size = fd.nFileSizeLow;
1.1.1.13  root     12357:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     12358:                REG16(AX) = 0;
1.1.1.14  root     12359:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12360:                find->attrib = 8;
                   12361:                find->size = 0;
                   12362:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12363:                dtainfo->allowable_mask &= ~8;
1.1       root     12364:                REG16(AX) = 0;
                   12365:        } else {
                   12366:                REG16(AX) = 0x12;
1.1.1.3   root     12367:                m_CF = 1;
1.1       root     12368:        }
                   12369: }
                   12370: 
                   12371: inline void msdos_int_21h_50h()
                   12372: {
1.1.1.8   root     12373:        if(current_psp != REG16(BX)) {
                   12374:                process_t *process = msdos_process_info_get(current_psp);
                   12375:                if(process != NULL) {
                   12376:                        process->psp = REG16(BX);
                   12377:                }
                   12378:                current_psp = REG16(BX);
1.1.1.23  root     12379:                msdos_sda_update(current_psp);
1.1.1.8   root     12380:        }
1.1       root     12381: }
                   12382: 
                   12383: inline void msdos_int_21h_51h()
                   12384: {
                   12385:        REG16(BX) = current_psp;
                   12386: }
                   12387: 
                   12388: inline void msdos_int_21h_52h()
                   12389: {
1.1.1.25  root     12390:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     12391:        i386_load_segment_descriptor(ES);
1.1.1.25  root     12392:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     12393: }
                   12394: 
1.1.1.43  root     12395: inline void msdos_int_21h_53h()
                   12396: {
                   12397:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   12398:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   12399:        
                   12400:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   12401:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   12402:        dpb->shift_count = 0;
                   12403:        dpb->reserved_sectors = 0;
                   12404:        dpb->fat_num = bpb->fat_num;
                   12405:        dpb->root_entries = bpb->root_entries;
                   12406:        dpb->first_data_sector = 0;
                   12407:        if(bpb->sectors_per_cluster != 0) {
                   12408:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   12409:        } else {
                   12410:                dpb->highest_cluster_num = 0;
                   12411:        }
                   12412:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   12413:        dpb->first_dir_sector = 0;
                   12414:        dpb->device_driver_header = 0;
                   12415:        dpb->media_type = bpb->media_type;
                   12416:        dpb->drive_accessed = 0;
                   12417:        dpb->next_dpb_ofs = 0xffff;
                   12418:        dpb->next_dpb_seg = 0xffff;
                   12419:        dpb->first_free_cluster = 0;
                   12420:        dpb->free_clusters = 0xffff;
                   12421: }
                   12422: 
1.1       root     12423: inline void msdos_int_21h_54h()
                   12424: {
                   12425:        process_t *process = msdos_process_info_get(current_psp);
                   12426:        
                   12427:        REG8(AL) = process->verify;
                   12428: }
                   12429: 
                   12430: inline void msdos_int_21h_55h()
                   12431: {
                   12432:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   12433:        
                   12434:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   12435:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   12436:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   12437:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   12438:        psp->parent_psp = current_psp;
                   12439: }
                   12440: 
                   12441: inline void msdos_int_21h_56h(int lfn)
                   12442: {
                   12443:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     12444:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   12445:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     12446:        
1.1.1.63! root     12447:        if(msdos_is_existing_file(dst) || msdos_is_existing_dir(dst)) {
        !          12448:                REG16(AX) = 0x05; // access denied
        !          12449:                m_CF = 1;
        !          12450:        } else if(rename(src, dst)) {
1.1       root     12451:                REG16(AX) = errno;
1.1.1.3   root     12452:                m_CF = 1;
1.1       root     12453:        }
                   12454: }
                   12455: 
                   12456: inline void msdos_int_21h_57h()
                   12457: {
                   12458:        FILETIME time, local;
1.1.1.14  root     12459:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     12460:        HANDLE hHandle;
1.1       root     12461:        
1.1.1.21  root     12462:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     12463:                REG16(AX) = (UINT16)GetLastError();
                   12464:                m_CF = 1;
                   12465:                return;
                   12466:        }
                   12467:        ctime = atime = mtime = NULL;
                   12468:        
1.1       root     12469:        switch(REG8(AL)) {
                   12470:        case 0x00:
1.1.1.6   root     12471:        case 0x01:
1.1.1.14  root     12472:                mtime = &time;
1.1.1.6   root     12473:                break;
                   12474:        case 0x04:
                   12475:        case 0x05:
1.1.1.14  root     12476:                atime = &time;
1.1       root     12477:                break;
1.1.1.6   root     12478:        case 0x06:
                   12479:        case 0x07:
1.1.1.14  root     12480:                ctime = &time;
                   12481:                break;
                   12482:        default:
1.1.1.22  root     12483:                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     12484:                REG16(AX) = 0x01;
                   12485:                m_CF = 1;
                   12486:                return;
                   12487:        }
                   12488:        if(REG8(AL) & 1) {
1.1       root     12489:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   12490:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     12491:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     12492:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12493:                        m_CF = 1;
1.1       root     12494:                }
1.1.1.14  root     12495:        } else {
1.1.1.21  root     12496:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     12497:                        // assume a device and use the current time
                   12498:                        GetSystemTimeAsFileTime(&time);
                   12499:                }
                   12500:                FileTimeToLocalFileTime(&time, &local);
                   12501:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     12502:        }
                   12503: }
                   12504: 
                   12505: inline void msdos_int_21h_58h()
                   12506: {
                   12507:        switch(REG8(AL)) {
                   12508:        case 0x00:
1.1.1.7   root     12509:                REG16(AX) = malloc_strategy;
                   12510:                break;
                   12511:        case 0x01:
1.1.1.24  root     12512: //             switch(REG16(BX)) {
                   12513:                switch(REG8(BL)) {
1.1.1.7   root     12514:                case 0x0000:
                   12515:                case 0x0001:
                   12516:                case 0x0002:
                   12517:                case 0x0040:
                   12518:                case 0x0041:
                   12519:                case 0x0042:
                   12520:                case 0x0080:
                   12521:                case 0x0081:
                   12522:                case 0x0082:
                   12523:                        malloc_strategy = REG16(BX);
1.1.1.23  root     12524:                        msdos_sda_update(current_psp);
1.1.1.7   root     12525:                        break;
                   12526:                default:
1.1.1.22  root     12527:                        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     12528:                        REG16(AX) = 0x01;
                   12529:                        m_CF = 1;
                   12530:                        break;
                   12531:                }
                   12532:                break;
                   12533:        case 0x02:
1.1.1.19  root     12534:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     12535:                break;
                   12536:        case 0x03:
1.1.1.24  root     12537: //             switch(REG16(BX)) {
                   12538:                switch(REG8(BL)) {
1.1.1.7   root     12539:                case 0x0000:
1.1.1.19  root     12540:                        msdos_mem_unlink_umb();
                   12541:                        break;
1.1.1.7   root     12542:                case 0x0001:
1.1.1.19  root     12543:                        msdos_mem_link_umb();
1.1.1.7   root     12544:                        break;
                   12545:                default:
1.1.1.22  root     12546:                        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     12547:                        REG16(AX) = 0x01;
                   12548:                        m_CF = 1;
                   12549:                        break;
                   12550:                }
1.1       root     12551:                break;
                   12552:        default:
1.1.1.22  root     12553:                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     12554:                REG16(AX) = 0x01;
1.1.1.3   root     12555:                m_CF = 1;
1.1       root     12556:                break;
                   12557:        }
                   12558: }
                   12559: 
                   12560: inline void msdos_int_21h_59h()
                   12561: {
1.1.1.47  root     12562:        if(REG16(BX) == 0x0000) {
                   12563:                sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   12564:                
                   12565:                REG16(AX) = sda->extended_error_code;
                   12566:                REG8(BH)  = sda->error_class;
                   12567:                REG8(BL)  = sda->suggested_action;
                   12568:                REG8(CH)  = sda->locus_of_last_error;
                   12569:                // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
                   12570:                if(sda->int21h_5d0ah_called != 0) {
                   12571:                        REG8(CL)  = sda->int21h_5d0ah_cl;
                   12572:                        REG16(DX) = sda->int21h_5d0ah_dx;
                   12573: //                     REG16(SI) = sda->int21h_5d0ah_si;
                   12574:                        REG16(DI) = sda->last_error_pointer.w.l;
                   12575: //                     SREG(DS)  = sda->int21h_5d0ah_ds;
                   12576: //                     i386_load_segment_descriptor(DS);
                   12577:                        SREG(ES)  = sda->last_error_pointer.w.h;
                   12578:                        i386_load_segment_descriptor(ES);
                   12579:                }
                   12580:                sda->int21h_5d0ah_called = 0;
                   12581: //     } else if(REG16(BX) == 0x0001) {
                   12582: //             // European MS-DOS 4.0 - Get Hard Error Information
                   12583:        } else {
                   12584:                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));
                   12585:                REG16(AX) = 0x01;
                   12586:                m_CF = 1;
                   12587:        }
1.1       root     12588: }
                   12589: 
                   12590: inline void msdos_int_21h_5ah()
                   12591: {
1.1.1.3   root     12592:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12593:        int len = strlen(path);
                   12594:        char tmp[MAX_PATH];
                   12595:        
1.1.1.60  root     12596:        if(GetTempFileNameA(path, "TMP", 0, tmp)) {
1.1       root     12597:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12598:                
1.1.1.60  root     12599:                SetFileAttributesA(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     12600:                REG16(AX) = fd;
                   12601:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12602:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12603:                
                   12604:                strcpy(path, tmp);
                   12605:                int dx = REG16(DX) + len;
1.1.1.3   root     12606:                int ds = SREG(DS);
1.1       root     12607:                while(dx > 0xffff) {
                   12608:                        dx -= 0x10;
                   12609:                        ds++;
                   12610:                }
                   12611:                REG16(DX) = dx;
1.1.1.3   root     12612:                SREG(DS) = ds;
                   12613:                i386_load_segment_descriptor(DS);
1.1       root     12614:        } else {
                   12615:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12616:                m_CF = 1;
1.1       root     12617:        }
                   12618: }
                   12619: 
                   12620: inline void msdos_int_21h_5bh()
                   12621: {
1.1.1.45  root     12622:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     12623:        
1.1.1.45  root     12624:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12625:                // already exists
                   12626:                REG16(AX) = 0x50;
1.1.1.3   root     12627:                m_CF = 1;
1.1       root     12628:        } else {
                   12629:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12630:                
                   12631:                if(fd != -1) {
1.1.1.60  root     12632:                        SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     12633:                        REG16(AX) = fd;
                   12634:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12635:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12636:                } else {
                   12637:                        REG16(AX) = errno;
1.1.1.3   root     12638:                        m_CF = 1;
1.1       root     12639:                }
                   12640:        }
                   12641: }
                   12642: 
                   12643: inline void msdos_int_21h_5ch()
                   12644: {
                   12645:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12646:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12647:        
1.1.1.20  root     12648:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     12649:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     12650:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     12651:                        UINT32 pos = _tell(fd);
                   12652:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   12653:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     12654:                                REG16(AX) = errno;
1.1.1.3   root     12655:                                m_CF = 1;
1.1       root     12656:                        }
1.1.1.20  root     12657:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     12658:                        
1.1       root     12659:                        // some seconds may be passed in _locking()
1.1.1.35  root     12660:                        REQUEST_HARDWRE_UPDATE();
1.1       root     12661:                } else {
                   12662:                        REG16(AX) = 0x01;
1.1.1.3   root     12663:                        m_CF = 1;
1.1       root     12664:                }
                   12665:        } else {
                   12666:                REG16(AX) = 0x06;
1.1.1.3   root     12667:                m_CF = 1;
1.1       root     12668:        }
                   12669: }
                   12670: 
1.1.1.22  root     12671: inline void msdos_int_21h_5dh()
                   12672: {
                   12673:        switch(REG8(AL)) {
1.1.1.45  root     12674:        case 0x00:
                   12675:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
                   12676:                        // current system
                   12677:                        static bool reenter = false;
                   12678:                        if(!reenter) {
                   12679:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
                   12680:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
                   12681:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
                   12682:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
                   12683:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
                   12684:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
                   12685:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
                   12686:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
                   12687:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
                   12688:                                i386_load_segment_descriptor(DS);
                   12689:                                i386_load_segment_descriptor(ES);
                   12690:                                reenter = true;
                   12691:                                try {
                   12692:                                        msdos_syscall(0x21);
                   12693:                                } catch(...) {
                   12694:                                }
                   12695:                                reenter = false;
                   12696:                        }
                   12697:                } else {
                   12698:                        REG16(AX) = 0x49; //  network software not installed
                   12699:                        m_CF = 1;
                   12700:                }
                   12701:                break;
1.1.1.22  root     12702:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     12703:                SREG(DS) = (SDA_TOP >> 4);
                   12704:                i386_load_segment_descriptor(DS);
                   12705:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   12706:                REG16(CX) = 0x80;
                   12707:                REG16(DX) = 0x1a;
                   12708:                break;
1.1.1.45  root     12709:        case 0x07: // get redirected printer mode
                   12710:        case 0x08: // set redirected printer mode
                   12711:        case 0x09: // flush redirected printer output
                   12712:                REG16(AX) = 0x49; //  network software not installed
                   12713:                m_CF = 1;
                   12714:                break;
1.1.1.43  root     12715:        case 0x0a: // set extended error information
                   12716:                {
                   12717:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     12718:                        sda->int21h_5d0ah_called    = 1;
1.1.1.43  root     12719:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47  root     12720:                        // XXX: which one is correct ???
                   12721: #if 1
                   12722:                        // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43  root     12723:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   12724:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47  root     12725:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43  root     12726:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47  root     12727: #else
                   12728:                        // PC DOS 7 Technical Update
                   12729:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
                   12730:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
                   12731:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
                   12732:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
                   12733: #endif
                   12734:                        sda->int21h_5d0ah_dx        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   12735: //                     sda->int21h_5d0ah_si        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43  root     12736:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47  root     12737: //                     sda->int21h_5d0ah_ds        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43  root     12738:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   12739:                }
                   12740:                break;
1.1.1.23  root     12741:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     12742:                REG16(AX) = 0x01;
                   12743:                m_CF = 1;
                   12744:                break;
                   12745:        default:
                   12746:                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));
                   12747:                REG16(AX) = 0x01;
                   12748:                m_CF = 1;
                   12749:                break;
                   12750:        }
                   12751: }
                   12752: 
1.1.1.42  root     12753: inline void msdos_int_21h_5eh()
                   12754: {
                   12755:        switch(REG8(AL)) {
                   12756:        case 0x00:
                   12757:                {
                   12758:                        char name[256] = {0};
                   12759:                        DWORD dwSize = 256;
                   12760:                        
1.1.1.60  root     12761:                        if(GetComputerNameA(name, &dwSize)) {
1.1.1.42  root     12762:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12763:                                for(int i = 0; i < 15; i++) {
                   12764:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   12765:                                }
                   12766:                                dest[15] = '\0';
                   12767:                                REG8(CH) = 0x01; // nonzero valid
                   12768:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   12769:                        } else {
                   12770:                                REG16(AX) = 0x01;
                   12771:                                m_CF = 1;
                   12772:                        }
                   12773:                }
                   12774:                break;
                   12775:        default:
1.1.1.45  root     12776: //             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));
                   12777: //             REG16(AX) = 0x01;
                   12778:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     12779:                m_CF = 1;
                   12780:                break;
                   12781:        }
                   12782: }
                   12783: 
1.1.1.30  root     12784: inline void msdos_int_21h_5fh()
                   12785: {
                   12786:        switch(REG8(AL)) {
1.1.1.42  root     12787:        case 0x05:
1.1.1.44  root     12788:                REG16(BP) = 0;
                   12789:                for(int i = 0; i < 26; i++) {
                   12790:                        if(msdos_is_remote_drive(i)) {
                   12791:                                REG16(BP)++;
1.1.1.42  root     12792:                        }
                   12793:                }
1.1.1.30  root     12794:        case 0x02:
1.1.1.44  root     12795:                for(int i = 0, index = 0; i < 26; i++) {
                   12796:                        if(msdos_is_remote_drive(i)) {
                   12797:                                if(index == REG16(BX)) {
                   12798:                                        char volume[] = "A:";
1.1.1.30  root     12799:                                        volume[0] = 'A' + i;
1.1.1.44  root     12800:                                        DWORD dwSize = 128;
                   12801:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
1.1.1.60  root     12802:                                        WNetGetConnectionA(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
1.1.1.44  root     12803:                                        REG8(BH) = 0x00; // valid
                   12804:                                        REG8(BL) = 0x04; // disk drive
                   12805:                                        REG16(CX) = 0x00; // LANtastic
                   12806:                                        return;
1.1.1.30  root     12807:                                }
1.1.1.44  root     12808:                                index++;
1.1.1.30  root     12809:                        }
                   12810:                }
                   12811:                REG16(AX) = 0x12; // no more files
                   12812:                m_CF = 1;
                   12813:                break;
1.1.1.44  root     12814:        case 0x07:
                   12815:                if(msdos_is_valid_drive(REG8(DL))) {
                   12816:                        msdos_cds_update(REG8(DL));
                   12817:                } else {
                   12818:                        REG16(AX) = 0x0f; // invalid drive
                   12819:                        m_CF = 1;
                   12820:                }
                   12821:                break;
                   12822:        case 0x08:
                   12823:                if(msdos_is_valid_drive(REG8(DL))) {
                   12824:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   12825:                        cds->drive_attrib = 0x0000;
                   12826:                } else {
                   12827:                        REG16(AX) = 0x0f; // invalid drive
                   12828:                        m_CF = 1;
                   12829:                }
                   12830:                break;
1.1.1.30  root     12831:        default:
1.1.1.45  root     12832: //             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));
                   12833: //             REG16(AX) = 0x01;
                   12834:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     12835:                m_CF = 1;
                   12836:                break;
                   12837:        }
                   12838: }
                   12839: 
1.1       root     12840: inline void msdos_int_21h_60h(int lfn)
                   12841: {
1.1.1.45  root     12842:        char full[MAX_PATH];
                   12843:        const char *path = NULL;
1.1.1.14  root     12844:        
1.1       root     12845:        if(lfn) {
1.1.1.14  root     12846:                char *name;
                   12847:                *full = '\0';
1.1.1.60  root     12848:                GetFullPathNameA((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     12849:                switch(REG8(CL)) {
                   12850:                case 1:
1.1.1.60  root     12851:                        GetShortPathNameA(full, full, MAX_PATH);
1.1.1.14  root     12852:                        my_strupr(full);
                   12853:                        break;
                   12854:                case 2:
1.1.1.60  root     12855:                        GetLongPathNameA(full, full, MAX_PATH);
1.1.1.14  root     12856:                        break;
                   12857:                }
                   12858:                path = full;
                   12859:        } else {
                   12860:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12861:        }
                   12862:        if(*path != '\0') {
                   12863:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     12864:        } else {
1.1.1.14  root     12865:                REG16(AX) = (UINT16)GetLastError();
                   12866:                m_CF = 1;
1.1       root     12867:        }
                   12868: }
                   12869: 
                   12870: inline void msdos_int_21h_61h()
                   12871: {
                   12872:        REG8(AL) = 0;
                   12873: }
                   12874: 
                   12875: inline void msdos_int_21h_62h()
                   12876: {
                   12877:        REG16(BX) = current_psp;
                   12878: }
                   12879: 
                   12880: inline void msdos_int_21h_63h()
                   12881: {
                   12882:        switch(REG8(AL)) {
                   12883:        case 0x00:
1.1.1.3   root     12884:                SREG(DS) = (DBCS_TABLE >> 4);
                   12885:                i386_load_segment_descriptor(DS);
1.1       root     12886:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   12887:                REG8(AL) = 0x00;
                   12888:                break;
1.1.1.22  root     12889:        case 0x01: // set korean input mode
                   12890:        case 0x02: // get korean input mode
                   12891:                REG8(AL) = 0xff; // not supported
                   12892:                break;
1.1       root     12893:        default:
1.1.1.22  root     12894:                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     12895:                REG16(AX) = 0x01;
1.1.1.3   root     12896:                m_CF = 1;
1.1       root     12897:                break;
                   12898:        }
                   12899: }
                   12900: 
1.1.1.25  root     12901: UINT16 get_extended_country_info(UINT8 func)
1.1       root     12902: {
1.1.1.25  root     12903:        switch(func) {
1.1.1.17  root     12904:        case 0x01:
                   12905:                if(REG16(CX) >= 5) {
1.1.1.19  root     12906:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     12907:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   12908:                                REG16(CX) = sizeof(data);
                   12909:                        ZeroMemory(data, sizeof(data));
                   12910:                        data[0] = 0x01;
                   12911:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     12912:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     12913:                        *(UINT16 *)(data + 5) = active_code_page;
                   12914:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     12915: //                     REG16(AX) = active_code_page;
1.1.1.17  root     12916:                } else {
1.1.1.25  root     12917:                        return(0x08); // insufficient memory
1.1.1.17  root     12918:                }
                   12919:                break;
                   12920:        case 0x02:
                   12921:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12922:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   12923:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     12924: //             REG16(AX) = active_code_page;
1.1.1.17  root     12925:                REG16(CX) = 0x05;
                   12926:                break;
1.1.1.23  root     12927:        case 0x03:
                   12928:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12929:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   12930:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     12931: //             REG16(AX) = active_code_page;
1.1.1.23  root     12932:                REG16(CX) = 0x05;
                   12933:                break;
1.1.1.17  root     12934:        case 0x04:
                   12935:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   12936:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   12937:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     12938: //             REG16(AX) = active_code_page;
1.1.1.17  root     12939:                REG16(CX) = 0x05;
                   12940:                break;
                   12941:        case 0x05:
                   12942:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   12943:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   12944:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     12945: //             REG16(AX) = active_code_page;
1.1.1.17  root     12946:                REG16(CX) = 0x05;
                   12947:                break;
                   12948:        case 0x06:
                   12949:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   12950:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   12951:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     12952: //             REG16(AX) = active_code_page;
1.1.1.17  root     12953:                REG16(CX) = 0x05;
                   12954:                break;
1.1       root     12955:        case 0x07:
1.1.1.3   root     12956:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   12957:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   12958:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     12959: //             REG16(AX) = active_code_page;
1.1       root     12960:                REG16(CX) = 0x05;
                   12961:                break;
1.1.1.25  root     12962:        default:
                   12963:                return(0x01); // function number invalid
                   12964:        }
                   12965:        return(0x00);
                   12966: }
                   12967: 
                   12968: inline void msdos_int_21h_65h()
                   12969: {
                   12970:        char tmp[0x10000];
                   12971:        
                   12972:        switch(REG8(AL)) {
1.1.1.43  root     12973:        case 0x00:
                   12974:                if(REG16(CX) >= 7) {
                   12975:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   12976:                        REG16(AX) = system_code_page;
                   12977:                } else {
                   12978:                        REG16(AX) = 0x0c;
                   12979:                        m_CF = 1;
                   12980:                }
                   12981:                break;
1.1.1.25  root     12982:        case 0x01:
                   12983:        case 0x02:
                   12984:        case 0x03:
                   12985:        case 0x04:
                   12986:        case 0x05:
                   12987:        case 0x06:
                   12988:        case 0x07:
                   12989:                {
                   12990:                        UINT16 result = get_extended_country_info(REG8(AL));
                   12991:                        if(result) {
                   12992:                                REG16(AX) = result;
                   12993:                                m_CF = 1;
                   12994:                        } else {
                   12995:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   12996:                        }
                   12997:                }
                   12998:                break;
1.1       root     12999:        case 0x20:
1.1.1.25  root     13000:        case 0xa0:
1.1.1.19  root     13001:                memset(tmp, 0, sizeof(tmp));
                   13002:                tmp[0] = REG8(DL);
1.1       root     13003:                my_strupr(tmp);
                   13004:                REG8(DL) = tmp[0];
                   13005:                break;
                   13006:        case 0x21:
1.1.1.25  root     13007:        case 0xa1:
1.1       root     13008:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13009:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     13010:                my_strupr(tmp);
1.1.1.3   root     13011:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     13012:                break;
                   13013:        case 0x22:
1.1.1.25  root     13014:        case 0xa2:
1.1.1.3   root     13015:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     13016:                break;
1.1.1.25  root     13017:        case 0x23:
                   13018:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45  root     13019:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     13020:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45  root     13021:                        REG16(AX) = 0x00;
                   13022:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   13023:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     13024:                        REG16(AX) = 0x01;
                   13025:                } else {
                   13026:                        REG16(AX) = 0x02;
                   13027:                }
                   13028:                break;
1.1       root     13029:        default:
1.1.1.22  root     13030:                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     13031:                REG16(AX) = 0x01;
1.1.1.3   root     13032:                m_CF = 1;
1.1       root     13033:                break;
                   13034:        }
                   13035: }
                   13036: 
                   13037: inline void msdos_int_21h_66h()
                   13038: {
                   13039:        switch(REG8(AL)) {
                   13040:        case 0x01:
                   13041:                REG16(BX) = active_code_page;
                   13042:                REG16(DX) = system_code_page;
                   13043:                break;
                   13044:        case 0x02:
                   13045:                if(active_code_page == REG16(BX)) {
                   13046:                        REG16(AX) = 0xeb41;
                   13047:                } else if(_setmbcp(REG16(BX)) == 0) {
                   13048:                        active_code_page = REG16(BX);
1.1.1.17  root     13049:                        msdos_nls_tables_update();
1.1       root     13050:                        REG16(AX) = 0xeb41;
1.1.1.32  root     13051:                        SetConsoleCP(active_code_page);
                   13052:                        SetConsoleOutputCP(active_code_page);
1.1       root     13053:                } else {
                   13054:                        REG16(AX) = 0x25;
1.1.1.3   root     13055:                        m_CF = 1;
1.1       root     13056:                }
                   13057:                break;
                   13058:        default:
1.1.1.22  root     13059:                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     13060:                REG16(AX) = 0x01;
1.1.1.3   root     13061:                m_CF = 1;
1.1       root     13062:                break;
                   13063:        }
                   13064: }
                   13065: 
                   13066: inline void msdos_int_21h_67h()
                   13067: {
                   13068:        process_t *process = msdos_process_info_get(current_psp);
                   13069:        
                   13070:        if(REG16(BX) <= MAX_FILES) {
                   13071:                process->max_files = max(REG16(BX), 20);
                   13072:        } else {
                   13073:                REG16(AX) = 0x08;
1.1.1.3   root     13074:                m_CF = 1;
1.1       root     13075:        }
                   13076: }
                   13077: 
                   13078: inline void msdos_int_21h_68h()
                   13079: {
                   13080:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     13081:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     13082:        
1.1.1.20  root     13083:        if(fd < process->max_files && file_handler[fd].valid) {
                   13084:                // fflush(_fdopen(fd, ""));
1.1       root     13085:        } else {
                   13086:                REG16(AX) = 0x06;
1.1.1.3   root     13087:                m_CF = 1;
1.1       root     13088:        }
                   13089: }
                   13090: 
                   13091: inline void msdos_int_21h_69h()
                   13092: {
1.1.1.3   root     13093:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13094:        char path[] = "A:\\";
                   13095:        char volume_label[MAX_PATH];
                   13096:        DWORD serial_number = 0;
                   13097:        char file_system[MAX_PATH];
                   13098:        
                   13099:        if(REG8(BL) == 0) {
                   13100:                path[0] = 'A' + _getdrive() - 1;
                   13101:        } else {
                   13102:                path[0] = 'A' + REG8(BL) - 1;
                   13103:        }
                   13104:        
                   13105:        switch(REG8(AL)) {
                   13106:        case 0x00:
1.1.1.60  root     13107:                if(GetVolumeInformationA(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
1.1       root     13108:                        info->info_level = 0;
                   13109:                        info->serial_number = serial_number;
                   13110:                        memset(info->volume_label, 0x20, 11);
                   13111:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   13112:                        memset(info->file_system, 0x20, 8);
                   13113:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   13114:                } else {
                   13115:                        REG16(AX) = errno;
1.1.1.3   root     13116:                        m_CF = 1;
1.1       root     13117:                }
                   13118:                break;
                   13119:        case 0x01:
                   13120:                REG16(AX) = 0x03;
1.1.1.3   root     13121:                m_CF = 1;
1.1.1.45  root     13122:                break;
                   13123:        default:
                   13124:                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));
                   13125:                REG16(AX) = 0x01;
                   13126:                m_CF = 1;
                   13127:                break;
1.1       root     13128:        }
                   13129: }
                   13130: 
                   13131: inline void msdos_int_21h_6ah()
                   13132: {
                   13133:        REG8(AH) = 0x68;
                   13134:        msdos_int_21h_68h();
                   13135: }
                   13136: 
                   13137: inline void msdos_int_21h_6bh()
                   13138: {
1.1.1.45  root     13139:        REG8(AL) = 0x00;
1.1       root     13140: }
                   13141: 
                   13142: inline void msdos_int_21h_6ch(int lfn)
                   13143: {
1.1.1.45  root     13144:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     13145:        int mode = REG8(BL) & 0x03;
                   13146:        
                   13147:        if(mode < 0x03) {
1.1.1.29  root     13148:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     13149:                        // file exists
                   13150:                        if(REG8(DL) & 1) {
1.1.1.37  root     13151:                                int fd = -1;
                   13152:                                int sio_port = 0;
                   13153:                                int lpt_port = 0;
1.1       root     13154:                                
1.1.1.45  root     13155:                                if(msdos_is_device_path(path)) {
                   13156:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     13157:                                } else {
1.1.1.13  root     13158:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     13159:                                }
1.1       root     13160:                                if(fd != -1) {
                   13161:                                        REG16(AX) = fd;
                   13162:                                        REG16(CX) = 1;
1.1.1.45  root     13163:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     13164:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13165:                                } else {
                   13166:                                        REG16(AX) = errno;
1.1.1.3   root     13167:                                        m_CF = 1;
1.1       root     13168:                                }
                   13169:                        } else if(REG8(DL) & 2) {
1.1.1.60  root     13170:                                int attr = GetFileAttributesA(path);
1.1.1.37  root     13171:                                int fd = -1;
                   13172:                                int sio_port = 0;
                   13173:                                int lpt_port = 0;
1.1       root     13174:                                
1.1.1.45  root     13175:                                if(msdos_is_device_path(path)) {
                   13176:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     13177:                                } else {
                   13178:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   13179:                                }
                   13180:                                if(fd != -1) {
                   13181:                                        if(attr == -1) {
                   13182:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   13183:                                        }
1.1.1.60  root     13184:                                        SetFileAttributesA(path, attr);
1.1       root     13185:                                        REG16(AX) = fd;
                   13186:                                        REG16(CX) = 3;
1.1.1.45  root     13187:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     13188:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13189:                                } else {
                   13190:                                        REG16(AX) = errno;
1.1.1.3   root     13191:                                        m_CF = 1;
1.1       root     13192:                                }
                   13193:                        } else {
                   13194:                                REG16(AX) = 0x50;
1.1.1.3   root     13195:                                m_CF = 1;
1.1       root     13196:                        }
                   13197:                } else {
                   13198:                        // file not exists
                   13199:                        if(REG8(DL) & 0x10) {
                   13200:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   13201:                                
                   13202:                                if(fd != -1) {
1.1.1.60  root     13203:                                        SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     13204:                                        REG16(AX) = fd;
                   13205:                                        REG16(CX) = 2;
                   13206:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     13207:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13208:                                } else {
                   13209:                                        REG16(AX) = errno;
1.1.1.3   root     13210:                                        m_CF = 1;
1.1       root     13211:                                }
                   13212:                        } else {
                   13213:                                REG16(AX) = 0x02;
1.1.1.3   root     13214:                                m_CF = 1;
1.1       root     13215:                        }
                   13216:                }
                   13217:        } else {
                   13218:                REG16(AX) = 0x0c;
1.1.1.3   root     13219:                m_CF = 1;
1.1       root     13220:        }
                   13221: }
                   13222: 
1.1.1.43  root     13223: inline void msdos_int_21h_70h()
                   13224: {
                   13225:        switch(REG8(AL)) {
1.1.1.48  root     13226:        case 0x00: // get ??? info
                   13227:        case 0x01: // set above info
                   13228: //             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));
                   13229:                REG16(AX) = 0x7000;
                   13230:                m_CF = 1;
                   13231:                break;
                   13232:        case 0x02: // set general internationalization info
1.1.1.43  root     13233:                if(REG16(CX) >= 7) {
                   13234:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   13235:                        msdos_nls_tables_update();
                   13236:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   13237:                        REG16(AX) = system_code_page;
                   13238:                } else {
                   13239:                        REG16(AX) = 0x0c;
                   13240:                        m_CF = 1;
                   13241:                }
                   13242:                break;
                   13243:        default:
                   13244:                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     13245:                REG16(AX) = 0x7000;
1.1.1.43  root     13246:                m_CF = 1;
                   13247:                break;
                   13248:        }
                   13249: }
                   13250: 
1.1       root     13251: inline void msdos_int_21h_710dh()
                   13252: {
                   13253:        // reset drive
                   13254: }
                   13255: 
1.1.1.48  root     13256: inline void msdos_int_21h_7141h()
1.1.1.17  root     13257: {
                   13258:        if(REG16(SI) == 0) {
1.1.1.48  root     13259:                msdos_int_21h_41h(1);
1.1.1.17  root     13260:                return;
                   13261:        }
                   13262:        if(REG16(SI) != 1) {
                   13263:                REG16(AX) = 5;
                   13264:                m_CF = 1;
                   13265:        }
                   13266:        /* wild card and matching attributes... */
                   13267:        char tmp[MAX_PATH * 2];
                   13268:        // copy search pathname (and quick check overrun)
                   13269:        ZeroMemory(tmp, sizeof(tmp));
                   13270:        tmp[MAX_PATH - 1] = '\0';
                   13271:        tmp[MAX_PATH] = 1;
                   13272:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   13273:        
                   13274:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   13275:                REG16(AX) = 1;
                   13276:                m_CF = 1;
                   13277:                return;
                   13278:        }
                   13279:        for(char *s = tmp; *s; ++s) {
                   13280:                if(*s == '/') {
                   13281:                        *s = '\\';
                   13282:                }
                   13283:        }
1.1.1.60  root     13284:        char *tmp_name = my_strrchr(tmp, '\\');
1.1.1.17  root     13285:        if(tmp_name) {
                   13286:                ++tmp_name;
                   13287:        } else {
                   13288:                tmp_name = strchr(tmp, ':');
                   13289:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   13290:        }
                   13291:        
                   13292:        WIN32_FIND_DATAA fd;
                   13293:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   13294:        if(fh == INVALID_HANDLE_VALUE) {
                   13295:                REG16(AX) = 2;
                   13296:                m_CF = 1;
                   13297:                return;
                   13298:        }
                   13299:        do {
                   13300:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   13301:                        strcpy(tmp_name, fd.cFileName);
1.1.1.48  root     13302:                        if(remove(msdos_trimmed_path(tmp, 1))) {
1.1.1.17  root     13303:                                REG16(AX) = 5;
                   13304:                                m_CF = 1;
                   13305:                                break;
                   13306:                        }
                   13307:                }
                   13308:        } while(FindNextFileA(fh, &fd));
                   13309:        if(!m_CF) {
                   13310:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   13311:                        m_CF = 1;
                   13312:                        REG16(AX) = 2;
                   13313:                }
                   13314:        }
                   13315:        FindClose(fh);
                   13316: }
                   13317: 
1.1       root     13318: inline void msdos_int_21h_714eh()
                   13319: {
                   13320:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13321:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   13322:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.60  root     13323:        WIN32_FIND_DATAA fd;
1.1       root     13324:        
1.1.1.13  root     13325:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   13326:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13327:                FindClose(dtainfo->find_handle);
                   13328:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13329:        }
                   13330:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     13331:        dtainfo->allowable_mask = REG8(CL);
                   13332:        dtainfo->required_mask = REG8(CH);
                   13333:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     13334:        
1.1.1.14  root     13335:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   13336:                dtainfo->allowable_mask &= ~8;
1.1       root     13337:        }
1.1.1.60  root     13338:        if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14  root     13339:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.60  root     13340:                        if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     13341:                                FindClose(dtainfo->find_handle);
                   13342:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13343:                                break;
                   13344:                        }
                   13345:                }
                   13346:        }
1.1.1.13  root     13347:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13348:                find->attrib = fd.dwFileAttributes;
                   13349:                msdos_find_file_conv_local_time(&fd);
                   13350:                if(REG16(SI) == 0) {
                   13351:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13352:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13353:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13354:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13355:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13356:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13357:                } else {
                   13358:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13359:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13360:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13361:                }
                   13362:                find->size_hi = fd.nFileSizeHigh;
                   13363:                find->size_lo = fd.nFileSizeLow;
                   13364:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13365:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13366:                REG16(AX) = dtainfo - dtalist + 1;
                   13367:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13368:                // volume label
                   13369:                find->attrib = 8;
                   13370:                find->size_hi = find->size_lo = 0;
                   13371:                strcpy(find->full_name, process->volume_label);
                   13372:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13373:                dtainfo->allowable_mask &= ~8;
                   13374:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     13375:        } else {
                   13376:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     13377:                m_CF = 1;
1.1       root     13378:        }
                   13379: }
                   13380: 
                   13381: inline void msdos_int_21h_714fh()
                   13382: {
                   13383:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13384:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1.1.60  root     13385:        WIN32_FIND_DATAA fd;
1.1       root     13386:        
1.1.1.14  root     13387:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13388:                REG16(AX) = 6;
1.1.1.13  root     13389:                m_CF = 1;
                   13390:                return;
                   13391:        }
1.1.1.14  root     13392:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13393:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60  root     13394:                if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14  root     13395:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.60  root     13396:                                if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     13397:                                        FindClose(dtainfo->find_handle);
                   13398:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13399:                                        break;
                   13400:                                }
                   13401:                        }
                   13402:                } else {
1.1.1.13  root     13403:                        FindClose(dtainfo->find_handle);
                   13404:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13405:                }
                   13406:        }
1.1.1.13  root     13407:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13408:                find->attrib = fd.dwFileAttributes;
                   13409:                msdos_find_file_conv_local_time(&fd);
                   13410:                if(REG16(SI) == 0) {
                   13411:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13412:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13413:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13414:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13415:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13416:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13417:                } else {
                   13418:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13419:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13420:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13421:                }
                   13422:                find->size_hi = fd.nFileSizeHigh;
                   13423:                find->size_lo = fd.nFileSizeLow;
                   13424:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13425:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13426:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13427:                // volume label
                   13428:                find->attrib = 8;
                   13429:                find->size_hi = find->size_lo = 0;
                   13430:                strcpy(find->full_name, process->volume_label);
                   13431:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13432:                dtainfo->allowable_mask &= ~8;
1.1       root     13433:        } else {
                   13434:                REG16(AX) = 0x12;
1.1.1.3   root     13435:                m_CF = 1;
1.1       root     13436:        }
                   13437: }
                   13438: 
                   13439: inline void msdos_int_21h_71a0h()
                   13440: {
                   13441:        DWORD max_component_len, file_sys_flag;
                   13442:        
1.1.1.60  root     13443:        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     13444:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   13445:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     13446:                REG16(CX) = (UINT16)max_component_len;          // 255
                   13447:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   13448:        } else {
                   13449:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13450:                m_CF = 1;
1.1       root     13451:        }
                   13452: }
                   13453: 
                   13454: inline void msdos_int_21h_71a1h()
                   13455: {
1.1.1.14  root     13456:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13457:                REG16(AX) = 6;
1.1.1.13  root     13458:                m_CF = 1;
                   13459:                return;
                   13460:        }
1.1.1.14  root     13461:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13462:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13463:                FindClose(dtainfo->find_handle);
                   13464:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13465:        }
                   13466: }
                   13467: 
                   13468: inline void msdos_int_21h_71a6h()
                   13469: {
                   13470:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     13471:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13472:        
1.1.1.3   root     13473:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13474:        struct _stat64 status;
                   13475:        DWORD serial_number = 0;
                   13476:        
1.1.1.20  root     13477:        if(fd < process->max_files && file_handler[fd].valid) {
                   13478:                if(_fstat64(fd, &status) == 0) {
                   13479:                        if(file_handler[fd].path[1] == ':') {
1.1       root     13480:                                // NOTE: we need to consider the network file path "\\host\share\"
                   13481:                                char volume[] = "A:\\";
1.1.1.20  root     13482:                                volume[0] = file_handler[fd].path[1];
1.1.1.60  root     13483:                                GetVolumeInformationA(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
1.1       root     13484:                        }
1.1.1.60  root     13485:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributesA(file_handler[fd].path);
1.1       root     13486:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   13487:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   13488:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   13489:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   13490:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   13491:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   13492:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   13493:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   13494:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   13495:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     13496:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     13497:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     13498:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     13499:                } else {
                   13500:                        REG16(AX) = errno;
1.1.1.3   root     13501:                        m_CF = 1;
1.1       root     13502:                }
                   13503:        } else {
                   13504:                REG16(AX) = 0x06;
1.1.1.3   root     13505:                m_CF = 1;
1.1       root     13506:        }
                   13507: }
                   13508: 
                   13509: inline void msdos_int_21h_71a7h()
                   13510: {
                   13511:        switch(REG8(BL)) {
                   13512:        case 0x00:
1.1.1.3   root     13513:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     13514:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13515:                        m_CF = 1;
1.1       root     13516:                }
                   13517:                break;
                   13518:        case 0x01:
                   13519:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     13520:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     13521:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13522:                        m_CF = 1;
1.1       root     13523:                }
                   13524:                break;
                   13525:        default:
1.1.1.22  root     13526:                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     13527:                REG16(AX) = 0x7100;
1.1.1.3   root     13528:                m_CF = 1;
1.1       root     13529:                break;
                   13530:        }
                   13531: }
                   13532: 
                   13533: inline void msdos_int_21h_71a8h()
                   13534: {
                   13535:        if(REG8(DH) == 0) {
                   13536:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     13537:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13538:                memset(fcb, 0x20, sizeof(fcb));
                   13539:                int len = strlen(tmp);
1.1.1.21  root     13540:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     13541:                        if(tmp[i] == '.') {
                   13542:                                pos = 8;
                   13543:                        } else {
                   13544:                                if(msdos_lead_byte_check(tmp[i])) {
                   13545:                                        fcb[pos++] = tmp[i++];
                   13546:                                }
                   13547:                                fcb[pos++] = tmp[i];
                   13548:                        }
                   13549:                }
1.1.1.3   root     13550:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     13551:        } else {
1.1.1.3   root     13552:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13553:        }
                   13554: }
                   13555: 
1.1.1.22  root     13556: inline void msdos_int_21h_71aah()
                   13557: {
                   13558:        char drv[] = "A:", path[MAX_PATH];
                   13559:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13560:        
                   13561:        if(REG8(BL) == 0) {
                   13562:                drv[0] = 'A' + _getdrive() - 1;
                   13563:        } else {
                   13564:                drv[0] = 'A' + REG8(BL) - 1;
                   13565:        }
                   13566:        switch(REG8(BH)) {
                   13567:        case 0x00:
1.1.1.44  root     13568:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13569:                        REG16(AX) = 0x0f; // invalid drive
                   13570:                        m_CF = 1;
1.1.1.60  root     13571:                } else if(DefineDosDeviceA(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
1.1.1.44  root     13572:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     13573:                        m_CF = 1;
                   13574:                }
                   13575:                break;
                   13576:        case 0x01:
1.1.1.44  root     13577:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13578:                        REG16(AX) = 0x0f; // invalid drive
                   13579:                        m_CF = 1;
1.1.1.60  root     13580:                } else if(DefineDosDeviceA(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     13581:                        REG16(AX) = 0x0f; // invalid drive
                   13582:                        m_CF = 1;
                   13583:                }
                   13584:                break;
                   13585:        case 0x02:
1.1.1.44  root     13586:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13587:                        REG16(AX) = 0x0f; // invalid drive
                   13588:                        m_CF = 1;
1.1.1.60  root     13589:                } else if(QueryDosDeviceA(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     13590:                        REG16(AX) = 0x0f; // invalid drive
                   13591:                        m_CF = 1;
                   13592:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   13593:                        REG16(AX) = 0x0f; // invalid drive
                   13594:                        m_CF = 1;
                   13595:                } else {
                   13596:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   13597:                }
                   13598:                break;
                   13599:        default:
                   13600:                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     13601:                REG16(AX) = 0x7100;
1.1.1.22  root     13602:                m_CF = 1;
                   13603:                break;
                   13604:        }
                   13605: }
                   13606: 
1.1.1.14  root     13607: inline void msdos_int_21h_7300h()
                   13608: {
1.1.1.44  root     13609:        REG8(AL) = REG8(CL);
                   13610:        REG8(AH) = 0;
1.1.1.14  root     13611: }
                   13612: 
                   13613: inline void msdos_int_21h_7302h()
                   13614: {
                   13615:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   13616:        UINT16 seg, ofs;
                   13617:        
                   13618:        if(REG16(CX) < 0x3f) {
                   13619:                REG8(AL) = 0x18;
                   13620:                m_CF = 1;
                   13621:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   13622:                REG8(AL) = 0xff;
                   13623:                m_CF = 1;
                   13624:        } else {
                   13625:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   13626:        }
                   13627: }
                   13628: 
1.1       root     13629: inline void msdos_int_21h_7303h()
                   13630: {
1.1.1.3   root     13631:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13632:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     13633:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   13634:        
1.1.1.60  root     13635:        if(GetDiskFreeSpaceA(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
1.1       root     13636:                info->size_of_structure = sizeof(ext_space_info_t);
                   13637:                info->structure_version = 0;
                   13638:                info->sectors_per_cluster = sectors_per_cluster;
                   13639:                info->bytes_per_sector = bytes_per_sector;
                   13640:                info->available_clusters_on_drive = free_clusters;
                   13641:                info->total_clusters_on_drive = total_clusters;
                   13642:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   13643:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   13644:                info->available_allocation_units = free_clusters;       // ???
                   13645:                info->total_allocation_units = total_clusters;          // ???
                   13646:        } else {
                   13647:                REG16(AX) = errno;
1.1.1.3   root     13648:                m_CF = 1;
1.1       root     13649:        }
                   13650: }
                   13651: 
1.1.1.30  root     13652: inline void msdos_int_21h_dbh()
                   13653: {
                   13654:        // Novell NetWare - Workstation - Get Number of Local Drives
                   13655:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   13656:        REG8(AL) = dos_info->last_drive;
                   13657: }
                   13658: 
                   13659: inline void msdos_int_21h_dch()
                   13660: {
                   13661:        // Novell NetWare - Connection Services - Get Connection Number
                   13662:        REG8(AL) = 0x00;
                   13663: }
                   13664: 
1.1.1.32  root     13665: inline void msdos_int_24h()
                   13666: {
                   13667:        const char *message = NULL;
                   13668:        int key = 0;
                   13669:        
                   13670:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   13671:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   13672:                        if(active_code_page == 932) {
                   13673:                                message = critical_error_table[i].message_japanese;
                   13674:                        }
                   13675:                        if(message == NULL) {
                   13676:                                message = critical_error_table[i].message_english;
                   13677:                        }
                   13678:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   13679:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   13680:                        
                   13681:                        SREG(ES) = WORK_TOP >> 4;
                   13682:                        i386_load_segment_descriptor(ES);
                   13683:                        REG16(DI) = 0x0000;
                   13684:                        break;
                   13685:                }
                   13686:        }
                   13687:        fprintf(stderr, "\n%s", message);
                   13688:        if(!(REG8(AH) & 0x80)) {
                   13689:                if(REG8(AH) & 0x01) {
                   13690:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   13691:                } else {
                   13692:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   13693:                }
                   13694:        }
                   13695:        fprintf(stderr, "\n");
                   13696:        
1.1.1.33  root     13697:        {
1.1.1.32  root     13698:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     13699:        }
1.1.1.32  root     13700:        if(REG8(AH) & 0x10) {
                   13701:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   13702:        }
                   13703:        if(REG8(AH) & 0x20) {
                   13704:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   13705:        }
                   13706:        if(REG8(AH) & 0x08) {
                   13707:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   13708:        }
                   13709:        fprintf(stderr, "? ");
                   13710:        
                   13711:        while(1) {
                   13712:                while(!_kbhit()) {
                   13713:                        Sleep(10);
                   13714:                }
                   13715:                key = _getch();
                   13716:                
                   13717:                if(key == 'I' || key == 'i') {
                   13718:                        if(REG8(AH) & 0x20) {
                   13719:                                REG8(AL) = 0;
                   13720:                                break;
                   13721:                        }
                   13722:                } else if(key == 'R' || key == 'r') {
                   13723:                        if(REG8(AH) & 0x10) {
                   13724:                                REG8(AL) = 1;
                   13725:                                break;
                   13726:                        }
                   13727:                } else if(key == 'A' || key == 'a') {
                   13728:                        REG8(AL) = 2;
                   13729:                        break;
                   13730:                } else if(key == 'F' || key == 'f') {
                   13731:                        if(REG8(AH) & 0x08) {
                   13732:                                REG8(AL) = 3;
                   13733:                                break;
                   13734:                        }
                   13735:                }
                   13736:        }
                   13737:        fprintf(stderr, "%c\n", key);
                   13738: }
                   13739: 
1.1       root     13740: inline void msdos_int_25h()
                   13741: {
                   13742:        UINT16 seg, ofs;
                   13743:        DWORD dwSize;
                   13744:        
1.1.1.3   root     13745: #if defined(HAS_I386)
                   13746:        I386OP(pushf)();
                   13747: #else
                   13748:        PREFIX86(_pushf());
                   13749: #endif
1.1       root     13750:        
                   13751:        if(!(REG8(AL) < 26)) {
                   13752:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13753:                m_CF = 1;
1.1       root     13754:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13755:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13756:                m_CF = 1;
1.1       root     13757:        } else {
                   13758:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13759:                char dev[64];
                   13760:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13761:                
1.1.1.60  root     13762:                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     13763:                if(hFile == INVALID_HANDLE_VALUE) {
                   13764:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13765:                        m_CF = 1;
1.1       root     13766:                } else {
1.1.1.19  root     13767:                        UINT32 top_sector  = REG16(DX);
                   13768:                        UINT16 sector_num  = REG16(CX);
                   13769:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13770:                        
                   13771:                        if(sector_num == 0xffff) {
                   13772:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13773:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13774:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13775:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13776:                                buffer_addr = (seg << 4) + ofs;
                   13777:                        }
                   13778: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13779: //                             REG8(AL) = 0x02; // drive not ready
                   13780: //                             m_CF = 1;
                   13781: //                     } else 
                   13782:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13783:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13784:                                m_CF = 1;
1.1.1.19  root     13785:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13786:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     13787:                                m_CF = 1;
1.1       root     13788:                        }
                   13789:                        CloseHandle(hFile);
                   13790:                }
                   13791:        }
                   13792: }
                   13793: 
                   13794: inline void msdos_int_26h()
                   13795: {
1.1.1.42  root     13796:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     13797:        UINT16 seg, ofs;
                   13798:        DWORD dwSize;
                   13799:        
1.1.1.3   root     13800: #if defined(HAS_I386)
                   13801:        I386OP(pushf)();
                   13802: #else
                   13803:        PREFIX86(_pushf());
                   13804: #endif
1.1       root     13805:        
                   13806:        if(!(REG8(AL) < 26)) {
                   13807:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13808:                m_CF = 1;
1.1       root     13809:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13810:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13811:                m_CF = 1;
1.1       root     13812:        } else {
                   13813:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13814:                char dev[64];
                   13815:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13816:                
                   13817:                if(dpb->media_type == 0xf8) {
                   13818:                        // this drive is not a floppy
1.1.1.6   root     13819: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   13820: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   13821: //                     }
1.1       root     13822:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13823:                        m_CF = 1;
1.1       root     13824:                } else {
1.1.1.60  root     13825:                        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     13826:                        if(hFile == INVALID_HANDLE_VALUE) {
                   13827:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13828:                                m_CF = 1;
1.1       root     13829:                        } else {
1.1.1.19  root     13830:                                UINT32 top_sector  = REG16(DX);
                   13831:                                UINT16 sector_num  = REG16(CX);
                   13832:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13833:                                
                   13834:                                if(sector_num == 0xffff) {
                   13835:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13836:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13837:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13838:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13839:                                        buffer_addr = (seg << 4) + ofs;
                   13840:                                }
1.1       root     13841:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13842:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13843:                                        m_CF = 1;
1.1.1.19  root     13844:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13845:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13846:                                        m_CF = 1;
1.1.1.19  root     13847:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13848:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     13849:                                        m_CF = 1;
1.1       root     13850:                                }
                   13851:                                CloseHandle(hFile);
                   13852:                        }
                   13853:                }
                   13854:        }
                   13855: }
                   13856: 
                   13857: inline void msdos_int_27h()
                   13858: {
1.1.1.29  root     13859:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   13860:        try {
                   13861:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13862:        } catch(...) {
                   13863:                // recover the broken mcb
                   13864:                int mcb_seg = SREG(CS) - 1;
                   13865:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     13866:                
1.1.1.29  root     13867:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     13868:                        mcb->mz = 'M';
                   13869:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   13870:                        
1.1.1.29  root     13871:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     13872:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     13873:                        } else {
1.1.1.39  root     13874:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     13875:                        }
                   13876:                } else {
                   13877:                        mcb->mz = 'Z';
1.1.1.30  root     13878:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     13879:                }
                   13880:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13881:        }
1.1.1.3   root     13882:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     13883: }
                   13884: 
                   13885: inline void msdos_int_29h()
                   13886: {
1.1.1.50  root     13887:        msdos_putch_fast(REG8(AL));
1.1       root     13888: }
                   13889: 
                   13890: inline void msdos_int_2eh()
                   13891: {
                   13892:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   13893:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13894:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     13895:        char *token = my_strtok(tmp, " ");
                   13896:        strcpy(command, token);
                   13897:        strcpy(opt, token + strlen(token) + 1);
                   13898:        
                   13899:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   13900:        param->env_seg = 0;
                   13901:        param->cmd_line.w.l = 44;
                   13902:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13903:        param->fcb1.w.l = 24;
                   13904:        param->fcb1.w.h = (WORK_TOP >> 4);
                   13905:        param->fcb2.w.l = 24;
                   13906:        param->fcb2.w.h = (WORK_TOP >> 4);
                   13907:        
                   13908:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   13909:        
                   13910:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   13911:        cmd_line->len = strlen(opt);
                   13912:        strcpy(cmd_line->cmd, opt);
                   13913:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   13914:        
1.1.1.28  root     13915:        try {
                   13916:                if(msdos_process_exec(command, param, 0)) {
                   13917:                        REG16(AX) = 0xffff; // error before processing command
                   13918:                } else {
                   13919:                        // set flag to set retval to ax when the started process is terminated
                   13920:                        process_t *process = msdos_process_info_get(current_psp);
                   13921:                        process->called_by_int2eh = true;
                   13922:                }
                   13923:        } catch(...) {
                   13924:                REG16(AX) = 0xffff; // error before processing command
                   13925:        }
1.1       root     13926: }
                   13927: 
1.1.1.29  root     13928: inline void msdos_int_2fh_05h()
                   13929: {
                   13930:        switch(REG8(AL)) {
                   13931:        case 0x00:
1.1.1.49  root     13932:                // critical error handler is installed
1.1.1.32  root     13933:                REG8(AL) = 0xff;
                   13934:                break;
                   13935:        case 0x01:
                   13936:        case 0x02:
                   13937:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   13938:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   13939:                                const char *message = NULL;
                   13940:                                if(active_code_page == 932) {
                   13941:                                        message = standard_error_table[i].message_japanese;
                   13942:                                }
                   13943:                                if(message == NULL) {
                   13944:                                        message = standard_error_table[i].message_english;
                   13945:                                }
                   13946:                                strcpy((char *)(mem + WORK_TOP), message);
                   13947:                                
                   13948:                                SREG(ES) = WORK_TOP >> 4;
                   13949:                                i386_load_segment_descriptor(ES);
                   13950:                                REG16(DI) = 0x0000;
                   13951:                                REG8(AL) = 0x01;
                   13952:                                break;
                   13953:                        }
                   13954:                }
1.1.1.29  root     13955:                break;
                   13956:        default:
                   13957:                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     13958:                REG16(AX) = 0x01;
1.1.1.29  root     13959:                m_CF = 1;
                   13960:        }
                   13961: }
                   13962: 
1.1.1.44  root     13963: inline void msdos_int_2fh_06h()
                   13964: {
                   13965:        switch(REG8(AL)) {
                   13966:        case 0x00:
                   13967:                // ASSIGN is not installed
1.1.1.49  root     13968: //             REG8(AL) = 0x00;
1.1.1.44  root     13969:                break;
                   13970:        case 0x01:
                   13971:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   13972:                REG16(AX) = 0x01;
                   13973:                m_CF = 1;
                   13974:                break;
                   13975:        default:
                   13976:                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));
                   13977:                REG16(AX) = 0x01;
                   13978:                m_CF = 1;
                   13979:                break;
                   13980:        }
                   13981: }
                   13982: 
1.1.1.22  root     13983: inline void msdos_int_2fh_11h()
                   13984: {
                   13985:        switch(REG8(AL)) {
                   13986:        case 0x00:
1.1.1.29  root     13987:                if(i386_read_stack() == 0xdada) {
1.1.1.53  root     13988: #ifdef SUPPORT_MSCDEX
                   13989:                        // MSCDEX is installed
                   13990:                        REG8(AL) = 0xff;
                   13991:                        i386_write_stack(0xadad);
                   13992: #else
1.1.1.29  root     13993:                        // MSCDEX is not installed
                   13994: //                     REG8(AL) = 0x00;
1.1.1.53  root     13995: #endif
1.1.1.29  root     13996:                } else {
                   13997:                        // Network Redirector is not installed
                   13998: //                     REG8(AL) = 0x00;
                   13999:                }
1.1.1.22  root     14000:                break;
                   14001:        default:
1.1.1.43  root     14002: //             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     14003:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     14004:                m_CF = 1;
                   14005:                break;
                   14006:        }
                   14007: }
                   14008: 
1.1.1.21  root     14009: inline void msdos_int_2fh_12h()
                   14010: {
                   14011:        switch(REG8(AL)) {
1.1.1.22  root     14012:        case 0x00:
1.1.1.29  root     14013:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     14014:                REG8(AL) = 0xff;
                   14015:                break;
1.1.1.29  root     14016: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   14017:        case 0x02:
                   14018:                {
                   14019:                        UINT16 stack = i386_read_stack();
                   14020:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   14021:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   14022:                        i386_load_segment_descriptor(ES);
                   14023:                }
                   14024:                break;
1.1.1.30  root     14025:        case 0x03:
                   14026:                SREG(DS) = (DEVICE_TOP >> 4);
                   14027:                i386_load_segment_descriptor(DS);
                   14028:                break;
1.1.1.29  root     14029:        case 0x04:
                   14030:                {
                   14031:                        UINT16 stack = i386_read_stack();
                   14032:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   14033: #if defined(HAS_I386)
                   14034:                        m_ZF = (REG8(AL) == '\\');
                   14035: #else
                   14036:                        m_ZeroVal = (REG8(AL) != '\\');
                   14037: #endif
                   14038:                }
                   14039:                break;
                   14040:        case 0x05:
1.1.1.49  root     14041:                {
                   14042:                        UINT16 c = i386_read_stack();
                   14043:                        if((c >> 0) & 0xff) {
                   14044:                                msdos_putch((c >> 0) & 0xff);
                   14045:                        }
                   14046:                        if((c >> 8) & 0xff) {
                   14047:                                msdos_putch((c >> 8) & 0xff);
                   14048:                        }
                   14049:                }
1.1.1.29  root     14050:                break;
1.1.1.49  root     14051: //     case 0x06: // DOS 3.0+ internal - Invoke Critical Error
1.1.1.29  root     14052: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   14053: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
1.1.1.49  root     14054: //     case 0x09: // DOS 3.0+ internal - Flush and Free Disk Buffer
1.1.1.29  root     14055: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   14056: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   14057: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   14058:        case 0x0d:
                   14059:                {
                   14060:                        SYSTEMTIME time;
                   14061:                        FILETIME file_time;
                   14062:                        WORD dos_date, dos_time;
                   14063:                        GetLocalTime(&time);
                   14064:                        SystemTimeToFileTime(&time, &file_time);
                   14065:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   14066:                        REG16(AX) = dos_date;
                   14067:                        REG16(DX) = dos_time;
                   14068:                }
                   14069:                break;
                   14070: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   14071: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   14072: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   14073:        case 0x11:
                   14074:                {
                   14075:                        char path[MAX_PATH], *p;
                   14076:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   14077:                        my_strupr(path);
                   14078:                        while((p = my_strchr(path, '/')) != NULL) {
                   14079:                                *p = '\\';
                   14080:                        }
                   14081:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   14082:                }
                   14083:                break;
                   14084:        case 0x12:
                   14085:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   14086:                break;
                   14087:        case 0x13:
                   14088:                {
                   14089:                        char tmp[2] = {0};
                   14090:                        tmp[0] = i386_read_stack();
                   14091:                        my_strupr(tmp);
                   14092:                        REG8(AL) = tmp[0];
                   14093:                }
                   14094:                break;
                   14095:        case 0x14:
                   14096: #if defined(HAS_I386)
                   14097:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   14098: #else
                   14099:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   14100: #endif
                   14101:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   14102:                break;
                   14103: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     14104:        case 0x16:
                   14105:                if(REG16(BX) < 20) {
                   14106:                        SREG(ES) = SFT_TOP >> 4;
                   14107:                        i386_load_segment_descriptor(ES);
                   14108:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   14109:                        
                   14110:                        // update system file table
                   14111:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   14112:                        if(file_handler[REG16(BX)].valid) {
                   14113:                                int count = 0;
                   14114:                                for(int i = 0; i < 20; i++) {
                   14115:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   14116:                                                count++;
                   14117:                                        }
                   14118:                                }
                   14119:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   14120:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   14121:                                _lseek(REG16(BX), 0, SEEK_END);
                   14122:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   14123:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   14124:                        } else {
                   14125:                                memset(sft, 0, 0x3b);
                   14126:                        }
                   14127:                } else {
                   14128:                        REG16(AX) = 0x06;
                   14129:                        m_CF = 1;
                   14130:                }
                   14131:                break;
1.1.1.49  root     14132:        case 0x17:
                   14133:                {
                   14134:                        UINT16 drive = i386_read_stack();
                   14135:                        if(msdos_is_valid_drive(drive)) {
                   14136:                                msdos_cds_update(drive);
                   14137:                        }
                   14138:                        REG16(SI) = 88 * drive;
                   14139:                        SREG(DS) = (CDS_TOP >> 4);
                   14140:                        i386_load_segment_descriptor(DS);
                   14141:                }
                   14142:                break;
1.1.1.29  root     14143: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   14144: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   14145:        case 0x1a:
                   14146:                {
                   14147:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   14148:                        if(path[1] == ':') {
                   14149:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   14150:                                        REG8(AL) = path[0] - 'a' + 1;
                   14151:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   14152:                                        REG8(AL) = path[0] - 'A' + 1;
                   14153:                                } else {
                   14154:                                        REG8(AL) = 0xff; // invalid
                   14155:                                }
                   14156:                                strcpy(full, path);
                   14157:                                strcpy(path, full + 2);
1.1.1.60  root     14158:                        } else if(GetFullPathNameA(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
1.1.1.29  root     14159:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   14160:                                        REG8(AL) = full[0] - 'a' + 1;
                   14161:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   14162:                                        REG8(AL) = full[0] - 'A' + 1;
                   14163:                                } else {
                   14164:                                        REG8(AL) = 0xff; // invalid
                   14165:                                }
                   14166:                        } else {
                   14167:                                REG8(AL) = 0x00; // default
                   14168:                        }
                   14169:                }
                   14170:                break;
                   14171:        case 0x1b:
                   14172:                {
                   14173:                        int year = REG16(CX) + 1980;
                   14174:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   14175:                }
                   14176:                break;
                   14177: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   14178: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   14179:        case 0x1e:
                   14180:                {
                   14181:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   14182:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
1.1.1.60  root     14183:                        if(GetFullPathNameA(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathNameA(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
1.1.1.29  root     14184: #if defined(HAS_I386)
                   14185:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   14186: #else
                   14187:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   14188: #endif
                   14189:                        } else {
                   14190: #if defined(HAS_I386)
                   14191:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   14192: #else
                   14193:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   14194: #endif
                   14195:                        }
                   14196:                }
                   14197:                break;
1.1.1.49  root     14198:        case 0x1f:
                   14199:                {
                   14200:                        UINT16 drive = i386_read_stack();
                   14201:                        if(msdos_is_valid_drive(drive)) {
                   14202:                                msdos_cds_update(drive);
                   14203:                        }
                   14204:                        REG16(SI) = 88 * drive;
                   14205:                        SREG(ES) = (CDS_TOP >> 4);
                   14206:                        i386_load_segment_descriptor(ES);
                   14207:                }
                   14208:                break;
1.1.1.21  root     14209:        case 0x20:
                   14210:                {
                   14211:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   14212:                        
                   14213:                        if(fd < 20) {
                   14214:                                SREG(ES) = current_psp;
                   14215:                                i386_load_segment_descriptor(ES);
                   14216:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   14217:                        } else {
                   14218:                                REG16(AX) = 0x06;
                   14219:                                m_CF = 1;
                   14220:                        }
                   14221:                }
                   14222:                break;
1.1.1.29  root     14223:        case 0x21:
                   14224:                msdos_int_21h_60h(0);
                   14225:                break;
1.1.1.49  root     14226:        case 0x22:
                   14227:                {
                   14228:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14229:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00) != 0xff) {
                   14230:                                sda->extended_error_code = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00);
                   14231:                        }
                   14232:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01) != 0xff) {
                   14233:                                sda->error_class         = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01);
                   14234:                        }
                   14235:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02) != 0xff) {
                   14236:                                sda->suggested_action    = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02);
                   14237:                        }
                   14238:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03) != 0xff) {
                   14239:                                sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03);
                   14240:                        }
                   14241:                }
                   14242:                break;
1.1.1.29  root     14243: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   14244: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   14245:        case 0x25:
                   14246:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   14247:                break;
                   14248:        case 0x26:
                   14249:                REG8(AL) = REG8(CL);
                   14250:                msdos_int_21h_3dh();
                   14251:                break;
                   14252:        case 0x27:
                   14253:                msdos_int_21h_3eh();
                   14254:                break;
                   14255:        case 0x28:
                   14256:                REG16(AX) = REG16(BP);
                   14257:                msdos_int_21h_42h();
                   14258:                break;
                   14259:        case 0x29:
                   14260:                msdos_int_21h_3fh();
                   14261:                break;
                   14262: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   14263:        case 0x2b:
                   14264:                REG16(AX) = REG16(BP);
                   14265:                msdos_int_21h_44h();
                   14266:                break;
                   14267:        case 0x2c:
                   14268:                REG16(BX) = DEVICE_TOP >> 4;
                   14269:                REG16(AX) = 22;
                   14270:                break;
                   14271:        case 0x2d:
                   14272:                {
                   14273:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14274:                        REG16(AX) = sda->extended_error_code;
                   14275:                }
                   14276:                break;
                   14277:        case 0x2e:
                   14278:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     14279:                        SREG(ES) = 0x0001;
                   14280:                        i386_load_segment_descriptor(ES);
                   14281:                        REG16(DI) = 0x00;
                   14282:                } else if(REG8(DL) == 0x08) {
1.1.1.49  root     14283:                        // dummy parameter error message read routine is at fffc:0010
                   14284:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.22  root     14285:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     14286:                        REG16(DI) = 0x0010;
1.1.1.22  root     14287:                }
                   14288:                break;
1.1.1.29  root     14289:        case 0x2f:
                   14290:                if(REG16(DX) != 0) {
1.1.1.30  root     14291:                        dos_major_version = REG8(DL);
                   14292:                        dos_minor_version = REG8(DH);
1.1.1.29  root     14293:                } else {
                   14294:                        REG8(DL) = 7;
                   14295:                        REG8(DH) = 10;
                   14296:                }
                   14297:                break;
                   14298: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   14299: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     14300:        default:
                   14301:                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));
                   14302:                REG16(AX) = 0x01;
                   14303:                m_CF = 1;
                   14304:                break;
                   14305:        }
                   14306: }
                   14307: 
1.1.1.30  root     14308: inline void msdos_int_2fh_13h()
                   14309: {
                   14310:        static UINT16 prevDS = 0, prevDX = 0;
                   14311:        static UINT16 prevES = 0, prevBX = 0;
                   14312:        UINT16 tmp;
                   14313:        
                   14314:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   14315:        i386_load_segment_descriptor(DS);
                   14316:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   14317:        
                   14318:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   14319:        i386_load_segment_descriptor(ES);
                   14320:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   14321: }
                   14322: 
1.1.1.22  root     14323: inline void msdos_int_2fh_14h()
                   14324: {
                   14325:        switch(REG8(AL)) {
                   14326:        case 0x00:
1.1.1.29  root     14327:                // NLSFUNC.COM is installed
                   14328:                REG8(AL) = 0xff;
1.1.1.25  root     14329:                break;
                   14330:        case 0x01:
                   14331:        case 0x03:
                   14332:                REG8(AL) = 0x00;
                   14333:                active_code_page = REG16(BX);
                   14334:                msdos_nls_tables_update();
                   14335:                break;
                   14336:        case 0x02:
                   14337:                REG8(AL) = get_extended_country_info(REG16(BP));
                   14338:                break;
                   14339:        case 0x04:
1.1.1.42  root     14340:                for(int i = 0;; i++) {
                   14341:                        if(country_table[i].code == REG16(DX)) {
                   14342:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   14343:                                break;
                   14344:                        } else if(country_table[i].code == -1) {
                   14345:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   14346:                                break;
                   14347:                        }
                   14348:                }
1.1.1.25  root     14349:                REG8(AL) = 0x00;
1.1.1.22  root     14350:                break;
                   14351:        default:
                   14352:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14353:                REG16(AX) = 0x01;
                   14354:                m_CF = 1;
                   14355:                break;
                   14356:        }
                   14357: }
                   14358: 
                   14359: inline void msdos_int_2fh_15h()
                   14360: {
                   14361:        switch(REG8(AL)) {
1.1.1.29  root     14362:        case 0x00: // CD-ROM - Installation Check
                   14363:                if(REG16(BX) == 0x0000) {
1.1.1.53  root     14364: #ifdef SUPPORT_MSCDEX
1.1.1.43  root     14365:                        // MSCDEX is installed
                   14366:                        REG16(BX) = 0;
                   14367:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     14368:                                if(msdos_is_cdrom_drive(i)) {
                   14369:                                        if(REG16(BX) == 0) {
                   14370:                                                REG16(CX) = i;
1.1.1.43  root     14371:                                        }
1.1.1.44  root     14372:                                        REG16(BX)++;
1.1.1.43  root     14373:                                }
                   14374:                        }
                   14375: #else
1.1.1.29  root     14376:                        // MSCDEX is not installed
                   14377: //                     REG8(AL) = 0x00;
1.1.1.43  root     14378: #endif
1.1.1.29  root     14379:                } else {
                   14380:                        // GRAPHICS.COM is not installed
                   14381: //                     REG8(AL) = 0x00;
                   14382:                }
1.1.1.22  root     14383:                break;
1.1.1.43  root     14384:        case 0x0b:
1.1.1.44  root     14385:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   14386:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   14387:                REG16(BX) = 0xadad;
1.1.1.43  root     14388:                break;
                   14389:        case 0x0d:
1.1.1.44  root     14390:                for(int i = 0, n = 0; i < 26; i++) {
                   14391:                        if(msdos_is_cdrom_drive(i)) {
                   14392:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     14393:                        }
                   14394:                }
                   14395:                break;
1.1.1.22  root     14396:        case 0xff:
1.1.1.29  root     14397:                if(REG16(BX) == 0x0000) {
                   14398:                        // CORELCDX is not installed
                   14399:                } else {
                   14400:                        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));
                   14401:                        REG16(AX) = 0x01;
                   14402:                        m_CF = 1;
                   14403:                }
1.1.1.22  root     14404:                break;
1.1.1.21  root     14405:        default:
1.1.1.22  root     14406:                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     14407:                REG16(AX) = 0x01;
                   14408:                m_CF = 1;
                   14409:                break;
                   14410:        }
                   14411: }
                   14412: 
1.1       root     14413: inline void msdos_int_2fh_16h()
                   14414: {
                   14415:        switch(REG8(AL)) {
                   14416:        case 0x00:
1.1.1.14  root     14417:                if(no_windows) {
1.1.1.29  root     14418:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   14419: //                     REG8(AL) = 0x00;
1.1.1.14  root     14420:                } else {
1.1.1.30  root     14421:                        REG8(AL) = win_major_version;
                   14422:                        REG8(AH) = win_minor_version;
1.1       root     14423:                }
                   14424:                break;
1.1.1.43  root     14425:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     14426:                // from DOSBox
                   14427:                i386_set_a20_line(1);
                   14428:                break;
1.1.1.49  root     14429:        case 0x06: // Windows Enhanced Mode & 286 DOSX Exit Broadcast
1.1.1.43  root     14430:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   14431:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   14432:                break;
                   14433:        case 0x07:
                   14434:                // Virtual Device Call API
                   14435:                break;
1.1.1.22  root     14436:        case 0x0a:
                   14437:                if(!no_windows) {
                   14438:                        REG16(AX) = 0x0000;
1.1.1.30  root     14439:                        REG8(BH) = win_major_version;
                   14440:                        REG8(BL) = win_minor_version;
1.1.1.49  root     14441: //                     REG16(CX) = 0x0002; // standard
1.1.1.22  root     14442:                        REG16(CX) = 0x0003; // enhanced
                   14443:                }
                   14444:                break;
1.1.1.30  root     14445:        case 0x0b:
                   14446:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     14447:        case 0x0e:
                   14448:        case 0x0f:
1.1.1.30  root     14449:        case 0x10:
1.1.1.22  root     14450:        case 0x11:
                   14451:        case 0x12:
                   14452:        case 0x13:
                   14453:        case 0x14:
1.1.1.30  root     14454:        case 0x15:
1.1.1.43  root     14455:        case 0x81:
                   14456:        case 0x82:
1.1.1.44  root     14457:        case 0x84:
1.1.1.49  root     14458:        case 0x85:
1.1.1.33  root     14459:        case 0x86:
1.1.1.22  root     14460:        case 0x87:
1.1.1.30  root     14461:        case 0x89:
1.1.1.33  root     14462:        case 0x8a:
1.1.1.22  root     14463:                // function not supported, do not clear AX
                   14464:                break;
1.1.1.14  root     14465:        case 0x80:
                   14466:                Sleep(10);
1.1.1.35  root     14467:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     14468:                REG8(AL) = 0x00;
1.1.1.14  root     14469:                break;
1.1.1.33  root     14470:        case 0x83:
                   14471:                REG16(BX) = 0x01; // system vm id
                   14472:                break;
1.1.1.22  root     14473:        case 0x8e:
                   14474:                REG16(AX) = 0x00; // failed
                   14475:                break;
1.1.1.20  root     14476:        case 0x8f:
                   14477:                switch(REG8(DH)) {
                   14478:                case 0x01:
1.1.1.49  root     14479: //                     REG16(AX) = 0x0000; // close command selected but not yet acknowledged
                   14480: //                     REG16(AX) = 0x0001; // close command issued and acknowledged
                   14481:                        REG16(AX) = 0x168f; // close command not selected -- application should continue
                   14482:                        break;
                   14483:                default:
                   14484:                        REG16(AX) = 0x0000; // successful
1.1.1.20  root     14485:                        break;
                   14486:                }
                   14487:                break;
1.1       root     14488:        default:
1.1.1.22  root     14489:                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));
                   14490:                REG16(AX) = 0x01;
                   14491:                m_CF = 1;
                   14492:                break;
                   14493:        }
                   14494: }
                   14495: 
                   14496: inline void msdos_int_2fh_19h()
                   14497: {
                   14498:        switch(REG8(AL)) {
                   14499:        case 0x00:
1.1.1.29  root     14500:                // SHELLB.COM is not installed
                   14501: //             REG8(AL) = 0x00;
1.1.1.22  root     14502:                break;
                   14503:        case 0x01:
                   14504:        case 0x02:
                   14505:        case 0x03:
                   14506:        case 0x04:
                   14507:                REG16(AX) = 0x01;
                   14508:                m_CF = 1;
                   14509:                break;
1.1.1.29  root     14510:        case 0x80:
                   14511:                // IBM ROM-DOS v4.0 is not installed
                   14512: //             REG8(AL) = 0x00;
                   14513:                break;
1.1.1.22  root     14514:        default:
                   14515:                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     14516:                REG16(AX) = 0x01;
1.1.1.3   root     14517:                m_CF = 1;
1.1       root     14518:                break;
                   14519:        }
                   14520: }
                   14521: 
                   14522: inline void msdos_int_2fh_1ah()
                   14523: {
                   14524:        switch(REG8(AL)) {
                   14525:        case 0x00:
1.1.1.29  root     14526:                // ANSI.SYS is installed
1.1       root     14527:                REG8(AL) = 0xff;
                   14528:                break;
1.1.1.49  root     14529:        case 0x01:
1.1.1.50  root     14530:                if(REG8(CL) == 0x5f) {
                   14531:                        // set display information
                   14532:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   14533:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   14534:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   14535:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   14536:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   14537:                                
                   14538:                                if(cur_width != new_width || cur_height != new_height) {
                   14539:                                        pcbios_set_console_size(new_width, new_height, true);
                   14540:                                }
                   14541:                        }
                   14542:                } else if(REG8(CL) == 0x7f) {
1.1.1.49  root     14543:                        // get display information
1.1.1.50  root     14544:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   14545:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   14546:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   14547:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   14548:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   14549:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   14550:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   14551:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   14552:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   14553:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   14554:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.49  root     14555:                } else {
                   14556:                        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));
                   14557:                        REG16(AX) = 0x01;
                   14558:                        m_CF = 1;
                   14559:                }
                   14560:                break;
1.1       root     14561:        default:
1.1.1.22  root     14562:                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));
                   14563:                REG16(AX) = 0x01;
                   14564:                m_CF = 1;
                   14565:                break;
                   14566:        }
                   14567: }
                   14568: 
1.1.1.30  root     14569: inline void msdos_int_2fh_40h()
1.1.1.22  root     14570: {
                   14571:        switch(REG8(AL)) {
                   14572:        case 0x00:
1.1.1.30  root     14573:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   14574:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     14575:                break;
1.1.1.43  root     14576:        case 0x10:
                   14577:                // OS/2 v2.0+ - Installation Check
                   14578:                REG16(AX) = 0x01;
                   14579:                m_CF = 1;
                   14580:                break;
1.1.1.22  root     14581:        default:
                   14582:                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     14583:                REG16(AX) = 0x01;
1.1.1.3   root     14584:                m_CF = 1;
1.1       root     14585:                break;
                   14586:        }
                   14587: }
                   14588: 
                   14589: inline void msdos_int_2fh_43h()
                   14590: {
                   14591:        switch(REG8(AL)) {
                   14592:        case 0x00:
1.1.1.29  root     14593:                // XMS is installed ?
1.1.1.19  root     14594: #ifdef SUPPORT_XMS
                   14595:                if(support_xms) {
                   14596:                        REG8(AL) = 0x80;
1.1.1.44  root     14597:                }
                   14598: #endif
                   14599:                break;
                   14600:        case 0x08:
                   14601: #ifdef SUPPORT_XMS
                   14602:                if(support_xms) {
                   14603:                        REG8(AL) = 0x43;
                   14604:                        REG8(BL) = 0x01; // IBM PC/AT
                   14605:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   14606:                }
1.1.1.19  root     14607: #endif
                   14608:                break;
                   14609:        case 0x10:
                   14610:                SREG(ES) = XMS_TOP >> 4;
                   14611:                i386_load_segment_descriptor(ES);
1.1.1.26  root     14612:                REG16(BX) = 0x15;
1.1       root     14613:                break;
1.1.1.44  root     14614:        case 0xe0:
                   14615:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   14616:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   14617:                        break;
                   14618:                }
1.1       root     14619:        default:
1.1.1.22  root     14620:                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));
                   14621:                REG16(AX) = 0x01;
                   14622:                m_CF = 1;
                   14623:                break;
                   14624:        }
                   14625: }
                   14626: 
                   14627: inline void msdos_int_2fh_46h()
                   14628: {
                   14629:        switch(REG8(AL)) {
                   14630:        case 0x80:
1.1.1.29  root     14631:                // Windows v3.0 is not installed
                   14632: //             REG8(AL) = 0x00;
1.1.1.22  root     14633:                break;
                   14634:        default:
                   14635:                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));
                   14636:                REG16(AX) = 0x01;
                   14637:                m_CF = 1;
                   14638:                break;
                   14639:        }
                   14640: }
                   14641: 
                   14642: inline void msdos_int_2fh_48h()
                   14643: {
                   14644:        switch(REG8(AL)) {
                   14645:        case 0x00:
1.1.1.29  root     14646:                // DOSKEY is not installed
                   14647: //             REG8(AL) = 0x00;
1.1.1.22  root     14648:                break;
                   14649:        case 0x10:
                   14650:                msdos_int_21h_0ah();
                   14651:                REG16(AX) = 0x00;
                   14652:                break;
                   14653:        default:
                   14654:                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     14655:                REG16(AX) = 0x01;
1.1.1.3   root     14656:                m_CF = 1;
1.1       root     14657:                break;
                   14658:        }
                   14659: }
                   14660: 
                   14661: inline void msdos_int_2fh_4ah()
                   14662: {
                   14663:        switch(REG8(AL)) {
1.1.1.29  root     14664: #ifdef SUPPORT_HMA
                   14665:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   14666:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14667:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14668:                                // restore first free mcb in high memory area
                   14669:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14670:                        }
                   14671:                        int offset = 0xffff;
                   14672:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   14673:                                REG16(DI) = offset + 0x10;
                   14674:                        } else {
                   14675:                                REG16(DI) = 0xffff;
                   14676:                        }
                   14677:                } else {
                   14678:                        // HMA is already used
                   14679:                        REG16(BX) = 0;
                   14680:                        REG16(DI) = 0xffff;
                   14681:                }
                   14682:                SREG(ES) = 0xffff;
                   14683:                i386_load_segment_descriptor(ES);
                   14684:                break;
                   14685:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   14686:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14687:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14688:                                // restore first free mcb in high memory area
                   14689:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14690:                        }
                   14691:                        int size = REG16(BX), offset;
                   14692:                        if((size % 16) != 0) {
                   14693:                                size &= ~15;
                   14694:                                size += 16;
                   14695:                        }
                   14696:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   14697:                                REG16(BX) = size;
                   14698:                                REG16(DI) = offset + 0x10;
                   14699:                                is_hma_used_by_int_2fh = true;
                   14700:                        } else {
                   14701:                                REG16(BX) = 0;
                   14702:                                REG16(DI) = 0xffff;
                   14703:                        }
                   14704:                } else {
                   14705:                        // HMA is already used
                   14706:                        REG16(BX) = 0;
                   14707:                        REG16(DI) = 0xffff;
                   14708:                }
                   14709:                SREG(ES) = 0xffff;
                   14710:                i386_load_segment_descriptor(ES);
                   14711:                break;
                   14712:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   14713:                if(REG8(DL) == 0x00) {
                   14714:                        if(!is_hma_used_by_xms) {
                   14715:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14716:                                        // restore first free mcb in high memory area
                   14717:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14718:                                        is_hma_used_by_int_2fh = false;
                   14719:                                }
                   14720:                                int size = REG16(BX), offset;
                   14721:                                if((size % 16) != 0) {
                   14722:                                        size &= ~15;
                   14723:                                        size += 16;
                   14724:                                }
                   14725:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   14726: //                                     REG16(BX) = size;
                   14727:                                        SREG(ES) = 0xffff;
                   14728:                                        i386_load_segment_descriptor(ES);
                   14729:                                        REG16(DI) = offset + 0x10;
                   14730:                                        is_hma_used_by_int_2fh = true;
                   14731:                                } else {
                   14732:                                        REG16(DI) = 0xffff;
                   14733:                                }
                   14734:                        } else {
                   14735:                                REG16(DI) = 0xffff;
                   14736:                        }
                   14737:                } else if(REG8(DL) == 0x01) {
                   14738:                        if(!is_hma_used_by_xms) {
                   14739:                                int size = REG16(BX);
                   14740:                                if((size % 16) != 0) {
                   14741:                                        size &= ~15;
                   14742:                                        size += 16;
                   14743:                                }
                   14744:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   14745:                                        // memory block address is not changed
                   14746:                                } else {
                   14747:                                        REG16(DI) = 0xffff;
                   14748:                                }
                   14749:                        } else {
                   14750:                                REG16(DI) = 0xffff;
                   14751:                        }
                   14752:                } else if(REG8(DL) == 0x02) {
                   14753:                        if(!is_hma_used_by_xms) {
                   14754:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14755:                                        // restore first free mcb in high memory area
                   14756:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14757:                                        is_hma_used_by_int_2fh = false;
                   14758:                                } else {
                   14759:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   14760:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   14761:                                                is_hma_used_by_int_2fh = false;
                   14762:                                        }
                   14763:                                }
                   14764:                        }
                   14765:                } else {
                   14766:                        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));
                   14767:                        REG16(AX) = 0x01;
                   14768:                        m_CF = 1;
                   14769:                }
                   14770:                break;
                   14771:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   14772:                if(!is_hma_used_by_xms) {
                   14773:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14774:                                // restore first free mcb in high memory area
                   14775:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14776:                                is_hma_used_by_int_2fh = false;
                   14777:                        }
                   14778:                        REG16(AX) = 0x0000;
                   14779:                        SREG(ES) = 0xffff;
                   14780:                        i386_load_segment_descriptor(ES);
                   14781:                        REG16(DI) = 0x10;
                   14782:                }
                   14783:                break;
                   14784: #else
1.1       root     14785:        case 0x01:
                   14786:        case 0x02:
1.1.1.29  root     14787:                // HMA is already used
1.1.1.27  root     14788:                REG16(BX) = 0x0000;
1.1.1.3   root     14789:                SREG(ES) = 0xffff;
                   14790:                i386_load_segment_descriptor(ES);
1.1       root     14791:                REG16(DI) = 0xffff;
                   14792:                break;
1.1.1.19  root     14793:        case 0x03:
                   14794:                // unable to allocate
                   14795:                REG16(DI) = 0xffff;
                   14796:                break;
                   14797:        case 0x04:
                   14798:                // function not supported, do not clear AX
                   14799:                break;
1.1.1.29  root     14800: #endif
                   14801:        case 0x10:
1.1.1.42  root     14802:                switch(REG16(BX)) {
                   14803:                case 0x0000:
                   14804:                case 0x0001:
                   14805:                case 0x0002:
                   14806:                case 0x0003:
                   14807:                case 0x0004:
                   14808:                case 0x0005:
                   14809:                case 0x0006:
                   14810:                case 0x0007:
                   14811:                case 0x0008:
                   14812:                case 0x000a:
                   14813:                case 0x1234:
                   14814:                        // SMARTDRV v4.00+ is not installed
                   14815:                        break;
                   14816:                default:
1.1.1.29  root     14817:                        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));
                   14818:                        REG16(AX) = 0x01;
                   14819:                        m_CF = 1;
1.1.1.42  root     14820:                        break;
1.1.1.29  root     14821:                }
                   14822:                break;
                   14823:        case 0x11:
1.1.1.42  root     14824:                switch(REG16(BX)) {
                   14825:                case 0x0000:
                   14826:                case 0x0001:
                   14827:                case 0x0002:
                   14828:                case 0x0003:
                   14829:                case 0x0004:
                   14830:                case 0x0005:
                   14831:                case 0x0006:
                   14832:                case 0x0007:
                   14833:                case 0x0008:
                   14834:                case 0x0009:
                   14835:                case 0x000a:
                   14836:                case 0x000b:
                   14837:                case 0xfffe:
                   14838:                case 0xffff:
1.1.1.29  root     14839:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     14840:                        break;
                   14841:                default:
                   14842:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14843:                        REG16(AX) = 0x01;
                   14844:                        m_CF = 1;
                   14845:                        break;
                   14846:                }
                   14847:                break;
                   14848:        case 0x12:
                   14849:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   14850:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   14851:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   14852:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     14853:                } else {
                   14854:                        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));
                   14855:                        REG16(AX) = 0x01;
                   14856:                        m_CF = 1;
                   14857:                }
1.1.1.22  root     14858:                break;
1.1.1.42  root     14859:        case 0x13:
                   14860:                // DBLSPACE.BIN is not installed
                   14861:                break;
1.1.1.22  root     14862:        default:
                   14863:                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));
                   14864:                REG16(AX) = 0x01;
                   14865:                m_CF = 1;
                   14866:                break;
                   14867:        }
                   14868: }
                   14869: 
                   14870: inline void msdos_int_2fh_4bh()
                   14871: {
                   14872:        switch(REG8(AL)) {
1.1.1.24  root     14873:        case 0x01:
1.1.1.22  root     14874:        case 0x02:
1.1.1.29  root     14875:                // Task Switcher is not installed
1.1.1.24  root     14876:                break;
                   14877:        case 0x03:
                   14878:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   14879:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     14880:                break;
1.1.1.30  root     14881:        case 0x04:
                   14882:                REG16(BX) = 0x0000; // free switcher id successfully
                   14883:                break;
1.1.1.43  root     14884:        case 0x05:
                   14885:                REG16(BX) = 0x0000; // no instance data chain
                   14886:                SREG(ES) = 0x0000;
                   14887:                i386_load_segment_descriptor(ES);
                   14888:                break;
1.1       root     14889:        default:
1.1.1.22  root     14890:                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     14891:                REG16(AX) = 0x01;
1.1.1.3   root     14892:                m_CF = 1;
1.1       root     14893:                break;
                   14894:        }
                   14895: }
                   14896: 
1.1.1.44  root     14897: inline void msdos_int_2fh_4dh()
                   14898: {
                   14899:        switch(REG8(AL)) {
                   14900:        case 0x00:
                   14901:                // KKCFUNC is not installed ???
                   14902:                break;
                   14903:        default:
                   14904: //             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));
                   14905:                REG16(AX) = 0x01; // invalid function
                   14906:                m_CF = 1;
                   14907:                break;
                   14908:        }
                   14909: }
                   14910: 
1.1       root     14911: inline void msdos_int_2fh_4fh()
                   14912: {
                   14913:        switch(REG8(AL)) {
                   14914:        case 0x00:
1.1.1.29  root     14915:                // BILING is installed
1.1.1.27  root     14916:                REG16(AX) = 0x0000;
                   14917:                REG8(DL) = 0x01;        // major version
                   14918:                REG8(DH) = 0x00;        // minor version
1.1       root     14919:                break;
                   14920:        case 0x01:
1.1.1.27  root     14921:                REG16(AX) = 0x0000;
1.1       root     14922:                REG16(BX) = active_code_page;
                   14923:                break;
                   14924:        default:
1.1.1.22  root     14925:                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));
                   14926:                REG16(AX) = 0x01;
                   14927:                m_CF = 1;
                   14928:                break;
                   14929:        }
                   14930: }
                   14931: 
                   14932: inline void msdos_int_2fh_55h()
                   14933: {
                   14934:        switch(REG8(AL)) {
                   14935:        case 0x00:
                   14936:        case 0x01:
                   14937: //             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));
                   14938:                break;
                   14939:        default:
                   14940:                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     14941:                REG16(AX) = 0x01;
1.1.1.3   root     14942:                m_CF = 1;
1.1       root     14943:                break;
                   14944:        }
                   14945: }
                   14946: 
1.1.1.44  root     14947: inline void msdos_int_2fh_56h()
                   14948: {
                   14949:        switch(REG8(AL)) {
                   14950:        case 0x00:
                   14951:                // INTERLNK is not installed
                   14952:                break;
                   14953:        case 0x01:
                   14954:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   14955: //             if(msdos_is_remote_drive(REG8(BH))) {
                   14956: //                     REG8(AL) = 0x00;
                   14957: //             }
                   14958:                break;
                   14959:        default:
                   14960:                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));
                   14961:                REG16(AX) = 0x01;
                   14962:                m_CF = 1;
                   14963:                break;
                   14964:        }
                   14965: }
                   14966: 
1.1.1.24  root     14967: inline void msdos_int_2fh_adh()
                   14968: {
                   14969:        switch(REG8(AL)) {
                   14970:        case 0x00:
1.1.1.29  root     14971:                // DISPLAY.SYS is installed
1.1.1.24  root     14972:                REG8(AL) = 0xff;
                   14973:                REG16(BX) = 0x100; // ???
                   14974:                break;
                   14975:        case 0x01:
                   14976:                active_code_page = REG16(BX);
                   14977:                msdos_nls_tables_update();
                   14978:                REG16(AX) = 0x01;
                   14979:                break;
                   14980:        case 0x02:
                   14981:                REG16(BX) = active_code_page;
                   14982:                break;
                   14983:        case 0x03:
                   14984:                // FIXME
                   14985:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   14986:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   14987:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   14988:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   14989:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   14990:                break;
                   14991:        case 0x80:
1.1.1.49  root     14992:                // KEYB.COM is not installed
                   14993:                break;
1.1.1.24  root     14994:        default:
                   14995:                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));
                   14996:                REG16(AX) = 0x01;
                   14997:                m_CF = 1;
                   14998:                break;
                   14999:        }
                   15000: }
                   15001: 
1.1       root     15002: inline void msdos_int_2fh_aeh()
                   15003: {
                   15004:        switch(REG8(AL)) {
                   15005:        case 0x00:
1.1.1.28  root     15006:                // FIXME: we need to check the given command line
                   15007:                REG8(AL) = 0x00; // the command should be executed as usual
                   15008: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     15009:                break;
                   15010:        case 0x01:
                   15011:                {
                   15012:                        char command[MAX_PATH];
                   15013:                        memset(command, 0, sizeof(command));
1.1.1.3   root     15014:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     15015:                        
                   15016:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   15017:                        param->env_seg = 0;
                   15018:                        param->cmd_line.w.l = 44;
                   15019:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   15020:                        param->fcb1.w.l = 24;
                   15021:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   15022:                        param->fcb2.w.l = 24;
                   15023:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   15024:                        
                   15025:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   15026:                        
                   15027:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     15028:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   15029:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     15030:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   15031:                        
1.1.1.28  root     15032:                        try {
                   15033:                                msdos_process_exec(command, param, 0);
                   15034:                        } catch(...) {
                   15035:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     15036:                        }
                   15037:                }
                   15038:                break;
                   15039:        default:
1.1.1.22  root     15040:                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     15041:                REG16(AX) = 0x01;
1.1.1.3   root     15042:                m_CF = 1;
1.1       root     15043:                break;
                   15044:        }
                   15045: }
                   15046: 
1.1.1.34  root     15047: inline void msdos_int_2fh_b7h()
                   15048: {
                   15049:        switch(REG8(AL)) {
                   15050:        case 0x00:
                   15051:                // APPEND is not installed
                   15052: //             REG8(AL) = 0x00;
                   15053:                break;
1.1.1.44  root     15054:        case 0x06:
                   15055:                REG16(BX) = 0x0000;
                   15056:                break;
1.1.1.34  root     15057:        case 0x07:
1.1.1.43  root     15058:        case 0x11:
1.1.1.34  root     15059:                // COMMAND.COM calls this service without checking APPEND is installed
                   15060:                break;
                   15061:        default:
                   15062:                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));
                   15063:                REG16(AX) = 0x01;
                   15064:                m_CF = 1;
                   15065:                break;
                   15066:        }
                   15067: }
                   15068: 
1.1.1.24  root     15069: inline void msdos_int_33h_0000h()
                   15070: {
                   15071:        REG16(AX) = 0xffff; // hardware/driver installed
                   15072:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15073: }
                   15074: 
                   15075: inline void msdos_int_33h_0001h()
                   15076: {
1.1.1.34  root     15077:        if(mouse.hidden > 0) {
                   15078:                mouse.hidden--;
                   15079:        }
                   15080:        if(mouse.hidden == 0) {
1.1.1.61  root     15081:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), (dwConsoleMode | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS) & ~(ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE));
1.1.1.59  root     15082:                pic[1].imr &= ~0x10; // enable irq12
1.1.1.24  root     15083:        }
                   15084: }
                   15085: 
                   15086: inline void msdos_int_33h_0002h()
                   15087: {
1.1.1.34  root     15088:        mouse.hidden++;
1.1.1.61  root     15089:        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
1.1.1.59  root     15090:        pic[1].imr |= 0x10; // disable irq12
1.1.1.24  root     15091: }
                   15092: 
                   15093: inline void msdos_int_33h_0003h()
                   15094: {
1.1.1.34  root     15095: //     if(mouse.hidden > 0) {
                   15096:                update_console_input();
                   15097: //     }
1.1.1.24  root     15098:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     15099:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   15100:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   15101: }
                   15102: 
                   15103: inline void msdos_int_33h_0004h()
                   15104: {
                   15105:        mouse.position.x = REG16(CX);
                   15106:        mouse.position.x = REG16(DX);
1.1.1.24  root     15107: }
                   15108: 
                   15109: inline void msdos_int_33h_0005h()
                   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].pressed_times, 0x7fff);
                   15117:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   15118:                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     15119:                mouse.buttons[idx].pressed_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_0006h()
                   15127: {
1.1.1.34  root     15128: //     if(mouse.hidden > 0) {
                   15129:                update_console_input();
                   15130: //     }
1.1.1.24  root     15131:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   15132:                int idx = REG16(BX);
1.1.1.34  root     15133:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   15134:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   15135:                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     15136:                mouse.buttons[idx].released_times = 0;
                   15137:        } else {
                   15138:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   15139:        }
                   15140:        REG16(AX) = mouse.get_buttons();
                   15141: }
                   15142: 
                   15143: inline void msdos_int_33h_0007h()
                   15144: {
                   15145:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   15146:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   15147: }
                   15148: 
                   15149: inline void msdos_int_33h_0008h()
                   15150: {
                   15151:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   15152:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   15153: }
                   15154: 
                   15155: inline void msdos_int_33h_0009h()
                   15156: {
                   15157:        mouse.hot_spot[0] = REG16(BX);
                   15158:        mouse.hot_spot[1] = REG16(CX);
                   15159: }
                   15160: 
1.1.1.49  root     15161: inline void msdos_int_33h_000ah()
                   15162: {
                   15163:        mouse.screen_mask = REG16(CX);
                   15164:        mouse.cursor_mask = REG16(DX);
                   15165: }
                   15166: 
1.1.1.24  root     15167: inline void msdos_int_33h_000bh()
                   15168: {
1.1.1.34  root     15169: //     if(mouse.hidden > 0) {
                   15170:                update_console_input();
                   15171: //     }
1.1.1.24  root     15172:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   15173:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   15174:        mouse.prev_position.x = mouse.position.x;
                   15175:        mouse.prev_position.y = mouse.position.y;
                   15176:        REG16(CX) = dx;
                   15177:        REG16(DX) = dy;
                   15178: }
                   15179: 
                   15180: inline void msdos_int_33h_000ch()
                   15181: {
                   15182:        mouse.call_mask = REG16(CX);
                   15183:        mouse.call_addr.w.l = REG16(DX);
                   15184:        mouse.call_addr.w.h = SREG(ES);
                   15185: }
                   15186: 
                   15187: inline void msdos_int_33h_000fh()
                   15188: {
                   15189:        mouse.mickey.x = REG16(CX);
                   15190:        mouse.mickey.y = REG16(DX);
                   15191: }
                   15192: 
                   15193: inline void msdos_int_33h_0011h()
                   15194: {
                   15195:        REG16(AX) = 0xffff;
                   15196:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15197: }
                   15198: 
                   15199: inline void msdos_int_33h_0014h()
                   15200: {
                   15201:        UINT16 old_mask = mouse.call_mask;
                   15202:        UINT16 old_ofs = mouse.call_addr.w.l;
                   15203:        UINT16 old_seg = mouse.call_addr.w.h;
                   15204:        
                   15205:        mouse.call_mask = REG16(CX);
                   15206:        mouse.call_addr.w.l = REG16(DX);
                   15207:        mouse.call_addr.w.h = SREG(ES);
                   15208:        
                   15209:        REG16(CX) = old_mask;
                   15210:        REG16(DX) = old_ofs;
                   15211:        SREG(ES) = old_seg;
                   15212:        i386_load_segment_descriptor(ES);
                   15213: }
                   15214: 
                   15215: inline void msdos_int_33h_0015h()
                   15216: {
                   15217:        REG16(BX) = sizeof(mouse);
                   15218: }
                   15219: 
                   15220: inline void msdos_int_33h_0016h()
                   15221: {
                   15222:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   15223: }
                   15224: 
                   15225: inline void msdos_int_33h_0017h()
                   15226: {
                   15227:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   15228: }
                   15229: 
1.1.1.43  root     15230: inline void msdos_int_33h_0018h()
                   15231: {
                   15232:        for(int i = 0; i < 8; i++) {
                   15233:                if(REG16(CX) & (1 << i)) {
                   15234:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   15235:                                // event handler already exists
                   15236:                                REG16(AX) = 0xffff;
                   15237:                                break;
                   15238:                        }
                   15239:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   15240:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   15241:                }
                   15242:        }
                   15243: }
                   15244: 
                   15245: inline void msdos_int_33h_0019h()
                   15246: {
                   15247:        UINT16 call_mask = REG16(CX);
                   15248:        
                   15249:        REG16(CX) = 0;
                   15250:        
                   15251:        for(int i = 0; i < 8; i++) {
                   15252:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   15253:                        for(int j = 0; j < 8; j++) {
                   15254:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   15255:                                        REG16(CX) |= (1 << j);
                   15256:                                }
                   15257:                        }
                   15258:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   15259:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   15260:                        break;
                   15261:                }
                   15262:        }
                   15263: }
                   15264: 
1.1.1.24  root     15265: inline void msdos_int_33h_001ah()
                   15266: {
                   15267:        mouse.sensitivity[0] = REG16(BX);
                   15268:        mouse.sensitivity[1] = REG16(CX);
                   15269:        mouse.sensitivity[2] = REG16(DX);
                   15270: }
                   15271: 
                   15272: inline void msdos_int_33h_001bh()
                   15273: {
                   15274:        REG16(BX) = mouse.sensitivity[0];
                   15275:        REG16(CX) = mouse.sensitivity[1];
                   15276:        REG16(DX) = mouse.sensitivity[2];
                   15277: }
                   15278: 
                   15279: inline void msdos_int_33h_001dh()
                   15280: {
                   15281:        mouse.display_page = REG16(BX);
                   15282: }
                   15283: 
                   15284: inline void msdos_int_33h_001eh()
                   15285: {
                   15286:        REG16(BX) = mouse.display_page;
                   15287: }
                   15288: 
1.1.1.34  root     15289: inline void msdos_int_33h_001fh()
                   15290: {
                   15291:        // from DOSBox
                   15292:        REG16(BX) = 0x0000;
                   15293:        SREG(ES) = 0x0000;
                   15294:        i386_load_segment_descriptor(ES);
                   15295:        mouse.enabled = false;
                   15296:        mouse.old_hidden = mouse.hidden;
                   15297:        mouse.hidden = 1;
                   15298: }
                   15299: 
                   15300: inline void msdos_int_33h_0020h()
                   15301: {
                   15302:        // from DOSBox
                   15303:        mouse.enabled = true;
                   15304:        mouse.hidden = mouse.old_hidden;
                   15305: }
                   15306: 
1.1.1.24  root     15307: inline void msdos_int_33h_0021h()
                   15308: {
                   15309:        REG16(AX) = 0xffff;
                   15310:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15311: }
                   15312: 
                   15313: inline void msdos_int_33h_0022h()
                   15314: {
                   15315:        mouse.language = REG16(BX);
                   15316: }
                   15317: 
                   15318: inline void msdos_int_33h_0023h()
                   15319: {
                   15320:        REG16(BX) = mouse.language;
                   15321: }
                   15322: 
                   15323: inline void msdos_int_33h_0024h()
                   15324: {
                   15325:        REG16(BX) = 0x0805; // V8.05
                   15326:        REG16(CX) = 0x0400; // PS/2
                   15327: }
                   15328: 
1.1.1.49  root     15329: inline void msdos_int_33h_0025h()
                   15330: {
                   15331:        REG16(AX) = 0x8000; // driver (not TSR), software text cursor
                   15332: }
                   15333: 
1.1.1.24  root     15334: inline void msdos_int_33h_0026h()
                   15335: {
                   15336:        REG16(BX) = 0x0000;
                   15337:        REG16(CX) = mouse.max_position.x;
                   15338:        REG16(DX) = mouse.max_position.y;
                   15339: }
                   15340: 
1.1.1.49  root     15341: inline void msdos_int_33h_0027h()
                   15342: {
                   15343: //     if(mouse.hidden > 0) {
                   15344:                update_console_input();
                   15345: //     }
                   15346:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   15347:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   15348:        mouse.prev_position.x = mouse.position.x;
                   15349:        mouse.prev_position.y = mouse.position.y;
                   15350:        REG16(AX) = mouse.screen_mask;
                   15351:        REG16(BX) = mouse.cursor_mask;
                   15352:        REG16(CX) = dx;
                   15353:        REG16(DX) = dy;
                   15354: }
                   15355: 
                   15356: inline void msdos_int_33h_0028h()
                   15357: {
                   15358:        if(REG16(CX) != 0) {
                   15359:                UINT8 tmp = REG8(AL);
                   15360:                REG8(AL) = REG8(CL);
                   15361:                pcbios_int_10h_00h();
                   15362:                REG8(AL) = tmp;
                   15363:        }
                   15364:        REG8(CL) = 0x00; // successful
                   15365: }
                   15366: 
                   15367: inline void msdos_int_33h_0029h()
                   15368: {
                   15369:        switch(REG16(CX)) {
                   15370:        case 0x0000:
                   15371:                REG16(CX) = 0x0003;
                   15372:                sprintf((char *)(mem + WORK_TOP), "TEXT Mode (80x25)$");
                   15373:                break;
                   15374:        case 0x0003:
                   15375:                REG16(CX) = 0x0070;
                   15376:                sprintf((char *)(mem + WORK_TOP), "V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15377:                break;
                   15378:        case 0x0070:
                   15379:                REG16(CX) = 0x0071;
                   15380:                sprintf((char *)(mem + WORK_TOP), "Extended CGA V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15381:                break;
                   15382:        case 0x0071:
                   15383:                REG16(CX) = 0x0073;
                   15384:                sprintf((char *)(mem + WORK_TOP), "Extended CGA TEXT Mode (80x25)$");
                   15385:                break;
                   15386:        default:
                   15387:                REG16(CX) = 0x0000;
                   15388:                break;
                   15389:        }
                   15390:        if(REG16(CX) != 0) {
                   15391:                SREG(DS) = (WORK_TOP >> 4);
                   15392:        } else {
                   15393:                SREG(DS) = 0x0000;
                   15394:        }
                   15395:        i386_load_segment_descriptor(DS);
                   15396:        REG16(DX) = 0x0000;
                   15397: }
                   15398: 
1.1.1.24  root     15399: inline void msdos_int_33h_002ah()
                   15400: {
1.1.1.34  root     15401:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     15402:        REG16(BX) = mouse.hot_spot[0];
                   15403:        REG16(CX) = mouse.hot_spot[1];
                   15404:        REG16(DX) = 4; // PS/2
                   15405: }
                   15406: 
                   15407: inline void msdos_int_33h_0031h()
                   15408: {
                   15409:        REG16(AX) = mouse.min_position.x;
                   15410:        REG16(BX) = mouse.min_position.y;
                   15411:        REG16(CX) = mouse.max_position.x;
                   15412:        REG16(DX) = mouse.max_position.y;
                   15413: }
                   15414: 
                   15415: inline void msdos_int_33h_0032h()
                   15416: {
                   15417:        REG16(AX) = 0;
1.1.1.49  root     15418:        REG16(AX) |= 0x8000; // 0025h
1.1.1.24  root     15419:        REG16(AX) |= 0x4000; // 0026h
1.1.1.49  root     15420:        REG16(AX) |= 0x2000; // 0027h
1.1.1.24  root     15421: //     REG16(AX) |= 0x1000; // 0028h
                   15422: //     REG16(AX) |= 0x0800; // 0029h
                   15423:        REG16(AX) |= 0x0400; // 002ah
                   15424: //     REG16(AX) |= 0x0200; // 002bh
                   15425: //     REG16(AX) |= 0x0100; // 002ch
                   15426: //     REG16(AX) |= 0x0080; // 002dh
                   15427: //     REG16(AX) |= 0x0040; // 002eh
                   15428:        REG16(AX) |= 0x0020; // 002fh
                   15429: //     REG16(AX) |= 0x0010; // 0030h
                   15430:        REG16(AX) |= 0x0008; // 0031h
                   15431:        REG16(AX) |= 0x0004; // 0032h
                   15432: //     REG16(AX) |= 0x0002; // 0033h
                   15433: //     REG16(AX) |= 0x0001; // 0034h
                   15434: }
                   15435: 
1.1.1.49  root     15436: inline void msdos_int_33h_004dh()
                   15437: {
                   15438:        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), "Copyright 2017 MS-DOS Player");
                   15439: }
                   15440: 
                   15441: inline void msdos_int_33h_006dh()
                   15442: {
                   15443:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x08; // V8.05
                   15444:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = 0x05;
                   15445: }
                   15446: 
1.1.1.19  root     15447: inline void msdos_int_67h_40h()
                   15448: {
                   15449:        if(!support_ems) {
                   15450:                REG8(AH) = 0x84;
                   15451:        } else {
                   15452:                REG8(AH) = 0x00;
                   15453:        }
                   15454: }
                   15455: 
                   15456: inline void msdos_int_67h_41h()
                   15457: {
                   15458:        if(!support_ems) {
                   15459:                REG8(AH) = 0x84;
                   15460:        } else {
                   15461:                REG8(AH) = 0x00;
                   15462:                REG16(BX) = EMS_TOP >> 4;
                   15463:        }
                   15464: }
                   15465: 
                   15466: inline void msdos_int_67h_42h()
                   15467: {
                   15468:        if(!support_ems) {
                   15469:                REG8(AH) = 0x84;
                   15470:        } else {
                   15471:                REG8(AH) = 0x00;
                   15472:                REG16(BX) = free_ems_pages;
                   15473:                REG16(DX) = MAX_EMS_PAGES;
                   15474:        }
                   15475: }
                   15476: 
                   15477: inline void msdos_int_67h_43h()
                   15478: {
                   15479:        if(!support_ems) {
                   15480:                REG8(AH) = 0x84;
                   15481:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15482:                REG8(AH) = 0x87;
                   15483:        } else if(REG16(BX) > free_ems_pages) {
                   15484:                REG8(AH) = 0x88;
                   15485:        } else if(REG16(BX) == 0) {
                   15486:                REG8(AH) = 0x89;
                   15487:        } else {
1.1.1.31  root     15488:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15489:                        if(!ems_handles[i].allocated) {
                   15490:                                ems_allocate_pages(i, REG16(BX));
                   15491:                                REG8(AH) = 0x00;
                   15492:                                REG16(DX) = i;
                   15493:                                return;
                   15494:                        }
                   15495:                }
                   15496:                REG8(AH) = 0x85;
                   15497:        }
                   15498: }
                   15499: 
                   15500: inline void msdos_int_67h_44h()
                   15501: {
                   15502:        if(!support_ems) {
                   15503:                REG8(AH) = 0x84;
1.1.1.31  root     15504:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15505:                REG8(AH) = 0x83;
                   15506:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   15507:                REG8(AH) = 0x8a;
                   15508: //     } else if(!(REG8(AL) < 4)) {
                   15509: //             REG8(AH) = 0x8b;
                   15510:        } else if(REG16(BX) == 0xffff) {
                   15511:                ems_unmap_page(REG8(AL) & 3);
                   15512:                REG8(AH) = 0x00;
                   15513:        } else {
                   15514:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   15515:                REG8(AH) = 0x00;
                   15516:        }
                   15517: }
                   15518: 
                   15519: inline void msdos_int_67h_45h()
                   15520: {
                   15521:        if(!support_ems) {
                   15522:                REG8(AH) = 0x84;
1.1.1.31  root     15523:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15524:                REG8(AH) = 0x83;
                   15525:        } else {
                   15526:                ems_release_pages(REG16(DX));
                   15527:                REG8(AH) = 0x00;
                   15528:        }
                   15529: }
                   15530: 
                   15531: inline void msdos_int_67h_46h()
                   15532: {
                   15533:        if(!support_ems) {
                   15534:                REG8(AH) = 0x84;
                   15535:        } else {
1.1.1.29  root     15536: //             REG16(AX) = 0x0032; // EMS 3.2
                   15537:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     15538:        }
                   15539: }
                   15540: 
                   15541: inline void msdos_int_67h_47h()
                   15542: {
                   15543:        // NOTE: the map data should be stored in the specified ems page, not process data
                   15544:        process_t *process = msdos_process_info_get(current_psp);
                   15545:        
                   15546:        if(!support_ems) {
                   15547:                REG8(AH) = 0x84;
1.1.1.31  root     15548: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15549: //             REG8(AH) = 0x83;
                   15550:        } else if(process->ems_pages_stored) {
                   15551:                REG8(AH) = 0x8d;
                   15552:        } else {
                   15553:                for(int i = 0; i < 4; i++) {
                   15554:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   15555:                        process->ems_pages[i].page   = ems_pages[i].page;
                   15556:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   15557:                }
                   15558:                process->ems_pages_stored = true;
                   15559:                REG8(AH) = 0x00;
                   15560:        }
                   15561: }
                   15562: 
                   15563: inline void msdos_int_67h_48h()
                   15564: {
                   15565:        // NOTE: the map data should be restored from the specified ems page, not process data
                   15566:        process_t *process = msdos_process_info_get(current_psp);
                   15567:        
                   15568:        if(!support_ems) {
                   15569:                REG8(AH) = 0x84;
1.1.1.31  root     15570: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15571: //             REG8(AH) = 0x83;
                   15572:        } else if(!process->ems_pages_stored) {
                   15573:                REG8(AH) = 0x8e;
                   15574:        } else {
                   15575:                for(int i = 0; i < 4; i++) {
                   15576:                        if(process->ems_pages[i].mapped) {
                   15577:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   15578:                        } else {
                   15579:                                ems_unmap_page(i);
                   15580:                        }
                   15581:                }
                   15582:                process->ems_pages_stored = false;
                   15583:                REG8(AH) = 0x00;
                   15584:        }
                   15585: }
                   15586: 
                   15587: inline void msdos_int_67h_4bh()
                   15588: {
                   15589:        if(!support_ems) {
                   15590:                REG8(AH) = 0x84;
                   15591:        } else {
                   15592:                REG8(AH) = 0x00;
                   15593:                REG16(BX) = 0;
1.1.1.31  root     15594:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15595:                        if(ems_handles[i].allocated) {
                   15596:                                REG16(BX)++;
                   15597:                        }
                   15598:                }
                   15599:        }
                   15600: }
                   15601: 
                   15602: inline void msdos_int_67h_4ch()
                   15603: {
                   15604:        if(!support_ems) {
                   15605:                REG8(AH) = 0x84;
1.1.1.31  root     15606:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15607:                REG8(AH) = 0x83;
                   15608:        } else {
                   15609:                REG8(AH) = 0x00;
                   15610:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   15611:        }
                   15612: }
                   15613: 
                   15614: inline void msdos_int_67h_4dh()
                   15615: {
                   15616:        if(!support_ems) {
                   15617:                REG8(AH) = 0x84;
                   15618:        } else {
                   15619:                REG8(AH) = 0x00;
                   15620:                REG16(BX) = 0;
1.1.1.31  root     15621:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15622:                        if(ems_handles[i].allocated) {
                   15623:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   15624:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   15625:                                REG16(BX)++;
                   15626:                        }
                   15627:                }
                   15628:        }
                   15629: }
                   15630: 
1.1.1.20  root     15631: inline void msdos_int_67h_4eh()
                   15632: {
                   15633:        if(!support_ems) {
                   15634:                REG8(AH) = 0x84;
                   15635:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15636:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   15637:                        // save page map
                   15638:                        for(int i = 0; i < 4; i++) {
                   15639:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15640:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15641:                        }
                   15642:                }
                   15643:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15644:                        // restore page map
                   15645:                        for(int i = 0; i < 4; i++) {
                   15646:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15647:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15648:                                
1.1.1.31  root     15649:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     15650:                                        ems_map_page(i, handle, page);
                   15651:                                } else {
                   15652:                                        ems_unmap_page(i);
                   15653:                                }
                   15654:                        }
                   15655:                }
                   15656:                REG8(AH) = 0x00;
                   15657:        } else if(REG8(AL) == 0x03) {
                   15658:                REG8(AH) = 0x00;
1.1.1.21  root     15659:                REG8(AL) = 4 * 4;
                   15660:        } else {
1.1.1.22  root     15661:                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     15662:                REG8(AH) = 0x8f;
                   15663:        }
                   15664: }
                   15665: 
                   15666: inline void msdos_int_67h_4fh()
                   15667: {
                   15668:        if(!support_ems) {
                   15669:                REG8(AH) = 0x84;
                   15670:        } else if(REG8(AL) == 0x00) {
                   15671:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15672:                
                   15673:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   15674:                for(int i = 0; i < count; i++) {
                   15675:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   15676:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15677:                        
                   15678: //                     if(!(physical < 4)) {
                   15679: //                             REG8(AH) = 0x8b;
                   15680: //                             return;
                   15681: //                     }
                   15682:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     15683:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   15684:                        *(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     15685:                }
                   15686:                REG8(AH) = 0x00;
                   15687:        } else if(REG8(AL) == 0x01) {
                   15688:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15689:                
                   15690:                for(int i = 0; i < count; i++) {
                   15691:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   15692:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15693:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   15694:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   15695:                        
                   15696: //                     if(!(physical < 4)) {
                   15697: //                             REG8(AH) = 0x8b;
                   15698: //                             return;
                   15699: //                     } else
1.1.1.41  root     15700:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     15701:                                ems_map_page(physical & 3, handle, logical);
                   15702:                        } else {
1.1.1.41  root     15703:                                ems_unmap_page(physical & 3);
1.1.1.21  root     15704:                        }
                   15705:                }
                   15706:                REG8(AH) = 0x00;
                   15707:        } else if(REG8(AL) == 0x02) {
                   15708:                REG8(AH) = 0x00;
                   15709:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     15710:        } else {
1.1.1.22  root     15711:                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     15712:                REG8(AH) = 0x8f;
                   15713:        }
                   15714: }
                   15715: 
                   15716: inline void msdos_int_67h_50h()
                   15717: {
                   15718:        if(!support_ems) {
                   15719:                REG8(AH) = 0x84;
1.1.1.31  root     15720:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     15721:                REG8(AH) = 0x83;
                   15722:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15723:                for(int i = 0; i < REG16(CX); i++) {
                   15724:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15725:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15726:                        
                   15727:                        if(REG8(AL) == 0x01) {
                   15728:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15729:                        }
                   15730: //                     if(!(physical < 4)) {
                   15731: //                             REG8(AH) = 0x8b;
                   15732: //                             return;
                   15733: //                     } else
                   15734:                        if(logical == 0xffff) {
                   15735:                                ems_unmap_page(physical & 3);
                   15736:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15737:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15738:                        } else {
                   15739:                                REG8(AH) = 0x8a;
                   15740:                                return;
                   15741:                        }
                   15742:                }
                   15743:                REG8(AH) = 0x00;
                   15744:        } else {
1.1.1.22  root     15745:                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     15746:                REG8(AH) = 0x8f;
                   15747:        }
                   15748: }
                   15749: 
1.1.1.19  root     15750: inline void msdos_int_67h_51h()
                   15751: {
                   15752:        if(!support_ems) {
                   15753:                REG8(AH) = 0x84;
1.1.1.31  root     15754:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15755:                REG8(AH) = 0x83;
                   15756:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15757:                REG8(AH) = 0x87;
                   15758:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   15759:                REG8(AH) = 0x88;
                   15760:        } else {
                   15761:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   15762:                REG8(AH) = 0x00;
                   15763:        }
                   15764: }
                   15765: 
1.1.1.20  root     15766: inline void msdos_int_67h_52h()
                   15767: {
                   15768:        if(!support_ems) {
                   15769:                REG8(AH) = 0x84;
1.1.1.31  root     15770: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15771: //             REG8(AH) = 0x83;
1.1.1.20  root     15772:        } else if(REG8(AL) == 0x00) {
                   15773:                REG8(AL) = 0x00; // handle is volatile
                   15774:                REG8(AH) = 0x00;
                   15775:        } else if(REG8(AL) == 0x01) {
                   15776:                if(REG8(BL) == 0x00) {
                   15777:                        REG8(AH) = 0x00;
                   15778:                } else {
                   15779:                        REG8(AH) = 0x90; // undefined attribute type
                   15780:                }
                   15781:        } else if(REG8(AL) == 0x02) {
                   15782:                REG8(AL) = 0x00; // only volatile handles supported
                   15783:                REG8(AH) = 0x00;
                   15784:        } else {
1.1.1.22  root     15785:                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     15786:                REG8(AH) = 0x8f;
                   15787:        }
                   15788: }
                   15789: 
1.1.1.19  root     15790: inline void msdos_int_67h_53h()
                   15791: {
                   15792:        if(!support_ems) {
                   15793:                REG8(AH) = 0x84;
1.1.1.31  root     15794:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15795:                REG8(AH) = 0x83;
                   15796:        } else if(REG8(AL) == 0x00) {
                   15797:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   15798:                REG8(AH) = 0x00;
                   15799:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     15800:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15801:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15802:                                REG8(AH) = 0xa1;
                   15803:                                return;
                   15804:                        }
                   15805:                }
                   15806:                REG8(AH) = 0x00;
                   15807:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   15808:        } else {
1.1.1.22  root     15809:                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     15810:                REG8(AH) = 0x8f;
1.1.1.19  root     15811:        }
                   15812: }
                   15813: 
                   15814: inline void msdos_int_67h_54h()
                   15815: {
                   15816:        if(!support_ems) {
                   15817:                REG8(AH) = 0x84;
                   15818:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     15819:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15820:                        if(ems_handles[i].allocated) {
                   15821:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   15822:                        } else {
                   15823:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   15824:                        }
                   15825:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   15826:                }
                   15827:                REG8(AH) = 0x00;
                   15828:                REG8(AL) = MAX_EMS_HANDLES;
                   15829:        } else if(REG8(AL) == 0x01) {
                   15830:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     15831:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15832:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15833:                                REG8(AH) = 0x00;
                   15834:                                REG16(DX) = i;
                   15835:                                break;
                   15836:                        }
                   15837:                }
                   15838:        } else if(REG8(AL) == 0x02) {
                   15839:                REG8(AH) = 0x00;
                   15840:                REG16(BX) = MAX_EMS_HANDLES;
                   15841:        } else {
1.1.1.22  root     15842:                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     15843:                REG8(AH) = 0x8f;
                   15844:        }
                   15845: }
                   15846: 
1.1.1.49  root     15847: inline void msdos_int_67h_55h()
                   15848: {
                   15849:        if(!support_ems) {
                   15850:                REG8(AH) = 0x84;
                   15851:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15852:                REG8(AH) = 0x83;
                   15853:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15854:                UINT16 jump_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
                   15855:                UINT16 jump_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
                   15856:                UINT8  entries  = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
                   15857:                UINT16 map_ofs  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   15858:                UINT16 map_seg  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
                   15859:                
                   15860:                for(int i = 0; i < (int)entries; i++) {
                   15861:                        int logical  = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 0);
                   15862:                        int physical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 2);
                   15863:                        
                   15864:                        if(REG8(AL) == 0x01) {
                   15865:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15866:                        }
                   15867: //                     if(!(physical < 4)) {
                   15868: //                             REG8(AH) = 0x8b;
                   15869: //                             return;
                   15870: //                     } else
                   15871:                        if(logical == 0xffff) {
                   15872:                                ems_unmap_page(physical & 3);
                   15873:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15874:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15875:                        } else {
                   15876:                                REG8(AH) = 0x8a;
                   15877:                                return;
                   15878:                        }
                   15879:                }
                   15880:                i386_jmp_far(jump_seg, jump_ofs);
                   15881:                REG8(AH) = 0x00;
                   15882:        } else {
                   15883:                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));
                   15884:                REG8(AH) = 0x8f;
                   15885:        }
                   15886: }
                   15887: 
                   15888: inline void msdos_int_67h_56h()
                   15889: {
                   15890:        if(!support_ems) {
                   15891:                REG8(AH) = 0x84;
                   15892:        } else if(REG8(AL) == 0x02) {
                   15893:                REG16(BX) = (2 + 2) * 4;
                   15894:                REG8(AH) = 0x00;
                   15895:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15896:                REG8(AH) = 0x83;
                   15897:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15898:                UINT16 call_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  0);
                   15899:                UINT16 call_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  2);
                   15900:                UINT8  new_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  4);
                   15901:                UINT16 new_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  5);
                   15902:                UINT16 new_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  7);
                   15903: #if 0
                   15904:                UINT8  old_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  9);
                   15905:                UINT16 old_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 10);
                   15906:                UINT16 old_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 12);
                   15907: #endif
                   15908:                UINT16 handles[4], pages[4];
                   15909:                
                   15910:                // alter page map and call routine is at fffc:001f
                   15911:                if(!(call_seg == 0 && call_ofs == 0)) {
                   15912:                        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   15913:                        mem[DUMMY_TOP + 0x20] = (call_ofs >> 0) & 0xff;
                   15914:                        mem[DUMMY_TOP + 0x21] = (call_ofs >> 8) & 0xff;
                   15915:                        mem[DUMMY_TOP + 0x22] = (call_seg >> 0) & 0xff;
                   15916:                        mem[DUMMY_TOP + 0x23] = (call_seg >> 8) & 0xff;
                   15917:                } else {
                   15918:                        // invalid call addr :-(
                   15919:                        mem[DUMMY_TOP + 0x1f] = 0x90;   // nop
                   15920:                        mem[DUMMY_TOP + 0x20] = 0x90;   // nop
                   15921:                        mem[DUMMY_TOP + 0x21] = 0x90;   // nop
                   15922:                        mem[DUMMY_TOP + 0x22] = 0x90;   // nop
                   15923:                        mem[DUMMY_TOP + 0x23] = 0x90;   // nop
                   15924:                }
                   15925:                // do call far (push cs/ip) in old mapping
                   15926:                i386_call_far(DUMMY_TOP >> 4, 0x001f);
                   15927:                
                   15928:                // get old mapping data
                   15929: #if 0
                   15930:                for(int i = 0; i < (int)old_entries; i++) {
                   15931:                        int logical  = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 0);
                   15932:                        int physical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 2);
                   15933:                        
                   15934:                        if(REG8(AL) == 0x01) {
                   15935:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15936:                        }
                   15937: //                     if(!(physical < 4)) {
                   15938: //                             REG8(AH) = 0x8b;
                   15939: //                             return;
                   15940: //                     } else
                   15941:                        if(logical == 0xffff) {
                   15942:                                ems_unmap_page(physical & 3);
                   15943:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15944:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15945:                        } else {
                   15946:                                REG8(AH) = 0x8a;
                   15947:                                return;
                   15948:                        }
                   15949:                }
                   15950: #endif
                   15951:                for(int i = 0; i < 4; i++) {
                   15952:                        handles[i] = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15953:                        pages  [i] = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15954:                }
                   15955:                
                   15956:                // set new mapping
                   15957:                for(int i = 0; i < (int)new_entries; i++) {
                   15958:                        int logical  = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 0);
                   15959:                        int physical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 2);
                   15960:                        
                   15961:                        if(REG8(AL) == 0x01) {
                   15962:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15963:                        }
                   15964: //                     if(!(physical < 4)) {
                   15965: //                             REG8(AH) = 0x8b;
                   15966: //                             return;
                   15967: //                     } else
                   15968:                        if(logical == 0xffff) {
                   15969:                                ems_unmap_page(physical & 3);
                   15970:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15971:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15972:                        } else {
                   15973:                                REG8(AH) = 0x8a;
                   15974:                                return;
                   15975:                        }
                   15976:                }
                   15977:                
                   15978:                // push old mapping data in new mapping
                   15979:                for(int i = 0; i < 4; i++) {
                   15980:                        i386_push16(handles[i]);
                   15981:                        i386_push16(pages  [i]);
                   15982:                }
                   15983:                REG8(AH) = 0x00;
                   15984:        } else {
                   15985:                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));
                   15986:                REG8(AH) = 0x8f;
                   15987:        }
                   15988: }
                   15989: 
1.1.1.20  root     15990: inline void msdos_int_67h_57h_tmp()
                   15991: {
                   15992:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15993:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15994:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   15995:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   15996:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   15997:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   15998:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15999:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   16000:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   16001:        
1.1.1.32  root     16002:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     16003:        UINT32 src_addr, dest_addr;
                   16004:        UINT32 src_addr_max, dest_addr_max;
                   16005:        
                   16006:        if(src_type == 0) {
                   16007:                src_buffer = mem;
                   16008:                src_addr = (src_seg << 4) + src_ofs;
                   16009:                src_addr_max = MAX_MEM;
                   16010:        } else {
1.1.1.31  root     16011:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     16012:                        REG8(AH) = 0x83;
                   16013:                        return;
                   16014:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   16015:                        REG8(AH) = 0x8a;
                   16016:                        return;
                   16017:                }
1.1.1.32  root     16018:                if(ems_handles[src_handle].buffer != NULL) {
                   16019:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   16020:                }
1.1.1.20  root     16021:                src_addr = src_ofs;
1.1.1.32  root     16022:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     16023:        }
                   16024:        if(dest_type == 0) {
                   16025:                dest_buffer = mem;
                   16026:                dest_addr = (dest_seg << 4) + dest_ofs;
                   16027:                dest_addr_max = MAX_MEM;
                   16028:        } else {
1.1.1.31  root     16029:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     16030:                        REG8(AH) = 0x83;
                   16031:                        return;
                   16032:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   16033:                        REG8(AH) = 0x8a;
                   16034:                        return;
                   16035:                }
1.1.1.32  root     16036:                if(ems_handles[dest_handle].buffer != NULL) {
                   16037:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   16038:                }
1.1.1.20  root     16039:                dest_addr = dest_ofs;
1.1.1.32  root     16040:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     16041:        }
1.1.1.32  root     16042:        if(src_buffer != NULL && dest_buffer != NULL) {
                   16043:                for(int i = 0; i < copy_length; i++) {
                   16044:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   16045:                                if(REG8(AL) == 0x00) {
                   16046:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   16047:                                } else if(REG8(AL) == 0x01) {
                   16048:                                        UINT8 tmp = dest_buffer[dest_addr];
                   16049:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   16050:                                        src_buffer[src_addr++] = tmp;
                   16051:                                }
                   16052:                        } else {
                   16053:                                REG8(AH) = 0x93;
                   16054:                                return;
1.1.1.20  root     16055:                        }
                   16056:                }
1.1.1.32  root     16057:                REG8(AH) = 0x00;
                   16058:        } else {
                   16059:                REG8(AH) = 0x80;
1.1.1.20  root     16060:        }
                   16061: }
                   16062: 
                   16063: inline void msdos_int_67h_57h()
                   16064: {
                   16065:        if(!support_ems) {
                   16066:                REG8(AH) = 0x84;
                   16067:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   16068:                struct {
                   16069:                        UINT16 handle;
                   16070:                        UINT16 page;
                   16071:                        bool mapped;
                   16072:                } tmp_pages[4];
                   16073:                
                   16074:                // unmap pages to copy memory data to ems buffer
                   16075:                for(int i = 0; i < 4; i++) {
                   16076:                        tmp_pages[i].handle = ems_pages[i].handle;
                   16077:                        tmp_pages[i].page   = ems_pages[i].page;
                   16078:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   16079:                        ems_unmap_page(i);
                   16080:                }
                   16081:                
                   16082:                // run move/exchange operation
                   16083:                msdos_int_67h_57h_tmp();
                   16084:                
                   16085:                // restore unmapped pages
                   16086:                for(int i = 0; i < 4; i++) {
                   16087:                        if(tmp_pages[i].mapped) {
                   16088:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   16089:                        }
                   16090:                }
                   16091:        } else {
1.1.1.22  root     16092:                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     16093:                REG8(AH) = 0x8f;
                   16094:        }
                   16095: }
                   16096: 
                   16097: inline void msdos_int_67h_58h()
                   16098: {
                   16099:        if(!support_ems) {
                   16100:                REG8(AH) = 0x84;
                   16101:        } else if(REG8(AL) == 0x00) {
                   16102:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     16103:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   16104:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     16105:                }
                   16106:                REG8(AH) = 0x00;
                   16107:                REG16(CX) = 4;
                   16108:        } else if(REG8(AL) == 0x01) {
                   16109:                REG8(AH) = 0x00;
                   16110:                REG16(CX) = 4;
                   16111:        } else {
1.1.1.22  root     16112:                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     16113:                REG8(AH) = 0x8f;
                   16114:        }
                   16115: }
                   16116: 
1.1.1.42  root     16117: inline void msdos_int_67h_59h()
                   16118: {
                   16119:        if(!support_ems) {
                   16120:                REG8(AH) = 0x84;
                   16121:        } else if(REG8(AL) == 0x00) {
1.1.1.49  root     16122:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1024;
                   16123:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 0;
                   16124:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 4 * 4;
                   16125:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = 0;
                   16126:                REG8(AH) = 0x00;
                   16127: //             REG8(AH) = 0xa4; // access denied by operating system
1.1.1.42  root     16128:        } else if(REG8(AL) == 0x01) {
                   16129:                REG8(AH) = 0x00;
                   16130:                REG16(BX) = free_ems_pages;
                   16131:                REG16(DX) = MAX_EMS_PAGES;
                   16132:        } else {
                   16133:                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));
                   16134:                REG8(AH) = 0x8f;
                   16135:        }
                   16136: }
                   16137: 
1.1.1.20  root     16138: inline void msdos_int_67h_5ah()
                   16139: {
                   16140:        if(!support_ems) {
1.1.1.19  root     16141:                REG8(AH) = 0x84;
1.1.1.20  root     16142:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   16143:                REG8(AH) = 0x87;
                   16144:        } else if(REG16(BX) > free_ems_pages) {
                   16145:                REG8(AH) = 0x88;
                   16146: //     } else if(REG16(BX) == 0) {
                   16147: //             REG8(AH) = 0x89;
                   16148:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     16149:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     16150:                        if(!ems_handles[i].allocated) {
                   16151:                                ems_allocate_pages(i, REG16(BX));
                   16152:                                REG8(AH) = 0x00;
                   16153:                                REG16(DX) = i;
                   16154:                                return;
                   16155:                        }
                   16156:                }
                   16157:                REG8(AH) = 0x85;
                   16158:        } else {
1.1.1.22  root     16159:                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     16160:                REG8(AH) = 0x8f;
1.1.1.19  root     16161:        }
                   16162: }
                   16163: 
1.1.1.49  root     16164: inline void msdos_int_67h_5bh()
                   16165: {
                   16166:        static UINT8  stored_bl = 0x00;
                   16167:        static UINT16 stored_es = 0x0000;
                   16168:        static UINT16 stored_di = 0x0000;
                   16169:        
                   16170:        if(!support_ems) {
                   16171:                REG8(AH) = 0x84;
                   16172:        } else if(REG8(AL) == 0x00) {
                   16173:                if(stored_bl == 0x00) {
                   16174:                        if(!(stored_es == 0 && stored_di == 0)) {
                   16175:                                for(int i = 0; i < 4; i++) {
                   16176:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   16177:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   16178:                                }
                   16179:                        }
                   16180:                        SREG(ES) = stored_es;
                   16181:                        i386_load_segment_descriptor(ES);
                   16182:                        REG16(DI) = stored_di;
                   16183:                } else {
                   16184:                        REG8(BL) = stored_bl;
                   16185:                }
                   16186:                REG8(AH) = 0x00;
                   16187:        } else if(REG8(AL) == 0x01) {
                   16188:                if(REG8(BL) == 0x00) {
                   16189:                        if(!(SREG(ES) == 0 && REG16(DI) == 0)) {
                   16190:                                for(int i = 0; i < 4; i++) {
                   16191:                                        UINT16 handle = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0);
                   16192:                                        UINT16 page   = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2);
                   16193:                                        
                   16194:                                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   16195:                                                ems_map_page(i, handle, page);
                   16196:                                        } else {
                   16197:                                                ems_unmap_page(i);
                   16198:                                        }
                   16199:                                }
                   16200:                        }
                   16201:                }
                   16202:                stored_bl = REG8(BL);
                   16203:                stored_es = SREG(ES);
                   16204:                stored_di = REG16(DI);
                   16205:                REG8(AH) = 0x00;
                   16206:        } else if(REG8(AL) == 0x02) {
                   16207:                REG16(DX) = 4 * 4;
                   16208:                REG8(AH) = 0x00;
                   16209:        } else if(REG8(AL) == 0x03) {
                   16210:                REG8(BL) = 0x00; // not supported
                   16211:                REG8(AH) = 0x00;
                   16212:        } else if(REG8(AL) == 0x04) {
                   16213:                REG8(AH) = 0x00;
                   16214:        } else if(REG8(AL) == 0x05) {
                   16215:                REG8(BL) = 0x00; // not supported
                   16216:                REG8(AH) = 0x00;
                   16217:        } else {
                   16218:                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));
                   16219:                REG8(AH) = 0x8f;
                   16220:        }
                   16221: }
                   16222: 
1.1.1.43  root     16223: inline void msdos_int_67h_5dh()
                   16224: {
                   16225:        if(!support_ems) {
                   16226:                REG8(AH) = 0x84;
                   16227:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   16228:                REG8(AH) = 0xa4; // operating system denied access
                   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.49  root     16235: inline void msdos_int_67h_70h()
                   16236: {
                   16237:        if(!support_ems) {
                   16238:                REG8(AH) = 0x84;
                   16239:        } else if(REG8(AL) == 0x00) {
                   16240:                REG8(AL) = 0x00;
                   16241:                REG8(AH) = 0x00;
                   16242:        } else if(REG8(AL) == 0x01) {
                   16243:                REG8(AL) = 0x00;
                   16244: //             REG8(AH) = (REG8(BL) == 0x00) ? 0x00 : 0x80;
                   16245:                REG8(AH) = 0x00;
                   16246:        } else {
                   16247:                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));
                   16248:                REG8(AH) = 0x8f;
                   16249:        }
                   16250: }
                   16251: 
1.1.1.30  root     16252: inline void msdos_int_67h_deh()
                   16253: {
1.1.1.63! root     16254: #if defined(SUPPORT_VCPI)
        !          16255:        if(!support_ems) {
        !          16256:                REG8(AH) = 0x84;
        !          16257:        } else if(REG8(AL) == 0x00) {
        !          16258:                REG8(AH) = 0x00;
        !          16259:                REG16(BX) = 0x0100;
        !          16260:        } else if(REG8(AL) == 0x01) {
        !          16261:                REG8(AH) = 0x00;
        !          16262:                // from DOSBox
        !          16263:                for(int ct = 0; ct < 0xff; ct++) {
        !          16264:                        *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x00) = 0x67;            // access bits
        !          16265:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x01) = ct * 0x10;       // mapping
        !          16266:                        *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x03) = 0x00;
        !          16267:                }
        !          16268:                for(int ct = 0xff; ct < 0x100; ct++) {
        !          16269:                        *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x00) = 0x67;            // access bits
        !          16270:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x01) = (ct - 0xff) * 0x10 + 0x1100;     // mapping
        !          16271:                        *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x03) = 0x00;
        !          16272:                }
        !          16273:                REG16(DI) += 0x400;             // advance pointer by 0x100*4
        !          16274:                
        !          16275:                // Set up three descriptor table entries
        !          16276:                UINT32 cbseg_low  = (DUMMY_TOP & 0x00ffff) << 16;
        !          16277:                UINT32 cbseg_high = (DUMMY_TOP & 0x1f0000) >> 16;
        !          16278:                // Descriptor 1 (code segment, callback segment)
        !          16279:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x0000ffff | cbseg_low ;
        !          16280:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = 0x00009a00 | cbseg_high;
        !          16281:                // Descriptor 2 (data segment, full access)
        !          16282:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x08) = 0x0000ffff;
        !          16283:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c) = 0x00009200;
        !          16284:                // Descriptor 3 (full access)
        !          16285:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10) = 0x0000ffff;
        !          16286:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x14) = 0x00009200;
        !          16287:                // Offset in code segment of protected mode entry point
        !          16288:                REG32(EBX) = 0x2a; // fffc:002a
        !          16289:        } else if(REG8(AL) == 0x02) {
        !          16290:                REG8(AH) = 0x00;
        !          16291:                REG32(EDX) = (MAX_MEM - 1) & 0xfffff000;
        !          16292:        } else if(REG8(AL) == 0x03) {
        !          16293:                REG8(AH) = 0x00;
        !          16294:                REG32(EDX) = 0;
        !          16295:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
        !          16296:                        if(emb_handle->handle == 0) {
        !          16297:                                REG32(EDX) += emb_handle->size_kb;
        !          16298:                        }
        !          16299:                }
        !          16300:                REG32(EDX) /= 4;
        !          16301:        } else if(REG8(AL) == 0x04) {
        !          16302:                emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(4);
        !          16303:                if(emb_handle != NULL) {
        !          16304:                        REG8(AH) = 0x00;
        !          16305:                        REG32(EDX) = emb_handle->address;
        !          16306:                }
        !          16307:        } else if(REG8(AL) == 0x05) {
        !          16308:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
        !          16309:                        if(emb_handle->handle != 0 && emb_handle->address == REG32(EDX)) {
        !          16310:                                REG8(AH) = 0x00;
        !          16311:                                msdos_xms_free_emb_handle(emb_handle);
        !          16312:                                break;
        !          16313:                        }
        !          16314:                }
        !          16315:        } else if(REG8(AL) == 0x06) {
        !          16316:                REG8(AH) = 0x00;
        !          16317:                REG32(EDX) = REG16(CX) << 12;
        !          16318:        } else if(REG8(AL) == 0x07) {
        !          16319:                REG8(AH) = 0x00;
        !          16320:                REG32(EBX) = m_cr[0];
        !          16321:        } else if(REG8(AL) == 0x08) {
        !          16322:                REG8(AH) = 0x00;
        !          16323:                for(int i = 0; i < 8; i++) {
        !          16324:                        *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i) = m_dr[i];
        !          16325:                }
        !          16326:        } else if(REG8(AL) == 0x09) {
        !          16327:                REG8(AH) = 0x00;
        !          16328:                for(int i = 0; i < 8; i++) {
        !          16329:                        m_dr[i] = *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i);
        !          16330:                }
        !          16331:        } else if(REG8(AL) == 0x0a) {
        !          16332:                REG8(AH) = 0x00;
        !          16333:                REG16(BX) = pic[0].icw2;
        !          16334:                REG16(CX) = pic[1].icw2;
        !          16335:        } else if(REG8(AL) == 0x0b) {
        !          16336:                REG8(AH) = 0x00;
        !          16337:                pic[0].icw2 = REG8(BL);
        !          16338:                pic[1].icw2 = REG8(CL);
        !          16339:        } else if(REG8(AL) == 0x0c) {
        !          16340:                // from DOSBox
        !          16341:                m_IF = 0;
        !          16342:                m_CPL = 0;
        !          16343:                
        !          16344:                // Read data from ESI (linear address)
        !          16345:                UINT32 new_cr3      = *(UINT32 *)(mem + REG32(ESI) + 0x00);
        !          16346:                UINT32 new_gdt_addr = *(UINT32 *)(mem + REG32(ESI) + 0x04);
        !          16347:                UINT32 new_idt_addr = *(UINT32 *)(mem + REG32(ESI) + 0x08);
        !          16348:                UINT16 new_ldt      = *(UINT16 *)(mem + REG32(ESI) + 0x0c);
        !          16349:                UINT16 new_tr       = *(UINT16 *)(mem + REG32(ESI) + 0x0e);
        !          16350:                UINT32 new_eip      = *(UINT32 *)(mem + REG32(ESI) + 0x10);
        !          16351:                UINT16 new_cs       = *(UINT16 *)(mem + REG32(ESI) + 0x14);
        !          16352:                
        !          16353:                // Get GDT and IDT entries
        !          16354:                UINT16 new_gdt_limit = *(UINT16 *)(mem + new_gdt_addr + 0);
        !          16355:                UINT32 new_gdt_base  = *(UINT32 *)(mem + new_gdt_addr + 2);
        !          16356:                UINT16 new_idt_limit = *(UINT16 *)(mem + new_idt_addr + 0);
        !          16357:                UINT32 new_idt_base  = *(UINT32 *)(mem + new_idt_addr + 2);
        !          16358:                
        !          16359:                // Switch to protected mode, paging enabled if necessary
        !          16360:                if(new_cr3 != 0) {
        !          16361:                        m_cr[0] |= 0x80000000;
        !          16362:                }
        !          16363:                m_cr[3] = new_cr3;
        !          16364:                
        !          16365:                *(UINT8 *)(mem + new_gdt_base + (new_tr & 0xfff8) + 5) &= 0xfd;
        !          16366:                
        !          16367:                // Load tables and initialize segment registers
        !          16368:                m_gdtr.limit = new_gdt_limit;
        !          16369:                m_gdtr.base = new_gdt_base;
        !          16370:                m_idtr.limit = new_idt_limit;
        !          16371:                m_idtr.base = new_idt_base;
        !          16372:                
        !          16373:                SREG(DS) = 0x00;
        !          16374:                SREG(ES) = 0x00;
        !          16375:                SREG(FS) = 0x00;
        !          16376:                SREG(GS) = 0x00;
        !          16377:                i386_load_segment_descriptor(DS);
        !          16378:                i386_load_segment_descriptor(ES);
        !          16379:                i386_load_segment_descriptor(FS);
        !          16380:                i386_load_segment_descriptor(GS);
        !          16381:                
        !          16382: //             i386_set_a20_line(1);
        !          16383:                
        !          16384:                /* Switch to protected mode */
        !          16385:                m_VM = m_NT = 0;
        !          16386:                m_IOP1 = m_IOP2 = 1;
        !          16387:                
        !          16388:                i386_jmp_far(new_cs, new_eip);
        !          16389:        } else {
        !          16390:                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));
        !          16391:                REG8(AH) = 0x8f;
        !          16392:        }
        !          16393: #else
1.1.1.30  root     16394:        REG8(AH) = 0x84;
1.1.1.63! root     16395: #endif
1.1.1.30  root     16396: }
                   16397: 
1.1.1.19  root     16398: #ifdef SUPPORT_XMS
                   16399: 
1.1.1.32  root     16400: void msdos_xms_init()
1.1.1.26  root     16401: {
1.1.1.30  root     16402:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   16403:        emb_handle_top->address = EMB_TOP;
                   16404:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     16405:        xms_a20_local_enb_count = 0;
                   16406: }
                   16407: 
1.1.1.32  root     16408: void msdos_xms_finish()
                   16409: {
                   16410:        msdos_xms_release();
                   16411: }
                   16412: 
                   16413: void msdos_xms_release()
1.1.1.30  root     16414: {
                   16415:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   16416:                emb_handle_t *next_handle = emb_handle->next;
                   16417:                free(emb_handle);
                   16418:                emb_handle = next_handle;
                   16419:        }
                   16420: }
                   16421: 
                   16422: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   16423: {
                   16424:        if(handle != 0) {
                   16425:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16426:                        if(emb_handle->handle == handle) {
                   16427:                                return(emb_handle);
                   16428:                        }
                   16429:                }
                   16430:        }
                   16431:        return(NULL);
                   16432: }
                   16433: 
                   16434: int msdos_xms_get_unused_emb_handle_id()
                   16435: {
                   16436:        for(int handle = 1;; handle++) {
                   16437:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   16438:                        return(handle);
                   16439:                }
                   16440:        }
                   16441:        return(0);
                   16442: }
                   16443: 
                   16444: int msdos_xms_get_unused_emb_handle_count()
                   16445: {
                   16446:        int count = 64; //255;
                   16447:        
                   16448:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16449:                if(emb_handle->handle != 0) {
                   16450:                        if(--count == 1) {
                   16451:                                break;
                   16452:                        }
                   16453:                }
                   16454:        }
                   16455:        return(count);
                   16456: }
                   16457: 
                   16458: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   16459: {
                   16460:        if(emb_handle->size_kb > size_kb) {
                   16461:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   16462:                
                   16463:                new_handle->address = emb_handle->address + size_kb * 1024;
                   16464:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   16465:                emb_handle->size_kb = size_kb;
                   16466:                
                   16467:                new_handle->prev = emb_handle;
                   16468:                new_handle->next = emb_handle->next;
                   16469:                if(emb_handle->next != NULL) {
                   16470:                        emb_handle->next->prev = new_handle;
                   16471:                }
                   16472:                emb_handle->next = new_handle;
                   16473:        }
                   16474: }
                   16475: 
                   16476: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   16477: {
                   16478:        emb_handle_t *next_handle = emb_handle->next;
                   16479:        
                   16480:        if(next_handle != NULL) {
                   16481:                emb_handle->size_kb += next_handle->size_kb;
                   16482:                
                   16483:                if(next_handle->next != NULL) {
                   16484:                        next_handle->next->prev = emb_handle;
                   16485:                }
                   16486:                emb_handle->next = next_handle->next;
                   16487:                free(next_handle);
                   16488:        }
                   16489: }
                   16490: 
                   16491: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   16492: {
                   16493:        emb_handle_t *target_handle = NULL;
                   16494:        
                   16495:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16496:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   16497:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   16498:                                target_handle = emb_handle;
                   16499:                        }
                   16500:                }
                   16501:        }
                   16502:        if(target_handle != NULL) {
                   16503:                if(target_handle->size_kb > size_kb) {
                   16504:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   16505:                }
                   16506: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16507:                return(target_handle);
                   16508:        }
                   16509:        return(NULL);
                   16510: }
                   16511: 
                   16512: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   16513: {
                   16514:        emb_handle_t *prev_handle = emb_handle->prev;
                   16515:        emb_handle_t *next_handle = emb_handle->next;
                   16516:        
                   16517:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   16518:                msdos_xms_combine_emb_handles(prev_handle);
                   16519:                emb_handle = prev_handle;
                   16520:        }
                   16521:        if(next_handle != NULL && next_handle->handle == 0) {
                   16522:                msdos_xms_combine_emb_handles(emb_handle);
                   16523:        }
                   16524:        emb_handle->handle = 0;
                   16525: }
                   16526: 
1.1.1.19  root     16527: inline void msdos_call_xms_00h()
                   16528: {
1.1.1.29  root     16529: #if defined(HAS_I386)
                   16530:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.63! root     16531:        REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
        !          16532: //     REG16(BX) = 0x035f; // V3.95 (Driver Revision)
1.1.1.29  root     16533: #else
                   16534:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   16535:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   16536: #endif
                   16537: //     REG16(DX) = 0x0000; // HMA does not exist
                   16538:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     16539: }
                   16540: 
                   16541: inline void msdos_call_xms_01h()
                   16542: {
1.1.1.29  root     16543:        if(REG8(AL) == 0x40) {
                   16544:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   16545:                // DX=KB free extended memory returned by last call of function 08h
                   16546:                REG16(AX) = 0x0000;
                   16547:                REG8(BL) = 0x91;
                   16548:                REG16(DX) = xms_dx_after_call_08h;
                   16549:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16550:                REG16(AX) = 0x0000;
                   16551:                REG8(BL) = 0x81; // Vdisk was detected
                   16552: #ifdef SUPPORT_HMA
                   16553:        } else if(is_hma_used_by_int_2fh) {
                   16554:                REG16(AX) = 0x0000;
                   16555:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16556:        } else if(is_hma_used_by_xms) {
                   16557:                REG16(AX) = 0x0000;
                   16558:                REG8(BL) = 0x91; // HMA is already in use
                   16559:        } else {
                   16560:                REG16(AX) = 0x0001;
                   16561:                is_hma_used_by_xms = true;
                   16562: #else
                   16563:        } else {
                   16564:                REG16(AX) = 0x0000;
                   16565:                REG8(BL) = 0x91; // HMA is already in use
                   16566: #endif
                   16567:        }
1.1.1.19  root     16568: }
                   16569: 
                   16570: inline void msdos_call_xms_02h()
                   16571: {
1.1.1.29  root     16572:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16573:                REG16(AX) = 0x0000;
                   16574:                REG8(BL) = 0x81; // Vdisk was detected
                   16575: #ifdef SUPPORT_HMA
                   16576:        } else if(is_hma_used_by_int_2fh) {
                   16577:                REG16(AX) = 0x0000;
                   16578:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16579:        } else if(!is_hma_used_by_xms) {
                   16580:                REG16(AX) = 0x0000;
                   16581:                REG8(BL) = 0x93; // HMA is not allocated
                   16582:        } else {
                   16583:                REG16(AX) = 0x0001;
                   16584:                is_hma_used_by_xms = false;
                   16585:                // restore first free mcb in high memory area
                   16586:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   16587: #else
                   16588:        } else {
                   16589:                REG16(AX) = 0x0000;
                   16590:                REG8(BL) = 0x91; // HMA is already in use
                   16591: #endif
                   16592:        }
1.1.1.19  root     16593: }
                   16594: 
                   16595: inline void msdos_call_xms_03h()
                   16596: {
                   16597:        i386_set_a20_line(1);
                   16598:        REG16(AX) = 0x0001;
                   16599:        REG8(BL) = 0x00;
                   16600: }
                   16601: 
                   16602: inline void msdos_call_xms_04h()
                   16603: {
1.1.1.21  root     16604:        i386_set_a20_line(0);
                   16605:        REG16(AX) = 0x0001;
                   16606:        REG8(BL) = 0x00;
1.1.1.19  root     16607: }
                   16608: 
                   16609: inline void msdos_call_xms_05h()
                   16610: {
                   16611:        i386_set_a20_line(1);
                   16612:        REG16(AX) = 0x0001;
                   16613:        REG8(BL) = 0x00;
1.1.1.21  root     16614:        xms_a20_local_enb_count++;
1.1.1.19  root     16615: }
                   16616: 
                   16617: void msdos_call_xms_06h()
                   16618: {
1.1.1.21  root     16619:        if(xms_a20_local_enb_count > 0) {
1.1.1.45  root     16620:                if(--xms_a20_local_enb_count == 0) {
                   16621:                        i386_set_a20_line(0);
                   16622:                        REG16(AX) = 0x0001;
                   16623:                        REG8(BL) = 0x00;
                   16624:                } else {
                   16625:                        REG16(AX) = 0x0000;
                   16626:                        REG8(BL) = 0x94;
                   16627:                }
1.1.1.21  root     16628:        } else {
1.1.1.45  root     16629:                i386_set_a20_line(0);
1.1.1.21  root     16630:                REG16(AX) = 0x0001;
                   16631:                REG8(BL) = 0x00;
1.1.1.19  root     16632:        }
                   16633: }
                   16634: 
                   16635: inline void msdos_call_xms_07h()
                   16636: {
                   16637:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   16638:        REG8(BL) = 0x00;
                   16639: }
                   16640: 
                   16641: inline void msdos_call_xms_08h()
                   16642: {
1.1.1.45  root     16643:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     16644:        
1.1.1.30  root     16645:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16646:                if(emb_handle->handle == 0) {
1.1.1.45  root     16647:                        if(eax < emb_handle->size_kb) {
                   16648:                                eax = emb_handle->size_kb;
1.1.1.19  root     16649:                        }
1.1.1.45  root     16650:                        edx += emb_handle->size_kb;
1.1.1.19  root     16651:                }
                   16652:        }
1.1.1.45  root     16653:        if(eax > 65535) {
                   16654:                eax = 65535;
                   16655:        }
                   16656:        if(edx > 65535) {
                   16657:                edx = 65535;
                   16658:        }
                   16659:        if(eax == 0 && edx == 0) {
1.1.1.19  root     16660:                REG8(BL) = 0xa0;
                   16661:        } else {
                   16662:                REG8(BL) = 0x00;
                   16663:        }
1.1.1.45  root     16664: #if defined(HAS_I386)
                   16665:        REG32(EAX) = eax;
                   16666:        REG32(EDX) = edx;
                   16667: #else
                   16668:        REG16(AX) = (UINT16)eax;
                   16669:        REG16(DX) = (UINT16)edx;
                   16670: #endif
1.1.1.29  root     16671:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     16672: }
                   16673: 
1.1.1.30  root     16674: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     16675: {
1.1.1.30  root     16676:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   16677:        
                   16678:        if(emb_handle != NULL) {
                   16679:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16680:                
                   16681:                REG16(AX) = 0x0001;
                   16682:                REG16(DX) = emb_handle->handle;
                   16683:                REG8(BL) = 0x00;
                   16684:        } else {
                   16685:                REG16(AX) = REG16(DX) = 0x0000;
                   16686:                REG8(BL) = 0xa0;
1.1.1.19  root     16687:        }
1.1.1.30  root     16688: }
                   16689: 
                   16690: inline void msdos_call_xms_09h()
                   16691: {
                   16692:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     16693: }
                   16694: 
                   16695: inline void msdos_call_xms_0ah()
                   16696: {
1.1.1.30  root     16697:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16698:        
                   16699:        if(emb_handle == NULL) {
1.1.1.19  root     16700:                REG16(AX) = 0x0000;
                   16701:                REG8(BL) = 0xa2;
1.1.1.45  root     16702: //     } else if(emb_handle->lock > 0) {
                   16703: //             REG16(AX) = 0x0000;
                   16704: //             REG8(BL) = 0xab;
1.1.1.19  root     16705:        } else {
1.1.1.30  root     16706:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     16707:                
                   16708:                REG16(AX) = 0x0001;
                   16709:                REG8(BL) = 0x00;
                   16710:        }
                   16711: }
                   16712: 
                   16713: inline void msdos_call_xms_0bh()
                   16714: {
                   16715:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   16716:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   16717:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   16718:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   16719:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   16720:        
                   16721:        UINT8 *src_buffer, *dest_buffer;
                   16722:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     16723:        emb_handle_t *emb_handle;
1.1.1.19  root     16724:        
                   16725:        if(src_handle == 0) {
                   16726:                src_buffer = mem;
                   16727:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   16728:                src_addr_max = MAX_MEM;
                   16729:        } else {
1.1.1.30  root     16730:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     16731:                        REG16(AX) = 0x0000;
                   16732:                        REG8(BL) = 0xa3;
                   16733:                        return;
                   16734:                }
1.1.1.30  root     16735:                src_buffer = mem + emb_handle->address;
                   16736:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16737:        }
                   16738:        if(dest_handle == 0) {
                   16739:                dest_buffer = mem;
                   16740:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   16741:                dest_addr_max = MAX_MEM;
                   16742:        } else {
1.1.1.30  root     16743:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     16744:                        REG16(AX) = 0x0000;
                   16745:                        REG8(BL) = 0xa5;
                   16746:                        return;
                   16747:                }
1.1.1.30  root     16748:                dest_buffer = mem + emb_handle->address;
                   16749:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16750:        }
                   16751:        for(int i = 0; i < copy_length; i++) {
                   16752:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   16753:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   16754:                } else {
                   16755:                        break;
                   16756:                }
                   16757:        }
                   16758:        REG16(AX) = 0x0001;
                   16759:        REG8(BL) = 0x00;
                   16760: }
                   16761: 
                   16762: inline void msdos_call_xms_0ch()
                   16763: {
1.1.1.30  root     16764:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16765:        
                   16766:        if(emb_handle == NULL) {
1.1.1.19  root     16767:                REG16(AX) = 0x0000;
                   16768:                REG8(BL) = 0xa2;
                   16769:        } else {
1.1.1.45  root     16770:                if(emb_handle->lock < 255) {
                   16771:                        emb_handle->lock++;
                   16772:                }
1.1.1.19  root     16773:                REG16(AX) = 0x0001;
1.1.1.30  root     16774:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   16775:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     16776:        }
                   16777: }
                   16778: 
                   16779: inline void msdos_call_xms_0dh()
                   16780: {
1.1.1.30  root     16781:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16782:        
                   16783:        if(emb_handle == NULL) {
1.1.1.19  root     16784:                REG16(AX) = 0x0000;
                   16785:                REG8(BL) = 0xa2;
1.1.1.30  root     16786:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     16787:                REG16(AX) = 0x0000;
                   16788:                REG8(BL) = 0xaa;
                   16789:        } else {
1.1.1.30  root     16790:                emb_handle->lock--;
1.1.1.19  root     16791:                REG16(AX) = 0x0001;
                   16792:                REG8(BL) = 0x00;
                   16793:        }
                   16794: }
                   16795: 
                   16796: inline void msdos_call_xms_0eh()
                   16797: {
1.1.1.30  root     16798:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16799:        
                   16800:        if(emb_handle == NULL) {
1.1.1.19  root     16801:                REG16(AX) = 0x0000;
                   16802:                REG8(BL) = 0xa2;
                   16803:        } else {
                   16804:                REG16(AX) = 0x0001;
1.1.1.30  root     16805:                REG8(BH) = emb_handle->lock;
                   16806:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   16807:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     16808:        }
                   16809: }
                   16810: 
1.1.1.30  root     16811: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     16812: {
1.1.1.30  root     16813:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16814:        
                   16815:        if(emb_handle == NULL) {
1.1.1.19  root     16816:                REG16(AX) = 0x0000;
                   16817:                REG8(BL) = 0xa2;
1.1.1.30  root     16818:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     16819:                REG16(AX) = 0x0000;
                   16820:                REG8(BL) = 0xab;
                   16821:        } else {
1.1.1.30  root     16822:                if(emb_handle->size_kb < size_kb) {
                   16823:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   16824:                                msdos_xms_combine_emb_handles(emb_handle);
                   16825:                                if(emb_handle->size_kb > size_kb) {
                   16826:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16827:                                }
                   16828:                        } else {
                   16829:                                int old_handle = emb_handle->handle;
                   16830:                                int old_size_kb = emb_handle->size_kb;
                   16831:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   16832:                                
                   16833:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   16834:                                msdos_xms_free_emb_handle(emb_handle);
                   16835:                                
                   16836:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   16837:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   16838:                                }
                   16839:                                emb_handle->handle = old_handle;
                   16840:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   16841:                                free(buffer);
                   16842:                        }
                   16843:                } else if(emb_handle->size_kb > size_kb) {
                   16844:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16845:                }
                   16846:                if(emb_handle->size_kb != size_kb) {
                   16847:                        REG16(AX) = 0x0000;
                   16848:                        REG8(BL) = 0xa0;
                   16849:                } else {
                   16850:                        REG16(AX) = 0x0001;
                   16851:                        REG8(BL) = 0x00;
                   16852:                }
1.1.1.19  root     16853:        }
                   16854: }
                   16855: 
1.1.1.30  root     16856: inline void msdos_call_xms_0fh()
                   16857: {
                   16858:        msdos_call_xms_0fh(REG16(BX));
                   16859: }
                   16860: 
1.1.1.19  root     16861: inline void msdos_call_xms_10h()
                   16862: {
                   16863:        int seg;
                   16864:        
                   16865:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   16866:                REG16(AX) = 0x0001;
                   16867:                REG16(BX) = seg;
                   16868:        } else {
                   16869:                REG16(AX) = 0x0000;
                   16870:                REG8(BL) = 0xb0;
                   16871:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   16872:        }
                   16873: }
                   16874: 
                   16875: inline void msdos_call_xms_11h()
                   16876: {
                   16877:        int mcb_seg = REG16(DX) - 1;
                   16878:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16879:        
                   16880:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16881:                msdos_mem_free(REG16(DX));
                   16882:                REG16(AX) = 0x0001;
                   16883:                REG8(BL) = 0x00;
                   16884:        } else {
                   16885:                REG16(AX) = 0x0000;
                   16886:                REG8(BL) = 0xb2;
                   16887:        }
                   16888: }
                   16889: 
                   16890: inline void msdos_call_xms_12h()
                   16891: {
                   16892:        int mcb_seg = REG16(DX) - 1;
                   16893:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16894:        int max_paragraphs;
                   16895:        
                   16896:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16897:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   16898:                        REG16(AX) = 0x0001;
                   16899:                        REG8(BL) = 0x00;
                   16900:                } else {
                   16901:                        REG16(AX) = 0x0000;
                   16902:                        REG8(BL) = 0xb0;
                   16903:                        REG16(DX) = max_paragraphs;
                   16904:                }
                   16905:        } else {
                   16906:                REG16(AX) = 0x0000;
                   16907:                REG8(BL) = 0xb2;
                   16908:        }
                   16909: }
                   16910: 
1.1.1.29  root     16911: #if defined(HAS_I386)
                   16912: 
                   16913: inline void msdos_call_xms_88h()
                   16914: {
                   16915:        REG32(EAX) = REG32(EDX) = 0x0000;
                   16916:        
1.1.1.30  root     16917:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16918:                if(emb_handle->handle == 0) {
                   16919:                        if(REG32(EAX) < emb_handle->size_kb) {
                   16920:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     16921:                        }
1.1.1.30  root     16922:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     16923:                }
                   16924:        }
                   16925:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   16926:                REG8(BL) = 0xa0;
                   16927:        } else {
                   16928:                REG8(BL) = 0x00;
                   16929:        }
                   16930:        REG32(ECX) = EMB_END - 1;
                   16931: }
                   16932: 
                   16933: inline void msdos_call_xms_89h()
                   16934: {
1.1.1.30  root     16935:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     16936: }
                   16937: 
                   16938: inline void msdos_call_xms_8eh()
                   16939: {
1.1.1.30  root     16940:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16941:        
                   16942:        if(emb_handle == NULL) {
1.1.1.29  root     16943:                REG16(AX) = 0x0000;
                   16944:                REG8(BL) = 0xa2;
                   16945:        } else {
                   16946:                REG16(AX) = 0x0001;
1.1.1.30  root     16947:                REG8(BH) = emb_handle->lock;
                   16948:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   16949:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     16950:        }
                   16951: }
                   16952: 
                   16953: inline void msdos_call_xms_8fh()
                   16954: {
1.1.1.30  root     16955:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     16956: }
                   16957: 
                   16958: #endif
1.1.1.19  root     16959: #endif
                   16960: 
1.1.1.26  root     16961: UINT16 msdos_get_equipment()
                   16962: {
                   16963:        static UINT16 equip = 0;
                   16964:        
                   16965:        if(equip == 0) {
                   16966: #ifdef SUPPORT_FPU
                   16967:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   16968: #endif
                   16969:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   16970:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   16971: //             equip |= (1 << 8);      // 0 if DMA installed
                   16972:                equip |= (2 << 9);      // number of serial ports
                   16973:                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     16974:                
                   16975:                // check only A: and B: if it is floppy drive
                   16976:                int n = 0;
                   16977:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     16978:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   16979:                                n++;
1.1.1.28  root     16980:                        }
                   16981:                }
                   16982:                if(n != 0) {
                   16983:                        equip |= (1 << 0);      // floppy disk(s) installed
                   16984:                        n--;
                   16985:                        equip |= (n << 6);      // number of floppies installed less 1
                   16986:                }
                   16987: //             if(joyGetNumDevs() != 0) {
                   16988: //                     equip |= (1 << 12);     // game port installed
                   16989: //             }
1.1.1.26  root     16990:        }
                   16991:        return(equip);
                   16992: }
                   16993: 
1.1       root     16994: void msdos_syscall(unsigned num)
                   16995: {
1.1.1.22  root     16996: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     16997:        if(num == 0x08 || num == 0x1c) {
                   16998:                // don't log the timer interrupts
1.1.1.45  root     16999: //             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     17000:        } else if(num == 0x30) {
                   17001:                // dummy interrupt for call 0005h (call near)
                   17002:                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     17003:        } else if(num == 0x65) {
1.1.1.22  root     17004:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     17005:                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     17006:        } else if(num == 0x66) {
1.1.1.22  root     17007:                // dummy interrupt for XMS (call far)
1.1.1.33  root     17008:                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     17009:        } else if(num >= 0x68 && num <= 0x6f) {
1.1.1.45  root     17010:                // dummy interrupt
1.1.1.22  root     17011:        } else {
1.1.1.33  root     17012:                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     17013:        }
                   17014: #endif
1.1.1.36  root     17015:        // update cursor position
                   17016:        if(cursor_moved) {
                   17017:                pcbios_update_cursor_position();
                   17018:                cursor_moved = false;
                   17019:        }
1.1.1.50  root     17020: #ifdef USE_SERVICE_THREAD
                   17021:        // this is called from dummy loop to wait until a serive that waits input is done
                   17022:        if(!in_service)
                   17023: #endif
1.1.1.33  root     17024:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     17025:        
1.1       root     17026:        switch(num) {
                   17027:        case 0x00:
1.1.1.28  root     17028:                try {
                   17029:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17030:                        error("division by zero\n");
                   17031:                } catch(...) {
                   17032:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   17033:                }
1.1       root     17034:                break;
                   17035:        case 0x04:
1.1.1.28  root     17036:                try {
                   17037:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17038:                        error("overflow\n");
                   17039:                } catch(...) {
                   17040:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   17041:                }
1.1       root     17042:                break;
                   17043:        case 0x06:
                   17044:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     17045:                if(!ignore_illegal_insn) {
1.1.1.28  root     17046:                        try {
                   17047:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17048:                                error("illegal instruction\n");
                   17049:                        } catch(...) {
                   17050:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   17051:                        }
1.1.1.14  root     17052:                } else {
                   17053: #if defined(HAS_I386)
1.1.1.39  root     17054:                        m_eip = m_int6h_skip_eip;
                   17055: #elif defined(HAS_I286)
                   17056:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     17057: #else
1.1.1.39  root     17058:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     17059: #endif
                   17060:                }
1.1       root     17061:                break;
1.1.1.33  root     17062:        case 0x09:
                   17063:                // ctrl-break is pressed
                   17064:                if(raise_int_1bh) {
                   17065: #if defined(HAS_I386)
                   17066:                        m_ext = 0; // not an external interrupt
                   17067:                        i386_trap(0x1b, 1, 0);
                   17068:                        m_ext = 1;
                   17069: #else
                   17070:                        PREFIX86(_interrupt)(0x1b);
                   17071: #endif
                   17072:                        raise_int_1bh = false;
                   17073:                }
1.1.1.8   root     17074:        case 0x08:
1.1.1.14  root     17075: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     17076:        case 0x0b:
                   17077:        case 0x0c:
                   17078:        case 0x0d:
                   17079:        case 0x0e:
                   17080:        case 0x0f:
                   17081:                // EOI
                   17082:                pic[0].isr &= ~(1 << (num - 0x08));
                   17083:                pic_update();
                   17084:                break;
1.1       root     17085:        case 0x10:
                   17086:                // PC BIOS - Video
1.1.1.14  root     17087:                if(!restore_console_on_exit) {
1.1.1.15  root     17088:                        change_console_size(scr_width, scr_height);
1.1       root     17089:                }
1.1.1.3   root     17090:                m_CF = 0;
1.1       root     17091:                switch(REG8(AH)) {
1.1.1.16  root     17092:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     17093:                case 0x01: pcbios_int_10h_01h(); break;
                   17094:                case 0x02: pcbios_int_10h_02h(); break;
                   17095:                case 0x03: pcbios_int_10h_03h(); break;
                   17096:                case 0x05: pcbios_int_10h_05h(); break;
                   17097:                case 0x06: pcbios_int_10h_06h(); break;
                   17098:                case 0x07: pcbios_int_10h_07h(); break;
                   17099:                case 0x08: pcbios_int_10h_08h(); break;
                   17100:                case 0x09: pcbios_int_10h_09h(); break;
                   17101:                case 0x0a: pcbios_int_10h_0ah(); break;
                   17102:                case 0x0b: break;
1.1.1.40  root     17103:                case 0x0c: pcbios_int_10h_0ch(); break;
                   17104:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     17105:                case 0x0e: pcbios_int_10h_0eh(); break;
                   17106:                case 0x0f: pcbios_int_10h_0fh(); break;
                   17107:                case 0x10: break;
1.1.1.14  root     17108:                case 0x11: pcbios_int_10h_11h(); break;
                   17109:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     17110:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     17111:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     17112:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     17113:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   17114:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     17115:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     17116:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   17117:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     17118:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     17119:                case 0x6f: break;
1.1.1.22  root     17120:                case 0x80: m_CF = 1; break; // unknown
                   17121:                case 0x81: m_CF = 1; break; // unknown
1.1       root     17122:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     17123:                case 0x83: pcbios_int_10h_83h(); break;
                   17124:                case 0x8b: break;
                   17125:                case 0x8c: m_CF = 1; break; // unknown
                   17126:                case 0x8d: m_CF = 1; break; // unknown
                   17127:                case 0x8e: m_CF = 1; break; // unknown
                   17128:                case 0x90: pcbios_int_10h_90h(); break;
                   17129:                case 0x91: pcbios_int_10h_91h(); break;
                   17130:                case 0x92: break;
                   17131:                case 0x93: break;
                   17132:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     17133:                case 0xfa: break; // ega register interface library is not installed
1.1       root     17134:                case 0xfe: pcbios_int_10h_feh(); break;
                   17135:                case 0xff: pcbios_int_10h_ffh(); break;
                   17136:                default:
1.1.1.22  root     17137:                        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));
                   17138:                        m_CF = 1;
1.1       root     17139:                        break;
                   17140:                }
                   17141:                break;
                   17142:        case 0x11:
                   17143:                // PC BIOS - Get Equipment List
1.1.1.26  root     17144:                REG16(AX) = msdos_get_equipment();
1.1       root     17145:                break;
                   17146:        case 0x12:
                   17147:                // PC BIOS - Get Memory Size
1.1.1.33  root     17148:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     17149:                break;
                   17150:        case 0x13:
1.1.1.42  root     17151:                // PC BIOS - Disk I/O
                   17152:                {
                   17153:                        static UINT8 last = 0x00;
                   17154:                        switch(REG8(AH)) {
                   17155:                        case 0x00: pcbios_int_13h_00h(); break;
                   17156:                        case 0x01: // get last status
                   17157:                                REG8(AH) = last;
                   17158:                                break;
                   17159:                        case 0x02: pcbios_int_13h_02h(); break;
                   17160:                        case 0x03: pcbios_int_13h_03h(); break;
                   17161:                        case 0x04: pcbios_int_13h_04h(); break;
                   17162:                        case 0x08: pcbios_int_13h_08h(); break;
                   17163:                        case 0x0a: pcbios_int_13h_02h(); break;
                   17164:                        case 0x0b: pcbios_int_13h_03h(); break;
                   17165:                        case 0x0d: pcbios_int_13h_00h(); break;
                   17166:                        case 0x10: pcbios_int_13h_10h(); break;
                   17167:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     17168:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     17169:                        case 0x05: // format
                   17170:                        case 0x06:
                   17171:                        case 0x07:
                   17172:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   17173:                                m_CF = 1;
                   17174:                                break;
                   17175:                        case 0x09:
                   17176:                        case 0x0c: // seek
                   17177:                        case 0x11: // recalib
                   17178:                        case 0x14:
                   17179:                        case 0x17:
                   17180:                                REG8(AH) = 0x00; // successful completion
                   17181:                                break;
1.1.1.43  root     17182:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   17183:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   17184:                                REG8(AH) = 0x01; // invalid function
                   17185:                                m_CF = 1;
                   17186:                                break;
1.1.1.42  root     17187:                        default:
                   17188:                                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));
                   17189:                                REG8(AH) = 0x01; // invalid function
                   17190:                                m_CF = 1;
                   17191:                                break;
                   17192:                        }
                   17193:                        last = REG8(AH);
                   17194:                }
1.1       root     17195:                break;
                   17196:        case 0x14:
                   17197:                // PC BIOS - Serial I/O
1.1.1.25  root     17198:                switch(REG8(AH)) {
                   17199:                case 0x00: pcbios_int_14h_00h(); break;
                   17200:                case 0x01: pcbios_int_14h_01h(); break;
                   17201:                case 0x02: pcbios_int_14h_02h(); break;
                   17202:                case 0x03: pcbios_int_14h_03h(); break;
                   17203:                case 0x04: pcbios_int_14h_04h(); break;
                   17204:                case 0x05: pcbios_int_14h_05h(); break;
                   17205:                default:
                   17206:                        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));
                   17207:                        break;
                   17208:                }
1.1       root     17209:                break;
                   17210:        case 0x15:
                   17211:                // PC BIOS
1.1.1.3   root     17212:                m_CF = 0;
1.1       root     17213:                switch(REG8(AH)) {
1.1.1.14  root     17214:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     17215:                case 0x23: pcbios_int_15h_23h(); break;
                   17216:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     17217:                case 0x41: break;
1.1       root     17218:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     17219:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     17220:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     17221:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     17222:                case 0x86: pcbios_int_15h_86h(); break;
                   17223:                case 0x87: pcbios_int_15h_87h(); break;
                   17224:                case 0x88: pcbios_int_15h_88h(); break;
                   17225:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     17226:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     17227:                case 0xc0: // PS/2 ???
1.1.1.54  root     17228: #ifndef EXT_BIOS_TOP
1.1.1.22  root     17229:                case 0xc1:
1.1.1.54  root     17230: #endif
1.1.1.30  root     17231:                case 0xc3: // PS50+ ???
                   17232:                case 0xc4:
1.1.1.22  root     17233:                        REG8(AH) = 0x86;
                   17234:                        m_CF = 1;
                   17235:                        break;
1.1.1.54  root     17236: #ifdef EXT_BIOS_TOP
                   17237:                case 0xc1: pcbios_int_15h_c1h(); break;
                   17238: #endif
                   17239:                case 0xc2: pcbios_int_15h_c2h(); break;
1.1.1.3   root     17240: #if defined(HAS_I386)
1.1       root     17241:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     17242: #endif
1.1       root     17243:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     17244:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     17245:                default:
1.1.1.22  root     17246:                        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));
                   17247:                        REG8(AH) = 0x86;
1.1.1.3   root     17248:                        m_CF = 1;
1.1       root     17249:                        break;
                   17250:                }
                   17251:                break;
                   17252:        case 0x16:
                   17253:                // PC BIOS - Keyboard
1.1.1.3   root     17254:                m_CF = 0;
1.1       root     17255:                switch(REG8(AH)) {
                   17256:                case 0x00: pcbios_int_16h_00h(); break;
                   17257:                case 0x01: pcbios_int_16h_01h(); break;
                   17258:                case 0x02: pcbios_int_16h_02h(); break;
                   17259:                case 0x03: pcbios_int_16h_03h(); break;
                   17260:                case 0x05: pcbios_int_16h_05h(); break;
1.1.1.60  root     17261:                case 0x09: pcbios_int_16h_09h(); break;
                   17262:                case 0x0a: pcbios_int_16h_0ah(); break;
1.1       root     17263:                case 0x10: pcbios_int_16h_00h(); break;
1.1.1.60  root     17264:                case 0x11: pcbios_int_16h_11h(); break;
1.1       root     17265:                case 0x12: pcbios_int_16h_12h(); break;
                   17266:                case 0x13: pcbios_int_16h_13h(); break;
                   17267:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     17268:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     17269:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     17270:                case 0xda: break; // unknown
1.1.1.43  root     17271:                case 0xdb: break; // unknown
1.1.1.22  root     17272:                case 0xff: break; // unknown
1.1       root     17273:                default:
1.1.1.22  root     17274:                        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     17275:                        break;
                   17276:                }
                   17277:                break;
                   17278:        case 0x17:
                   17279:                // PC BIOS - Printer
1.1.1.37  root     17280:                m_CF = 0;
                   17281:                switch(REG8(AH)) {
                   17282:                case 0x00: pcbios_int_17h_00h(); break;
                   17283:                case 0x01: pcbios_int_17h_01h(); break;
                   17284:                case 0x02: pcbios_int_17h_02h(); break;
                   17285:                case 0x03: pcbios_int_17h_03h(); break;
                   17286:                case 0x50: pcbios_int_17h_50h(); break;
                   17287:                case 0x51: pcbios_int_17h_51h(); break;
                   17288:                case 0x52: pcbios_int_17h_52h(); break;
                   17289:                case 0x84: pcbios_int_17h_84h(); break;
                   17290:                case 0x85: pcbios_int_17h_85h(); break;
                   17291:                default:
                   17292:                        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));
                   17293:                        break;
                   17294:                }
1.1       root     17295:                break;
                   17296:        case 0x1a:
                   17297:                // PC BIOS - Timer
1.1.1.3   root     17298:                m_CF = 0;
1.1       root     17299:                switch(REG8(AH)) {
                   17300:                case 0x00: pcbios_int_1ah_00h(); break;
                   17301:                case 0x01: break;
                   17302:                case 0x02: pcbios_int_1ah_02h(); break;
                   17303:                case 0x03: break;
                   17304:                case 0x04: pcbios_int_1ah_04h(); break;
                   17305:                case 0x05: break;
                   17306:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   17307:                case 0x0b: break;
1.1.1.14  root     17308:                case 0x35: break; // Word Perfect Third Party Interface?
                   17309:                case 0x36: break; // Word Perfect Third Party Interface
                   17310:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     17311:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     17312:                case 0xb1: break; // PCI BIOS v2.0c+
                   17313:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     17314:                default:
1.1.1.22  root     17315:                        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     17316:                        break;
                   17317:                }
                   17318:                break;
1.1.1.33  root     17319:        case 0x1b:
                   17320:                mem[0x471] = 0x00;
                   17321:                break;
1.1       root     17322:        case 0x20:
1.1.1.28  root     17323:                try {
                   17324:                        msdos_process_terminate(SREG(CS), retval, 1);
                   17325:                } catch(...) {
                   17326:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   17327:                }
1.1       root     17328:                break;
1.1.1.49  root     17329:        case 0x30:
1.1.1.46  root     17330:                // dummy interrupt for case map routine pointed in the country info
                   17331: //             if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
                   17332: //                     REG8(AL) = 0x00;
                   17333: //                     break;
                   17334: //             }
1.1       root     17335:        case 0x21:
                   17336:                // MS-DOS System Call
1.1.1.3   root     17337:                m_CF = 0;
1.1.1.28  root     17338:                try {
1.1.1.46  root     17339:                        switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28  root     17340:                        case 0x00: msdos_int_21h_00h(); break;
                   17341:                        case 0x01: msdos_int_21h_01h(); break;
                   17342:                        case 0x02: msdos_int_21h_02h(); break;
                   17343:                        case 0x03: msdos_int_21h_03h(); break;
                   17344:                        case 0x04: msdos_int_21h_04h(); break;
                   17345:                        case 0x05: msdos_int_21h_05h(); break;
                   17346:                        case 0x06: msdos_int_21h_06h(); break;
                   17347:                        case 0x07: msdos_int_21h_07h(); break;
                   17348:                        case 0x08: msdos_int_21h_08h(); break;
                   17349:                        case 0x09: msdos_int_21h_09h(); break;
                   17350:                        case 0x0a: msdos_int_21h_0ah(); break;
                   17351:                        case 0x0b: msdos_int_21h_0bh(); break;
                   17352:                        case 0x0c: msdos_int_21h_0ch(); break;
                   17353:                        case 0x0d: msdos_int_21h_0dh(); break;
                   17354:                        case 0x0e: msdos_int_21h_0eh(); break;
                   17355:                        case 0x0f: msdos_int_21h_0fh(); break;
                   17356:                        case 0x10: msdos_int_21h_10h(); break;
                   17357:                        case 0x11: msdos_int_21h_11h(); break;
                   17358:                        case 0x12: msdos_int_21h_12h(); break;
                   17359:                        case 0x13: msdos_int_21h_13h(); break;
                   17360:                        case 0x14: msdos_int_21h_14h(); break;
                   17361:                        case 0x15: msdos_int_21h_15h(); break;
                   17362:                        case 0x16: msdos_int_21h_16h(); break;
                   17363:                        case 0x17: msdos_int_21h_17h(); break;
                   17364:                        case 0x18: msdos_int_21h_18h(); break;
                   17365:                        case 0x19: msdos_int_21h_19h(); break;
                   17366:                        case 0x1a: msdos_int_21h_1ah(); break;
                   17367:                        case 0x1b: msdos_int_21h_1bh(); break;
                   17368:                        case 0x1c: msdos_int_21h_1ch(); break;
                   17369:                        case 0x1d: msdos_int_21h_1dh(); break;
                   17370:                        case 0x1e: msdos_int_21h_1eh(); break;
                   17371:                        case 0x1f: msdos_int_21h_1fh(); break;
                   17372:                        case 0x20: msdos_int_21h_20h(); break;
                   17373:                        case 0x21: msdos_int_21h_21h(); break;
                   17374:                        case 0x22: msdos_int_21h_22h(); break;
                   17375:                        case 0x23: msdos_int_21h_23h(); break;
                   17376:                        case 0x24: msdos_int_21h_24h(); break;
                   17377:                        case 0x25: msdos_int_21h_25h(); break;
                   17378:                        case 0x26: msdos_int_21h_26h(); break;
                   17379:                        case 0x27: msdos_int_21h_27h(); break;
                   17380:                        case 0x28: msdos_int_21h_28h(); break;
                   17381:                        case 0x29: msdos_int_21h_29h(); break;
                   17382:                        case 0x2a: msdos_int_21h_2ah(); break;
                   17383:                        case 0x2b: msdos_int_21h_2bh(); break;
                   17384:                        case 0x2c: msdos_int_21h_2ch(); break;
                   17385:                        case 0x2d: msdos_int_21h_2dh(); break;
                   17386:                        case 0x2e: msdos_int_21h_2eh(); break;
                   17387:                        case 0x2f: msdos_int_21h_2fh(); break;
                   17388:                        case 0x30: msdos_int_21h_30h(); break;
                   17389:                        case 0x31: msdos_int_21h_31h(); break;
                   17390:                        case 0x32: msdos_int_21h_32h(); break;
                   17391:                        case 0x33: msdos_int_21h_33h(); break;
                   17392:                        case 0x34: msdos_int_21h_34h(); break;
                   17393:                        case 0x35: msdos_int_21h_35h(); break;
                   17394:                        case 0x36: msdos_int_21h_36h(); break;
                   17395:                        case 0x37: msdos_int_21h_37h(); break;
                   17396:                        case 0x38: msdos_int_21h_38h(); break;
                   17397:                        case 0x39: msdos_int_21h_39h(0); break;
                   17398:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   17399:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   17400:                        case 0x3c: msdos_int_21h_3ch(); break;
                   17401:                        case 0x3d: msdos_int_21h_3dh(); break;
                   17402:                        case 0x3e: msdos_int_21h_3eh(); break;
                   17403:                        case 0x3f: msdos_int_21h_3fh(); break;
                   17404:                        case 0x40: msdos_int_21h_40h(); break;
                   17405:                        case 0x41: msdos_int_21h_41h(0); break;
                   17406:                        case 0x42: msdos_int_21h_42h(); break;
                   17407:                        case 0x43: msdos_int_21h_43h(0); break;
                   17408:                        case 0x44: msdos_int_21h_44h(); break;
                   17409:                        case 0x45: msdos_int_21h_45h(); break;
                   17410:                        case 0x46: msdos_int_21h_46h(); break;
                   17411:                        case 0x47: msdos_int_21h_47h(0); break;
                   17412:                        case 0x48: msdos_int_21h_48h(); break;
                   17413:                        case 0x49: msdos_int_21h_49h(); break;
                   17414:                        case 0x4a: msdos_int_21h_4ah(); break;
                   17415:                        case 0x4b: msdos_int_21h_4bh(); break;
                   17416:                        case 0x4c: msdos_int_21h_4ch(); break;
                   17417:                        case 0x4d: msdos_int_21h_4dh(); break;
                   17418:                        case 0x4e: msdos_int_21h_4eh(); break;
                   17419:                        case 0x4f: msdos_int_21h_4fh(); break;
                   17420:                        case 0x50: msdos_int_21h_50h(); break;
                   17421:                        case 0x51: msdos_int_21h_51h(); break;
                   17422:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     17423:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     17424:                        case 0x54: msdos_int_21h_54h(); break;
                   17425:                        case 0x55: msdos_int_21h_55h(); break;
                   17426:                        case 0x56: msdos_int_21h_56h(0); break;
                   17427:                        case 0x57: msdos_int_21h_57h(); break;
                   17428:                        case 0x58: msdos_int_21h_58h(); break;
                   17429:                        case 0x59: msdos_int_21h_59h(); break;
                   17430:                        case 0x5a: msdos_int_21h_5ah(); break;
                   17431:                        case 0x5b: msdos_int_21h_5bh(); break;
                   17432:                        case 0x5c: msdos_int_21h_5ch(); break;
                   17433:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     17434:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     17435:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     17436:                        case 0x60: msdos_int_21h_60h(0); break;
                   17437:                        case 0x61: msdos_int_21h_61h(); break;
                   17438:                        case 0x62: msdos_int_21h_62h(); break;
                   17439:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     17440:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     17441:                        case 0x65: msdos_int_21h_65h(); break;
                   17442:                        case 0x66: msdos_int_21h_66h(); break;
                   17443:                        case 0x67: msdos_int_21h_67h(); break;
                   17444:                        case 0x68: msdos_int_21h_68h(); break;
                   17445:                        case 0x69: msdos_int_21h_69h(); break;
                   17446:                        case 0x6a: msdos_int_21h_6ah(); break;
                   17447:                        case 0x6b: msdos_int_21h_6bh(); break;
                   17448:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.48  root     17449:                        case 0x6d: // Find First ROM Program
                   17450:                        case 0x6e: // Find Next ROM Program
                   17451:                        case 0x6f: // Get/Set ROM Scan Start Address
                   17452:                                REG8(AL) = 0x00; // if not supported (DOS <5, MS-DOS 5+ non-ROM versions)
                   17453:                                break;
1.1.1.43  root     17454:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.48  root     17455:                        case 0x71: // Windows95 - Long Filename Functions
1.1.1.28  root     17456:                                switch(REG8(AL)) {
                   17457:                                case 0x0d: msdos_int_21h_710dh(); break;
                   17458:                                case 0x39: msdos_int_21h_39h(1); break;
                   17459:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   17460:                                case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.48  root     17461:                                case 0x41: msdos_int_21h_7141h(); break;
1.1.1.28  root     17462:                                case 0x43: msdos_int_21h_43h(1); break;
                   17463:                                case 0x47: msdos_int_21h_47h(1); break;
                   17464:                                case 0x4e: msdos_int_21h_714eh(); break;
                   17465:                                case 0x4f: msdos_int_21h_714fh(); break;
                   17466:                                case 0x56: msdos_int_21h_56h(1); break;
                   17467:                                case 0x60: msdos_int_21h_60h(1); break;
                   17468:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   17469:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   17470:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   17471:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   17472:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   17473:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45  root     17474:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     17475:                                case 0xaa: msdos_int_21h_71aah(); break;
                   17476:                                default:
                   17477:                                        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));
                   17478:                                        REG16(AX) = 0x7100;
                   17479:                                        m_CF = 1;
                   17480:                                        break;
                   17481:                                }
                   17482:                                break;
1.1.1.48  root     17483:                        case 0x72: // Windows95 beta - LFN FindClose
                   17484: //                             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));
                   17485:                                REG16(AX) = 0x7200;
                   17486:                                m_CF = 1;
                   17487:                                break;
                   17488:                        case 0x73: // Windows95 - FAT32 Functions
1.1.1.28  root     17489:                                switch(REG8(AL)) {
                   17490:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     17491:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     17492:                                case 0x02: msdos_int_21h_7302h(); break;
                   17493:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     17494:                                // 0x04: Set DPB to Use for Formatting
                   17495:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     17496:                                default:
                   17497:                                        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));
                   17498:                                        REG16(AX) = 0x7300;
                   17499:                                        m_CF = 1;
                   17500:                                        break;
                   17501:                                }
1.1       root     17502:                                break;
1.1.1.30  root     17503:                        case 0xdb: msdos_int_21h_dbh(); break;
                   17504:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     17505:                        default:
1.1.1.22  root     17506:                                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     17507:                                REG16(AX) = 0x01;
1.1.1.3   root     17508:                                m_CF = 1;
1.1       root     17509:                                break;
                   17510:                        }
1.1.1.28  root     17511:                } catch(int error) {
                   17512:                        REG16(AX) = error;
                   17513:                        m_CF = 1;
                   17514:                } catch(...) {
                   17515:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     17516:                        m_CF = 1;
1.1       root     17517:                }
1.1.1.3   root     17518:                if(m_CF) {
1.1.1.23  root     17519:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     17520:                        sda->int21h_5d0ah_called = 0;
1.1.1.23  root     17521:                        sda->extended_error_code = REG16(AX);
                   17522:                        switch(sda->extended_error_code) {
                   17523:                        case  4: // Too many open files
                   17524:                        case  8: // Insufficient memory
                   17525:                                sda->error_class = 1; // Out of resource
                   17526:                                break;
                   17527:                        case  5: // Access denied
                   17528:                                sda->error_class = 3; // Authorization
                   17529:                                break;
                   17530:                        case  7: // Memory control block destroyed
                   17531:                                sda->error_class = 4; // Internal
                   17532:                                break;
                   17533:                        case  2: // File not found
                   17534:                        case  3: // Path not found
                   17535:                        case 15: // Invaid drive specified
                   17536:                        case 18: // No more files
                   17537:                                sda->error_class = 8; // Not found
                   17538:                                break;
                   17539:                        case 32: // Sharing violation
                   17540:                        case 33: // Lock violation
                   17541:                                sda->error_class = 10; // Locked
                   17542:                                break;
                   17543: //                     case 16: // Removal of current directory attempted
                   17544:                        case 19: // Attempted write on protected disk
                   17545:                        case 21: // Drive not ready
                   17546: //                     case 29: // Write failure
                   17547: //                     case 30: // Read failure
                   17548: //                     case 82: // Cannot create subdirectory
                   17549:                                sda->error_class = 11; // Media
                   17550:                                break;
                   17551:                        case 80: // File already exists
                   17552:                                sda->error_class = 12; // Already exist
                   17553:                                break;
                   17554:                        default:
                   17555:                                sda->error_class = 13; // Unknown
                   17556:                                break;
                   17557:                        }
                   17558:                        sda->suggested_action = 1; // Retry
                   17559:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     17560:                }
1.1.1.33  root     17561:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     17562:                        // raise int 23h
                   17563: #if defined(HAS_I386)
                   17564:                        m_ext = 0; // not an external interrupt
                   17565:                        i386_trap(0x23, 1, 0);
                   17566:                        m_ext = 1;
                   17567: #else
                   17568:                        PREFIX86(_interrupt)(0x23);
                   17569: #endif
                   17570:                }
1.1       root     17571:                break;
                   17572:        case 0x22:
                   17573:                fatalerror("int 22h (terminate address)\n");
                   17574:        case 0x23:
1.1.1.28  root     17575:                try {
                   17576:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   17577:                } catch(...) {
                   17578:                        fatalerror("failed to terminate the current process by int 23h\n");
                   17579:                }
1.1       root     17580:                break;
                   17581:        case 0x24:
1.1.1.32  root     17582: /*
1.1.1.28  root     17583:                try {
                   17584:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17585:                } catch(...) {
                   17586:                        fatalerror("failed to terminate the current process by int 24h\n");
                   17587:                }
1.1.1.32  root     17588: */
                   17589:                msdos_int_24h();
1.1       root     17590:                break;
                   17591:        case 0x25:
                   17592:                msdos_int_25h();
                   17593:                break;
                   17594:        case 0x26:
                   17595:                msdos_int_26h();
                   17596:                break;
                   17597:        case 0x27:
1.1.1.28  root     17598:                try {
                   17599:                        msdos_int_27h();
                   17600:                } catch(...) {
                   17601:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   17602:                }
1.1       root     17603:                break;
                   17604:        case 0x28:
                   17605:                Sleep(10);
1.1.1.35  root     17606:                REQUEST_HARDWRE_UPDATE();
1.1       root     17607:                break;
                   17608:        case 0x29:
                   17609:                msdos_int_29h();
                   17610:                break;
                   17611:        case 0x2e:
                   17612:                msdos_int_2eh();
                   17613:                break;
                   17614:        case 0x2f:
                   17615:                // multiplex interrupt
                   17616:                switch(REG8(AH)) {
1.1.1.22  root     17617:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     17618:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     17619:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     17620:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     17621:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     17622:                case 0x14: msdos_int_2fh_14h(); break;
                   17623:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     17624:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     17625:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     17626:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     17627:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     17628:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     17629:                case 0x46: msdos_int_2fh_46h(); break;
                   17630:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     17631:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     17632:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     17633:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     17634:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     17635:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     17636:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     17637:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     17638:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     17639:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     17640:                default:
1.1.1.30  root     17641:                        switch(REG8(AL)) {
                   17642:                        case 0x00:
                   17643:                                // This is not installed
                   17644: //                             REG8(AL) = 0x00;
                   17645:                                break;
1.1.1.33  root     17646:                        case 0x01:
1.1.1.42  root     17647:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   17648:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   17649:                                        break;
                   17650:                                }
1.1.1.33  root     17651:                                // Banyan VINES v4+ is not installed
                   17652:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   17653:                                        break;
                   17654:                                }
1.1.1.42  root     17655:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   17656:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   17657:                                        break;
                   17658:                                }
1.1.1.30  root     17659:                        default:
1.1.1.42  root     17660:                                // NORTON UTILITIES 5.0+
                   17661:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   17662:                                        break;
                   17663:                                }
1.1.1.30  root     17664:                                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     17665:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     17666:                                m_CF = 1;
                   17667:                                break;
                   17668:                        }
                   17669:                        break;
1.1       root     17670:                }
                   17671:                break;
1.1.1.24  root     17672:        case 0x33:
                   17673:                switch(REG8(AH)) {
                   17674:                case 0x00:
                   17675:                        // Mouse
1.1.1.49  root     17676:                        switch(REG16(AX)) {
                   17677:                        case 0x0000: msdos_int_33h_0000h(); break;
                   17678:                        case 0x0001: msdos_int_33h_0001h(); break;
                   17679:                        case 0x0002: msdos_int_33h_0002h(); break;
                   17680:                        case 0x0003: msdos_int_33h_0003h(); break;
                   17681:                        case 0x0004: msdos_int_33h_0004h(); break;
                   17682:                        case 0x0005: msdos_int_33h_0005h(); break;
                   17683:                        case 0x0006: msdos_int_33h_0006h(); break;
                   17684:                        case 0x0007: msdos_int_33h_0007h(); break;
                   17685:                        case 0x0008: msdos_int_33h_0008h(); break;
                   17686:                        case 0x0009: msdos_int_33h_0009h(); break;
                   17687:                        case 0x000a: msdos_int_33h_000ah(); break;
                   17688:                        case 0x000b: msdos_int_33h_000bh(); break;
                   17689:                        case 0x000c: msdos_int_33h_000ch(); break;
                   17690:                        case 0x000d: break; // MS MOUSE v1.0+ - Light Pen Emulation On
                   17691:                        case 0x000e: break; // MS MOUSE v1.0+ - Light Pen Emulation Off
                   17692:                        case 0x000f: msdos_int_33h_000fh(); break;
                   17693:                        case 0x0010: break; // MS MOUSE v1.0+ - Define Screen Region for Updating
                   17694:                        case 0x0011: msdos_int_33h_0011h(); break;
                   17695:                        case 0x0012: REG16(AX) = 0xffff; break; // MS MOUSE - Set Large Graphics Cursor Block
                   17696:                        case 0x0013: break; // MS MOUSE v5.0+ - Define Double-Speed Threshold
                   17697:                        case 0x0014: msdos_int_33h_0014h(); break;
                   17698:                        case 0x0015: msdos_int_33h_0015h(); break;
                   17699:                        case 0x0016: msdos_int_33h_0016h(); break;
                   17700:                        case 0x0017: msdos_int_33h_0017h(); break;
                   17701:                        case 0x0018: msdos_int_33h_0018h(); break;
                   17702:                        case 0x0019: msdos_int_33h_0019h(); break;
                   17703:                        case 0x001a: msdos_int_33h_001ah(); break;
                   17704:                        case 0x001b: msdos_int_33h_001bh(); break;
                   17705:                        case 0x001c: break; // MS MOUSE v6.0+ - Set Interrupt Rate
                   17706:                        case 0x001d: msdos_int_33h_001dh(); break;
                   17707:                        case 0x001e: msdos_int_33h_001eh(); break;
                   17708:                        case 0x001f: msdos_int_33h_001fh(); break;
                   17709:                        case 0x0020: msdos_int_33h_0020h(); break;
                   17710:                        case 0x0021: msdos_int_33h_0021h(); break;
                   17711:                        case 0x0022: msdos_int_33h_0022h(); break;
                   17712:                        case 0x0023: msdos_int_33h_0023h(); break;
                   17713:                        case 0x0024: msdos_int_33h_0024h(); break;
                   17714:                        case 0x0025: msdos_int_33h_0025h(); break;
                   17715:                        case 0x0026: msdos_int_33h_0026h(); break;
                   17716:                        case 0x0027: msdos_int_33h_0027h(); break;
                   17717:                        case 0x0028: msdos_int_33h_0028h(); break;
                   17718:                        case 0x0029: msdos_int_33h_0029h(); break;
                   17719:                        case 0x002a: msdos_int_33h_002ah(); break;
                   17720:                        // 0x002b: MS MOUSE v7.0+ - Load Acceleration Profiles
                   17721:                        // 0x002c: MS MOUSE v7.0+ - Get Acceleration Profiles
                   17722:                        // 0x002d: MS MOUSE v7.0+ - Select Acceleration Profile
                   17723:                        // 0x002e: MS MOUSE v8.10+ - Set Acceleration Profile Names
                   17724:                        case 0x002f: break; // Mouse Hardware Reset
                   17725:                        // 0x0030: MS MOUSE v7.04+ - Get/Set BallPoint Information
                   17726:                        case 0x0031: msdos_int_33h_0031h(); break;
                   17727:                        case 0x0032: msdos_int_33h_0032h(); break;
                   17728:                        // 0x0033: MS MOUSE v7.05+ - Get Switch Settings And Acceleration Profile Data
                   17729:                        // 0x0034: MS MOUSE v8.0+ - Get Initialization File
                   17730:                        // 0x0035: MS MOUSE v8.10+ - LCD Screen Large Pointer Support
                   17731:                        case 0x004d: msdos_int_33h_004dh(); break;
                   17732:                        case 0x006d: msdos_int_33h_006dh(); break;
1.1.1.24  root     17733:                        default:
                   17734:                                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));
                   17735:                                break;
                   17736:                        }
                   17737:                        break;
                   17738:                default:
                   17739:                        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));
                   17740:                        break;
                   17741:                }
                   17742:                break;
1.1.1.59  root     17743:        case 0x65:
1.1.1.19  root     17744:                // dummy interrupt for EMS (int 67h)
                   17745:                switch(REG8(AH)) {
                   17746:                case 0x40: msdos_int_67h_40h(); break;
                   17747:                case 0x41: msdos_int_67h_41h(); break;
                   17748:                case 0x42: msdos_int_67h_42h(); break;
                   17749:                case 0x43: msdos_int_67h_43h(); break;
                   17750:                case 0x44: msdos_int_67h_44h(); break;
                   17751:                case 0x45: msdos_int_67h_45h(); break;
                   17752:                case 0x46: msdos_int_67h_46h(); break;
                   17753:                case 0x47: msdos_int_67h_47h(); break;
                   17754:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     17755:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   17756:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     17757:                case 0x4b: msdos_int_67h_4bh(); break;
                   17758:                case 0x4c: msdos_int_67h_4ch(); break;
                   17759:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     17760:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     17761:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     17762:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     17763:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     17764:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     17765:                case 0x53: msdos_int_67h_53h(); break;
                   17766:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.49  root     17767:                case 0x55: msdos_int_67h_55h(); break;
                   17768:                case 0x56: msdos_int_67h_56h(); break;
1.1.1.20  root     17769:                case 0x57: msdos_int_67h_57h(); break;
                   17770:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     17771:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     17772:                case 0x5a: msdos_int_67h_5ah(); break;
1.1.1.49  root     17773:                case 0x5b: msdos_int_67h_5bh(); break;
1.1.1.43  root     17774:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   17775:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.49  root     17776:                case 0x70: msdos_int_67h_70h(); break;
1.1.1.31  root     17777:                // 0xde: VCPI
1.1.1.30  root     17778:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     17779:                default:
1.1.1.22  root     17780:                        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     17781:                        REG8(AH) = 0x84;
                   17782:                        break;
                   17783:                }
                   17784:                break;
                   17785: #ifdef SUPPORT_XMS
1.1.1.59  root     17786:        case 0x66:
1.1.1.19  root     17787:                // dummy interrupt for XMS (call far)
1.1.1.28  root     17788:                try {
                   17789:                        switch(REG8(AH)) {
                   17790:                        case 0x00: msdos_call_xms_00h(); break;
                   17791:                        case 0x01: msdos_call_xms_01h(); break;
                   17792:                        case 0x02: msdos_call_xms_02h(); break;
                   17793:                        case 0x03: msdos_call_xms_03h(); break;
                   17794:                        case 0x04: msdos_call_xms_04h(); break;
                   17795:                        case 0x05: msdos_call_xms_05h(); break;
                   17796:                        case 0x06: msdos_call_xms_06h(); break;
                   17797:                        case 0x07: msdos_call_xms_07h(); break;
                   17798:                        case 0x08: msdos_call_xms_08h(); break;
                   17799:                        case 0x09: msdos_call_xms_09h(); break;
                   17800:                        case 0x0a: msdos_call_xms_0ah(); break;
                   17801:                        case 0x0b: msdos_call_xms_0bh(); break;
                   17802:                        case 0x0c: msdos_call_xms_0ch(); break;
                   17803:                        case 0x0d: msdos_call_xms_0dh(); break;
                   17804:                        case 0x0e: msdos_call_xms_0eh(); break;
                   17805:                        case 0x0f: msdos_call_xms_0fh(); break;
                   17806:                        case 0x10: msdos_call_xms_10h(); break;
                   17807:                        case 0x11: msdos_call_xms_11h(); break;
                   17808:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     17809: #if defined(HAS_I386)
                   17810:                        case 0x88: msdos_call_xms_88h(); break;
                   17811:                        case 0x89: msdos_call_xms_89h(); break;
                   17812:                        case 0x8e: msdos_call_xms_8eh(); break;
                   17813:                        case 0x8f: msdos_call_xms_8fh(); break;
                   17814: #endif
1.1.1.28  root     17815:                        default:
                   17816:                                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));
                   17817:                                REG16(AX) = 0x0000;
                   17818:                                REG8(BL) = 0x80; // function not implemented
                   17819:                                break;
                   17820:                        }
                   17821:                } catch(...) {
1.1.1.19  root     17822:                        REG16(AX) = 0x0000;
1.1.1.28  root     17823:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     17824:                }
                   17825:                break;
                   17826: #endif
1.1.1.59  root     17827: /*
                   17828:        case 0x67:
                   17829:                // int 67h handler is in EMS device driver (EMMXXXX0) and it calls int 65h
                   17830:                // NOTE: some softwares get address of int 67h handler and recognize the address is in EMS device driver
                   17831:                break;
                   17832: */
                   17833:        case 0x69:
1.1.1.24  root     17834:                // irq12 (mouse)
                   17835:                mouse_push_ax = REG16(AX);
                   17836:                mouse_push_bx = REG16(BX);
                   17837:                mouse_push_cx = REG16(CX);
                   17838:                mouse_push_dx = REG16(DX);
                   17839:                mouse_push_si = REG16(SI);
                   17840:                mouse_push_di = REG16(DI);
                   17841:                
1.1.1.43  root     17842:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     17843:                        REG16(AX) = mouse.status_irq;
                   17844:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     17845:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17846:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     17847:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17848:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17849:                        
1.1.1.49  root     17850:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17851:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr.w.l & 0xff;
                   17852:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr.w.l >> 8;
                   17853:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr.w.h & 0xff;
                   17854:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.59  root     17855:                        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17856:                        mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.43  root     17857:                        break;
1.1.1.24  root     17858:                }
1.1.1.43  root     17859:                for(int i = 0; i < 8; i++) {
                   17860:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17861:                                REG16(AX) = mouse.status_irq_alt;
                   17862:                                REG16(BX) = mouse.get_buttons();
                   17863:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17864:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   17865:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17866:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17867:                                
1.1.1.49  root     17868:                                mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17869:                                mem[DUMMY_TOP + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   17870:                                mem[DUMMY_TOP + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   17871:                                mem[DUMMY_TOP + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   17872:                                mem[DUMMY_TOP + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
1.1.1.59  root     17873:                                mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17874:                                mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.43  root     17875:                                break;
                   17876:                        }
                   17877:                }
1.1.1.59  root     17878:                if(mouse.status_irq_ps2 && mouse.call_addr_ps2.dw && mouse.enabled_ps2) {
1.1.1.54  root     17879:                        UINT16 data_1st, data_2nd, data_3rd;
                   17880:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   17881:                        i386_push16(data_1st);
                   17882:                        i386_push16(data_2nd);
                   17883:                        i386_push16(data_3rd);
                   17884:                        i386_push16(0x0000);
                   17885:                        
                   17886:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17887:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr_ps2.w.l & 0xff;
                   17888:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr_ps2.w.l >> 8;
                   17889:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr_ps2.w.h & 0xff;
                   17890:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr_ps2.w.h >> 8;
1.1.1.59  root     17891:                        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6ah (dummy)
                   17892:                        mem[DUMMY_TOP + 0x08] = 0x6a;
1.1.1.54  root     17893:                        break;
                   17894:                }
1.1.1.43  root     17895:                // invalid call addr :-(
1.1.1.49  root     17896:                mem[DUMMY_TOP + 0x02] = 0x90;   // nop
                   17897:                mem[DUMMY_TOP + 0x03] = 0x90;   // nop
                   17898:                mem[DUMMY_TOP + 0x04] = 0x90;   // nop
                   17899:                mem[DUMMY_TOP + 0x05] = 0x90;   // nop
                   17900:                mem[DUMMY_TOP + 0x06] = 0x90;   // nop
1.1.1.59  root     17901:                mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17902:                mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.24  root     17903:                break;
1.1.1.59  root     17904:        case 0x6a:
                   17905:                // end of ps/2 mouse bios
                   17906:                i386_pop16();
                   17907:                i386_pop16();
                   17908:                i386_pop16();
                   17909:                i386_pop16();
1.1.1.24  root     17910:        case 0x6b:
                   17911:                // end of irq12 (mouse)
                   17912:                REG16(AX) = mouse_push_ax;
                   17913:                REG16(BX) = mouse_push_bx;
                   17914:                REG16(CX) = mouse_push_cx;
                   17915:                REG16(DX) = mouse_push_dx;
                   17916:                REG16(SI) = mouse_push_si;
                   17917:                REG16(DI) = mouse_push_di;
                   17918:                
                   17919:                // EOI
                   17920:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   17921:                        pic[0].isr &= ~(1 << 2); // master
                   17922:                }
                   17923:                pic_update();
                   17924:                break;
                   17925:        case 0x6c:
1.1.1.19  root     17926:                // dummy interrupt for case map routine pointed in the country info
                   17927:                if(REG8(AL) >= 0x80) {
                   17928:                        char tmp[2] = {0};
                   17929:                        tmp[0] = REG8(AL);
                   17930:                        my_strupr(tmp);
                   17931:                        REG8(AL) = tmp[0];
                   17932:                }
                   17933:                break;
1.1.1.27  root     17934:        case 0x6d:
                   17935:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   17936:                REG8(AL) = 0x86; // not supported
                   17937:                m_CF = 1;
                   17938:                break;
1.1.1.32  root     17939:        case 0x6e:
                   17940:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   17941:                {
                   17942:                        UINT16 code = REG16(AX);
                   17943:                        if(code & 0xf0) {
                   17944:                                code = (code & 7) | ((code & 0x10) >> 1);
                   17945:                        }
                   17946:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   17947:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   17948:                                        const char *message = NULL;
                   17949:                                        if(active_code_page == 932) {
                   17950:                                                message = param_error_table[i].message_japanese;
                   17951:                                        }
                   17952:                                        if(message == NULL) {
                   17953:                                                message = param_error_table[i].message_english;
                   17954:                                        }
                   17955:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   17956:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   17957:                                        
                   17958:                                        SREG(ES) = WORK_TOP >> 4;
                   17959:                                        i386_load_segment_descriptor(ES);
                   17960:                                        REG16(DI) = 0x0000;
                   17961:                                        break;
                   17962:                                }
                   17963:                        }
                   17964:                }
                   17965:                break;
1.1.1.49  root     17966:        case 0x6f:
                   17967:                // dummy interrupt for end of alter page map and call
                   17968:                {
                   17969:                        UINT16 handles[4], pages[4];
                   17970:                        
                   17971:                        // pop old mapping data in new mapping
                   17972:                        for(int i = 0; i < 4; i++) {
                   17973:                                pages  [3 - i] = i386_pop16();
                   17974:                                handles[3 - i] = i386_pop16();
                   17975:                        }
                   17976:                        
                   17977:                        // restore old mapping
                   17978:                        for(int i = 0; i < 4; i++) {
                   17979:                                UINT16 handle = handles[i];
                   17980:                                UINT16 page   = pages  [i];
                   17981:                                
                   17982:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   17983:                                        ems_map_page(i, handle, page);
                   17984:                                } else {
                   17985:                                        ems_unmap_page(i);
                   17986:                                }
                   17987:                        }
                   17988:                        // do ret_far (pop cs/ip) in old mapping
                   17989:                }
                   17990:                break;
1.1.1.8   root     17991:        case 0x70:
                   17992:        case 0x71:
                   17993:        case 0x72:
                   17994:        case 0x73:
                   17995:        case 0x74:
                   17996:        case 0x75:
                   17997:        case 0x76:
                   17998:        case 0x77:
                   17999:                // EOI
                   18000:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   18001:                        pic[0].isr &= ~(1 << 2); // master
                   18002:                }
                   18003:                pic_update();
                   18004:                break;
1.1       root     18005:        default:
1.1.1.22  root     18006: //             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     18007:                break;
                   18008:        }
                   18009:        
                   18010:        // update cursor position
                   18011:        if(cursor_moved) {
1.1.1.36  root     18012:                pcbios_update_cursor_position();
1.1       root     18013:                cursor_moved = false;
                   18014:        }
                   18015: }
                   18016: 
                   18017: // init
                   18018: 
                   18019: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   18020: {
                   18021:        // init file handler
                   18022:        memset(file_handler, 0, sizeof(file_handler));
                   18023:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   18024:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   18025:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     18026: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     18027:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     18028: #else
                   18029:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   18030: #endif
                   18031:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     18032:        }
1.1.1.21  root     18033:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45  root     18034: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   18035:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     18036:        }
1.1       root     18037:        _dup2(0, DUP_STDIN);
                   18038:        _dup2(1, DUP_STDOUT);
                   18039:        _dup2(2, DUP_STDERR);
1.1.1.21  root     18040:        _dup2(3, DUP_STDAUX);
                   18041:        _dup2(4, DUP_STDPRN);
1.1       root     18042:        
1.1.1.24  root     18043:        // init mouse
                   18044:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     18045:        mouse.enabled = true;   // from DOSBox
                   18046:        mouse.hidden = 1;       // hidden in default ???
                   18047:        mouse.old_hidden = 1;   // from DOSBox
                   18048:        mouse.max_position.x = 8 * (scr_width  - 1);
                   18049:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     18050:        mouse.mickey.x = 8;
                   18051:        mouse.mickey.y = 16;
                   18052:        
1.1.1.26  root     18053: #ifdef SUPPORT_XMS
                   18054:        // init xms
                   18055:        msdos_xms_init();
                   18056: #endif
                   18057:        
1.1       root     18058:        // init process
                   18059:        memset(process, 0, sizeof(process));
                   18060:        
1.1.1.13  root     18061:        // init dtainfo
                   18062:        msdos_dta_info_init();
                   18063:        
1.1       root     18064:        // init memory
                   18065:        memset(mem, 0, sizeof(mem));
                   18066:        
                   18067:        // bios data area
1.1.1.23  root     18068:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     18069:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   18070:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.58  root     18071: //     CONSOLE_FONT_INFO cfi;
1.1.1.57  root     18072: //     GetCurrentConsoleFont(hStdout, FALSE, &cfi);
1.1.1.14  root     18073:        
                   18074:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   18075:        text_vram_top_address = TEXT_VRAM_TOP;
                   18076:        text_vram_end_address = text_vram_top_address + regen;
                   18077:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   18078:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     18079:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1.1.14  root     18080:        
                   18081:        if(regen > 0x4000) {
                   18082:                regen = 0x8000;
                   18083:                vram_pages = 1;
                   18084:        } else if(regen > 0x2000) {
                   18085:                regen = 0x4000;
                   18086:                vram_pages = 2;
                   18087:        } else if(regen > 0x1000) {
                   18088:                regen = 0x2000;
                   18089:                vram_pages = 4;
                   18090:        } else {
                   18091:                regen = 0x1000;
                   18092:                vram_pages = 8;
                   18093:        }
1.1       root     18094:        
1.1.1.25  root     18095:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   18096:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     18097:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   18098:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     18099:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     18100:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   18101:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     18102: #ifdef EXT_BIOS_TOP
1.1.1.25  root     18103:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     18104: #endif
1.1.1.26  root     18105:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     18106:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     18107:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   18108:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     18109:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     18110:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   18111:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     18112:        *(UINT16 *)(mem + 0x44e) = 0;
                   18113:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     18114:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     18115:        *(UINT8  *)(mem + 0x460) = 7;
                   18116:        *(UINT8  *)(mem + 0x461) = 7;
                   18117:        *(UINT8  *)(mem + 0x462) = 0;
                   18118:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     18119:        *(UINT8  *)(mem + 0x465) = 0x09;
                   18120:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     18121:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     18122:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   18123:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     18124:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
1.1.1.58  root     18125:        *(UINT16 *)(mem + 0x485) = font_height;
1.1.1.14  root     18126:        *(UINT8  *)(mem + 0x487) = 0x60;
                   18127:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     18128: #ifdef EXT_BIOS_TOP
1.1.1.25  root     18129:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     18130: #endif
1.1.1.14  root     18131:        
                   18132:        // initial screen
                   18133:        SMALL_RECT rect;
                   18134:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     18135:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     18136:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   18137:                for(int x = 0; x < scr_width; x++) {
                   18138:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   18139:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   18140:                }
                   18141:        }
1.1       root     18142:        
1.1.1.19  root     18143:        // init mcb
1.1       root     18144:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     18145:        
                   18146:        // iret table
                   18147:        // note: int 2eh vector should address the routine in command.com,
                   18148:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   18149:        // so move iret table into allocated memory block
                   18150:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.58  root     18151:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, (IRET_SIZE + 5 * 128) >> 4);
1.1.1.19  root     18152:        IRET_TOP = seg << 4;
1.1.1.58  root     18153:        seg += (IRET_SIZE + 5 * 128) >> 4;
1.1.1.25  root     18154:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     18155:        
1.1.1.58  root     18156:        // note: SO1 checks int 21h vector and if it aims iret (cfh)
                   18157:        // it is recognized SO1 is not running on MS-DOS environment
                   18158:        for(int i = 0; i < 128; i++) {
                   18159:                // jmp far (IRET_TOP >> 4):(interrupt number)
                   18160:                *(UINT8  *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 0) = 0xea;
                   18161:                *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 1) = i;
                   18162:                *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 3) = IRET_TOP >> 4;
                   18163:        }
                   18164:        
1.1.1.19  root     18165:        // dummy xms/ems device
1.1.1.33  root     18166:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     18167:        XMS_TOP = seg << 4;
                   18168:        seg += XMS_SIZE >> 4;
                   18169:        
                   18170:        // environment
1.1.1.33  root     18171:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     18172:        int env_seg = seg;
                   18173:        int ofs = 0;
1.1.1.32  root     18174:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   18175:        char comspec_added = 0;
1.1.1.33  root     18176:        char lastdrive_added = 0;
1.1.1.32  root     18177:        char env_msdos_path[ENV_SIZE] = {0};
                   18178:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     18179:        char prompt_added = 0;
1.1.1.32  root     18180:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     18181:        char tz_added = 0;
1.1.1.45  root     18182:        const char *path, *short_path;
1.1.1.32  root     18183:        
                   18184:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   18185:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18186:                        strcpy(env_append, short_path);
                   18187:                }
                   18188:        }
                   18189:        if((path = getenv("APPEND")) != NULL) {
                   18190:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18191:                        if(env_append[0] != '\0') {
                   18192:                                strcat(env_append, ";");
                   18193:                        }
                   18194:                        strcat(env_append, short_path);
                   18195:                }
                   18196:        }
                   18197:        
                   18198:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   18199:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18200:                        strcpy(comspec_path, short_path);
                   18201:                }
                   18202:        }
                   18203:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   18204:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18205:                        strcpy(comspec_path, short_path);
                   18206:                }
                   18207:        }
1.1       root     18208:        
1.1.1.28  root     18209:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     18210:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18211:                        strcpy(env_msdos_path, short_path);
                   18212:                        strcpy(env_path, short_path);
1.1.1.14  root     18213:                }
                   18214:        }
1.1.1.28  root     18215:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     18216:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18217:                        if(env_path[0] != '\0') {
                   18218:                                strcat(env_path, ";");
                   18219:                        }
                   18220:                        strcat(env_path, short_path);
1.1.1.9   root     18221:                }
                   18222:        }
1.1.1.32  root     18223:        
1.1.1.60  root     18224:        if(GetTempPathA(ENV_SIZE, env_temp) != 0) {
1.1.1.32  root     18225:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     18226:        }
1.1.1.32  root     18227:        for(int i = 0; i < 4; i++) {
                   18228:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   18229:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   18230:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18231:                                strcpy(env_temp, short_path);
                   18232:                                break;
                   18233:                        }
                   18234:                }
1.1.1.24  root     18235:        }
1.1.1.32  root     18236:        
1.1.1.9   root     18237:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     18238:                // lower to upper
1.1.1.28  root     18239:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     18240:                strcpy(tmp, *p);
                   18241:                for(int i = 0;; i++) {
                   18242:                        if(tmp[i] == '=') {
                   18243:                                tmp[i] = '\0';
                   18244:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     18245:                                my_strupr(name);
1.1       root     18246:                                tmp[i] = '=';
                   18247:                                break;
                   18248:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     18249:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     18250:                        }
                   18251:                }
1.1.1.33  root     18252:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   18253:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   18254:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     18255:                        // ignore non standard environments
                   18256:                } else {
1.1.1.33  root     18257:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     18258:                                if(env_append[0] != '\0') {
                   18259:                                        sprintf(tmp, "APPEND=%s", env_append);
                   18260:                                } else {
                   18261:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   18262:                                }
                   18263:                                append_added = 1;
                   18264:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     18265:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     18266:                                comspec_added = 1;
1.1.1.33  root     18267:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   18268:                                char *env = getenv("MSDOS_LASTDRIVE");
                   18269:                                if(env != NULL) {
                   18270:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   18271:                                }
                   18272:                                lastdrive_added = 1;
                   18273:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     18274:                                if(env_msdos_path[0] != '\0') {
                   18275:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   18276:                                } else {
                   18277:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   18278:                                }
1.1.1.33  root     18279:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     18280:                                if(env_path[0] != '\0') {
                   18281:                                        sprintf(tmp, "PATH=%s", env_path);
                   18282:                                } else {
                   18283:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   18284:                                }
1.1.1.32  root     18285:                                path_added = 1;
1.1.1.33  root     18286:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   18287:                                prompt_added = 1;
1.1.1.28  root     18288:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   18289:                                if(env_temp[0] != '\0') {
                   18290:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   18291:                                } else {
                   18292:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   18293:                                }
1.1.1.32  root     18294:                                temp_added = 1;
1.1.1.33  root     18295:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     18296:                                if(env_temp[0] != '\0') {
                   18297:                                        sprintf(tmp, "TMP=%s", env_temp);
                   18298:                                } else {
                   18299:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     18300:                                }
1.1.1.32  root     18301:                                tmp_added = 1;
1.1.1.33  root     18302:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   18303:                                char *env = getenv("MSDOS_TZ");
                   18304:                                if(env != NULL) {
                   18305:                                        sprintf(tmp, "TZ=%s", env);
                   18306:                                }
                   18307:                                tz_added = 1;
1.1       root     18308:                        }
                   18309:                        int len = strlen(tmp);
1.1.1.14  root     18310:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     18311:                                fatalerror("too many environments\n");
                   18312:                        }
                   18313:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   18314:                        ofs += len + 1;
                   18315:                }
                   18316:        }
1.1.1.32  root     18317:        if(!append_added && env_append[0] != '\0') {
                   18318:                #define SET_ENV(name, value) { \
                   18319:                        char tmp[ENV_SIZE]; \
                   18320:                        sprintf(tmp, "%s=%s", name, value); \
                   18321:                        int len = strlen(tmp); \
                   18322:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   18323:                                fatalerror("too many environments\n"); \
                   18324:                        } \
                   18325:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   18326:                        ofs += len + 1; \
                   18327:                }
                   18328:                SET_ENV("APPEND", env_append);
                   18329:        }
                   18330:        if(!comspec_added) {
                   18331:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   18332:        }
1.1.1.33  root     18333:        if(!lastdrive_added) {
                   18334:                SET_ENV("LASTDRIVE", "Z");
                   18335:        }
1.1.1.32  root     18336:        if(!path_added) {
                   18337:                SET_ENV("PATH", env_path);
                   18338:        }
1.1.1.33  root     18339:        if(!prompt_added) {
                   18340:                SET_ENV("PROMPT", "$P$G");
                   18341:        }
1.1.1.32  root     18342:        if(!temp_added) {
                   18343:                SET_ENV("TEMP", env_temp);
                   18344:        }
                   18345:        if(!tmp_added) {
                   18346:                SET_ENV("TMP", env_temp);
                   18347:        }
1.1.1.33  root     18348:        if(!tz_added) {
                   18349:                TIME_ZONE_INFORMATION tzi;
                   18350:                HKEY hKey, hSubKey;
                   18351:                char tzi_std_name[64];
                   18352:                char tz_std[8] = "GMT";
                   18353:                char tz_dlt[8] = "GST";
                   18354:                char tz_value[32];
                   18355:                
                   18356:                // timezone name from GetTimeZoneInformation may not be english
                   18357:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   18358:                setlocale(LC_CTYPE, "");
                   18359:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   18360:                
                   18361:                // get english timezone name from registry
1.1.1.60  root     18362:                if(RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
1.1.1.33  root     18363:                        for(DWORD i = 0; !tz_added; i++) {
                   18364:                                char reg_name[256], sub_key[1024], std_name[256];
                   18365:                                DWORD size;
                   18366:                                FILETIME ftTime;
1.1.1.60  root     18367:                                LONG result = RegEnumKeyExA(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
1.1.1.33  root     18368:                                
                   18369:                                if(result == ERROR_SUCCESS) {
                   18370:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
1.1.1.60  root     18371:                                        if(RegOpenKeyExA(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   18372:                                                if(RegQueryValueExA(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
1.1.1.33  root     18373:                                                        // search english timezone name from table
1.1.1.37  root     18374:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     18375:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   18376:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   18377:                                                                                if(tz_table[j].std != NULL) {
                   18378:                                                                                        strcpy(tz_std, tz_table[j].std);
                   18379:                                                                                }
                   18380:                                                                                if(tz_table[j].dlt != NULL) {
                   18381:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   18382:                                                                                }
                   18383:                                                                                tz_added = 1;
                   18384:                                                                                break;
                   18385:                                                                        }
                   18386:                                                                }
                   18387:                                                        }
                   18388:                                                }
                   18389:                                                RegCloseKey(hSubKey);
                   18390:                                        }
                   18391:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   18392:                                        break;
                   18393:                                }
                   18394:                        }
                   18395:                        RegCloseKey(hKey);
                   18396:                }
                   18397:                if((tzi.Bias % 60) != 0) {
                   18398:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   18399:                } else {
                   18400:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   18401:                }
                   18402:                if(daylight) {
                   18403:                        strcat(tz_value, tz_dlt);
                   18404:                }
                   18405:                SET_ENV("TZ", tz_value);
                   18406:        }
1.1       root     18407:        seg += (ENV_SIZE >> 4);
                   18408:        
                   18409:        // psp
1.1.1.33  root     18410:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     18411:        current_psp = seg;
1.1.1.35  root     18412:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   18413:        psp->parent_psp = current_psp;
1.1       root     18414:        seg += (PSP_SIZE >> 4);
                   18415:        
1.1.1.19  root     18416:        // first free mcb in conventional memory
1.1.1.33  root     18417:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     18418:        first_mcb = seg;
                   18419:        
1.1.1.19  root     18420:        // dummy mcb to link to umb
1.1.1.33  root     18421: #if 0
1.1.1.39  root     18422:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     18423: #else
1.1.1.39  root     18424:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     18425: #endif
1.1.1.19  root     18426:        
                   18427:        // first free mcb in upper memory block
1.1.1.8   root     18428:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     18429:        
1.1.1.29  root     18430: #ifdef SUPPORT_HMA
                   18431:        // first free mcb in high memory area
                   18432:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   18433: #endif
                   18434:        
1.1.1.26  root     18435:        // interrupt vector
1.1.1.58  root     18436:        for(int i = 0; i < 256; i++) {
                   18437:                // 00-07: CPU exception handler
                   18438:                // 08-0F: IRQ 0-7
                   18439:                // 10-1F: PC BIOS
                   18440:                // 20-3F: MS-DOS system call
                   18441:                // 70-77: IRQ 8-15
                   18442:                *(UINT16 *)(mem + 4 * i + 0) = (i <= 0x3f || (i >= 0x70 && i <= 0x77)) ? (IRET_SIZE + 5 * i) : i;
1.1.1.26  root     18443:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   18444:        }
1.1.1.49  root     18445:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffc:0018 irq0 (system timer)
                   18446:        *(UINT16 *)(mem + 4 * 0x08 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     18447:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   18448:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   18449:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   18450:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
1.1.1.49  root     18451:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffc:0000 irq12 (mouse)
                   18452:        *(UINT16 *)(mem + 4 * 0x74 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     18453:        
1.1.1.29  root     18454:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     18455:        static const struct {
                   18456:                UINT16 attributes;
                   18457:                char *dev_name;
                   18458:        } dummy_devices[] = {
                   18459:                {0x8013, "CON     "},
                   18460:                {0x8000, "AUX     "},
                   18461:                {0xa0c0, "PRN     "},
                   18462:                {0x8008, "CLOCK$  "},
                   18463:                {0x8000, "COM1    "},
                   18464:                {0xa0c0, "LPT1    "},
                   18465:                {0xa0c0, "LPT2    "},
                   18466:                {0xa0c0, "LPT3    "},
                   18467:                {0x8000, "COM2    "},
                   18468:                {0x8000, "COM3    "},
                   18469:                {0x8000, "COM4    "},
1.1.1.30  root     18470: //             {0xc000, "CONFIG$ "},
                   18471:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     18472:        };
                   18473:        static const UINT8 dummy_device_routine[] = {
                   18474:                // from NUL device of Windows 98 SE
                   18475:                // or word ptr ES:[BX+03],0100
                   18476:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   18477:                // retf
                   18478:                0xcb,
                   18479:        };
1.1.1.29  root     18480:        device_t *last = NULL;
1.1.1.32  root     18481:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     18482:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     18483:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   18484:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     18485:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     18486:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   18487:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     18488:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     18489:                last = device;
                   18490:        }
                   18491:        if(last != NULL) {
                   18492:                last->next_driver.w.l = 0;
                   18493:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     18494:        }
1.1.1.29  root     18495:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     18496:        
1.1.1.25  root     18497:        // dos info
                   18498:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   18499:        dos_info->magic_word = 1;
                   18500:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   18501:        dos_info->first_dpb.w.l = 0;
                   18502:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   18503:        dos_info->first_sft.w.l = 0;
                   18504:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     18505:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     18506:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     18507:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     18508:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   18509:        dos_info->max_sector_len = 512;
                   18510:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   18511:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   18512:        dos_info->cds.w.l = 0;
                   18513:        dos_info->cds.w.h = CDS_TOP >> 4;
                   18514:        dos_info->fcb_table.w.l = 0;
                   18515:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   18516:        dos_info->last_drive = 'Z' - 'A' + 1;
                   18517:        dos_info->buffers_x = 20;
                   18518:        dos_info->buffers_y = 0;
                   18519:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     18520:        dos_info->nul_device.next_driver.w.l = 22;
                   18521:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     18522:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     18523:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   18524:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18525:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   18526:        dos_info->disk_buf_heads.w.l = 0;
                   18527:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     18528:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     18529:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   18530:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     18531:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     18532:        
                   18533:        char *env;
                   18534:        if((env = getenv("LASTDRIVE")) != NULL) {
                   18535:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18536:                        dos_info->last_drive = env[0] - 'A' + 1;
                   18537:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18538:                        dos_info->last_drive = env[0] - 'a' + 1;
                   18539:                }
                   18540:        }
                   18541:        if((env = getenv("windir")) != NULL) {
                   18542:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18543:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   18544:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18545:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   18546:                }
                   18547:        }
                   18548: #if defined(HAS_I386)
                   18549:        dos_info->i386_or_later = 1;
                   18550: #else
                   18551:        dos_info->i386_or_later = 0;
                   18552: #endif
                   18553:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   18554:        
1.1.1.27  root     18555:        // ems (int 67h) and xms
1.1.1.25  root     18556:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   18557:        xms_device->next_driver.w.l = 0xffff;
                   18558:        xms_device->next_driver.w.h = 0xffff;
                   18559:        xms_device->attributes = 0xc000;
1.1.1.29  root     18560:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   18561:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18562:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   18563:        
1.1.1.59  root     18564:        mem[XMS_TOP + 0x12] = 0xcd;     // int 65h (dummy)
                   18565:        mem[XMS_TOP + 0x13] = 0x65;
1.1.1.26  root     18566:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     18567: #ifdef SUPPORT_XMS
                   18568:        if(support_xms) {
1.1.1.59  root     18569:                mem[XMS_TOP + 0x15] = 0xcd;     // int 66h (dummy)
                   18570:                mem[XMS_TOP + 0x16] = 0x66;
1.1.1.26  root     18571:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     18572:        } else
                   18573: #endif
1.1.1.26  root     18574:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     18575:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     18576:        
1.1.1.26  root     18577:        // irq12 routine (mouse)
1.1.1.59  root     18578:        mem[DUMMY_TOP + 0x00] = 0xcd;   // int 69h (dummy)
                   18579:        mem[DUMMY_TOP + 0x01] = 0x69;
1.1.1.49  root     18580:        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far mouse
                   18581:        mem[DUMMY_TOP + 0x03] = 0xff;
                   18582:        mem[DUMMY_TOP + 0x04] = 0xff;
                   18583:        mem[DUMMY_TOP + 0x05] = 0xff;
                   18584:        mem[DUMMY_TOP + 0x06] = 0xff;
1.1.1.59  root     18585: //     mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6ah (dummy)
                   18586: //     mem[DUMMY_TOP + 0x08] = 0x6a;
1.1.1.49  root     18587:        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   18588:        mem[DUMMY_TOP + 0x08] = 0x6b;
                   18589:        mem[DUMMY_TOP + 0x09] = 0xcf;   // iret
1.1.1.24  root     18590:        
1.1.1.27  root     18591:        // case map routine
1.1.1.49  root     18592:        mem[DUMMY_TOP + 0x0a] = 0xcd;   // int 6ch (dummy)
                   18593:        mem[DUMMY_TOP + 0x0b] = 0x6c;
                   18594:        mem[DUMMY_TOP + 0x0c] = 0xcb;   // retf
1.1.1.27  root     18595:        
                   18596:        // font read routine
1.1.1.49  root     18597:        mem[DUMMY_TOP + 0x0d] = 0xcd;   // int 6dh (dummy)
                   18598:        mem[DUMMY_TOP + 0x0e] = 0x6d;
                   18599:        mem[DUMMY_TOP + 0x0f] = 0xcb;   // retf
1.1.1.19  root     18600:        
1.1.1.32  root     18601:        // error message read routine
1.1.1.49  root     18602:        mem[DUMMY_TOP + 0x10] = 0xcd;   // int 6eh (dummy)
                   18603:        mem[DUMMY_TOP + 0x11] = 0x6e;
                   18604:        mem[DUMMY_TOP + 0x12] = 0xcb;   // retf
1.1.1.32  root     18605:        
1.1.1.35  root     18606:        // dummy loop to wait BIOS/DOS service is done
1.1.1.49  root     18607:        mem[DUMMY_TOP + 0x13] = 0xe6;   // out f7h, al
                   18608:        mem[DUMMY_TOP + 0x14] = 0xf7;
                   18609:        mem[DUMMY_TOP + 0x15] = 0x78;   // js/jns -4
                   18610:        mem[DUMMY_TOP + 0x16] = 0xfc;
                   18611:        mem[DUMMY_TOP + 0x17] = 0xcb;   // retf
1.1.1.35  root     18612:        
1.1.1.50  root     18613:        // irq0 routine (system timer)
1.1.1.49  root     18614:        mem[DUMMY_TOP + 0x18] = 0xcd;   // int 1ch
                   18615:        mem[DUMMY_TOP + 0x19] = 0x1c;
                   18616:        mem[DUMMY_TOP + 0x1a] = 0xea;   // jmp far (IRET_TOP >> 4):0008
                   18617:        mem[DUMMY_TOP + 0x1b] = 0x08;
                   18618:        mem[DUMMY_TOP + 0x1c] = 0x00;
                   18619:        mem[DUMMY_TOP + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   18620:        mem[DUMMY_TOP + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
                   18621:        
                   18622:        // alter page map and call routine
                   18623:        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   18624:        mem[DUMMY_TOP + 0x20] = 0xff;
                   18625:        mem[DUMMY_TOP + 0x21] = 0xff;
                   18626:        mem[DUMMY_TOP + 0x22] = 0xff;
                   18627:        mem[DUMMY_TOP + 0x23] = 0xff;
                   18628:        mem[DUMMY_TOP + 0x24] = 0xcd;   // int 6fh (dummy)
                   18629:        mem[DUMMY_TOP + 0x25] = 0x6f;
                   18630:        mem[DUMMY_TOP + 0x26] = 0xcb;   // retf
1.1.1.14  root     18631:        
1.1.1.50  root     18632:        // call int 29h routine
                   18633:        mem[DUMMY_TOP + 0x27] = 0xcd;   // int 29h
                   18634:        mem[DUMMY_TOP + 0x28] = 0x29;
                   18635:        mem[DUMMY_TOP + 0x29] = 0xcb;   // retf
                   18636:        
1.1.1.63! root     18637:        // VCPI entry point
        !          18638:        mem[DUMMY_TOP + 0x2a] = 0xcd;   // int 65h
        !          18639:        mem[DUMMY_TOP + 0x2b] = 0x65;
        !          18640:        mem[DUMMY_TOP + 0x2c] = 0xcb;   // retf
        !          18641:        
1.1.1.26  root     18642:        // boot routine
1.1.1.59  root     18643:        mem[0xffff0 + 0x00] = 0xf4;     // halt to exit MS-DOS Player
                   18644: #if 1
                   18645:        mem[0xffff0 + 0x05] = '0';      // rom date (same as DOSBox)
                   18646:        mem[0xffff0 + 0x06] = '1';
                   18647:        mem[0xffff0 + 0x07] = '/';
                   18648:        mem[0xffff0 + 0x08] = '0';
                   18649:        mem[0xffff0 + 0x09] = '1';
                   18650:        mem[0xffff0 + 0x0a] = '/';
                   18651:        mem[0xffff0 + 0x0b] = '9';
                   18652:        mem[0xffff0 + 0x0c] = '2';
                   18653:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id (pc/at)
                   18654:        mem[0xffff0 + 0x0f] = 0x55;     // signature
                   18655: #else
                   18656:        mem[0xffff0 + 0x05] = '0';      // rom date (same as Windows 98 SE)
1.1.1.49  root     18657:        mem[0xffff0 + 0x06] = '2';
                   18658:        mem[0xffff0 + 0x07] = '/';
                   18659:        mem[0xffff0 + 0x08] = '2';
                   18660:        mem[0xffff0 + 0x09] = '2';
                   18661:        mem[0xffff0 + 0x0a] = '/';
                   18662:        mem[0xffff0 + 0x0b] = '0';
                   18663:        mem[0xffff0 + 0x0c] = '6';
1.1.1.59  root     18664:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id (pc/at)
1.1.1.49  root     18665:        mem[0xffff0 + 0x0f] = 0x00;
1.1.1.59  root     18666: #endif
1.1.1.24  root     18667:        
1.1       root     18668:        // param block
                   18669:        // + 0: param block (22bytes)
                   18670:        // +24: fcb1/2 (20bytes)
                   18671:        // +44: command tail (128bytes)
                   18672:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   18673:        param->env_seg = 0;
                   18674:        param->cmd_line.w.l = 44;
                   18675:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   18676:        param->fcb1.w.l = 24;
                   18677:        param->fcb1.w.h = (WORK_TOP >> 4);
                   18678:        param->fcb2.w.l = 24;
                   18679:        param->fcb2.w.h = (WORK_TOP >> 4);
                   18680:        
                   18681:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   18682:        
                   18683:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   18684:        if(argc > 1) {
                   18685:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   18686:                for(int i = 2; i < argc; i++) {
                   18687:                        char tmp[128];
                   18688:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   18689:                        strcpy(cmd_line->cmd, tmp);
                   18690:                }
                   18691:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   18692:        } else {
                   18693:                cmd_line->len = 0;
                   18694:        }
                   18695:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   18696:        
                   18697:        // system file table
1.1.1.21  root     18698:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   18699:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     18700:        
1.1.1.19  root     18701:        // disk buffer header (from DOSBox)
                   18702:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   18703:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   18704:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   18705:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   18706:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   18707:        
1.1       root     18708:        // fcb table
                   18709:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     18710:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     18711:        
1.1.1.41  root     18712:        // drive parameter block
1.1.1.42  root     18713:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     18714:                // may be a floppy drive
1.1.1.44  root     18715:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   18716:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   18717:                cds->drive_attrib = 0x4000;     // physical drive
                   18718:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   18719:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   18720:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   18721:                cds->bs_offset = 2;
                   18722:                
1.1.1.41  root     18723:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   18724:                dpb->drive_num = i;
                   18725:                dpb->unit_num = i;
1.1.1.43  root     18726:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   18727:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     18728:        }
                   18729:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     18730:                msdos_cds_update(i);
1.1.1.42  root     18731:                UINT16 seg, ofs;
                   18732:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     18733:        }
                   18734:        
1.1.1.17  root     18735:        // nls stuff
                   18736:        msdos_nls_tables_init();
1.1       root     18737:        
                   18738:        // execute command
1.1.1.28  root     18739:        try {
1.1.1.52  root     18740:                if(msdos_process_exec(argv[0], param, 0, true)) {
1.1.1.28  root     18741:                        fatalerror("'%s' not found\n", argv[0]);
                   18742:                }
                   18743:        } catch(...) {
                   18744:                // we should not reach here :-(
                   18745:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     18746:        }
                   18747:        retval = 0;
                   18748:        return(0);
                   18749: }
                   18750: 
                   18751: #define remove_std_file(path) { \
                   18752:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   18753:        if(fd != -1) { \
                   18754:                _lseek(fd, 0, SEEK_END); \
                   18755:                int size = _tell(fd); \
                   18756:                _close(fd); \
                   18757:                if(size == 0) { \
                   18758:                        remove(path); \
                   18759:                } \
                   18760:        } \
                   18761: }
                   18762: 
                   18763: void msdos_finish()
                   18764: {
                   18765:        for(int i = 0; i < MAX_FILES; i++) {
                   18766:                if(file_handler[i].valid) {
                   18767:                        _close(i);
                   18768:                }
                   18769:        }
1.1.1.21  root     18770: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     18771:        remove_std_file("stdaux.txt");
1.1.1.21  root     18772: #endif
1.1.1.30  root     18773: #ifdef SUPPORT_XMS
                   18774:        msdos_xms_finish();
                   18775: #endif
1.1       root     18776:        msdos_dbcs_table_finish();
                   18777: }
                   18778: 
                   18779: /* ----------------------------------------------------------------------------
                   18780:        PC/AT hardware emulation
                   18781: ---------------------------------------------------------------------------- */
                   18782: 
                   18783: void hardware_init()
                   18784: {
1.1.1.3   root     18785:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     18786:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     18787:        m_IF = 1;
1.1.1.3   root     18788: #if defined(HAS_I386)
1.1       root     18789:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   18790:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     18791: #endif
                   18792:        i386_set_a20_line(0);
1.1.1.14  root     18793:        
1.1.1.19  root     18794:        ems_init();
1.1.1.25  root     18795:        dma_init();
1.1       root     18796:        pic_init();
1.1.1.25  root     18797:        pio_init();
1.1.1.8   root     18798: #ifdef PIT_ALWAYS_RUNNING
                   18799:        pit_init();
                   18800: #else
1.1       root     18801:        pit_active = 0;
                   18802: #endif
1.1.1.25  root     18803:        sio_init();
1.1.1.8   root     18804:        cmos_init();
                   18805:        kbd_init();
1.1       root     18806: }
                   18807: 
1.1.1.10  root     18808: void hardware_finish()
                   18809: {
                   18810: #if defined(HAS_I386)
                   18811:        vtlb_free(m_vtlb);
                   18812: #endif
1.1.1.19  root     18813:        ems_finish();
1.1.1.37  root     18814:        pio_finish();
1.1.1.25  root     18815:        sio_finish();
1.1.1.10  root     18816: }
                   18817: 
1.1.1.28  root     18818: void hardware_release()
                   18819: {
                   18820:        // release hardware resources when this program will be terminated abnormally
                   18821: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18822:        if(fp_debug_log != NULL) {
                   18823:                fclose(fp_debug_log);
                   18824:                fp_debug_log = NULL;
1.1.1.28  root     18825:        }
                   18826: #endif
                   18827: #if defined(HAS_I386)
                   18828:        vtlb_free(m_vtlb);
                   18829: #endif
                   18830:        ems_release();
1.1.1.37  root     18831:        pio_release();
1.1.1.28  root     18832:        sio_release();
                   18833: }
                   18834: 
1.1       root     18835: void hardware_run()
                   18836: {
1.1.1.22  root     18837: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     18838:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     18839:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     18840: #endif
1.1.1.51  root     18841: #ifdef USE_DEBUGGER
                   18842:        m_int_num = -1;
                   18843: #endif
1.1.1.54  root     18844:        while(!m_exit) {
1.1.1.50  root     18845:                hardware_run_cpu();
1.1       root     18846:        }
1.1.1.22  root     18847: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18848:        if(fp_debug_log != NULL) {
                   18849:                fclose(fp_debug_log);
                   18850:                fp_debug_log = NULL;
1.1.1.28  root     18851:        }
1.1.1.22  root     18852: #endif
1.1       root     18853: }
                   18854: 
1.1.1.50  root     18855: inline void hardware_run_cpu()
                   18856: {
                   18857: #if defined(HAS_I386)
                   18858:        CPU_EXECUTE_CALL(i386);
                   18859:        if(m_eip != m_prev_eip) {
                   18860:                idle_ops++;
                   18861:        }
                   18862: #else
                   18863:        CPU_EXECUTE_CALL(CPU_MODEL);
                   18864:        if(m_pc != m_prevpc) {
                   18865:                idle_ops++;
                   18866:        }
                   18867: #endif
                   18868: #ifdef USE_DEBUGGER
                   18869:        // Disallow reentering CPU_EXECUTE() in msdos_syscall()
                   18870:        if(m_int_num >= 0) {
                   18871:                unsigned num = (unsigned)m_int_num;
                   18872:                m_int_num = -1;
                   18873:                msdos_syscall(num);
                   18874:        }
                   18875: #endif
                   18876:        if(++update_ops == UPDATE_OPS) {
                   18877:                update_ops = 0;
                   18878:                hardware_update();
                   18879:        }
                   18880: }
                   18881: 
1.1       root     18882: void hardware_update()
                   18883: {
1.1.1.8   root     18884:        static UINT32 prev_time = 0;
                   18885:        UINT32 cur_time = timeGetTime();
                   18886:        
                   18887:        if(prev_time != cur_time) {
                   18888:                // update pit and raise irq0
                   18889: #ifndef PIT_ALWAYS_RUNNING
                   18890:                if(pit_active)
                   18891: #endif
                   18892:                {
                   18893:                        if(pit_run(0, cur_time)) {
                   18894:                                pic_req(0, 0, 1);
                   18895:                        }
                   18896:                        pit_run(1, cur_time);
                   18897:                        pit_run(2, cur_time);
                   18898:                }
1.1.1.24  root     18899:                
1.1.1.25  root     18900:                // update sio and raise irq4/3
1.1.1.29  root     18901:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     18902:                        sio_update(c);
                   18903:                }
                   18904:                
1.1.1.24  root     18905:                // update keyboard and mouse
1.1.1.14  root     18906:                static UINT32 prev_tick = 0;
                   18907:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     18908:                
1.1.1.14  root     18909:                if(prev_tick != cur_tick) {
                   18910:                        // update keyboard flags
                   18911:                        UINT8 state;
1.1.1.24  root     18912:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   18913:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   18914:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   18915:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   18916:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   18917:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   18918:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   18919:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     18920:                        mem[0x417] = state;
                   18921:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   18922:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   18923:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   18924:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     18925: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   18926: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     18927:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   18928:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   18929:                        mem[0x418] = state;
                   18930:                        
1.1.1.24  root     18931:                        // update console input if needed
1.1.1.34  root     18932:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     18933:                                update_console_input();
                   18934:                        }
1.1.1.57  root     18935:                        if(!(kbd_status & 1)) {
                   18936:                                if(key_buf_data != NULL) {
                   18937: #ifdef USE_SERVICE_THREAD
                   18938:                                        EnterCriticalSection(&key_buf_crit_sect);
                   18939: #endif
                   18940:                                        if(!key_buf_data->empty()) {
                   18941:                                                kbd_data = key_buf_data->read();
                   18942:                                                kbd_status |= 1;
                   18943:                                                key_changed = true;
                   18944:                                        }
                   18945: #ifdef USE_SERVICE_THREAD
                   18946:                                        LeaveCriticalSection(&key_buf_crit_sect);
                   18947: #endif
                   18948:                                }
                   18949:                        }
1.1.1.24  root     18950:                        
1.1.1.57  root     18951:                        // raise irq1 if key is pressed/released or key buffer is not empty
1.1.1.56  root     18952:                        if(!key_changed) {
1.1.1.55  root     18953: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     18954:                                EnterCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     18955: #endif
1.1.1.57  root     18956:                                if(!pcbios_is_key_buffer_empty()) {
                   18957: /*
                   18958:                                        if(!(kbd_status & 1)) {
                   18959:                                                UINT16 head = *(UINT16 *)(mem + 0x41a);
                   18960:                                                UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   18961:                                                if(head != tail) {
                   18962:                                                        int key_char = mem[0x400 + (head++)];
                   18963:                                                        int key_scan = mem[0x400 + (head++)];
                   18964:                                                        kbd_data = key_char ? key_char : key_scan;
                   18965:                                                        kbd_status |= 1;
                   18966:                                                }
                   18967:                                        }
                   18968: */
                   18969:                                        key_changed = true;
                   18970:                                }
1.1.1.55  root     18971: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     18972:                                LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     18973: #endif
1.1.1.56  root     18974:                        }
                   18975:                        if(key_changed) {
1.1.1.8   root     18976:                                pic_req(0, 1, 1);
1.1.1.56  root     18977:                                key_changed = false;
1.1.1.24  root     18978:                        }
                   18979:                        
                   18980:                        // raise irq12 if mouse status is changed
1.1.1.59  root     18981:                        if((mouse.status & 0x1f) && mouse.call_addr_ps2.dw && mouse.enabled_ps2) {
1.1.1.54  root     18982:                                mouse.status_irq = 0; // ???
                   18983:                                mouse.status_irq_alt = 0; // ???
                   18984:                                mouse.status_irq_ps2 = mouse.status & 0x1f;
                   18985:                                mouse.status = 0;
                   18986:                                pic_req(1, 4, 1);
1.1.1.59  root     18987:                        } else if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
1.1.1.43  root     18988:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   18989:                                mouse.status_irq_alt = 0; // ???
1.1.1.54  root     18990:                                mouse.status_irq_ps2 = 0; // ???
1.1.1.24  root     18991:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     18992:                                pic_req(1, 4, 1);
                   18993:                        } else {
                   18994:                                for(int i = 0; i < 8; i++) {
                   18995:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   18996:                                                mouse.status_irq = 0; // ???
                   18997:                                                mouse.status_irq_alt = 0;
1.1.1.54  root     18998:                                                mouse.status_irq_ps2 = 0; // ???
1.1.1.43  root     18999:                                                for(int j = 0; j < 8; j++) {
                   19000:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   19001:                                                                mouse.status_irq_alt |=  (1 << j);
                   19002:                                                                mouse.status_alt     &= ~(1 << j);
                   19003:                                                        }
                   19004:                                                }
                   19005:                                                pic_req(1, 4, 1);
                   19006:                                                break;
                   19007:                                        }
                   19008:                                }
1.1.1.8   root     19009:                        }
1.1.1.24  root     19010:                        
1.1.1.60  root     19011:                        prev_tick = cur_tick;
                   19012:                }
                   19013:                
                   19014:                // update cursor size/position by crtc
                   19015:                if(crtc_changed[10] != 0 || crtc_changed[11] != 0) {
                   19016:                        int size = (int)(crtc_regs[11] & 7) - (int)(crtc_regs[10] & 7) + 1;
                   19017:                        if(!((crtc_regs[10] & 0x20) != 0 || size < 0)) {
                   19018:                                ci_new.bVisible = TRUE;
                   19019:                                ci_new.dwSize = (size + 2) * 100 / (8 + 2);
1.1.1.59  root     19020:                        } else {
1.1.1.60  root     19021:                                ci_new.bVisible = FALSE;
                   19022:                        }
                   19023:                        crtc_changed[10] = crtc_changed[11] = 0;
                   19024:                }
                   19025:                if(crtc_changed[14] != 0 || crtc_changed[15] != 0) {
                   19026:                        if(cursor_moved) {
                   19027:                                pcbios_update_cursor_position();
                   19028:                                cursor_moved = false;
1.1.1.59  root     19029:                        }
1.1.1.60  root     19030:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   19031:                        int position = crtc_regs[14] * 256 + crtc_regs[15];
                   19032:                        int width = *(UINT16 *)(mem + 0x44a);
                   19033:                        COORD co;
                   19034:                        co.X = position % width;
                   19035:                        co.Y = position / width + scr_top;
                   19036:                        SetConsoleCursorPosition(hStdout, co);
1.1.1.59  root     19037:                        
1.1.1.60  root     19038:                        crtc_changed[14] = crtc_changed[15] = 0;
                   19039:                        cursor_moved_by_crtc = true;
                   19040:                }
                   19041:                
                   19042:                // update cursor info
                   19043:                if(!is_cursor_blink_off()) {
                   19044:                        ci_new.bVisible = TRUE;
                   19045:                }
                   19046:                if(!(ci_old.dwSize == ci_new.dwSize && ci_old.bVisible == ci_new.bVisible)) {
                   19047:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   19048:                        SetConsoleCursorInfo(hStdout, &ci_new);
1.1.1.8   root     19049:                }
1.1.1.60  root     19050:                ci_old = ci_new;
1.1.1.24  root     19051:                
1.1.1.19  root     19052:                // update daily timer counter
                   19053:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     19054:                
1.1.1.8   root     19055:                prev_time = cur_time;
1.1       root     19056:        }
                   19057: }
                   19058: 
1.1.1.19  root     19059: // ems
                   19060: 
                   19061: void ems_init()
                   19062: {
                   19063:        memset(ems_handles, 0, sizeof(ems_handles));
                   19064:        memset(ems_pages, 0, sizeof(ems_pages));
                   19065:        free_ems_pages = MAX_EMS_PAGES;
                   19066: }
                   19067: 
                   19068: void ems_finish()
                   19069: {
1.1.1.28  root     19070:        ems_release();
                   19071: }
                   19072: 
                   19073: void ems_release()
                   19074: {
1.1.1.31  root     19075:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     19076:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     19077:                        free(ems_handles[i].buffer);
                   19078:                        ems_handles[i].buffer = NULL;
                   19079:                }
                   19080:        }
                   19081: }
                   19082: 
                   19083: void ems_allocate_pages(int handle, int pages)
                   19084: {
                   19085:        if(pages > 0) {
                   19086:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   19087:        } else {
                   19088:                ems_handles[handle].buffer = NULL;
                   19089:        }
                   19090:        ems_handles[handle].pages = pages;
                   19091:        ems_handles[handle].allocated = true;
                   19092:        free_ems_pages -= pages;
                   19093: }
                   19094: 
                   19095: void ems_reallocate_pages(int handle, int pages)
                   19096: {
                   19097:        if(ems_handles[handle].allocated) {
                   19098:                if(ems_handles[handle].pages != pages) {
                   19099:                        UINT8 *new_buffer = NULL;
                   19100:                        
                   19101:                        if(pages > 0) {
                   19102:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   19103:                        }
1.1.1.32  root     19104:                        if(ems_handles[handle].buffer != NULL) {
                   19105:                                if(new_buffer != NULL) {
1.1.1.19  root     19106:                                        if(pages > ems_handles[handle].pages) {
                   19107:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   19108:                                        } else {
                   19109:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   19110:                                        }
                   19111:                                }
                   19112:                                free(ems_handles[handle].buffer);
                   19113:                                ems_handles[handle].buffer = NULL;
                   19114:                        }
                   19115:                        free_ems_pages += ems_handles[handle].pages;
                   19116:                        
                   19117:                        ems_handles[handle].buffer = new_buffer;
                   19118:                        ems_handles[handle].pages = pages;
                   19119:                        free_ems_pages -= pages;
                   19120:                }
                   19121:        } else {
                   19122:                ems_allocate_pages(handle, pages);
                   19123:        }
                   19124: }
                   19125: 
                   19126: void ems_release_pages(int handle)
                   19127: {
                   19128:        if(ems_handles[handle].allocated) {
1.1.1.32  root     19129:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     19130:                        free(ems_handles[handle].buffer);
                   19131:                        ems_handles[handle].buffer = NULL;
                   19132:                }
                   19133:                free_ems_pages += ems_handles[handle].pages;
                   19134:                ems_handles[handle].allocated = false;
                   19135:        }
                   19136: }
                   19137: 
                   19138: void ems_map_page(int physical, int handle, int logical)
                   19139: {
                   19140:        if(ems_pages[physical].mapped) {
                   19141:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   19142:                        return;
                   19143:                }
                   19144:                ems_unmap_page(physical);
                   19145:        }
1.1.1.32  root     19146:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     19147:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   19148:        }
                   19149:        ems_pages[physical].handle = handle;
                   19150:        ems_pages[physical].page = logical;
                   19151:        ems_pages[physical].mapped = true;
                   19152: }
                   19153: 
                   19154: void ems_unmap_page(int physical)
                   19155: {
                   19156:        if(ems_pages[physical].mapped) {
                   19157:                int handle = ems_pages[physical].handle;
                   19158:                int logical = ems_pages[physical].page;
                   19159:                
1.1.1.32  root     19160:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     19161:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   19162:                }
                   19163:                ems_pages[physical].mapped = false;
                   19164:        }
                   19165: }
                   19166: 
1.1.1.25  root     19167: // dma
1.1       root     19168: 
1.1.1.25  root     19169: void dma_init()
1.1       root     19170: {
1.1.1.26  root     19171:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     19172:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     19173: //             for(int ch = 0; ch < 4; ch++) {
                   19174: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   19175: //             }
1.1.1.25  root     19176:                dma_reset(c);
                   19177:        }
1.1       root     19178: }
                   19179: 
1.1.1.25  root     19180: void dma_reset(int c)
1.1       root     19181: {
1.1.1.25  root     19182:        dma[c].low_high = false;
                   19183:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   19184:        dma[c].mask = 0xff;
                   19185: }
                   19186: 
                   19187: void dma_write(int c, UINT32 addr, UINT8 data)
                   19188: {
                   19189:        int ch = (addr >> 1) & 3;
                   19190:        UINT8 bit = 1 << (data & 3);
                   19191:        
                   19192:        switch(addr & 0x0f) {
                   19193:        case 0x00: case 0x02: case 0x04: case 0x06:
                   19194:                if(dma[c].low_high) {
                   19195:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     19196:                } else {
1.1.1.25  root     19197:                        dma[c].ch[ch].bareg.b.l = data;
                   19198:                }
                   19199:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   19200:                dma[c].low_high = !dma[c].low_high;
                   19201:                break;
                   19202:        case 0x01: case 0x03: case 0x05: case 0x07:
                   19203:                if(dma[c].low_high) {
                   19204:                        dma[c].ch[ch].bcreg.b.h = data;
                   19205:                } else {
                   19206:                        dma[c].ch[ch].bcreg.b.l = data;
                   19207:                }
                   19208:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   19209:                dma[c].low_high = !dma[c].low_high;
                   19210:                break;
                   19211:        case 0x08:
                   19212:                // command register
                   19213:                dma[c].cmd = data;
                   19214:                break;
                   19215:        case 0x09:
                   19216:                // dma[c].request register
                   19217:                if(data & 4) {
                   19218:                        if(!(dma[c].req & bit)) {
                   19219:                                dma[c].req |= bit;
                   19220: //                             dma_run(c, ch);
                   19221:                        }
                   19222:                } else {
                   19223:                        dma[c].req &= ~bit;
                   19224:                }
                   19225:                break;
                   19226:        case 0x0a:
                   19227:                // single mask register
                   19228:                if(data & 4) {
                   19229:                        dma[c].mask |= bit;
                   19230:                } else {
                   19231:                        dma[c].mask &= ~bit;
                   19232:                }
                   19233:                break;
                   19234:        case 0x0b:
                   19235:                // mode register
                   19236:                dma[c].ch[data & 3].mode = data;
                   19237:                break;
                   19238:        case 0x0c:
                   19239:                dma[c].low_high = false;
                   19240:                break;
                   19241:        case 0x0d:
                   19242:                // clear master
                   19243:                dma_reset(c);
                   19244:                break;
                   19245:        case 0x0e:
                   19246:                // clear mask register
                   19247:                dma[c].mask = 0;
                   19248:                break;
                   19249:        case 0x0f:
                   19250:                // all mask register
                   19251:                dma[c].mask = data & 0x0f;
                   19252:                break;
                   19253:        }
                   19254: }
                   19255: 
                   19256: UINT8 dma_read(int c, UINT32 addr)
                   19257: {
                   19258:        int ch = (addr >> 1) & 3;
                   19259:        UINT8 val = 0xff;
                   19260:        
                   19261:        switch(addr & 0x0f) {
                   19262:        case 0x00: case 0x02: case 0x04: case 0x06:
                   19263:                if(dma[c].low_high) {
                   19264:                        val = dma[c].ch[ch].areg.b.h;
                   19265:                } else {
                   19266:                        val = dma[c].ch[ch].areg.b.l;
                   19267:                }
                   19268:                dma[c].low_high = !dma[c].low_high;
                   19269:                return(val);
                   19270:        case 0x01: case 0x03: case 0x05: case 0x07:
                   19271:                if(dma[c].low_high) {
                   19272:                        val = dma[c].ch[ch].creg.b.h;
                   19273:                } else {
                   19274:                        val = dma[c].ch[ch].creg.b.l;
                   19275:                }
                   19276:                dma[c].low_high = !dma[c].low_high;
                   19277:                return(val);
                   19278:        case 0x08:
                   19279:                // status register
                   19280:                val = (dma[c].req << 4) | dma[c].tc;
                   19281:                dma[c].tc = 0;
                   19282:                return(val);
                   19283:        case 0x0d:
1.1.1.26  root     19284:                // temporary register (intel 82374 does not support)
1.1.1.25  root     19285:                return(dma[c].tmp & 0xff);
1.1.1.26  root     19286:        case 0x0f:
                   19287:                // mask register (intel 82374 does support)
                   19288:                return(dma[c].mask);
1.1.1.25  root     19289:        }
                   19290:        return(0xff);
                   19291: }
                   19292: 
                   19293: void dma_page_write(int c, int ch, UINT8 data)
                   19294: {
                   19295:        dma[c].ch[ch].pagereg = data;
                   19296: }
                   19297: 
                   19298: UINT8 dma_page_read(int c, int ch)
                   19299: {
                   19300:        return(dma[c].ch[ch].pagereg);
                   19301: }
                   19302: 
                   19303: void dma_run(int c, int ch)
                   19304: {
                   19305:        UINT8 bit = 1 << ch;
                   19306:        
                   19307:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   19308:                // execute dma
                   19309:                while(dma[c].req & bit) {
                   19310:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   19311:                                // memory -> memory
                   19312:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   19313:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   19314:                                
                   19315:                                if(c == 0) {
                   19316:                                        dma[c].tmp = read_byte(saddr);
                   19317:                                        write_byte(daddr, dma[c].tmp);
                   19318:                                } else {
                   19319:                                        dma[c].tmp = read_word(saddr << 1);
                   19320:                                        write_word(daddr << 1, dma[c].tmp);
                   19321:                                }
                   19322:                                if(!(dma[c].cmd & 0x02)) {
                   19323:                                        if(dma[c].ch[0].mode & 0x20) {
                   19324:                                                dma[c].ch[0].areg.w--;
                   19325:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   19326:                                                        dma[c].ch[0].pagereg--;
                   19327:                                                }
                   19328:                                        } else {
                   19329:                                                dma[c].ch[0].areg.w++;
                   19330:                                                if(dma[c].ch[0].areg.w == 0) {
                   19331:                                                        dma[c].ch[0].pagereg++;
                   19332:                                                }
                   19333:                                        }
                   19334:                                }
                   19335:                                if(dma[c].ch[1].mode & 0x20) {
                   19336:                                        dma[c].ch[1].areg.w--;
                   19337:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   19338:                                                dma[c].ch[1].pagereg--;
                   19339:                                        }
                   19340:                                } else {
                   19341:                                        dma[c].ch[1].areg.w++;
                   19342:                                        if(dma[c].ch[1].areg.w == 0) {
                   19343:                                                dma[c].ch[1].pagereg++;
                   19344:                                        }
                   19345:                                }
                   19346:                                
                   19347:                                // check dma condition
                   19348:                                if(dma[c].ch[0].creg.w-- == 0) {
                   19349:                                        if(dma[c].ch[0].mode & 0x10) {
                   19350:                                                // self initialize
                   19351:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   19352:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   19353:                                        } else {
                   19354: //                                             dma[c].mask |= bit;
                   19355:                                        }
                   19356:                                }
                   19357:                                if(dma[c].ch[1].creg.w-- == 0) {
                   19358:                                        // terminal count
                   19359:                                        if(dma[c].ch[1].mode & 0x10) {
                   19360:                                                // self initialize
                   19361:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   19362:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   19363:                                        } else {
                   19364:                                                dma[c].mask |= bit;
                   19365:                                        }
                   19366:                                        dma[c].req &= ~bit;
                   19367:                                        dma[c].tc |= bit;
                   19368:                                }
                   19369:                        } else {
                   19370:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   19371:                                
                   19372:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   19373:                                        // verify
                   19374:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   19375:                                        // io -> memory
                   19376:                                        if(c == 0) {
                   19377:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   19378:                                                write_byte(addr, dma[c].tmp);
                   19379:                                        } else {
                   19380:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   19381:                                                write_word(addr << 1, dma[c].tmp);
                   19382:                                        }
                   19383:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   19384:                                        // memory -> io
                   19385:                                        if(c == 0) {
                   19386:                                                dma[c].tmp = read_byte(addr);
                   19387:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   19388:                                        } else {
                   19389:                                                dma[c].tmp = read_word(addr << 1);
                   19390:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   19391:                                        }
                   19392:                                }
                   19393:                                if(dma[c].ch[ch].mode & 0x20) {
                   19394:                                        dma[c].ch[ch].areg.w--;
                   19395:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   19396:                                                dma[c].ch[ch].pagereg--;
                   19397:                                        }
                   19398:                                } else {
                   19399:                                        dma[c].ch[ch].areg.w++;
                   19400:                                        if(dma[c].ch[ch].areg.w == 0) {
                   19401:                                                dma[c].ch[ch].pagereg++;
                   19402:                                        }
                   19403:                                }
                   19404:                                
                   19405:                                // check dma condition
                   19406:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   19407:                                        // terminal count
                   19408:                                        if(dma[c].ch[ch].mode & 0x10) {
                   19409:                                                // self initialize
                   19410:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   19411:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   19412:                                        } else {
                   19413:                                                dma[c].mask |= bit;
                   19414:                                        }
                   19415:                                        dma[c].req &= ~bit;
                   19416:                                        dma[c].tc |= bit;
                   19417:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   19418:                                        // single mode
                   19419:                                        break;
                   19420:                                }
                   19421:                        }
                   19422:                }
                   19423:        }
                   19424: }
                   19425: 
                   19426: // pic
                   19427: 
                   19428: void pic_init()
                   19429: {
                   19430:        memset(pic, 0, sizeof(pic));
                   19431:        pic[0].imr = pic[1].imr = 0xff;
                   19432:        
                   19433:        // from bochs bios
                   19434:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   19435:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   19436:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   19437:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   19438:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   19439:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   19440:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   19441:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   19442:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   19443: }
                   19444: 
                   19445: void pic_write(int c, UINT32 addr, UINT8 data)
                   19446: {
                   19447:        if(addr & 1) {
                   19448:                if(pic[c].icw2_r) {
                   19449:                        // icw2
                   19450:                        pic[c].icw2 = data;
                   19451:                        pic[c].icw2_r = 0;
                   19452:                } else if(pic[c].icw3_r) {
                   19453:                        // icw3
                   19454:                        pic[c].icw3 = data;
                   19455:                        pic[c].icw3_r = 0;
                   19456:                } else if(pic[c].icw4_r) {
                   19457:                        // icw4
                   19458:                        pic[c].icw4 = data;
                   19459:                        pic[c].icw4_r = 0;
                   19460:                } else {
                   19461:                        // ocw1
1.1       root     19462:                        pic[c].imr = data;
                   19463:                }
                   19464:        } else {
                   19465:                if(data & 0x10) {
                   19466:                        // icw1
                   19467:                        pic[c].icw1 = data;
                   19468:                        pic[c].icw2_r = 1;
                   19469:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   19470:                        pic[c].icw4_r = data & 1;
                   19471:                        pic[c].irr = 0;
                   19472:                        pic[c].isr = 0;
                   19473:                        pic[c].imr = 0;
                   19474:                        pic[c].prio = 0;
                   19475:                        if(!(pic[c].icw1 & 1)) {
                   19476:                                pic[c].icw4 = 0;
                   19477:                        }
                   19478:                        pic[c].ocw3 = 0;
                   19479:                } else if(data & 8) {
                   19480:                        // ocw3
                   19481:                        if(!(data & 2)) {
                   19482:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   19483:                        }
                   19484:                        if(!(data & 0x40)) {
                   19485:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   19486:                        }
                   19487:                        pic[c].ocw3 = data;
                   19488:                } else {
                   19489:                        // ocw2
                   19490:                        int level = 0;
                   19491:                        if(data & 0x40) {
                   19492:                                level = data & 7;
                   19493:                        } else {
                   19494:                                if(!pic[c].isr) {
                   19495:                                        return;
                   19496:                                }
                   19497:                                level = pic[c].prio;
                   19498:                                while(!(pic[c].isr & (1 << level))) {
                   19499:                                        level = (level + 1) & 7;
                   19500:                                }
                   19501:                        }
                   19502:                        if(data & 0x80) {
                   19503:                                pic[c].prio = (level + 1) & 7;
                   19504:                        }
                   19505:                        if(data & 0x20) {
                   19506:                                pic[c].isr &= ~(1 << level);
                   19507:                        }
                   19508:                }
                   19509:        }
                   19510:        pic_update();
                   19511: }
                   19512: 
                   19513: UINT8 pic_read(int c, UINT32 addr)
                   19514: {
                   19515:        if(addr & 1) {
                   19516:                return(pic[c].imr);
                   19517:        } else {
                   19518:                // polling mode is not supported...
                   19519:                //if(pic[c].ocw3 & 4) {
                   19520:                //      return ???;
                   19521:                //}
                   19522:                if(pic[c].ocw3 & 1) {
                   19523:                        return(pic[c].isr);
                   19524:                } else {
                   19525:                        return(pic[c].irr);
                   19526:                }
                   19527:        }
                   19528: }
                   19529: 
                   19530: void pic_req(int c, int level, int signal)
                   19531: {
                   19532:        if(signal) {
                   19533:                pic[c].irr |= (1 << level);
                   19534:        } else {
                   19535:                pic[c].irr &= ~(1 << level);
                   19536:        }
                   19537:        pic_update();
                   19538: }
                   19539: 
                   19540: int pic_ack()
                   19541: {
                   19542:        // ack (INTA=L)
                   19543:        pic[pic_req_chip].isr |= pic_req_bit;
                   19544:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   19545:        if(pic_req_chip > 0) {
                   19546:                // update isr and irr of master
                   19547:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   19548:                pic[pic_req_chip - 1].isr |= slave;
                   19549:                pic[pic_req_chip - 1].irr &= ~slave;
                   19550:        }
                   19551:        //if(pic[pic_req_chip].icw4 & 1) {
                   19552:                // 8086 mode
                   19553:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   19554:        //} else {
                   19555:        //      // 8080 mode
                   19556:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   19557:        //      if(pic[pic_req_chip].icw1 & 4) {
                   19558:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   19559:        //      } else {
                   19560:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   19561:        //      }
                   19562:        //      vector = 0xcd | (addr << 8);
                   19563:        //}
                   19564:        if(pic[pic_req_chip].icw4 & 2) {
                   19565:                // auto eoi
                   19566:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   19567:        }
                   19568:        return(vector);
                   19569: }
                   19570: 
                   19571: void pic_update()
                   19572: {
                   19573:        for(int c = 0; c < 2; c++) {
                   19574:                UINT8 irr = pic[c].irr;
                   19575:                if(c + 1 < 2) {
                   19576:                        // this is master
                   19577:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   19578:                                // request from slave
                   19579:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   19580:                        }
                   19581:                }
                   19582:                irr &= (~pic[c].imr);
                   19583:                if(!irr) {
                   19584:                        break;
                   19585:                }
                   19586:                if(!(pic[c].ocw3 & 0x20)) {
                   19587:                        irr |= pic[c].isr;
                   19588:                }
                   19589:                int level = pic[c].prio;
                   19590:                UINT8 bit = 1 << level;
                   19591:                while(!(irr & bit)) {
                   19592:                        level = (level + 1) & 7;
                   19593:                        bit = 1 << level;
                   19594:                }
                   19595:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   19596:                        // check slave
                   19597:                        continue;
                   19598:                }
                   19599:                if(pic[c].isr & bit) {
                   19600:                        break;
                   19601:                }
                   19602:                // interrupt request
                   19603:                pic_req_chip = c;
                   19604:                pic_req_level = level;
                   19605:                pic_req_bit = bit;
1.1.1.3   root     19606:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     19607:                return;
                   19608:        }
1.1.1.3   root     19609:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     19610: }
1.1       root     19611: 
1.1.1.25  root     19612: // pio
                   19613: 
                   19614: void pio_init()
                   19615: {
1.1.1.38  root     19616: //     bool conv_mode = (GetConsoleCP() == 932);
                   19617:        
1.1.1.26  root     19618:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     19619:        
1.1.1.25  root     19620:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     19621:                pio[c].stat = 0xdf;
1.1.1.25  root     19622:                pio[c].ctrl = 0x0c;
1.1.1.38  root     19623: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     19624:        }
                   19625: }
                   19626: 
1.1.1.37  root     19627: void pio_finish()
                   19628: {
                   19629:        pio_release();
                   19630: }
                   19631: 
                   19632: void pio_release()
                   19633: {
                   19634:        for(int c = 0; c < 2; c++) {
                   19635:                if(pio[c].fp != NULL) {
1.1.1.38  root     19636:                        if(pio[c].jis_mode) {
                   19637:                                fputc(0x1c, pio[c].fp);
                   19638:                                fputc(0x2e, pio[c].fp);
                   19639:                        }
1.1.1.37  root     19640:                        fclose(pio[c].fp);
                   19641:                        pio[c].fp = NULL;
                   19642:                }
                   19643:        }
                   19644: }
                   19645: 
1.1.1.25  root     19646: void pio_write(int c, UINT32 addr, UINT8 data)
                   19647: {
                   19648:        switch(addr & 3) {
                   19649:        case 0:
                   19650:                pio[c].data = data;
                   19651:                break;
                   19652:        case 2:
1.1.1.37  root     19653:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   19654:                        // strobe H -> L
                   19655:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   19656:                                // auto feed
                   19657:                                printer_out(c, 0x0d);
                   19658:                                printer_out(c, 0x0a);
                   19659:                        } else {
                   19660:                                printer_out(c, pio[c].data);
                   19661:                        }
                   19662:                        pio[c].stat &= ~0x40; // set ack
                   19663:                }
1.1.1.25  root     19664:                pio[c].ctrl = data;
                   19665:                break;
                   19666:        }
                   19667: }
                   19668: 
                   19669: UINT8 pio_read(int c, UINT32 addr)
                   19670: {
                   19671:        switch(addr & 3) {
                   19672:        case 0:
1.1.1.37  root     19673:                if(pio[c].ctrl & 0x20) {
                   19674:                        // input mode
                   19675:                        return(0xff);
                   19676:                }
1.1.1.25  root     19677:                return(pio[c].data);
                   19678:        case 1:
1.1.1.37  root     19679:                {
                   19680:                        UINT8 stat = pio[c].stat;
                   19681:                        pio[c].stat |= 0x40; // clear ack
                   19682:                        return(stat);
                   19683:                }
1.1.1.25  root     19684:        case 2:
                   19685:                return(pio[c].ctrl);
                   19686:        }
                   19687:        return(0xff);
                   19688: }
                   19689: 
1.1.1.37  root     19690: void printer_out(int c, UINT8 data)
                   19691: {
                   19692:        SYSTEMTIME time;
1.1.1.38  root     19693:        bool jis_mode = false;
1.1.1.37  root     19694:        
                   19695:        GetLocalTime(&time);
                   19696:        
                   19697:        if(pio[c].fp != NULL) {
                   19698:                // if at least 1000ms passed from last written, close the current file
                   19699:                FILETIME ftime1;
                   19700:                FILETIME ftime2;
                   19701:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   19702:                SystemTimeToFileTime(&time, &ftime2);
                   19703:                INT64 *time1 = (INT64 *)&ftime1;
                   19704:                INT64 *time2 = (INT64 *)&ftime2;
                   19705:                INT64 msec = (*time2 - *time1) / 10000;
                   19706:                
                   19707:                if(msec >= 1000) {
1.1.1.38  root     19708:                        if(pio[c].jis_mode) {
                   19709:                                fputc(0x1c, pio[c].fp);
                   19710:                                fputc(0x2e, pio[c].fp);
                   19711:                                jis_mode = true;
                   19712:                        }
1.1.1.37  root     19713:                        fclose(pio[c].fp);
                   19714:                        pio[c].fp = NULL;
                   19715:                }
                   19716:        }
                   19717:        if(pio[c].fp == NULL) {
                   19718:                // create a new file in the temp folder
                   19719:                char file_name[MAX_PATH];
                   19720:                
                   19721:                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     19722:                if(GetTempPathA(MAX_PATH, pio[c].path)) {
1.1.1.37  root     19723:                        strcat(pio[c].path, file_name);
                   19724:                } else {
                   19725:                        strcpy(pio[c].path, file_name);
                   19726:                }
1.1.1.38  root     19727:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     19728:        }
                   19729:        if(pio[c].fp != NULL) {
1.1.1.38  root     19730:                if(jis_mode) {
                   19731:                        fputc(0x1c, pio[c].fp);
                   19732:                        fputc(0x26, pio[c].fp);
                   19733:                }
1.1.1.37  root     19734:                fputc(data, pio[c].fp);
1.1.1.38  root     19735:                
                   19736:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   19737:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   19738:                        UINT8 buffer[4];
                   19739:                        fseek(pio[c].fp, 0, SEEK_SET);
                   19740:                        fread(buffer, 4, 1, pio[c].fp);
                   19741:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   19742:                                fclose(pio[c].fp);
                   19743:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   19744:                        }
                   19745:                }
1.1.1.37  root     19746:                pio[c].time = time;
                   19747:        }
                   19748: }
                   19749: 
1.1       root     19750: // pit
                   19751: 
1.1.1.22  root     19752: #define PIT_FREQ 1193182ULL
1.1       root     19753: #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)
                   19754: 
                   19755: void pit_init()
                   19756: {
1.1.1.8   root     19757:        memset(pit, 0, sizeof(pit));
1.1       root     19758:        for(int ch = 0; ch < 3; ch++) {
                   19759:                pit[ch].count = 0x10000;
                   19760:                pit[ch].ctrl_reg = 0x34;
                   19761:                pit[ch].mode = 3;
                   19762:        }
                   19763:        
                   19764:        // from bochs bios
                   19765:        pit_write(3, 0x34);
                   19766:        pit_write(0, 0x00);
                   19767:        pit_write(0, 0x00);
                   19768: }
                   19769: 
                   19770: void pit_write(int ch, UINT8 val)
                   19771: {
1.1.1.8   root     19772: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19773:        if(!pit_active) {
                   19774:                pit_active = 1;
                   19775:                pit_init();
                   19776:        }
1.1.1.8   root     19777: #endif
1.1       root     19778:        switch(ch) {
                   19779:        case 0:
                   19780:        case 1:
                   19781:        case 2:
                   19782:                // write count register
                   19783:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19784:                        if(pit[ch].ctrl_reg & 0x10) {
                   19785:                                pit[ch].low_write = 1;
                   19786:                        }
                   19787:                        if(pit[ch].ctrl_reg & 0x20) {
                   19788:                                pit[ch].high_write = 1;
                   19789:                        }
                   19790:                }
                   19791:                if(pit[ch].low_write) {
                   19792:                        pit[ch].count_reg = val;
                   19793:                        pit[ch].low_write = 0;
                   19794:                } else if(pit[ch].high_write) {
                   19795:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19796:                                pit[ch].count_reg = val << 8;
                   19797:                        } else {
                   19798:                                pit[ch].count_reg |= val << 8;
                   19799:                        }
                   19800:                        pit[ch].high_write = 0;
                   19801:                }
                   19802:                // start count
1.1.1.8   root     19803:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19804:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   19805:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   19806:                                pit[ch].prev_time = timeGetTime();
                   19807:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19808:                        }
                   19809:                }
                   19810:                break;
                   19811:        case 3: // ctrl reg
                   19812:                if((val & 0xc0) == 0xc0) {
                   19813:                        // i8254 read-back command
                   19814:                        for(ch = 0; ch < 3; ch++) {
                   19815:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   19816:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   19817:                                        pit[ch].status_latched = 1;
                   19818:                                }
                   19819:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   19820:                                        pit_latch_count(ch);
                   19821:                                }
                   19822:                        }
                   19823:                        break;
                   19824:                }
                   19825:                ch = (val >> 6) & 3;
                   19826:                if(val & 0x30) {
1.1.1.35  root     19827:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     19828:                        pit[ch].mode = modes[(val >> 1) & 7];
                   19829:                        pit[ch].count_latched = 0;
                   19830:                        pit[ch].low_read = pit[ch].high_read = 0;
                   19831:                        pit[ch].low_write = pit[ch].high_write = 0;
                   19832:                        pit[ch].ctrl_reg = val;
                   19833:                        // stop count
1.1.1.8   root     19834:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     19835:                        pit[ch].count_reg = 0;
                   19836:                } else if(!pit[ch].count_latched) {
                   19837:                        pit_latch_count(ch);
                   19838:                }
                   19839:                break;
                   19840:        }
                   19841: }
                   19842: 
                   19843: UINT8 pit_read(int ch)
                   19844: {
1.1.1.8   root     19845: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19846:        if(!pit_active) {
                   19847:                pit_active = 1;
                   19848:                pit_init();
                   19849:        }
1.1.1.8   root     19850: #endif
1.1       root     19851:        switch(ch) {
                   19852:        case 0:
                   19853:        case 1:
                   19854:        case 2:
                   19855:                if(pit[ch].status_latched) {
                   19856:                        pit[ch].status_latched = 0;
                   19857:                        return(pit[ch].status);
                   19858:                }
                   19859:                // if not latched, through current count
                   19860:                if(!pit[ch].count_latched) {
                   19861:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   19862:                                pit_latch_count(ch);
                   19863:                        }
                   19864:                }
                   19865:                // return latched count
                   19866:                if(pit[ch].low_read) {
                   19867:                        pit[ch].low_read = 0;
                   19868:                        if(!pit[ch].high_read) {
                   19869:                                pit[ch].count_latched = 0;
                   19870:                        }
                   19871:                        return(pit[ch].latch & 0xff);
                   19872:                } else if(pit[ch].high_read) {
                   19873:                        pit[ch].high_read = 0;
                   19874:                        pit[ch].count_latched = 0;
                   19875:                        return((pit[ch].latch >> 8) & 0xff);
                   19876:                }
                   19877:        }
                   19878:        return(0xff);
                   19879: }
                   19880: 
1.1.1.8   root     19881: int pit_run(int ch, UINT32 cur_time)
1.1       root     19882: {
1.1.1.8   root     19883:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     19884:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     19885:                pit[ch].prev_time = pit[ch].expired_time;
                   19886:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   19887:                if(cur_time >= pit[ch].expired_time) {
                   19888:                        pit[ch].prev_time = cur_time;
                   19889:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19890:                }
1.1.1.8   root     19891:                return(1);
1.1       root     19892:        }
1.1.1.8   root     19893:        return(0);
1.1       root     19894: }
                   19895: 
                   19896: void pit_latch_count(int ch)
                   19897: {
1.1.1.8   root     19898:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     19899:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     19900:                pit_run(ch, cur_time);
                   19901:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     19902:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19903:                
                   19904:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   19905:                        // decrement counter in 1msec period
                   19906:                        if(pit[ch].next_latch == 0) {
                   19907:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   19908:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19909:                        }
                   19910:                        if(pit[ch].latch > pit[ch].next_latch) {
                   19911:                                pit[ch].latch--;
                   19912:                        }
                   19913:                } else {
                   19914:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   19915:                        pit[ch].next_latch = 0;
                   19916:                }
1.1.1.8   root     19917:        } else {
                   19918:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     19919:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     19920:        }
                   19921:        pit[ch].count_latched = 1;
                   19922:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   19923:                // lower byte
                   19924:                pit[ch].low_read = 1;
                   19925:                pit[ch].high_read = 0;
                   19926:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19927:                // upper byte
                   19928:                pit[ch].low_read = 0;
                   19929:                pit[ch].high_read = 1;
                   19930:        } else {
                   19931:                // lower -> upper
1.1.1.14  root     19932:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     19933:        }
                   19934: }
                   19935: 
1.1.1.8   root     19936: int pit_get_expired_time(int ch)
1.1       root     19937: {
1.1.1.22  root     19938:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   19939:        UINT64 val = pit[ch].accum >> 10;
                   19940:        pit[ch].accum -= val << 10;
                   19941:        return((val != 0) ? val : 1);
1.1.1.8   root     19942: }
                   19943: 
1.1.1.25  root     19944: // sio
                   19945: 
                   19946: void sio_init()
                   19947: {
1.1.1.26  root     19948:        memset(sio, 0, sizeof(sio));
                   19949:        memset(sio_mt, 0, sizeof(sio_mt));
                   19950:        
1.1.1.29  root     19951:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19952:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19953:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19954:                
                   19955:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   19956:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     19957:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   19958:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     19959:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   19960:                sio[c].irq_identify = 0x01;     // no pending irq
                   19961:                
                   19962:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   19963:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   19964:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   19965:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   19966:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   19967:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   19968:                
1.1.1.26  root     19969:                if(sio_port_number[c] != 0) {
1.1.1.25  root     19970:                        sio[c].channel = c;
                   19971:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   19972:                }
                   19973:        }
                   19974: }
                   19975: 
                   19976: void sio_finish()
                   19977: {
1.1.1.29  root     19978:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19979:                if(sio_mt[c].hThread != NULL) {
                   19980:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   19981:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     19982:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     19983:                }
                   19984:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   19985:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   19986:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   19987:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   19988:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   19989:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     19990:        }
                   19991:        sio_release();
                   19992: }
                   19993: 
                   19994: void sio_release()
                   19995: {
1.1.1.29  root     19996:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     19997:                // sio_thread() may access the resources :-(
1.1.1.32  root     19998:                bool running = (sio_mt[c].hThread != NULL);
                   19999:                
                   20000:                if(running) {
                   20001:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   20002:                }
                   20003:                if(sio[c].send_buffer != NULL) {
                   20004:                        sio[c].send_buffer->release();
                   20005:                        delete sio[c].send_buffer;
                   20006:                        sio[c].send_buffer = NULL;
                   20007:                }
                   20008:                if(running) {
                   20009:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   20010:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   20011:                }
                   20012:                if(sio[c].recv_buffer != NULL) {
                   20013:                        sio[c].recv_buffer->release();
                   20014:                        delete sio[c].recv_buffer;
                   20015:                        sio[c].recv_buffer = NULL;
                   20016:                }
                   20017:                if(running) {
                   20018:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     20019:                }
1.1.1.25  root     20020:        }
                   20021: }
                   20022: 
                   20023: void sio_write(int c, UINT32 addr, UINT8 data)
                   20024: {
                   20025:        switch(addr & 7) {
                   20026:        case 0:
                   20027:                if(sio[c].selector & 0x80) {
                   20028:                        if(sio[c].divisor.b.l != data) {
                   20029:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   20030:                                sio[c].divisor.b.l = data;
                   20031:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   20032:                        }
                   20033:                } else {
                   20034:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     20035:                        if(sio[c].send_buffer != NULL) {
                   20036:                                sio[c].send_buffer->write(data);
                   20037:                        }
1.1.1.25  root     20038:                        // transmitter holding/shift registers are not empty
                   20039:                        sio[c].line_stat_buf &= ~0x60;
                   20040:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   20041:                        
                   20042:                        if(sio[c].irq_enable & 0x02) {
                   20043:                                sio_update_irq(c);
                   20044:                        }
                   20045:                }
                   20046:                break;
                   20047:        case 1:
                   20048:                if(sio[c].selector & 0x80) {
                   20049:                        if(sio[c].divisor.b.h != data) {
                   20050:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   20051:                                sio[c].divisor.b.h = data;
                   20052:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   20053:                        }
                   20054:                } else {
                   20055:                        if(sio[c].irq_enable != data) {
                   20056:                                sio[c].irq_enable = data;
                   20057:                                sio_update_irq(c);
                   20058:                        }
                   20059:                }
                   20060:                break;
                   20061:        case 3:
                   20062:                {
                   20063:                        UINT8 line_ctrl = data & 0x3f;
                   20064:                        bool set_brk = ((data & 0x40) != 0);
                   20065:                        
                   20066:                        if(sio[c].line_ctrl != line_ctrl) {
                   20067:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   20068:                                sio[c].line_ctrl = line_ctrl;
                   20069:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   20070:                        }
                   20071:                        if(sio[c].set_brk != set_brk) {
                   20072:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   20073:                                sio[c].set_brk = set_brk;
                   20074:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   20075:                        }
                   20076:                }
                   20077:                sio[c].selector = data;
                   20078:                break;
                   20079:        case 4:
                   20080:                {
                   20081:                        bool set_dtr = ((data & 0x01) != 0);
                   20082:                        bool set_rts = ((data & 0x02) != 0);
                   20083:                        
                   20084:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     20085: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     20086:                                sio[c].set_dtr = set_dtr;
                   20087:                                sio[c].set_rts = set_rts;
1.1.1.26  root     20088: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   20089:                                
                   20090:                                bool state_changed = false;
                   20091:                                
                   20092:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   20093:                                if(set_dtr) {
                   20094:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   20095:                                } else {
                   20096:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   20097:                                }
                   20098:                                if(set_rts) {
                   20099:                                        sio[c].modem_stat |= 0x10;      // cts on
                   20100:                                } else {
                   20101:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   20102:                                }
                   20103:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   20104:                                        if(!(sio[c].modem_stat & 0x02)) {
                   20105:                                                if(sio[c].irq_enable & 0x08) {
                   20106:                                                        state_changed = true;
                   20107:                                                }
                   20108:                                                sio[c].modem_stat |= 0x02;
                   20109:                                        }
                   20110:                                }
                   20111:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   20112:                                        if(!(sio[c].modem_stat & 0x01)) {
                   20113:                                                if(sio[c].irq_enable & 0x08) {
                   20114:                                                        state_changed = true;
                   20115:                                                }
                   20116:                                                sio[c].modem_stat |= 0x01;
                   20117:                                        }
                   20118:                                }
                   20119:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   20120:                                
                   20121:                                if(state_changed) {
                   20122:                                        sio_update_irq(c);
                   20123:                                }
1.1.1.25  root     20124:                        }
                   20125:                }
                   20126:                sio[c].modem_ctrl = data;
                   20127:                break;
                   20128:        case 7:
                   20129:                sio[c].scratch = data;
                   20130:                break;
                   20131:        }
                   20132: }
                   20133: 
                   20134: UINT8 sio_read(int c, UINT32 addr)
                   20135: {
                   20136:        switch(addr & 7) {
                   20137:        case 0:
                   20138:                if(sio[c].selector & 0x80) {
                   20139:                        return(sio[c].divisor.b.l);
                   20140:                } else {
                   20141:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     20142:                        UINT8 data = 0;
                   20143:                        if(sio[c].recv_buffer != NULL) {
                   20144:                                data = sio[c].recv_buffer->read();
                   20145:                        }
1.1.1.25  root     20146:                        // data is not ready
                   20147:                        sio[c].line_stat_buf &= ~0x01;
                   20148:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   20149:                        
                   20150:                        if(sio[c].irq_enable & 0x01) {
                   20151:                                sio_update_irq(c);
                   20152:                        }
                   20153:                        return(data);
                   20154:                }
                   20155:        case 1:
                   20156:                if(sio[c].selector & 0x80) {
                   20157:                        return(sio[c].divisor.b.h);
                   20158:                } else {
                   20159:                        return(sio[c].irq_enable);
                   20160:                }
                   20161:        case 2:
                   20162:                return(sio[c].irq_identify);
                   20163:        case 3:
                   20164:                return(sio[c].selector);
                   20165:        case 4:
                   20166:                return(sio[c].modem_ctrl);
                   20167:        case 5:
                   20168:                {
                   20169:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   20170:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   20171:                        sio[c].line_stat_err = 0x00;
                   20172:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   20173:                        
                   20174:                        bool state_changed = false;
                   20175:                        
                   20176:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   20177:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     20178:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     20179:                                        // transmitter holding register will be empty first
                   20180:                                        if(sio[c].irq_enable & 0x02) {
                   20181:                                                state_changed = true;
                   20182:                                        }
                   20183:                                        sio[c].line_stat_buf |= 0x20;
                   20184:                                }
                   20185:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   20186:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   20187:                                // transmitter shift register will be empty later
                   20188:                                sio[c].line_stat_buf |= 0x40;
                   20189:                        }
                   20190:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   20191:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     20192:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     20193:                                        // data is ready
                   20194:                                        if(sio[c].irq_enable & 0x01) {
                   20195:                                                state_changed = true;
                   20196:                                        }
                   20197:                                        sio[c].line_stat_buf |= 0x01;
                   20198:                                }
                   20199:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   20200:                        }
                   20201:                        if(state_changed) {
                   20202:                                sio_update_irq(c);
                   20203:                        }
                   20204:                        return(val);
                   20205:                }
                   20206:        case 6:
                   20207:                {
                   20208:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   20209:                        UINT8 val = sio[c].modem_stat;
                   20210:                        sio[c].modem_stat &= 0xf0;
                   20211:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   20212:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   20213:                        
                   20214:                        if(sio[c].modem_ctrl & 0x10) {
                   20215:                                // loop-back
                   20216:                                val &= 0x0f;
                   20217:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   20218:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   20219:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   20220:                        }
                   20221:                        return(val);
                   20222:                }
                   20223:        case 7:
                   20224:                return(sio[c].scratch);
                   20225:        }
                   20226:        return(0xff);
                   20227: }
                   20228: 
                   20229: void sio_update(int c)
                   20230: {
                   20231:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   20232:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     20233:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     20234:                        // transmitter holding/shift registers will be empty
                   20235:                        sio[c].line_stat_buf |= 0x60;
                   20236:                }
                   20237:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   20238:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   20239:                // transmitter shift register will be empty
                   20240:                sio[c].line_stat_buf |= 0x40;
                   20241:        }
                   20242:        if(!(sio[c].line_stat_buf & 0x01)) {
                   20243:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     20244:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     20245:                        // data is ready
                   20246:                        sio[c].line_stat_buf |= 0x01;
                   20247:                }
                   20248:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   20249:        }
                   20250:        sio_update_irq(c);
                   20251: }
                   20252: 
                   20253: void sio_update_irq(int c)
                   20254: {
                   20255:        int level = -1;
                   20256:        
                   20257:        if(sio[c].irq_enable & 0x08) {
                   20258:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   20259:                if((sio[c].modem_stat & 0x0f) != 0) {
                   20260:                        level = 0;
                   20261:                }
                   20262:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   20263:        }
                   20264:        if(sio[c].irq_enable & 0x02) {
                   20265:                if(sio[c].line_stat_buf & 0x20) {
                   20266:                        level = 1;
                   20267:                }
                   20268:        }
                   20269:        if(sio[c].irq_enable & 0x01) {
                   20270:                if(sio[c].line_stat_buf & 0x01) {
                   20271:                        level = 2;
                   20272:                }
                   20273:        }
                   20274:        if(sio[c].irq_enable & 0x04) {
                   20275:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   20276:                if(sio[c].line_stat_err != 0) {
                   20277:                        level = 3;
                   20278:                }
                   20279:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   20280:        }
1.1.1.29  root     20281:        
                   20282:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     20283:        if(level != -1) {
                   20284:                sio[c].irq_identify = level << 1;
1.1.1.29  root     20285:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     20286:        } else {
                   20287:                sio[c].irq_identify = 1;
1.1.1.29  root     20288:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     20289:        }
                   20290: }
                   20291: 
                   20292: DWORD WINAPI sio_thread(void *lpx)
                   20293: {
                   20294:        volatile sio_t *p = (sio_t *)lpx;
                   20295:        sio_mt_t *q = &sio_mt[p->channel];
                   20296:        
                   20297:        char name[] = "COM1";
1.1.1.26  root     20298:        name[3] = '0' + sio_port_number[p->channel];
                   20299:        HANDLE hComm = NULL;
                   20300:        COMMPROP commProp;
                   20301:        DCB dcb;
                   20302:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   20303:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   20304:        
1.1.1.60  root     20305:        if((hComm = CreateFileA(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
1.1.1.26  root     20306:                if(GetCommProperties(hComm, &commProp)) {
                   20307:                        dwSettableBaud = commProp.dwSettableBaud;
                   20308:                }
1.1.1.25  root     20309:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     20310: //             EscapeCommFunction(hComm, SETRTS);
                   20311: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     20312:                
1.1.1.54  root     20313:                while(!m_exit) {
1.1.1.25  root     20314:                        // setup comm port
                   20315:                        bool comm_state_changed = false;
                   20316:                        
                   20317:                        EnterCriticalSection(&q->csLineCtrl);
                   20318:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   20319:                                p->prev_divisor = p->divisor.w;
                   20320:                                p->prev_line_ctrl = p->line_ctrl;
                   20321:                                comm_state_changed = true;
                   20322:                        }
                   20323:                        LeaveCriticalSection(&q->csLineCtrl);
                   20324:                        
                   20325:                        if(comm_state_changed) {
1.1.1.26  root     20326:                                if(GetCommState(hComm, &dcb)) {
                   20327: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   20328:                                        DWORD baud = 115200 / p->prev_divisor;
                   20329:                                        dcb.BaudRate = 9600; // default
                   20330:                                        
                   20331:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   20332:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   20333:                                        // 134.5bps is not supported ???
                   20334: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   20335:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   20336:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   20337:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   20338:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   20339:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   20340:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   20341:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   20342:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   20343:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   20344: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   20345: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   20346: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   20347:                                        
                   20348:                                        switch(p->prev_line_ctrl & 0x03) {
                   20349:                                        case 0x00: dcb.ByteSize = 5; break;
                   20350:                                        case 0x01: dcb.ByteSize = 6; break;
                   20351:                                        case 0x02: dcb.ByteSize = 7; break;
                   20352:                                        case 0x03: dcb.ByteSize = 8; break;
                   20353:                                        }
                   20354:                                        switch(p->prev_line_ctrl & 0x04) {
                   20355:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   20356:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   20357:                                        }
                   20358:                                        switch(p->prev_line_ctrl & 0x38) {
                   20359:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   20360:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   20361:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   20362:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   20363:                                        default:   dcb.Parity = NOPARITY;    break;
                   20364:                                        }
                   20365:                                        dcb.fBinary = TRUE;
                   20366:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   20367:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   20368:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   20369:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   20370:                                        dcb.fTXContinueOnXoff = TRUE;
                   20371:                                        dcb.fOutX = dcb.fInX = FALSE;
                   20372:                                        dcb.fErrorChar = FALSE;
                   20373:                                        dcb.fNull = FALSE;
                   20374:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   20375:                                        dcb.fAbortOnError = FALSE;
                   20376:                                        
                   20377:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     20378:                                }
                   20379:                                
                   20380:                                // check again to apply all comm state changes
                   20381:                                Sleep(10);
                   20382:                                continue;
                   20383:                        }
                   20384:                        
                   20385:                        // set comm pins
                   20386:                        bool change_brk = false;
1.1.1.26  root     20387: //                     bool change_rts = false;
                   20388: //                     bool change_dtr = false;
1.1.1.25  root     20389:                        
                   20390:                        EnterCriticalSection(&q->csModemCtrl);
                   20391:                        if(p->prev_set_brk != p->set_brk) {
                   20392:                                p->prev_set_brk = p->set_brk;
                   20393:                                change_brk = true;
                   20394:                        }
1.1.1.26  root     20395: //                     if(p->prev_set_rts != p->set_rts) {
                   20396: //                             p->prev_set_rts = p->set_rts;
                   20397: //                             change_rts = true;
                   20398: //                     }
                   20399: //                     if(p->prev_set_dtr != p->set_dtr) {
                   20400: //                             p->prev_set_dtr = p->set_dtr;
                   20401: //                             change_dtr = true;
                   20402: //                     }
1.1.1.25  root     20403:                        LeaveCriticalSection(&q->csModemCtrl);
                   20404:                        
                   20405:                        if(change_brk) {
1.1.1.26  root     20406:                                static UINT32 clear_time = 0;
                   20407:                                if(p->prev_set_brk) {
                   20408:                                        EscapeCommFunction(hComm, SETBREAK);
                   20409:                                        clear_time = timeGetTime() + 200;
                   20410:                                } else {
                   20411:                                        // keep break for at least 200msec
                   20412:                                        UINT32 cur_time = timeGetTime();
                   20413:                                        if(clear_time > cur_time) {
                   20414:                                                Sleep(clear_time - cur_time);
                   20415:                                        }
                   20416:                                        EscapeCommFunction(hComm, CLRBREAK);
                   20417:                                }
1.1.1.25  root     20418:                        }
1.1.1.26  root     20419: //                     if(change_rts) {
                   20420: //                             if(p->prev_set_rts) {
                   20421: //                                     EscapeCommFunction(hComm, SETRTS);
                   20422: //                             } else {
                   20423: //                                     EscapeCommFunction(hComm, CLRRTS);
                   20424: //                             }
                   20425: //                     }
                   20426: //                     if(change_dtr) {
                   20427: //                             if(p->prev_set_dtr) {
                   20428: //                                     EscapeCommFunction(hComm, SETDTR);
                   20429: //                             } else {
                   20430: //                                     EscapeCommFunction(hComm, CLRDTR);
                   20431: //                             }
                   20432: //                     }
1.1.1.25  root     20433:                        
                   20434:                        // get comm pins
                   20435:                        DWORD dwModemStat = 0;
                   20436:                        
                   20437:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   20438:                                EnterCriticalSection(&q->csModemStat);
                   20439:                                if(dwModemStat & MS_RLSD_ON) {
                   20440:                                        p->modem_stat |= 0x80;
                   20441:                                } else {
                   20442:                                        p->modem_stat &= ~0x80;
                   20443:                                }
                   20444:                                if(dwModemStat & MS_RING_ON) {
                   20445:                                        p->modem_stat |= 0x40;
                   20446:                                } else {
                   20447:                                        p->modem_stat &= ~0x40;
                   20448:                                }
1.1.1.26  root     20449: //                             if(dwModemStat & MS_DSR_ON) {
                   20450: //                                     p->modem_stat |= 0x20;
                   20451: //                             } else {
                   20452: //                                     p->modem_stat &= ~0x20;
                   20453: //                             }
                   20454: //                             if(dwModemStat & MS_CTS_ON) {
                   20455: //                                     p->modem_stat |= 0x10;
                   20456: //                             } else {
                   20457: //                                     p->modem_stat &= ~0x10;
                   20458: //                             }
1.1.1.25  root     20459:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   20460:                                        p->modem_stat |= 0x08;
                   20461:                                }
                   20462:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   20463:                                        p->modem_stat |= 0x04;
                   20464:                                }
1.1.1.26  root     20465: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   20466: //                                     p->modem_stat |= 0x02;
                   20467: //                             }
                   20468: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   20469: //                                     p->modem_stat |= 0x01;
                   20470: //                             }
1.1.1.25  root     20471:                                LeaveCriticalSection(&q->csModemStat);
                   20472:                        }
                   20473:                        
                   20474:                        // send data
                   20475:                        DWORD dwSend = 0;
                   20476:                        
                   20477:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     20478:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     20479:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   20480:                        }
                   20481:                        LeaveCriticalSection(&q->csSendData);
                   20482:                        
                   20483:                        if(dwSend != 0) {
                   20484:                                DWORD dwWritten = 0;
                   20485:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   20486:                        }
                   20487:                        
                   20488:                        // get line status and recv data
                   20489:                        DWORD dwLineStat = 0;
                   20490:                        COMSTAT comStat;
                   20491:                        
                   20492:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   20493:                                EnterCriticalSection(&q->csLineStat);
                   20494:                                if(dwLineStat & CE_BREAK) {
                   20495:                                        p->line_stat_err |= 0x10;
                   20496:                                }
                   20497:                                if(dwLineStat & CE_FRAME) {
                   20498:                                        p->line_stat_err |= 0x08;
                   20499:                                }
                   20500:                                if(dwLineStat & CE_RXPARITY) {
                   20501:                                        p->line_stat_err |= 0x04;
                   20502:                                }
                   20503:                                if(dwLineStat & CE_OVERRUN) {
                   20504:                                        p->line_stat_err |= 0x02;
                   20505:                                }
                   20506:                                LeaveCriticalSection(&q->csLineStat);
                   20507:                                
                   20508:                                if(comStat.cbInQue != 0) {
                   20509:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     20510:                                        DWORD dwRecv = 0;
                   20511:                                        if(p->recv_buffer != NULL) {
                   20512:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   20513:                                        }
1.1.1.25  root     20514:                                        LeaveCriticalSection(&q->csRecvData);
                   20515:                                        
                   20516:                                        if(dwRecv != 0) {
                   20517:                                                DWORD dwRead = 0;
                   20518:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   20519:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     20520:                                                        if(p->recv_buffer != NULL) {
                   20521:                                                                for(int i = 0; i < dwRead; i++) {
                   20522:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   20523:                                                                }
1.1.1.25  root     20524:                                                        }
                   20525:                                                        LeaveCriticalSection(&q->csRecvData);
                   20526:                                                }
                   20527:                                        }
                   20528:                                }
                   20529:                        }
                   20530:                        Sleep(10);
                   20531:                }
                   20532:                CloseHandle(hComm);
                   20533:        }
                   20534:        return 0;
                   20535: }
                   20536: 
1.1.1.8   root     20537: // cmos
                   20538: 
                   20539: void cmos_init()
                   20540: {
                   20541:        memset(cmos, 0, sizeof(cmos));
                   20542:        cmos_addr = 0;
1.1       root     20543:        
1.1.1.8   root     20544:        // from DOSBox
                   20545:        cmos_write(0x0a, 0x26);
                   20546:        cmos_write(0x0b, 0x02);
                   20547:        cmos_write(0x0d, 0x80);
1.1       root     20548: }
                   20549: 
1.1.1.8   root     20550: void cmos_write(int addr, UINT8 val)
1.1       root     20551: {
1.1.1.8   root     20552:        cmos[addr & 0x7f] = val;
                   20553: }
                   20554: 
                   20555: #define CMOS_GET_TIME() { \
                   20556:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   20557:        if(prev_sec != cur_sec) { \
                   20558:                GetLocalTime(&time); \
                   20559:                prev_sec = cur_sec; \
                   20560:        } \
1.1       root     20561: }
1.1.1.8   root     20562: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     20563: 
1.1.1.8   root     20564: UINT8 cmos_read(int addr)
1.1       root     20565: {
1.1.1.8   root     20566:        static SYSTEMTIME time;
                   20567:        static UINT32 prev_sec = 0;
1.1       root     20568:        
1.1.1.8   root     20569:        switch(addr & 0x7f) {
                   20570:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   20571:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   20572:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   20573:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   20574:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   20575:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   20576:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   20577: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   20578:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   20579:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   20580:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   20581:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20582:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20583:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20584:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20585:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     20586:        }
1.1.1.8   root     20587:        return(cmos[addr & 0x7f]);
1.1       root     20588: }
                   20589: 
1.1.1.7   root     20590: // kbd (a20)
                   20591: 
                   20592: void kbd_init()
                   20593: {
1.1.1.8   root     20594:        kbd_data = kbd_command = 0;
1.1.1.7   root     20595:        kbd_status = 0x18;
                   20596: }
                   20597: 
                   20598: UINT8 kbd_read_data()
                   20599: {
1.1.1.57  root     20600:        UINT8 data = kbd_data;
                   20601:        kbd_data = 0;
1.1.1.8   root     20602:        kbd_status &= ~1;
1.1.1.57  root     20603:        return(data);
1.1.1.7   root     20604: }
                   20605: 
                   20606: void kbd_write_data(UINT8 val)
                   20607: {
                   20608:        switch(kbd_command) {
                   20609:        case 0xd1:
                   20610:                i386_set_a20_line((val >> 1) & 1);
                   20611:                break;
                   20612:        }
                   20613:        kbd_command = 0;
1.1.1.8   root     20614:        kbd_status &= ~8;
1.1.1.7   root     20615: }
                   20616: 
                   20617: UINT8 kbd_read_status()
                   20618: {
                   20619:        return(kbd_status);
                   20620: }
                   20621: 
                   20622: void kbd_write_command(UINT8 val)
                   20623: {
                   20624:        switch(val) {
                   20625:        case 0xd0:
                   20626:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     20627:                kbd_status |= 1;
1.1.1.7   root     20628:                break;
                   20629:        case 0xdd:
                   20630:                i386_set_a20_line(0);
                   20631:                break;
                   20632:        case 0xdf:
                   20633:                i386_set_a20_line(1);
                   20634:                break;
1.1.1.26  root     20635:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   20636:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     20637:                if(!(val & 1)) {
1.1.1.8   root     20638:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     20639:                                // reset pic
                   20640:                                pic_init();
                   20641:                                pic[0].irr = pic[1].irr = 0x00;
                   20642:                                pic[0].imr = pic[1].imr = 0xff;
                   20643:                        }
                   20644:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     20645:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   20646:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   20647:                        i386_jmp_far(selector, address);
1.1.1.7   root     20648:                }
                   20649:                i386_set_a20_line((val >> 1) & 1);
                   20650:                break;
                   20651:        }
                   20652:        kbd_command = val;
1.1.1.8   root     20653:        kbd_status |= 8;
1.1.1.7   root     20654: }
                   20655: 
1.1.1.9   root     20656: // vga
                   20657: 
                   20658: UINT8 vga_read_status()
                   20659: {
                   20660:        // 60hz
                   20661:        static const int period[3] = {16, 17, 17};
                   20662:        static int index = 0;
                   20663:        UINT32 time = timeGetTime() % period[index];
                   20664:        
                   20665:        index = (index + 1) % 3;
1.1.1.14  root     20666:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     20667: }
                   20668: 
1.1       root     20669: // i/o bus
                   20670: 
1.1.1.29  root     20671: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   20672: //#define SW1US_PATCH
                   20673: 
1.1.1.25  root     20674: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     20675: #ifdef USE_DEBUGGER
1.1.1.25  root     20676: {
1.1.1.33  root     20677:        if(now_debugging) {
                   20678:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20679:                        if(in_break_point.table[i].status == 1) {
                   20680:                                if(addr == in_break_point.table[i].addr) {
                   20681:                                        in_break_point.hit = i + 1;
                   20682:                                        now_suspended = true;
                   20683:                                        break;
                   20684:                                }
                   20685:                        }
                   20686:                }
1.1.1.25  root     20687:        }
1.1.1.33  root     20688:        return(debugger_read_io_byte(addr));
1.1.1.25  root     20689: }
1.1.1.33  root     20690: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     20691: #endif
1.1       root     20692: {
1.1.1.33  root     20693:        UINT8 val = 0xff;
                   20694:        
1.1       root     20695:        switch(addr) {
1.1.1.29  root     20696: #ifdef SW1US_PATCH
                   20697:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20698:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     20699:                val = sio_read(0, addr - 1);
                   20700:                break;
1.1.1.29  root     20701: #else
1.1.1.25  root     20702:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20703:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     20704:                val = dma_read(0, addr);
                   20705:                break;
1.1.1.29  root     20706: #endif
1.1.1.25  root     20707:        case 0x20: case 0x21:
1.1.1.33  root     20708:                val = pic_read(0, addr);
                   20709:                break;
1.1.1.25  root     20710:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     20711:                val = pit_read(addr & 0x03);
                   20712:                break;
1.1.1.7   root     20713:        case 0x60:
1.1.1.33  root     20714:                val = kbd_read_data();
                   20715:                break;
1.1.1.9   root     20716:        case 0x61:
1.1.1.33  root     20717:                val = system_port;
                   20718:                break;
1.1.1.7   root     20719:        case 0x64:
1.1.1.33  root     20720:                val = kbd_read_status();
                   20721:                break;
1.1       root     20722:        case 0x71:
1.1.1.33  root     20723:                val = cmos_read(cmos_addr);
                   20724:                break;
1.1.1.25  root     20725:        case 0x81:
1.1.1.33  root     20726:                val = dma_page_read(0, 2);
                   20727:                break;
1.1.1.25  root     20728:        case 0x82:
1.1.1.33  root     20729:                val = dma_page_read(0, 3);
                   20730:                break;
1.1.1.25  root     20731:        case 0x83:
1.1.1.33  root     20732:                val = dma_page_read(0, 1);
                   20733:                break;
1.1.1.25  root     20734:        case 0x87:
1.1.1.33  root     20735:                val = dma_page_read(0, 0);
                   20736:                break;
1.1.1.25  root     20737:        case 0x89:
1.1.1.33  root     20738:                val = dma_page_read(1, 2);
                   20739:                break;
1.1.1.25  root     20740:        case 0x8a:
1.1.1.33  root     20741:                val = dma_page_read(1, 3);
                   20742:                break;
1.1.1.25  root     20743:        case 0x8b:
1.1.1.33  root     20744:                val = dma_page_read(1, 1);
                   20745:                break;
1.1.1.25  root     20746:        case 0x8f:
1.1.1.33  root     20747:                val = dma_page_read(1, 0);
                   20748:                break;
1.1       root     20749:        case 0x92:
1.1.1.33  root     20750:                val = (m_a20_mask >> 19) & 2;
                   20751:                break;
1.1.1.25  root     20752:        case 0xa0: case 0xa1:
1.1.1.33  root     20753:                val = pic_read(1, addr);
                   20754:                break;
1.1.1.25  root     20755:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20756:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     20757:                val = dma_read(1, (addr - 0xc0) >> 1);
                   20758:                break;
1.1.1.37  root     20759:        case 0x278: case 0x279: case 0x27a:
                   20760:                val = pio_read(1, addr);
                   20761:                break;
1.1.1.29  root     20762:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     20763:                val = sio_read(3, addr);
                   20764:                break;
1.1.1.25  root     20765:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     20766:                val = sio_read(1, addr);
                   20767:                break;
1.1.1.25  root     20768:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     20769:                val = pio_read(0, addr);
                   20770:                break;
1.1.1.25  root     20771:        case 0x3ba: case 0x3da:
1.1.1.33  root     20772:                val = vga_read_status();
                   20773:                break;
1.1.1.37  root     20774:        case 0x3bc: case 0x3bd: case 0x3be:
                   20775:                val = pio_read(2, addr);
                   20776:                break;
1.1.1.58  root     20777:        case 0x3d5:
                   20778:                if(crtc_addr < 16) {
                   20779:                        val = crtc_regs[crtc_addr];
                   20780:                }
                   20781:                break;
1.1.1.29  root     20782:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     20783:                val = sio_read(2, addr);
                   20784:                break;
1.1.1.25  root     20785:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     20786:                val = sio_read(0, addr);
                   20787:                break;
1.1       root     20788:        default:
1.1.1.33  root     20789: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     20790:                break;
                   20791:        }
1.1.1.33  root     20792: #ifdef ENABLE_DEBUG_IOPORT
                   20793:        if(fp_debug_log != NULL) {
                   20794:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   20795:        }
                   20796: #endif
                   20797:        return(val);
1.1       root     20798: }
                   20799: 
                   20800: UINT16 read_io_word(offs_t addr)
                   20801: {
                   20802:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   20803: }
                   20804: 
1.1.1.33  root     20805: #ifdef USE_DEBUGGER
                   20806: UINT16 debugger_read_io_word(offs_t addr)
                   20807: {
                   20808:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   20809: }
                   20810: #endif
                   20811: 
1.1       root     20812: UINT32 read_io_dword(offs_t addr)
                   20813: {
                   20814:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   20815: }
                   20816: 
1.1.1.33  root     20817: #ifdef USE_DEBUGGER
                   20818: UINT32 debugger_read_io_dword(offs_t addr)
                   20819: {
                   20820:        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));
                   20821: }
                   20822: #endif
                   20823: 
1.1       root     20824: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     20825: #ifdef USE_DEBUGGER
                   20826: {
                   20827:        if(now_debugging) {
                   20828:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20829:                        if(out_break_point.table[i].status == 1) {
                   20830:                                if(addr == out_break_point.table[i].addr) {
                   20831:                                        out_break_point.hit = i + 1;
                   20832:                                        now_suspended = true;
                   20833:                                        break;
                   20834:                                }
                   20835:                        }
                   20836:                }
                   20837:        }
                   20838:        debugger_write_io_byte(addr, val);
                   20839: }
                   20840: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   20841: #endif
1.1       root     20842: {
1.1.1.25  root     20843: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     20844:        if(fp_debug_log != NULL) {
1.1.1.43  root     20845: #ifdef USE_SERVICE_THREAD
                   20846:                if(addr != 0xf7)
                   20847: #endif
1.1.1.33  root     20848:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     20849:        }
                   20850: #endif
1.1       root     20851:        switch(addr) {
1.1.1.29  root     20852: #ifdef SW1US_PATCH
                   20853:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20854:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   20855:                sio_write(0, addr - 1, val);
                   20856:                break;
                   20857: #else
1.1.1.25  root     20858:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20859:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   20860:                dma_write(0, addr, val);
                   20861:                break;
1.1.1.29  root     20862: #endif
1.1.1.25  root     20863:        case 0x20: case 0x21:
1.1       root     20864:                pic_write(0, addr, val);
                   20865:                break;
1.1.1.25  root     20866:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     20867:                pit_write(addr & 0x03, val);
                   20868:                break;
1.1.1.7   root     20869:        case 0x60:
                   20870:                kbd_write_data(val);
                   20871:                break;
1.1.1.9   root     20872:        case 0x61:
                   20873:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   20874:                        // beep on
                   20875: //                     MessageBeep(-1);
                   20876:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   20877:                        // beep off
                   20878:                }
                   20879:                system_port = val;
                   20880:                break;
1.1       root     20881:        case 0x64:
1.1.1.7   root     20882:                kbd_write_command(val);
1.1       root     20883:                break;
                   20884:        case 0x70:
                   20885:                cmos_addr = val;
                   20886:                break;
                   20887:        case 0x71:
1.1.1.8   root     20888:                cmos_write(cmos_addr, val);
1.1       root     20889:                break;
1.1.1.25  root     20890:        case 0x81:
                   20891:                dma_page_write(0, 2, val);
                   20892:        case 0x82:
                   20893:                dma_page_write(0, 3, val);
                   20894:        case 0x83:
                   20895:                dma_page_write(0, 1, val);
                   20896:        case 0x87:
                   20897:                dma_page_write(0, 0, val);
                   20898:        case 0x89:
                   20899:                dma_page_write(1, 2, val);
                   20900:        case 0x8a:
                   20901:                dma_page_write(1, 3, val);
                   20902:        case 0x8b:
                   20903:                dma_page_write(1, 1, val);
                   20904:        case 0x8f:
                   20905:                dma_page_write(1, 0, val);
1.1       root     20906:        case 0x92:
1.1.1.7   root     20907:                i386_set_a20_line((val >> 1) & 1);
1.1       root     20908:                break;
1.1.1.25  root     20909:        case 0xa0: case 0xa1:
1.1       root     20910:                pic_write(1, addr, val);
                   20911:                break;
1.1.1.25  root     20912:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20913:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     20914:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     20915:                break;
1.1.1.35  root     20916: #ifdef USE_SERVICE_THREAD
                   20917:        case 0xf7:
                   20918:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     20919:                if(in_service && cursor_moved) {
                   20920:                        // update cursor position before service is done
                   20921:                        pcbios_update_cursor_position();
                   20922:                        cursor_moved = false;
                   20923:                }
1.1.1.35  root     20924:                finish_service_loop();
                   20925:                break;
                   20926: #endif
1.1.1.37  root     20927:        case 0x278: case 0x279: case 0x27a:
                   20928:                pio_write(1, addr, val);
                   20929:                break;
1.1.1.29  root     20930:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   20931:                sio_write(3, addr, val);
                   20932:                break;
1.1.1.25  root     20933:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   20934:                sio_write(1, addr, val);
                   20935:                break;
                   20936:        case 0x378: case 0x379: case 0x37a:
                   20937:                pio_write(0, addr, val);
                   20938:                break;
1.1.1.37  root     20939:        case 0x3bc: case 0x3bd: case 0x3be:
                   20940:                pio_write(2, addr, val);
                   20941:                break;
1.1.1.58  root     20942:        case 0x3d4:
                   20943:                crtc_addr = val;
                   20944:                break;
                   20945:        case 0x3d5:
                   20946:                if(crtc_addr < 16) {
                   20947:                        if(crtc_regs[crtc_addr] != val) {
                   20948:                                crtc_regs[crtc_addr] = val;
                   20949:                                crtc_changed[crtc_addr] = 1;
                   20950:                        }
                   20951:                }
                   20952:                break;
1.1.1.29  root     20953:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   20954:                sio_write(2, addr, val);
                   20955:                break;
1.1.1.25  root     20956:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   20957:                sio_write(0, addr, val);
                   20958:                break;
1.1       root     20959:        default:
1.1.1.33  root     20960: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     20961:                break;
                   20962:        }
                   20963: }
                   20964: 
                   20965: void write_io_word(offs_t addr, UINT16 val)
                   20966: {
                   20967:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20968:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20969: }
                   20970: 
1.1.1.33  root     20971: #ifdef USE_DEBUGGER
                   20972: void debugger_write_io_word(offs_t addr, UINT16 val)
                   20973: {
                   20974:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20975:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20976: }
                   20977: #endif
                   20978: 
1.1       root     20979: void write_io_dword(offs_t addr, UINT32 val)
                   20980: {
                   20981:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20982:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20983:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20984:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20985: }
1.1.1.33  root     20986: 
                   20987: #ifdef USE_DEBUGGER
                   20988: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   20989: {
                   20990:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20991:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20992:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20993:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20994: }
                   20995: #endif

unix.superglobalmegacorp.com

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