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

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;
1.1.1.64  root      789:        #define i386_get_flags()                get_flags()
                    790:        #define i386_set_flags(x)               set_flags(x)
1.1.1.3   root      791: #else
                    792:        #define REG8(x)                         m_regs.b[x]
                    793:        #define REG16(x)                        m_regs.w[x]
                    794:        #define SREG(x)                         m_sregs[x]
                    795:        #define SREG_BASE(x)                    m_base[x]
1.1.1.33  root      796:        #define m_eip                           (m_pc - m_base[CS])
1.1.1.3   root      797:        #define m_CF                            m_CarryVal
                    798:        #define m_a20_mask                      AMASK
                    799:        #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
1.1.1.64  root      800:        void i386_sreg_load(UINT16 selector, UINT8 reg, bool *fault)
                    801:        {
                    802: #if defined(HAS_I286)
                    803:                i80286_data_descriptor(reg, selector);
                    804: #else
                    805:                m_sregs[reg] = selector;
                    806:                m_base[reg] = SegBase(reg);
                    807: #endif
                    808:        }
                    809:        #define i386_get_flags()                CompressFlags()
                    810:        #define i386_set_flags(x)               ExpandFlags(x)
1.1.1.3   root      811:        #if defined(HAS_I286)
                    812:                #define i386_set_a20_line(x)    i80286_set_a20_line(x)
                    813:        #else
                    814:                #define i386_set_a20_line(x)
                    815:        #endif
                    816:        #define i386_set_irq_line(x, y)         set_irq_line(x, y)
                    817: #endif
1.1       root      818: 
                    819: void i386_jmp_far(UINT16 selector, UINT32 address)
                    820: {
1.1.1.3   root      821: #if defined(HAS_I386)
1.1       root      822:        if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3   root      823:                i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1       root      824:        } else {
1.1.1.3   root      825:                SREG(CS) = selector;
                    826:                m_performed_intersegment_jump = 1;
                    827:                i386_load_segment_descriptor(CS);
                    828:                m_eip = address;
                    829:                CHANGE_PC(m_eip);
1.1       root      830:        }
1.1.1.3   root      831: #elif defined(HAS_I286)
                    832:        i80286_code_descriptor(selector, address, 1);
                    833: #else
                    834:        SREG(CS) = selector;
                    835:        i386_load_segment_descriptor(CS);
                    836:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    837: #endif
1.1       root      838: }
                    839: 
1.1.1.24  root      840: void i386_call_far(UINT16 selector, UINT32 address)
                    841: {
                    842: #if defined(HAS_I386)
                    843:        if(PROTECTED_MODE && !V8086_MODE) {
                    844:                i386_protected_mode_call(selector, address, 1, m_operand_size);
                    845:        } else {
                    846:                PUSH16(SREG(CS));
                    847:                PUSH16(m_eip);
                    848:                SREG(CS) = selector;
                    849:                m_performed_intersegment_jump = 1;
                    850:                i386_load_segment_descriptor(CS);
                    851:                m_eip = address;
                    852:                CHANGE_PC(m_eip);
                    853:        }
                    854: #else
                    855:        UINT16 ip = m_pc - SREG_BASE(CS);
                    856:        UINT16 cs = SREG(CS);
                    857: #if defined(HAS_I286)
                    858:        i80286_code_descriptor(selector, address, 2);
                    859: #else
                    860:        SREG(CS) = selector;
                    861:        i386_load_segment_descriptor(CS);
                    862:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    863: #endif
                    864:        PUSH(cs);
                    865:        PUSH(ip);
                    866:        CHANGE_PC(m_pc);
                    867: #endif
                    868: }
1.1.1.49  root      869: 
                    870: void i386_push16(UINT16 value)
                    871: {
                    872: #if defined(HAS_I386)
                    873:        PUSH16(value);
                    874: #else
                    875:        PUSH(value);
1.1.1.35  root      876: #endif
1.1.1.49  root      877: }
                    878: 
                    879: UINT16 i386_pop16()
                    880: {
                    881: #if defined(HAS_I386)
                    882:        return POP16();
                    883: #else
                    884:        UINT16 value;
                    885:        POP(value);
                    886:        return value;
                    887: #endif
                    888: }
1.1.1.24  root      889: 
1.1.1.29  root      890: UINT16 i386_read_stack()
                    891: {
                    892: #if defined(HAS_I386)
                    893:        UINT32 ea, new_esp;
                    894:        if( STACK_32BIT ) {
                    895:                new_esp = REG32(ESP) + 2;
                    896:                ea = i386_translate(SS, new_esp - 2, 0);
                    897:        } else {
                    898:                new_esp = REG16(SP) + 2;
                    899:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    900:        }
                    901:        return READ16(ea);
                    902: #else
                    903:        UINT16 sp = m_regs.w[SP] + 2;
                    904:        return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
                    905: #endif
                    906: }
                    907: 
1.1.1.53  root      908: void i386_write_stack(UINT16 value)
                    909: {
                    910: #if defined(HAS_I386)
                    911:        UINT32 ea, new_esp;
                    912:        if( STACK_32BIT ) {
                    913:                new_esp = REG32(ESP) + 2;
                    914:                ea = i386_translate(SS, new_esp - 2, 0);
                    915:        } else {
                    916:                new_esp = REG16(SP) + 2;
                    917:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    918:        }
                    919:        WRITE16(ea, value);
                    920: #else
                    921:        UINT16 sp = m_regs.w[SP] + 2;
                    922:        WriteWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK), value);
                    923: #endif
                    924: }
                    925: 
1.1       root      926: /* ----------------------------------------------------------------------------
1.1.1.33  root      927:        debugger
                    928: ---------------------------------------------------------------------------- */
                    929: 
                    930: #ifdef USE_DEBUGGER
                    931: #define TELNET_BLUE      0x0004 // text color contains blue.
                    932: #define TELNET_GREEN     0x0002 // text color contains green.
                    933: #define TELNET_RED       0x0001 // text color contains red.
                    934: #define TELNET_INTENSITY 0x0008 // text color is intensified.
                    935: 
                    936: int svr_socket = 0;
                    937: int cli_socket = 0;
                    938: 
1.1.1.55  root      939: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error);
1.1.1.33  root      940: 
                    941: void debugger_init()
                    942: {
                    943:        now_debugging = false;
                    944:        now_going = false;
                    945:        now_suspended = false;
                    946:        force_suspend = false;
                    947:        
                    948:        memset(&break_point, 0, sizeof(break_point_t));
                    949:        memset(&rd_break_point, 0, sizeof(break_point_t));
                    950:        memset(&wr_break_point, 0, sizeof(break_point_t));
                    951:        memset(&in_break_point, 0, sizeof(break_point_t));
                    952:        memset(&out_break_point, 0, sizeof(break_point_t));
                    953:        memset(&int_break_point, 0, sizeof(int_break_point_t));
                    954: }
                    955: 
1.1.1.45  root      956: void telnet_send(const char *string)
1.1.1.33  root      957: {
                    958:        char buffer[8192], *ptr;
                    959:        strcpy(buffer, string);
                    960:        while((ptr = strstr(buffer, "\n")) != NULL) {
                    961:                char tmp[8192];
                    962:                *ptr = '\0';
                    963:                sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
                    964:                strcpy(buffer, tmp);
                    965:        }
                    966:        
                    967:        int len = strlen(buffer), res;
                    968:        ptr = buffer;
                    969:        while(len > 0) {
                    970:                if((res = send(cli_socket, ptr, len, 0)) > 0) {
                    971:                        len -= res;
                    972:                        ptr += res;
                    973:                }
                    974:        }
                    975: }
                    976: 
                    977: void telnet_command(const char *format, ...)
                    978: {
                    979:        char buffer[1024];
                    980:        va_list ap;
                    981:        va_start(ap, format);
                    982:        vsprintf(buffer, format, ap);
                    983:        va_end(ap);
                    984:        
                    985:        telnet_send(buffer);
                    986: }
                    987: 
                    988: void telnet_printf(const char *format, ...)
                    989: {
                    990:        char buffer[1024];
                    991:        va_list ap;
                    992:        va_start(ap, format);
                    993:        vsprintf(buffer, format, ap);
                    994:        va_end(ap);
                    995:        
                    996:        if(fp_debugger != NULL) {
                    997:                fprintf(fp_debugger, "%s", buffer);
                    998:        }
                    999:        telnet_send(buffer);
                   1000: }
                   1001: 
                   1002: bool telnet_gets(char *str, int n)
                   1003: {
                   1004:        char buffer[1024];
                   1005:        int ptr = 0;
                   1006:        
                   1007:        telnet_command("\033[12l"); // local echo on
                   1008:        telnet_command("\033[2l");  // key unlock
                   1009:        
1.1.1.54  root     1010:        while(!m_exit) {
1.1.1.33  root     1011:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1012:                
                   1013:                if(len > 0 && buffer[0] != 0xff) {
                   1014:                        for(int i = 0; i < len; i++) {
                   1015:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                   1016:                                        str[ptr] = 0;
                   1017:                                        telnet_command("\033[2h");  // key lock
                   1018:                                        telnet_command("\033[12h"); // local echo off
1.1.1.54  root     1019:                                        return(!m_exit);
1.1.1.33  root     1020:                                } else if(buffer[i] == 0x08) {
                   1021:                                        if(ptr > 0) {
                   1022:                                                telnet_command("\033[0K"); // erase from cursor position
                   1023:                                                ptr--;
                   1024:                                        } else {
                   1025:                                                telnet_command("\033[1C"); // move cursor forward
                   1026:                                        }
                   1027:                                } else if(ptr < n - 1) {
1.1.1.37  root     1028:                                        if(buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
1.1.1.33  root     1029:                                                str[ptr++] = buffer[i];
                   1030:                                        }
                   1031:                                } else {
                   1032:                                        telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
                   1033:                                }
                   1034:                        }
                   1035:                } else if(len == -1) {
                   1036:                        if(WSAGetLastError() != WSAEWOULDBLOCK) {
                   1037:                                return(false);
                   1038:                        }
                   1039:                } else if(len == 0) {
                   1040:                        return(false);
                   1041:                }
                   1042:                Sleep(10);
                   1043:        }
1.1.1.54  root     1044:        return(!m_exit);
1.1.1.33  root     1045: }
                   1046: 
                   1047: bool telnet_kbhit()
                   1048: {
                   1049:        char buffer[1024];
                   1050:        
1.1.1.54  root     1051:        if(!m_exit) {
1.1.1.33  root     1052:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1053:                
                   1054:                if(len > 0) {
                   1055:                        for(int i = 0; i < len; i++) {
                   1056:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                   1057:                                        return(true);
                   1058:                                }
                   1059:                        }
                   1060:                } else if(len == 0) {
                   1061:                        return(true); // disconnected
                   1062:                }
                   1063:        }
                   1064:        return(false);
                   1065: }
                   1066: 
                   1067: bool telnet_disconnected()
                   1068: {
                   1069:        char buffer[1024];
                   1070:        int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1071:        
                   1072:        if(len == 0) {
                   1073:                return(true);
                   1074:        } else if(len == -1) {
                   1075:                if(WSAGetLastError() != WSAEWOULDBLOCK) {
                   1076:                        return(true);
                   1077:                }
                   1078:        }
                   1079:        return(false);
                   1080: }
                   1081: 
                   1082: void telnet_set_color(int color)
                   1083: {
                   1084:        telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
                   1085: }
                   1086: 
                   1087: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
                   1088: {
                   1089: //     UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
                   1090:        UINT8 ops[16];
                   1091:        for(int i = 0; i < 16; i++) {
                   1092:                ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
                   1093:        }
                   1094:        UINT8 *oprom = ops;
                   1095:        
                   1096: #if defined(HAS_I386)
                   1097:        if(m_operand_size) {
                   1098:                return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
                   1099:        } else
                   1100: #endif
                   1101:        return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
                   1102: }
                   1103: 
                   1104: void debugger_regs_info(char *buffer)
                   1105: {
1.1.1.64  root     1106:        UINT32 flags = i386_get_flags();
                   1107:        
1.1.1.33  root     1108: #if defined(HAS_I386)
                   1109:        if(m_operand_size) {
                   1110:                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",
                   1111:                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),
                   1112:                PROTECTED_MODE ? "PE" : "--",
                   1113:                (flags & 0x40000) ? 'A' : '-',
                   1114:                (flags & 0x20000) ? 'V' : '-',
                   1115:                (flags & 0x10000) ? 'R' : '-',
                   1116:                (flags & 0x04000) ? 'N' : '-',
                   1117:                (flags & 0x02000) ? '1' : '0',
                   1118:                (flags & 0x01000) ? '1' : '0',
                   1119:                (flags & 0x00800) ? 'O' : '-',
                   1120:                (flags & 0x00400) ? 'D' : '-',
                   1121:                (flags & 0x00200) ? 'I' : '-',
                   1122:                (flags & 0x00100) ? 'T' : '-',
                   1123:                (flags & 0x00080) ? 'S' : '-',
                   1124:                (flags & 0x00040) ? 'Z' : '-',
                   1125:                (flags & 0x00010) ? 'A' : '-',
                   1126:                (flags & 0x00004) ? 'P' : '-',
                   1127:                (flags & 0x00001) ? 'C' : '-');
                   1128:        } else {
                   1129: #endif
                   1130:                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",
                   1131:                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),
                   1132: #if defined(HAS_I386)
                   1133:                PROTECTED_MODE ? "PE" : "--",
                   1134: #else
                   1135:                "--",
                   1136: #endif
                   1137:                (flags & 0x40000) ? 'A' : '-',
                   1138:                (flags & 0x20000) ? 'V' : '-',
                   1139:                (flags & 0x10000) ? 'R' : '-',
                   1140:                (flags & 0x04000) ? 'N' : '-',
                   1141:                (flags & 0x02000) ? '1' : '0',
                   1142:                (flags & 0x01000) ? '1' : '0',
                   1143:                (flags & 0x00800) ? 'O' : '-',
                   1144:                (flags & 0x00400) ? 'D' : '-',
                   1145:                (flags & 0x00200) ? 'I' : '-',
                   1146:                (flags & 0x00100) ? 'T' : '-',
                   1147:                (flags & 0x00080) ? 'S' : '-',
                   1148:                (flags & 0x00040) ? 'Z' : '-',
                   1149:                (flags & 0x00010) ? 'A' : '-',
                   1150:                (flags & 0x00004) ? 'P' : '-',
                   1151:                (flags & 0x00001) ? 'C' : '-');
                   1152: #if defined(HAS_I386)
                   1153:        }
                   1154: #endif
                   1155: }
                   1156: 
                   1157: void debugger_process_info(char *buffer)
                   1158: {
                   1159:        UINT16 psp_seg = current_psp;
                   1160:        process_t *process;
                   1161:        bool check[0x10000] = {0};
                   1162:        
                   1163:        buffer[0] = '\0';
                   1164:        
                   1165:        while(!check[psp_seg] && (process  = msdos_process_info_get(psp_seg, false)) != NULL) {
                   1166:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1167:                char *file = process->module_path, *s;
                   1168:                char tmp[8192];
                   1169:                
                   1170:                while((s = strstr(file, "\\")) != NULL) {
                   1171:                        file = s + 1;
                   1172:                }
                   1173:                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));
                   1174:                strcat(tmp, buffer);
                   1175:                strcpy(buffer, tmp);
                   1176:                
                   1177:                check[psp_seg] = true;
                   1178:                psp_seg = psp->parent_psp;
                   1179:        }
                   1180: }
                   1181: 
                   1182: UINT32 debugger_get_val(const char *str)
                   1183: {
                   1184:        char tmp[1024];
                   1185:        
                   1186:        if(str == NULL || strlen(str) == 0) {
                   1187:                return(0);
                   1188:        }
                   1189:        strcpy(tmp, str);
                   1190:        
                   1191:        if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
                   1192:                // ank
                   1193:                return(tmp[1] & 0xff);
                   1194:        } else if(tmp[0] == '%') {
                   1195:                // decimal
                   1196:                return(strtoul(tmp + 1, NULL, 10));
                   1197:        }
                   1198:        return(strtoul(tmp, NULL, 16));
                   1199: }
                   1200: 
                   1201: UINT32 debugger_get_seg(const char *str, UINT32 val)
                   1202: {
                   1203:        char tmp[1024], *s;
                   1204:        
                   1205:        if(str == NULL || strlen(str) == 0) {
                   1206:                return(val);
                   1207:        }
                   1208:        strcpy(tmp, str);
                   1209:        
                   1210:        if((s = strstr(tmp, ":")) != NULL) {
                   1211:                // 0000:0000
                   1212:                *s = '\0';
                   1213:                return(debugger_get_val(tmp));
                   1214:        }
                   1215:        return(val);
                   1216: }
                   1217: 
                   1218: UINT32 debugger_get_ofs(const char *str)
                   1219: {
                   1220:        char tmp[1024], *s;
                   1221:        
                   1222:        if(str == NULL || strlen(str) == 0) {
                   1223:                return(0);
                   1224:        }
                   1225:        strcpy(tmp, str);
                   1226:        
                   1227:        if((s = strstr(tmp, ":")) != NULL) {
                   1228:                // 0000:0000
                   1229:                return(debugger_get_val(s + 1));
                   1230:        }
                   1231:        return(debugger_get_val(tmp));
                   1232: }
                   1233: 
                   1234: void debugger_main()
                   1235: {
                   1236:        telnet_command("\033[20h"); // cr-lf
                   1237:        
                   1238:        force_suspend = true;
                   1239:        now_going = false;
                   1240:        now_debugging = true;
                   1241:        Sleep(100);
                   1242:        
1.1.1.54  root     1243:        if(!m_exit && !now_suspended) {
1.1.1.33  root     1244:                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1245:                telnet_printf("waiting until cpu is suspended...\n");
                   1246:        }
1.1.1.54  root     1247:        while(!m_exit && !now_suspended) {
1.1.1.33  root     1248:                if(telnet_disconnected()) {
                   1249:                        break;
                   1250:                }
                   1251:                Sleep(10);
                   1252:        }
                   1253:        
                   1254:        char buffer[8192];
                   1255:        
                   1256:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1257:        debugger_process_info(buffer);
                   1258:        telnet_printf("%s", buffer);
                   1259:        debugger_regs_info(buffer);
                   1260:        telnet_printf("%s", buffer);
                   1261:        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1262:        telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
                   1263:        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1264:        debugger_dasm(buffer, SREG(CS), m_eip);
                   1265:        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1266:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1267:        
                   1268:        #define MAX_COMMAND_LEN 64
                   1269:        
                   1270:        char command[MAX_COMMAND_LEN + 1];
                   1271:        char prev_command[MAX_COMMAND_LEN + 1] = {0};
                   1272:        
                   1273:        UINT32 data_seg = SREG(DS);
                   1274:        UINT32 data_ofs = 0;
                   1275:        UINT32 dasm_seg = SREG(CS);
                   1276:        UINT32 dasm_ofs = m_eip;
                   1277:        
1.1.1.54  root     1278:        while(!m_exit) {
1.1.1.33  root     1279:                telnet_printf("- ");
                   1280:                command[0] = '\0';
                   1281:                
                   1282:                if(fi_debugger != NULL) {
                   1283:                        while(command[0] == '\0') {
                   1284:                                if(fgets(command, sizeof(command), fi_debugger) == NULL) {
                   1285:                                        break;
                   1286:                                }
                   1287:                                while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
                   1288:                                        command[strlen(command) - 1] = '\0';
                   1289:                                }
                   1290:                        }
                   1291:                        if(command[0] != '\0') {
                   1292:                                telnet_command("%s\n", command);
                   1293:                        }
                   1294:                }
                   1295:                if(command[0] == '\0') {
                   1296:                        if(!telnet_gets(command, sizeof(command))) {
                   1297:                                break;
                   1298:                        }
                   1299:                }
                   1300:                if(command[0] == '\0') {
                   1301:                        strcpy(command, prev_command);
                   1302:                } else {
                   1303:                        strcpy(prev_command, command);
                   1304:                }
                   1305:                if(fp_debugger != NULL) {
                   1306:                        fprintf(fp_debugger, "%s\n", command);
                   1307:                }
                   1308:                
1.1.1.54  root     1309:                if(!m_exit && command[0] != 0) {
1.1.1.33  root     1310:                        char *params[32], *token = NULL;
                   1311:                        int num = 0;
                   1312:                        
                   1313:                        if((token = strtok(command, " ")) != NULL) {
                   1314:                                params[num++] = token;
                   1315:                                while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
                   1316:                                        params[num++] = token;
                   1317:                                }
                   1318:                        }
                   1319:                        if(stricmp(params[0], "D") == 0) {
                   1320:                                if(num <= 3) {
                   1321:                                        if(num >= 2) {
                   1322:                                                data_seg = debugger_get_seg(params[1], data_seg);
                   1323:                                                data_ofs = debugger_get_ofs(params[1]);
                   1324:                                        }
                   1325:                                        UINT32 end_seg = data_seg;
                   1326:                                        UINT32 end_ofs = data_ofs + 8 * 16 - 1;
                   1327:                                        if(num == 3) {
                   1328:                                                end_seg = debugger_get_seg(params[2], data_seg);
                   1329:                                                end_ofs = debugger_get_ofs(params[2]);
                   1330:                                        }
                   1331:                                        UINT64 start_addr = (data_seg << 4) + data_ofs;
                   1332:                                        UINT64 end_addr = (end_seg << 4) + end_ofs;
1.1.1.37  root     1333: //                                     bool is_sjis = false;
1.1.1.33  root     1334:                                        
                   1335:                                        for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
                   1336:                                                if((addr & 0x0f) == 0) {
                   1337:                                                        if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
                   1338:                                                                data_seg += 0x1000;
                   1339:                                                                data_ofs -= 0x10000;
                   1340:                                                        }
                   1341:                                                        telnet_printf("%06X:%04X ", data_seg, data_ofs);
                   1342:                                                        memset(buffer, 0, sizeof(buffer));
                   1343:                                                }
                   1344:                                                if(addr < start_addr || addr > end_addr) {
                   1345:                                                        telnet_printf("   ");
                   1346:                                                        buffer[addr & 0x0f] = ' ';
                   1347:                                                } else {
                   1348:                                                        UINT8 data = debugger_read_byte(addr & ADDR_MASK);
                   1349:                                                        telnet_printf(" %02X", data);
1.1.1.37  root     1350: //                                                     if(is_sjis) {
1.1.1.33  root     1351: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1352: //                                                             is_sjis = false;
1.1.1.33  root     1353: //                                                     } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
                   1354: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1355: //                                                             is_sjis = true;
1.1.1.33  root     1356: //                                                     } else
                   1357:                                                        if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
                   1358:                                                                buffer[addr & 0x0f] = data;
                   1359:                                                        } else {
                   1360:                                                                buffer[addr & 0x0f] = '.';
                   1361:                                                        }
                   1362:                                                }
                   1363:                                                if((addr & 0x0f) == 0x0f) {
                   1364:                                                        telnet_printf("  %s\n", buffer);
                   1365:                                                }
                   1366:                                        }
                   1367:                                        if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
                   1368:                                                data_seg += 0x1000;
                   1369:                                                data_ofs -= 0x10000;
                   1370:                                        }
                   1371:                                        prev_command[1] = '\0'; // remove parameters to dump continuously
                   1372:                                } else {
                   1373:                                        telnet_printf("invalid parameter number\n");
                   1374:                                }
                   1375:                        } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 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++) {
                   1380:                                                debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
                   1381:                                        }
                   1382:                                } else {
                   1383:                                        telnet_printf("invalid parameter number\n");
                   1384:                                }
                   1385:                        } else if(stricmp(params[0], "EW") == 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 += 2) {
                   1390:                                                debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
                   1391:                                        }
                   1392:                                } else {
                   1393:                                        telnet_printf("invalid parameter number\n");
                   1394:                                }
                   1395:                        } else if(stricmp(params[0], "ED") == 0) {
                   1396:                                if(num >= 3) {
                   1397:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1398:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1399:                                        for(int i = 2, j = 0; i < num; i++, j += 4) {
                   1400:                                                debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
                   1401:                                        }
                   1402:                                } else {
                   1403:                                        telnet_printf("invalid parameter number\n");
                   1404:                                }
                   1405:                        } else if(stricmp(params[0], "EA") == 0) {
                   1406:                                if(num >= 3) {
                   1407:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1408:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1409:                                        strcpy(buffer, prev_command);
                   1410:                                        if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
                   1411:                                                int len = strlen(token);
                   1412:                                                for(int i = 0; i < len; i++) {
                   1413:                                                        debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
                   1414:                                                }
                   1415:                                        } else {
                   1416:                                                telnet_printf("invalid parameter\n");
                   1417:                                        }
                   1418:                                } else {
                   1419:                                        telnet_printf("invalid parameter number\n");
                   1420:                                }
                   1421:                        } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
                   1422:                                if(num == 2) {
                   1423:                                        telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
                   1424:                                } else {
                   1425:                                        telnet_printf("invalid parameter number\n");
                   1426:                                }
                   1427:                        } else if(stricmp(params[0], "IW") == 0) {
                   1428:                                if(num == 2) {
                   1429:                                        telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
                   1430:                                } else {
                   1431:                                        telnet_printf("invalid parameter number\n");
                   1432:                                }
                   1433:                        } else if(stricmp(params[0], "ID") == 0) {
                   1434:                                if(num == 2) {
                   1435:                                        telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
                   1436:                                } else {
                   1437:                                        telnet_printf("invalid parameter number\n");
                   1438:                                }
                   1439:                        } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
                   1440:                                if(num == 3) {
                   1441:                                        debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
                   1442:                                } else {
                   1443:                                        telnet_printf("invalid parameter number\n");
                   1444:                                }
                   1445:                        } else if(stricmp(params[0], "OW") == 0) {
                   1446:                                if(num == 3) {
                   1447:                                        debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
                   1448:                                } else {
                   1449:                                        telnet_printf("invalid parameter number\n");
                   1450:                                }
                   1451:                        } else if(stricmp(params[0], "OD") == 0) {
                   1452:                                if(num == 3) {
                   1453:                                        debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
                   1454:                                } else {
                   1455:                                        telnet_printf("invalid parameter number\n");
                   1456:                                }
                   1457:                        } else if(stricmp(params[0], "R") == 0) {
                   1458:                                if(num == 1) {
                   1459:                                        debugger_regs_info(buffer);
                   1460:                                        telnet_printf("%s", buffer);
                   1461:                                } else if(num == 3) {
                   1462: #if defined(HAS_I386)
                   1463:                                        if(stricmp(params[1], "EAX") == 0) {
                   1464:                                                REG32(EAX) = debugger_get_val(params[2]);
                   1465:                                        } else if(stricmp(params[1], "EBX") == 0) {
                   1466:                                                REG32(EBX) = debugger_get_val(params[2]);
                   1467:                                        } else if(stricmp(params[1], "ECX") == 0) {
                   1468:                                                REG32(ECX) = debugger_get_val(params[2]);
                   1469:                                        } else if(stricmp(params[1], "EDX") == 0) {
                   1470:                                                REG32(EDX) = debugger_get_val(params[2]);
                   1471:                                        } else if(stricmp(params[1], "ESP") == 0) {
                   1472:                                                REG32(ESP) = debugger_get_val(params[2]);
                   1473:                                        } else if(stricmp(params[1], "EBP") == 0) {
                   1474:                                                REG32(EBP) = debugger_get_val(params[2]);
                   1475:                                        } else if(stricmp(params[1], "ESI") == 0) {
                   1476:                                                REG32(ESI) = debugger_get_val(params[2]);
                   1477:                                        } else if(stricmp(params[1], "EDI") == 0) {
                   1478:                                                REG32(EDI) = debugger_get_val(params[2]);
                   1479:                                        } else
                   1480: #endif
                   1481:                                        if(stricmp(params[1], "AX") == 0) {
                   1482:                                                REG16(AX) = debugger_get_val(params[2]);
                   1483:                                        } else if(stricmp(params[1], "BX") == 0) {
                   1484:                                                REG16(BX) = debugger_get_val(params[2]);
                   1485:                                        } else if(stricmp(params[1], "CX") == 0) {
                   1486:                                                REG16(CX) = debugger_get_val(params[2]);
                   1487:                                        } else if(stricmp(params[1], "DX") == 0) {
                   1488:                                                REG16(DX) = debugger_get_val(params[2]);
                   1489:                                        } else if(stricmp(params[1], "SP") == 0) {
                   1490:                                                REG16(SP) = debugger_get_val(params[2]);
                   1491:                                        } else if(stricmp(params[1], "BP") == 0) {
                   1492:                                                REG16(BP) = debugger_get_val(params[2]);
                   1493:                                        } else if(stricmp(params[1], "SI") == 0) {
                   1494:                                                REG16(SI) = debugger_get_val(params[2]);
                   1495:                                        } else if(stricmp(params[1], "DI") == 0) {
                   1496:                                                REG16(DI) = debugger_get_val(params[2]);
                   1497:                                        } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
                   1498: #if defined(HAS_I386)
                   1499:                                                if(m_operand_size) {
                   1500:                                                        m_eip = debugger_get_val(params[2]);
                   1501:                                                } else {
                   1502:                                                        m_eip = debugger_get_val(params[2]) & 0xffff;
                   1503:                                                }
                   1504:                                                CHANGE_PC(m_eip);
                   1505: #else
                   1506:                                                m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
                   1507:                                                CHANGE_PC(m_pc);
                   1508: #endif
                   1509:                                        } else if(stricmp(params[1], "AL") == 0) {
                   1510:                                                REG8(AL) = debugger_get_val(params[2]);
                   1511:                                        } else if(stricmp(params[1], "AH") == 0) {
                   1512:                                                REG8(AH) = debugger_get_val(params[2]);
                   1513:                                        } else if(stricmp(params[1], "BL") == 0) {
                   1514:                                                REG8(BL) = debugger_get_val(params[2]);
                   1515:                                        } else if(stricmp(params[1], "BH") == 0) {
                   1516:                                                REG8(BH) = debugger_get_val(params[2]);
                   1517:                                        } else if(stricmp(params[1], "CL") == 0) {
                   1518:                                                REG8(CL) = debugger_get_val(params[2]);
                   1519:                                        } else if(stricmp(params[1], "CH") == 0) {
                   1520:                                                REG8(CH) = debugger_get_val(params[2]);
                   1521:                                        } else if(stricmp(params[1], "DL") == 0) {
                   1522:                                                REG8(DL) = debugger_get_val(params[2]);
                   1523:                                        } else if(stricmp(params[1], "DH") == 0) {
                   1524:                                                REG8(DH) = debugger_get_val(params[2]);
                   1525:                                        } else {
                   1526:                                                telnet_printf("unknown register %s\n", params[1]);
                   1527:                                        }
                   1528:                                } else {
                   1529:                                        telnet_printf("invalid parameter number\n");
                   1530:                                }
1.1.1.60  root     1531:                        } else if(stricmp(params[0], "S") == 0) {
1.1.1.33  root     1532:                                if(num >= 4) {
                   1533:                                        UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
                   1534:                                        UINT32 cur_ofs = debugger_get_ofs(params[1]);
                   1535:                                        UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
                   1536:                                        UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1537:                                        UINT8 list[32];
                   1538:                                        
                   1539:                                        for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1540:                                                list[j] = debugger_get_val(params[i]);
                   1541:                                        }
                   1542:                                        while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
                   1543:                                                bool found = true;
                   1544:                                                for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1545:                                                        if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
                   1546:                                                                found = false;
                   1547:                                                                break;
                   1548:                                                        }
                   1549:                                                }
                   1550:                                                if(found) {
                   1551:                                                        telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
                   1552:                                                }
                   1553:                                                if((cur_ofs += 1) > 0xffff) {
                   1554:                                                        cur_seg += 0x1000;
                   1555:                                                        cur_ofs -= 0x10000;
                   1556:                                                }
                   1557:                                        }
                   1558:                                } else {
                   1559:                                        telnet_printf("invalid parameter number\n");
                   1560:                                }
                   1561:                        } else if(stricmp(params[0], "U") == 0) {
                   1562:                                if(num <= 3) {
                   1563:                                        if(num >= 2) {
                   1564:                                                dasm_seg = debugger_get_seg(params[1], dasm_seg);
                   1565:                                                dasm_ofs = debugger_get_ofs(params[1]);
                   1566:                                        }
                   1567:                                        if(num == 3) {
                   1568:                                                UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
                   1569:                                                UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1570:                                                
                   1571:                                                while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
                   1572:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1573:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1574:                                                        for(int i = 0; i < len; i++) {
                   1575:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1576:                                                        }
                   1577:                                                        for(int i = len; i < 8; i++) {
                   1578:                                                                telnet_printf("  ");
                   1579:                                                        }
                   1580:                                                        telnet_printf("  %s\n", buffer);
                   1581:                                                        if((dasm_ofs += len) > 0xffff) {
                   1582:                                                                dasm_seg += 0x1000;
                   1583:                                                                dasm_ofs -= 0x10000;
                   1584:                                                        }
                   1585:                                                }
                   1586:                                        } else {
                   1587:                                                for(int i = 0; i < 16; i++) {
                   1588:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1589:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1590:                                                        for(int i = 0; i < len; i++) {
                   1591:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1592:                                                        }
                   1593:                                                        for(int i = len; i < 8; i++) {
                   1594:                                                                telnet_printf("  ");
                   1595:                                                        }
                   1596:                                                        telnet_printf("  %s\n", buffer);
                   1597:                                                        if((dasm_ofs += len) > 0xffff) {
                   1598:                                                                dasm_seg += 0x1000;
                   1599:                                                                dasm_ofs -= 0x10000;
                   1600:                                                        }
                   1601:                                                }
                   1602:                                        }
                   1603:                                        prev_command[1] = '\0'; // remove parameters to disassemble continuously
                   1604:                                } else {
                   1605:                                        telnet_printf("invalid parameter number\n");
                   1606:                                }
                   1607:                        } else if(stricmp(params[0], "H") == 0) {
                   1608:                                if(num == 3) {
                   1609:                                        UINT32 l = debugger_get_val(params[1]);
                   1610:                                        UINT32 r = debugger_get_val(params[2]);
                   1611:                                        telnet_printf("%08X  %08X\n", l + r, l - r);
                   1612:                                } else {
                   1613:                                        telnet_printf("invalid parameter number\n");
                   1614:                                }
                   1615:                        } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
                   1616:                                break_point_t *break_point_ptr;
                   1617:                                #define GET_BREAK_POINT_PTR() { \
1.1.1.58  root     1618:                                        if(params[0][0] == 'R' || params[0][0] == 'r') { \
1.1.1.33  root     1619:                                                break_point_ptr = &rd_break_point; \
1.1.1.58  root     1620:                                        } else if(params[0][0] == 'W' || params[0][0] == 'w') { \
1.1.1.33  root     1621:                                                break_point_ptr = &wr_break_point; \
1.1.1.58  root     1622:                                        } else if(params[0][0] == 'I' || params[0][0] == 'i') { \
1.1.1.33  root     1623:                                                break_point_ptr = &in_break_point; \
1.1.1.58  root     1624:                                        } else if(params[0][0] == 'O' || params[0][0] == 'o') { \
1.1.1.33  root     1625:                                                break_point_ptr = &out_break_point; \
                   1626:                                        } else { \
                   1627:                                                break_point_ptr = &break_point; \
                   1628:                                        } \
                   1629:                                }
                   1630:                                GET_BREAK_POINT_PTR();
                   1631:                                if(num == 2) {
1.1.1.58  root     1632:                                        UINT32 seg = 0;
                   1633:                                        if(params[0][0] == 'R' || params[0][0] == 'r' || params[0][0] == 'W' || params[0][0] == 'w') {
                   1634:                                                seg = debugger_get_seg(params[1], data_seg);
                   1635:                                        } else {
                   1636:                                                seg = debugger_get_seg(params[1], SREG(CS));
                   1637:                                        }
1.1.1.33  root     1638:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1639:                                        bool found = false;
                   1640:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1641:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
                   1642:                                                        break_point_ptr->table[i].addr = (seg << 4) + ofs;
                   1643:                                                        break_point_ptr->table[i].seg = seg;
                   1644:                                                        break_point_ptr->table[i].ofs = ofs;
                   1645:                                                        break_point_ptr->table[i].status = 1;
                   1646:                                                        found = true;
                   1647:                                                }
                   1648:                                        }
                   1649:                                        if(!found) {
                   1650:                                                telnet_printf("too many break points\n");
                   1651:                                        }
                   1652:                                } else {
                   1653:                                        telnet_printf("invalid parameter number\n");
                   1654:                                }
                   1655:                        } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
                   1656:                                break_point_t *break_point_ptr;
                   1657:                                GET_BREAK_POINT_PTR();
                   1658:                                if(num == 2) {
                   1659:                                        UINT32 addr = debugger_get_val(params[1]);
                   1660:                                        bool found = false;
                   1661:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1662:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
                   1663:                                                        break_point_ptr->table[i].addr = addr;
                   1664:                                                        break_point_ptr->table[i].status = 1;
                   1665:                                                        found = true;
                   1666:                                                }
                   1667:                                        }
                   1668:                                        if(!found) {
                   1669:                                                telnet_printf("too many break points\n");
                   1670:                                        }
                   1671:                                } else {
                   1672:                                        telnet_printf("invalid parameter number\n");
                   1673:                                }
                   1674:                        } 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) {
                   1675:                                break_point_t *break_point_ptr;
                   1676:                                GET_BREAK_POINT_PTR();
                   1677:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1678:                                        memset(break_point_ptr, 0, sizeof(break_point_t));
                   1679:                                } else if(num >= 2) {
                   1680:                                        for(int i = 1; i < num; i++) {
                   1681:                                                int index = debugger_get_val(params[i]);
                   1682:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1683:                                                        telnet_printf("invalid index %x\n", index);
                   1684:                                                } else {
                   1685:                                                        break_point_ptr->table[index - 1].addr = 0;
                   1686:                                                        break_point_ptr->table[index - 1].seg = 0;
                   1687:                                                        break_point_ptr->table[index - 1].ofs = 0;
                   1688:                                                        break_point_ptr->table[index - 1].status = 0;
                   1689:                                                }
                   1690:                                        }
                   1691:                                } else {
                   1692:                                        telnet_printf("invalid parameter number\n");
                   1693:                                }
                   1694:                        } 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 ||
                   1695:                                  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) {
                   1696:                                break_point_t *break_point_ptr;
                   1697:                                GET_BREAK_POINT_PTR();
                   1698:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1699:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1700:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1701:                                                if(break_point_ptr->table[i].status != 0) {
                   1702:                                                        break_point_ptr->table[i].status = enabled ? 1 : -1;
                   1703:                                                }
                   1704:                                        }
                   1705:                                } else if(num >= 2) {
                   1706:                                        for(int i = 1; i < num; i++) {
                   1707:                                                int index = debugger_get_val(params[i]);
                   1708:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1709:                                                        telnet_printf("invalid index %x\n", index);
                   1710:                                                } else if(break_point_ptr->table[index - 1].status == 0) {
                   1711:                                                        telnet_printf("break point %x is null\n", index);
                   1712:                                                } else {
                   1713:                                                        break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
                   1714:                                                }
                   1715:                                        }
                   1716:                                } else {
                   1717:                                        telnet_printf("invalid parameter number\n");
                   1718:                                }
                   1719:                        } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
                   1720:                                break_point_t *break_point_ptr;
                   1721:                                GET_BREAK_POINT_PTR();
                   1722:                                if(num == 1) {
                   1723:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1724:                                                if(break_point_ptr->table[i].status) {
                   1725:                                                        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);
                   1726:                                                }
                   1727:                                        }
                   1728:                                } else {
                   1729:                                        telnet_printf("invalid parameter number\n");
                   1730:                                }
                   1731:                        } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
                   1732:                                break_point_t *break_point_ptr;
                   1733:                                GET_BREAK_POINT_PTR();
                   1734:                                if(num == 1) {
                   1735:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1736:                                                if(break_point_ptr->table[i].status) {
                   1737:                                                        telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
                   1738:                                                }
                   1739:                                        }
                   1740:                                } else {
                   1741:                                        telnet_printf("invalid parameter number\n");
                   1742:                                }
                   1743:                        } else if(stricmp(params[0], "INTBP") == 0) {
                   1744:                                if(num >= 2 && num <= 4) {
                   1745:                                        int int_num = debugger_get_val(params[1]);
                   1746:                                        UINT8 ah = 0, ah_registered = 0;
                   1747:                                        UINT8 al = 0, al_registered = 0;
                   1748:                                        if(num >= 3) {
                   1749:                                                ah = debugger_get_val(params[2]);
                   1750:                                                ah_registered = 1;
                   1751:                                        }
                   1752:                                        if(num == 4) {
                   1753:                                                al = debugger_get_val(params[3]);
                   1754:                                                al_registered = 1;
                   1755:                                        }
                   1756:                                        bool found = false;
                   1757:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1758:                                                if(int_break_point.table[i].status == 0 || (
                   1759:                                                   int_break_point.table[i].int_num == int_num &&
                   1760:                                                   int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
                   1761:                                                   int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
                   1762:                                                        int_break_point.table[i].int_num = int_num;
                   1763:                                                        int_break_point.table[i].ah = ah;
                   1764:                                                        int_break_point.table[i].ah_registered = ah_registered;
                   1765:                                                        int_break_point.table[i].al = al;
                   1766:                                                        int_break_point.table[i].al_registered = al_registered;
                   1767:                                                        int_break_point.table[i].status = 1;
                   1768:                                                        found = true;
                   1769:                                                }
                   1770:                                        }
                   1771:                                        if(!found) {
                   1772:                                                telnet_printf("too many break points\n");
                   1773:                                        }
                   1774:                                } else {
                   1775:                                        telnet_printf("invalid parameter number\n");
                   1776:                                }
                   1777:                        } else if(stricmp(params[0], "INTBC") == 0) {
                   1778:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1779:                                        memset(&int_break_point, 0, sizeof(int_break_point_t));
                   1780:                                } else if(num >= 2) {
                   1781:                                        for(int i = 1; i < num; i++) {
                   1782:                                                int index = debugger_get_val(params[i]);
                   1783:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1784:                                                        telnet_printf("invalid index %x\n", index);
                   1785:                                                } else {
                   1786:                                                        int_break_point.table[index - 1].int_num = 0;
                   1787:                                                        int_break_point.table[index - 1].ah = 0;
                   1788:                                                        int_break_point.table[index - 1].ah_registered = 0;
                   1789:                                                        int_break_point.table[index - 1].al = 0;
                   1790:                                                        int_break_point.table[index - 1].al_registered = 0;
                   1791:                                                        int_break_point.table[index - 1].status = 0;
                   1792:                                                }
                   1793:                                        }
                   1794:                                } else {
                   1795:                                        telnet_printf("invalid parameter number\n");
                   1796:                                }
                   1797:                        } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
                   1798:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1799:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1800:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1801:                                                if(int_break_point.table[i].status != 0) {
                   1802:                                                        int_break_point.table[i].status = enabled ? 1 : -1;
                   1803:                                                }
                   1804:                                        }
                   1805:                                } else if(num >= 2) {
                   1806:                                        for(int i = 1; i < num; i++) {
                   1807:                                                int index = debugger_get_val(params[i]);
                   1808:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1809:                                                        telnet_printf("invalid index %x\n", index);
                   1810:                                                } else if(int_break_point.table[index - 1].status == 0) {
                   1811:                                                        telnet_printf("break point %x is null\n", index);
                   1812:                                                } else {
                   1813:                                                        int_break_point.table[index - 1].status = enabled ? 1 : -1;
                   1814:                                                }
                   1815:                                        }
                   1816:                                } else {
                   1817:                                        telnet_printf("invalid parameter number\n");
                   1818:                                }
                   1819:                        } else if(stricmp(params[0], "INTBL") == 0) {
                   1820:                                if(num == 1) {
                   1821:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1822:                                                if(int_break_point.table[i].status) {
                   1823:                                                        telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
                   1824:                                                        if(int_break_point.table[i].ah_registered) {
                   1825:                                                                telnet_printf(" %02X", int_break_point.table[i].ah);
                   1826:                                                        }
                   1827:                                                        if(int_break_point.table[i].al_registered) {
                   1828:                                                                telnet_printf(" %02X", int_break_point.table[i].al);
                   1829:                                                        }
                   1830:                                                        telnet_printf("\n");
                   1831:                                                }
                   1832:                                        }
                   1833:                                } else {
                   1834:                                        telnet_printf("invalid parameter number\n");
                   1835:                                }
                   1836:                        } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
                   1837:                                if(num == 1 || num == 2) {
                   1838:                                        break_point_t break_point_stored;
                   1839:                                        bool break_points_stored = false;
                   1840:                                        
                   1841:                                        if(stricmp(params[0], "P") == 0) {
                   1842:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1843:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1844:                                                break_points_stored = true;
                   1845:                                                
                   1846:                                                break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
                   1847:                                                break_point.table[0].status = 1;
                   1848:                                        } else if(num >= 2) {
                   1849:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1850:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1851:                                                break_points_stored = true;
                   1852:                                                
                   1853:                                                UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1854:                                                UINT32 ofs = debugger_get_ofs(params[1]);
                   1855:                                                break_point.table[0].addr = (seg << 4) + ofs;
                   1856:                                                break_point.table[0].seg = seg;
                   1857:                                                break_point.table[0].ofs = ofs;
                   1858:                                                break_point.table[0].status = 1;
                   1859:                                        }
                   1860:                                        break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1861:                                        now_going = true;
                   1862:                                        now_suspended = false;
                   1863:                                        
                   1864:                                        telnet_command("\033[2l"); // key unlock
1.1.1.54  root     1865:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1866:                                                if(telnet_kbhit()) {
                   1867:                                                        break;
                   1868:                                                }
                   1869:                                                Sleep(10);
                   1870:                                        }
                   1871:                                        now_going = false;
                   1872:                                        telnet_command("\033[2h"); // key lock
                   1873:                                        
                   1874:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1875:                                                Sleep(100);
1.1.1.54  root     1876:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1877:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1878:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1879:                                                }
                   1880:                                        }
1.1.1.54  root     1881:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1882:                                                if(telnet_disconnected()) {
                   1883:                                                        break;
                   1884:                                                }
                   1885:                                                Sleep(10);
                   1886:                                        }
                   1887:                                        dasm_seg = SREG(CS);
                   1888:                                        dasm_ofs = m_eip;
                   1889:                                        
                   1890:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1891:                                        debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1892:                                        telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1893:                                        
                   1894:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1895:                                        debugger_regs_info(buffer);
                   1896:                                        telnet_printf("%s", buffer);
                   1897:                                        
                   1898:                                        if(break_point.hit) {
                   1899:                                                if(stricmp(params[0], "G") == 0 && num == 1) {
                   1900:                                                        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1901:                                                        telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1902:                                                }
                   1903:                                        } else if(rd_break_point.hit) {
                   1904:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1905:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1906:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1907:                                                m_prev_cs, m_prev_eip);
                   1908:                                        } else if(wr_break_point.hit) {
                   1909:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1910:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1911:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1912:                                                m_prev_cs, m_prev_eip);
                   1913:                                        } else if(in_break_point.hit) {
                   1914:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1915:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1916:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1917:                                                m_prev_cs, m_prev_eip);
                   1918:                                        } else if(out_break_point.hit) {
                   1919:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1920:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1921:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1922:                                                m_prev_cs, m_prev_eip);
                   1923:                                        } else if(int_break_point.hit) {
                   1924:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1925:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1926:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1927:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1928:                                                }
                   1929:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1930:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1931:                                                }
                   1932:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1933:                                        } else {
                   1934:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1935:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1936:                                        }
                   1937:                                        if(break_points_stored) {
                   1938:                                                memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
                   1939:                                        }
                   1940:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1941:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1942:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1943:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1944:                                } else {
                   1945:                                        telnet_printf("invalid parameter number\n");
                   1946:                                }
                   1947:                        } else if(stricmp(params[0], "T") == 0) {
                   1948:                                if(num == 1 || num == 2) {
                   1949:                                        int steps = 1;
                   1950:                                        if(num >= 2) {
                   1951:                                                steps = debugger_get_val(params[1]);
                   1952:                                        }
                   1953:                                        
                   1954:                                        telnet_command("\033[2l"); // key unlock
                   1955:                                        while(steps-- > 0) {
                   1956:                                                break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1957:                                                now_going = false;
                   1958:                                                now_suspended = false;
                   1959:                                                
1.1.1.54  root     1960:                                                while(!m_exit && !now_suspended) {
1.1.1.33  root     1961:                                                        if(telnet_disconnected()) {
                   1962:                                                                break;
                   1963:                                                        }
                   1964:                                                        Sleep(10);
                   1965:                                                }
                   1966:                                                dasm_seg = SREG(CS);
                   1967:                                                dasm_ofs = m_eip;
                   1968:                                                
                   1969:                                                telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1970:                                                debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1971:                                                telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1972:                                                
                   1973:                                                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1974:                                                debugger_regs_info(buffer);
                   1975:                                                telnet_printf("%s", buffer);
                   1976:                                                
                   1977:                                                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()) {
                   1978:                                                        break;
                   1979:                                                }
                   1980:                                        }
                   1981:                                        telnet_command("\033[2h"); // key lock
                   1982:                                        
                   1983:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1984:                                                Sleep(100);
1.1.1.54  root     1985:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1986:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1987:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1988:                                                }
                   1989:                                        }
1.1.1.54  root     1990:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1991:                                                if(telnet_disconnected()) {
                   1992:                                                        break;
                   1993:                                                }
                   1994:                                                Sleep(10);
                   1995:                                        }
                   1996:                                        if(break_point.hit) {
                   1997:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1998:                                                telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1999:                                        } else if(rd_break_point.hit) {
                   2000:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2001:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   2002:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   2003:                                                m_prev_cs, m_prev_eip);
                   2004:                                        } else if(wr_break_point.hit) {
                   2005:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2006:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   2007:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   2008:                                                m_prev_cs, m_prev_eip);
                   2009:                                        } else if(in_break_point.hit) {
                   2010:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2011:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   2012:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   2013:                                                m_prev_cs, m_prev_eip);
                   2014:                                        } else if(out_break_point.hit) {
                   2015:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2016:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   2017:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   2018:                                                m_prev_cs, m_prev_eip);
                   2019:                                        } else if(int_break_point.hit) {
                   2020:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2021:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   2022:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   2023:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   2024:                                                }
                   2025:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   2026:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   2027:                                                }
                   2028:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   2029:                                        } else if(steps > 0) {
                   2030:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2031:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   2032:                                        }
                   2033:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   2034:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   2035:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   2036:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   2037:                                } else {
                   2038:                                        telnet_printf("invalid parameter number\n");
                   2039:                                }
                   2040:                        } else if(stricmp(params[0], "Q") == 0) {
                   2041:                                break;
                   2042:                        } else if(stricmp(params[0], "X") == 0) {
                   2043:                                debugger_process_info(buffer);
                   2044:                                telnet_printf("%s", buffer);
                   2045:                        } else if(stricmp(params[0], ">") == 0) {
                   2046:                                if(num == 2) {
                   2047:                                        if(fp_debugger != NULL) {
                   2048:                                                fclose(fp_debugger);
                   2049:                                                fp_debugger = NULL;
                   2050:                                        }
                   2051:                                        fp_debugger = fopen(params[1], "w");
                   2052:                                } else {
                   2053:                                        telnet_printf("invalid parameter number\n");
                   2054:                                }
                   2055:                        } else if(stricmp(params[0], "<") == 0) {
                   2056:                                if(num == 2) {
                   2057:                                        if(fi_debugger != NULL) {
                   2058:                                                fclose(fi_debugger);
                   2059:                                                fi_debugger = NULL;
                   2060:                                        }
                   2061:                                        fi_debugger = fopen(params[1], "r");
                   2062:                                } else {
                   2063:                                        telnet_printf("invalid parameter number\n");
                   2064:                                }
                   2065:                        } else if(stricmp(params[0], "?") == 0) {
                   2066:                                telnet_printf("D [<start> [<end>]] - dump memory\n");
                   2067:                                telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
                   2068:                                telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
                   2069:                                telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
                   2070:                                telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
                   2071:                                
                   2072:                                telnet_printf("R - show registers\n");
                   2073:                                telnet_printf("R <reg> <value> - edit register\n");
                   2074:                                telnet_printf("S <start> <end> <list> - search\n");
                   2075:                                telnet_printf("U [<start> [<end>]] - unassemble\n");
                   2076:                                
                   2077:                                telnet_printf("H <value> <value> - hexadd\n");
                   2078:                                
                   2079:                                telnet_printf("BP <address> - set breakpoint\n");
                   2080:                                telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
                   2081:                                telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
                   2082:                                telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
                   2083:                                telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
                   2084:                                telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
                   2085:                                
                   2086:                                telnet_printf("G - go (press enter key to break)\n");
                   2087:                                telnet_printf("G <address> - go and break at address\n");
                   2088:                                telnet_printf("P - trace one opcode (step over)\n");
                   2089:                                telnet_printf("T [<count>] - trace (step in)\n");
                   2090:                                telnet_printf("Q - quit\n");
                   2091:                                telnet_printf("X - show dos process info\n");
                   2092:                                
                   2093:                                telnet_printf("> <filename> - output logfile\n");
                   2094:                                telnet_printf("< <filename> - input commands from file\n");
                   2095:                                
                   2096:                                telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
                   2097:                                telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
                   2098:                        } else {
                   2099:                                telnet_printf("unknown command %s\n", params[0]);
                   2100:                        }
                   2101:                }
                   2102:        }
                   2103:        if(fp_debugger != NULL) {
                   2104:                fclose(fp_debugger);
                   2105:                fp_debugger = NULL;
                   2106:        }
                   2107:        if(fi_debugger != NULL) {
                   2108:                fclose(fi_debugger);
                   2109:                fi_debugger = NULL;
                   2110:        }
                   2111:        now_debugging = now_going = now_suspended = force_suspend = false;
                   2112:        closesocket(cli_socket);
                   2113: }
                   2114: 
                   2115: const char *debugger_get_ttermpro_path()
                   2116: {
                   2117:        static char path[MAX_PATH] = {0};
                   2118:        
                   2119:        if(getenv("ProgramFiles")) {
                   2120:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
                   2121:        }
                   2122:        return(path);
                   2123: }
                   2124: 
                   2125: const char *debugger_get_ttermpro_x86_path()
                   2126: {
                   2127:        static char path[MAX_PATH] = {0};
                   2128:        
                   2129:        if(getenv("ProgramFiles(x86)")) {
                   2130:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
                   2131:        }
                   2132:        return(path);
                   2133: }
                   2134: 
                   2135: const char *debugger_get_putty_path()
                   2136: {
                   2137:        static char path[MAX_PATH] = {0};
                   2138:        
                   2139:        if(getenv("ProgramFiles")) {
                   2140:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
                   2141:        }
                   2142:        return(path);
                   2143: }
                   2144: 
                   2145: const char *debugger_get_putty_x86_path()
                   2146: {
                   2147:        static char path[MAX_PATH] = {0};
                   2148:        
                   2149:        if(getenv("ProgramFiles(x86)")) {
                   2150:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
                   2151:        }
                   2152:        return(path);
                   2153: }
                   2154: 
                   2155: const char *debugger_get_telnet_path()
                   2156: {
                   2157:        // NOTE: When you run 32bit version of msdos.exe on Windows x64,
                   2158:        // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
                   2159:        // But 32bit version of telnet.exe will not be installed in SysWOW64
                   2160:        // and 64bit version of telnet.exe will be installed in System32.
                   2161:        static char path[MAX_PATH] = {0};
                   2162:        
                   2163:        if(getenv("windir") != NULL) {
                   2164:                sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
                   2165:        }
                   2166:        return(path);
                   2167: }
                   2168: 
                   2169: DWORD WINAPI debugger_thread(LPVOID)
                   2170: {
                   2171:        WSADATA was_data;
                   2172:        struct sockaddr_in svr_addr;
                   2173:        struct sockaddr_in cli_addr;
                   2174:        int cli_addr_len = sizeof(cli_addr);
                   2175:        int port = 23;
                   2176:        int bind_stat = SOCKET_ERROR;
                   2177:        struct timeval timeout;
                   2178:        
                   2179:        WSAStartup(MAKEWORD(2,0), &was_data);
                   2180:        
                   2181:        if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
                   2182:                memset(&svr_addr, 0, sizeof(svr_addr));
                   2183:                svr_addr.sin_family = AF_INET;
                   2184:                svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
                   2185:                
1.1.1.54  root     2186:                while(!m_exit && port < 10000) {
1.1.1.33  root     2187:                        svr_addr.sin_port = htons(port);
                   2188:                        if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
                   2189:                                break;
                   2190:                        } else {
                   2191:                                port = (port == 23) ? 9000 : (port + 1);
                   2192:                        }
                   2193:                }
                   2194:                if(bind_stat == 0) {
                   2195:                        timeout.tv_sec = 1;
                   2196:                        timeout.tv_usec = 0;
1.1.1.45  root     2197:                        setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
1.1.1.33  root     2198:                        
                   2199:                        listen(svr_socket, 1);
                   2200:                        
                   2201:                        char command[MAX_PATH] = {0};
1.1.1.60  root     2202:                        STARTUPINFOA si;
1.1.1.33  root     2203:                        PROCESS_INFORMATION pi;
                   2204:                        
                   2205:                        if(_access(debugger_get_ttermpro_path(), 0) == 0) {
                   2206:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
                   2207:                        } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
                   2208:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
                   2209:                        } else if(_access(debugger_get_putty_path(), 0) == 0) {
                   2210:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
                   2211:                        } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
                   2212:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
                   2213:                        } else if(_access(debugger_get_telnet_path(), 0) == 0) {
                   2214:                                sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
                   2215:                        }
                   2216:                        if(command[0] != '\0') {
1.1.1.60  root     2217:                                memset(&si, 0, sizeof(STARTUPINFOA));
1.1.1.33  root     2218:                                memset(&pi, 0, sizeof(PROCESS_INFORMATION));
1.1.1.60  root     2219:                                CreateProcessA(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
1.1.1.33  root     2220:                        }
                   2221:                        
1.1.1.54  root     2222:                        while(!m_exit) {
1.1.1.33  root     2223:                                if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
                   2224:                                        u_long val = 1;
                   2225:                                        ioctlsocket(cli_socket, FIONBIO, &val);
                   2226:                                        debugger_main();
                   2227:                                }
                   2228:                        }
                   2229:                }
                   2230:        }
                   2231:        WSACleanup();
                   2232:        return(0);
                   2233: }
                   2234: #endif
                   2235: 
                   2236: /* ----------------------------------------------------------------------------
1.1       root     2237:        main
                   2238: ---------------------------------------------------------------------------- */
                   2239: 
1.1.1.28  root     2240: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                   2241: {
                   2242:        if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33  root     2243:                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     2244: #ifdef USE_SERVICE_THREAD
                   2245:                        EnterCriticalSection(&key_buf_crit_sect);
                   2246: #endif
1.1.1.51  root     2247:                        pcbios_clear_key_buffer();
1.1.1.35  root     2248: #ifdef USE_SERVICE_THREAD
                   2249:                        LeaveCriticalSection(&key_buf_crit_sect);
                   2250: #endif
1.1.1.33  root     2251:                }
                   2252: //             key_code = key_recv = 0;
1.1.1.28  root     2253:                return TRUE;
                   2254:        } else if(dwCtrlType == CTRL_C_EVENT) {
                   2255:                return TRUE;
                   2256:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
                   2257:                // this program will be terminated abnormally, do minimum end process
                   2258:                exit_handler();
                   2259:                exit(1);
                   2260:        }
                   2261:        return FALSE;
                   2262: }
                   2263: 
                   2264: void exit_handler()
                   2265: {
                   2266:        if(temp_file_created) {
1.1.1.60  root     2267:                DeleteFileA(temp_file_path);
1.1.1.28  root     2268:                temp_file_created = false;
                   2269:        }
                   2270:        if(key_buf_char != NULL) {
                   2271:                key_buf_char->release();
                   2272:                delete key_buf_char;
                   2273:                key_buf_char = NULL;
                   2274:        }
                   2275:        if(key_buf_scan != NULL) {
                   2276:                key_buf_scan->release();
                   2277:                delete key_buf_scan;
                   2278:                key_buf_scan = NULL;
                   2279:        }
1.1.1.57  root     2280:        if(key_buf_data != NULL) {
                   2281:                key_buf_data->release();
                   2282:                delete key_buf_data;
                   2283:                key_buf_data = NULL;
                   2284:        }
1.1.1.32  root     2285: #ifdef SUPPORT_XMS
                   2286:        msdos_xms_release();
                   2287: #endif
1.1.1.28  root     2288:        hardware_release();
                   2289: }
                   2290: 
1.1.1.35  root     2291: #ifdef USE_VRAM_THREAD
1.1.1.28  root     2292: DWORD WINAPI vram_thread(LPVOID)
                   2293: {
1.1.1.54  root     2294:        while(!m_exit) {
1.1.1.28  root     2295:                EnterCriticalSection(&vram_crit_sect);
                   2296:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                   2297:                        vram_flush_char();
                   2298:                }
                   2299:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                   2300:                        vram_flush_attr();
                   2301:                }
                   2302:                vram_last_length_char = vram_length_char;
                   2303:                vram_last_length_attr = vram_length_attr;
                   2304:                LeaveCriticalSection(&vram_crit_sect);
                   2305:                // this is about half the maximum keyboard repeat rate - any
                   2306:                // lower tends to be jerky, any higher misses updates
                   2307:                Sleep(15);
                   2308:        }
                   2309:        return 0;
                   2310: }
                   2311: #endif
                   2312: 
1.1.1.45  root     2313: long get_section_in_exec_file(FILE *fp, const char *name)
1.1.1.28  root     2314: {
                   2315:        UINT8 header[0x400];
                   2316:        
                   2317:        long position = ftell(fp);
                   2318:        fseek(fp, 0, SEEK_SET);
                   2319:        fread(header, sizeof(header), 1, fp);
                   2320:        fseek(fp, position, SEEK_SET);
                   2321:        
                   2322:        try {
                   2323:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2324:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2325:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2326:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2327:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2328:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2329:                
                   2330:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                   2331:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                   2332:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                   2333:                                return(sectionHeader->PointerToRawData);
                   2334:                        }
                   2335:                }
                   2336:        } catch(...) {
                   2337:        }
                   2338:        return(0);
                   2339: }
                   2340: 
1.1.1.10  root     2341: bool is_started_from_command_prompt()
                   2342: {
1.1.1.58  root     2343:        bool result = false;
1.1.1.60  root     2344:        HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.58  root     2345:        
1.1.1.18  root     2346:        if(hLibrary) {
                   2347:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                   2348:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                   2349:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
1.1.1.58  root     2350:                if(lpfnGetConsoleProcessList) { // Windows XP or later
1.1.1.18  root     2351:                        DWORD pl;
1.1.1.58  root     2352:                        result = (lpfnGetConsoleProcessList(&pl, 1) > 1);
1.1.1.18  root     2353:                        FreeLibrary(hLibrary);
1.1.1.58  root     2354:                        return(result);
1.1.1.18  root     2355:                }
                   2356:                FreeLibrary(hLibrary);
                   2357:        }
                   2358:        
                   2359:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   2360:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                   2361:                DWORD dwParentProcessID = 0;
                   2362:                PROCESSENTRY32 pe32;
                   2363:                pe32.dwSize = sizeof(PROCESSENTRY32);
                   2364:                if(Process32First(hSnapshot, &pe32)) {
                   2365:                        do {
                   2366:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                   2367:                                        dwParentProcessID = pe32.th32ParentProcessID;
                   2368:                                        break;
                   2369:                                }
                   2370:                        } while(Process32Next(hSnapshot, &pe32));
                   2371:                }
                   2372:                CloseHandle(hSnapshot);
                   2373:                if(dwParentProcessID != 0) {
                   2374:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                   2375:                        if(hProcess != NULL) {
                   2376:                                HMODULE hMod;
                   2377:                                DWORD cbNeeded;
                   2378:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                   2379:                                        char module_name[MAX_PATH];
1.1.1.60  root     2380:                                        if(GetModuleBaseNameA(hProcess, hMod, module_name, sizeof(module_name))) {
1.1.1.58  root     2381:                                                result = (_strnicmp(module_name, "cmd.exe", 7) == 0);
1.1.1.18  root     2382:                                        }
                   2383:                                }
                   2384:                                CloseHandle(hProcess);
                   2385:                        }
                   2386:                }
                   2387:        }
1.1.1.58  root     2388:        return(result);
1.1.1.14  root     2389: }
                   2390: 
                   2391: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                   2392: {
1.1.1.60  root     2393:        HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.14  root     2394:        
1.1.1.60  root     2395:        if(hLibrary) {
                   2396:                typedef ULONGLONG (WINAPI* VerSetConditionMaskFunction)(ULONGLONG, DWORD, BYTE);
                   2397:                typedef BOOL(WINAPI* VerifyVersionInfoFunction)(LPOSVERSIONINFOEXA, DWORD, DWORDLONG);
                   2398:                
                   2399:                VerSetConditionMaskFunction lpfnVerSetConditionMask = reinterpret_cast<VerSetConditionMaskFunction>(::GetProcAddress(hLibrary, "VerSetConditionMask"));
                   2400:                VerifyVersionInfoFunction lpfnVerifyVersionInfo = reinterpret_cast<VerifyVersionInfoFunction>(::GetProcAddress(hLibrary, "VerifyVersionInfoA"));
                   2401:                
                   2402:                if(lpfnVerSetConditionMask && lpfnVerifyVersionInfo) { // Windows 2000 or later
                   2403:                        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
                   2404:                        OSVERSIONINFOEXA osvi;
                   2405:                        DWORDLONG dwlConditionMask = 0;
                   2406:                        int op = VER_GREATER_EQUAL;
                   2407:                        
                   2408:                        // Initialize the OSVERSIONINFOEXA structure.
                   2409:                        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEXA));
                   2410:                        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXA);
                   2411:                        osvi.dwMajorVersion = dwMajorVersion;
                   2412:                        osvi.dwMinorVersion = dwMinorVersion;
                   2413:                        osvi.wServicePackMajor = wServicePackMajor;
                   2414:                        osvi.wServicePackMinor = wServicePackMinor;
                   2415:                        
                   2416:                         // Initialize the condition mask.
                   2417:                        #define MY_VER_SET_CONDITION(_m_,_t_,_c_) ((_m_)=lpfnVerSetConditionMask((_m_),(_t_),(_c_)))
                   2418:                        
                   2419:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                   2420:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                   2421:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                   2422:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                   2423:                        
                   2424:                        // Perform the test.
                   2425:                        BOOL result = lpfnVerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                   2426:                        FreeLibrary(hLibrary);
                   2427:                        return(result);
                   2428:                }
                   2429:                FreeLibrary(hLibrary);
                   2430:        }
                   2431:        
                   2432:        OSVERSIONINFOA osvi;
                   2433:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
                   2434:        
                   2435:        if(GetVersionExA((LPOSVERSIONINFOA)&osvi)) {
                   2436:                if(osvi.dwPlatformId != VER_PLATFORM_WIN32_NT) {
                   2437:                        return(false);
                   2438:                } else if(osvi.dwMajorVersion > dwMajorVersion) {
                   2439:                        return(true);
                   2440:                } else if(osvi.dwMajorVersion < dwMajorVersion) {
                   2441:                        return(false);
                   2442:                } else if(osvi.dwMinorVersion > dwMinorVersion) {
                   2443:                        return(true);
                   2444:                } else if(osvi.dwMinorVersion < dwMinorVersion) {
                   2445:                        return(false);
                   2446:                }
                   2447:                // FIXME: check wServicePackMajor and wServicePackMinor :-(
                   2448:                return(true);
                   2449:        }
                   2450:        return(false);
1.1.1.14  root     2451: }
                   2452: 
1.1.1.61  root     2453: HWND get_console_window_handle()
1.1.1.58  root     2454: {
1.1.1.61  root     2455:        static HWND hwndFound = 0;
                   2456:        
                   2457:        if(hwndFound == 0) {
                   2458:                // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
                   2459:                char pszNewWindowTitle[1024];
                   2460:                char pszOldWindowTitle[1024];
                   2461:                
                   2462:                GetConsoleTitleA(pszOldWindowTitle, 1024);
                   2463:                wsprintfA(pszNewWindowTitle, "%d/%d", GetTickCount(), GetCurrentProcessId());
                   2464:                SetConsoleTitleA(pszNewWindowTitle);
                   2465:                Sleep(100);
                   2466:                hwndFound = FindWindowA(NULL, pszNewWindowTitle);
                   2467:                SetConsoleTitleA(pszOldWindowTitle);
                   2468:        }
                   2469:        return hwndFound;
                   2470: }
                   2471: 
                   2472: HDC get_console_window_device_context()
                   2473: {
                   2474:        return GetDC(get_console_window_handle());
                   2475: }
                   2476: 
                   2477: bool get_console_font_size(int *width, int *height)
                   2478: {
                   2479:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.58  root     2480:        bool result = false;
                   2481:        
1.1.1.62  root     2482:        if(is_winxp_or_later) {
                   2483:                HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
                   2484:                if(hLibrary) {
                   2485:                        typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
                   2486:                        GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
                   2487:                        if(lpfnGetCurrentConsoleFont) { // Windows XP or later
                   2488:                                CONSOLE_FONT_INFO fi;
                   2489:                                if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi) != 0) {
                   2490:                                        *width  = fi.dwFontSize.X;
                   2491:                                        *height = fi.dwFontSize.Y;
                   2492:                                        result = true;
                   2493:                                }
1.1.1.58  root     2494:                        }
1.1.1.62  root     2495:                        FreeLibrary(hLibrary);
                   2496:                }
                   2497:        } else {
                   2498:                CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2499:                RECT rect;
                   2500:                if(GetConsoleScreenBufferInfo(hStdout, &csbi) && GetClientRect(get_console_window_handle(), &rect)) {
                   2501:                        int cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
                   2502:                        int rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2503:                        *width  = rect.right / cols;
                   2504:                        *height = rect.bottom / rows;
                   2505:                        result = true;
1.1.1.58  root     2506:                }
                   2507:        }
                   2508:        return(result);
                   2509: }
                   2510: 
1.1.1.61  root     2511: bool set_console_font_size(int width, int height)
1.1.1.56  root     2512: {
                   2513:        // http://d.hatena.ne.jp/aharisu/20090427/1240852598
1.1.1.61  root     2514:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.56  root     2515:        bool result = false;
1.1.1.60  root     2516:        HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.56  root     2517:        
                   2518:        if(hLibrary) {
1.1.1.62  root     2519:                CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2520:                RECT rect;
                   2521:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   2522:                int cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
                   2523:                int rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2524:                
1.1.1.56  root     2525:                typedef BOOL (WINAPI* GetConsoleFontInfoFunction)(HANDLE, BOOL, DWORD, PCONSOLE_FONT_INFO);
                   2526:                typedef DWORD (WINAPI* GetNumberOfConsoleFontsFunction)(VOID);
1.1.1.60  root     2527:                typedef COORD (WINAPI* GetConsoleFontSizeFunction)(HANDLE, DWORD);
1.1.1.56  root     2528:                typedef BOOL (WINAPI* SetConsoleFontFunction)(HANDLE, DWORD);
                   2529:                typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
1.1.1.61  root     2530:                typedef BOOL (WINAPI* GetCurrentConsoleFontExFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFOEX);
                   2531:                typedef BOOL (WINAPI* SetCurrentConsoleFontExFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFOEX);
1.1.1.56  root     2532:                
                   2533:                GetConsoleFontInfoFunction lpfnGetConsoleFontInfo = reinterpret_cast<GetConsoleFontInfoFunction>(::GetProcAddress(hLibrary, "GetConsoleFontInfo"));
                   2534:                GetNumberOfConsoleFontsFunction lpfnGetNumberOfConsoleFonts = reinterpret_cast<GetNumberOfConsoleFontsFunction>(::GetProcAddress(hLibrary, "GetNumberOfConsoleFonts"));
1.1.1.60  root     2535:                GetConsoleFontSizeFunction lpfnGetConsoleFontSize = reinterpret_cast<GetConsoleFontSizeFunction>(::GetProcAddress(hLibrary, "GetConsoleFontSize"));
1.1.1.56  root     2536:                SetConsoleFontFunction lpfnSetConsoleFont = reinterpret_cast<SetConsoleFontFunction>(::GetProcAddress(hLibrary, "SetConsoleFont"));
                   2537:                GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
1.1.1.61  root     2538:                GetCurrentConsoleFontExFunction lpfnGetCurrentConsoleFontEx = reinterpret_cast<GetCurrentConsoleFontExFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFontEx"));
                   2539:                SetCurrentConsoleFontExFunction lpfnSetCurrentConsoleFontEx = reinterpret_cast<SetCurrentConsoleFontExFunction>(::GetProcAddress(hLibrary, "SetCurrentConsoleFontEx"));
1.1.1.56  root     2540:                
1.1.1.62  root     2541:                if(lpfnGetConsoleFontInfo && lpfnGetNumberOfConsoleFonts && lpfnSetConsoleFont) { // Windows 2000 or later
1.1.1.56  root     2542:                        DWORD dwFontNum = lpfnGetNumberOfConsoleFonts();
1.1.1.61  root     2543:                        if(dwFontNum) {
                   2544:                                CONSOLE_FONT_INFO* fonts = (CONSOLE_FONT_INFO*)malloc(sizeof(CONSOLE_FONT_INFO) * dwFontNum);
                   2545:                                lpfnGetConsoleFontInfo(hStdout, FALSE, dwFontNum, fonts);
                   2546:                                for(int i = 0; i < dwFontNum; i++) {
                   2547:                                        fonts[i].dwFontSize = lpfnGetConsoleFontSize(hStdout, fonts[i].nFont);
                   2548:                                        if(fonts[i].dwFontSize.X == width && fonts[i].dwFontSize.Y == height) {
1.1.1.62  root     2549:                                                if(lpfnSetConsoleFont(hStdout, fonts[i].nFont)) {
                   2550:                                                        if(is_winxp_or_later && lpfnGetCurrentConsoleFont) { // Windows XP or later
                   2551:                                                                CONSOLE_FONT_INFO fi;
                   2552:                                                                if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi)) {
                   2553:                                                                        if(fonts[i].dwFontSize.X == fi.dwFontSize.X && fonts[i].dwFontSize.Y == fi.dwFontSize.Y) {
                   2554:                                                                                result = true;
                   2555:                                                                                break;
                   2556:                                                                        }
                   2557:                                                                }
                   2558:                                                        } else {
                   2559:                                                                Sleep(10);
                   2560:                                                                if(GetClientRect(get_console_window_handle(), &rect)) {
                   2561:                                                                        if(fonts[i].dwFontSize.X * cols == rect.right && fonts[i].dwFontSize.Y * rows == rect.bottom) {
                   2562:                                                                                result = true;
                   2563:                                                                                break;
                   2564:                                                                        }
                   2565:                                                                }
1.1.1.58  root     2566:                                                        }
                   2567:                                                }
1.1.1.61  root     2568:                                        }
                   2569:                                }
                   2570:                                free(fonts);
                   2571:                        } else if(lpfnGetCurrentConsoleFontEx && lpfnSetCurrentConsoleFontEx) {
                   2572:                                // for Windows10 enhanced command prompt
                   2573:                                CONSOLE_FONT_INFOEX fi_old, fi_new;
                   2574:                                fi_old.cbSize = sizeof(CONSOLE_FONT_INFOEX);
                   2575:                                if(lpfnGetCurrentConsoleFontEx(hStdout, FALSE, &fi_old)) {
                   2576:                                        fi_new = fi_old;
                   2577:                                        fi_new.dwFontSize.X = width;
                   2578:                                        fi_new.dwFontSize.Y = height;
                   2579:                                        if(lpfnSetCurrentConsoleFontEx(hStdout, FALSE, &fi_new)) {
                   2580:                                                lpfnGetCurrentConsoleFontEx(hStdout, FALSE, &fi_new);
                   2581:                                                if(fi_new.dwFontSize.X == width && fi_new.dwFontSize.Y == height) {
                   2582:                                                        result = true;
                   2583:                                                } else {
                   2584:                                                        lpfnSetCurrentConsoleFontEx(hStdout, FALSE, &fi_old);
1.1.1.58  root     2585:                                                }
                   2586:                                        }
1.1.1.57  root     2587:                                }
1.1.1.56  root     2588:                        }
                   2589:                }
                   2590:                FreeLibrary(hLibrary);
                   2591:        }
                   2592:        return(result);
                   2593: }
                   2594: 
1.1.1.59  root     2595: bool is_cursor_blink_off()
                   2596: {
                   2597:        static int result = -1;
                   2598:        HKEY hKey;
                   2599:        char chData[64];
                   2600:        DWORD dwSize = sizeof(chData);
                   2601:        
                   2602:        if(result == -1) {
                   2603:                result = 0;
1.1.1.60  root     2604:                if(RegOpenKeyExA(HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   2605:                        if(RegQueryValueExA(hKey, "CursorBlinkRate", NULL, NULL, (LPBYTE)chData, &dwSize) == ERROR_SUCCESS) {
1.1.1.59  root     2606:                                if(strncmp(chData, "-1", 2) == 0) {
                   2607:                                        result = 1;
                   2608:                                }
                   2609:                        }
                   2610:                        RegCloseKey(hKey);
                   2611:                }
                   2612:        }
                   2613:        return(result != 0);
                   2614: }
                   2615: 
1.1.1.27  root     2616: void get_sio_port_numbers()
                   2617: {
                   2618:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                   2619:        HDEVINFO hDevInfo = 0;
                   2620:        HKEY hKey = 0;
1.1.1.60  root     2621:        if((hDevInfo = SetupDiGetClassDevsA(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
1.1.1.27  root     2622:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                   2623:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                   2624:                                char chData[256];
                   2625:                                DWORD dwType = 0;
                   2626:                                DWORD dwSize = sizeof(chData);
                   2627:                                int port_number = 0;
                   2628:                                
1.1.1.60  root     2629:                                if(RegQueryValueExA(hKey, "PortName", NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
1.1.1.27  root     2630:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                   2631:                                                port_number = atoi(chData + 3);
                   2632:                                        }
                   2633:                                }
                   2634:                                RegCloseKey(hKey);
                   2635:                                
1.1.1.29  root     2636:                                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     2637:                                        continue;
                   2638:                                }
                   2639:                                if(sio_port_number[0] == 0) {
                   2640:                                        sio_port_number[0] = port_number;
                   2641:                                } else if(sio_port_number[1] == 0) {
                   2642:                                        sio_port_number[1] = port_number;
1.1.1.29  root     2643:                                } else if(sio_port_number[2] == 0) {
                   2644:                                        sio_port_number[2] = port_number;
                   2645:                                } else if(sio_port_number[3] == 0) {
                   2646:                                        sio_port_number[3] = port_number;
1.1.1.27  root     2647:                                }
1.1.1.29  root     2648:                                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     2649:                                        break;
                   2650:                                }
                   2651:                        }
                   2652:                }
                   2653:        }
                   2654: }
                   2655: 
1.1.1.28  root     2656: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                   2657: 
1.1       root     2658: int main(int argc, char *argv[], char *envp[])
                   2659: {
1.1.1.9   root     2660:        int arg_offset = 0;
                   2661:        int standard_env = 0;
1.1.1.14  root     2662:        int buf_width = 0, buf_height = 0;
1.1.1.28  root     2663:        bool get_console_info_success = false;
1.1.1.56  root     2664:        bool get_console_font_success = false;
1.1.1.28  root     2665:        bool screen_size_changed = false;
                   2666:        
1.1.1.60  root     2667:        char path[MAX_PATH], full[MAX_PATH], *name = NULL;
                   2668:        GetModuleFileNameA(NULL, path, MAX_PATH);
                   2669:        GetFullPathNameA(path, MAX_PATH, full, &name);
1.1       root     2670:        
1.1.1.27  root     2671:        char dummy_argv_0[] = "msdos.exe";
                   2672:        char dummy_argv_1[MAX_PATH];
                   2673:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                   2674:        char new_exec_file[MAX_PATH];
                   2675:        bool convert_cmd_file = false;
1.1.1.28  root     2676:        unsigned int code_page = 0;
1.1.1.27  root     2677:        
                   2678:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root     2679:                // check if command file is embedded to this execution file
                   2680:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root     2681:                FILE* fp = fopen(full, "rb");
1.1.1.28  root     2682:                long offset = get_section_in_exec_file(fp, ".msdos");
                   2683:                if(offset != 0) {
1.1.1.30  root     2684:                        UINT8 buffer[16];
1.1.1.28  root     2685:                        fseek(fp, offset, SEEK_SET);
                   2686:                        fread(buffer, sizeof(buffer), 1, fp);
                   2687:                        
                   2688:                        // restore flags
                   2689:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                   2690:                        no_windows          = ((buffer[0] & 0x02) != 0);
                   2691:                        standard_env        = ((buffer[0] & 0x04) != 0);
                   2692:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                   2693:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                   2694:                        if((buffer[0] & 0x20) != 0) {
                   2695:                                get_sio_port_numbers();
                   2696:                        }
                   2697:                        if((buffer[0] & 0x40) != 0) {
                   2698:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                   2699:                                support_ems = true;
1.1.1.30  root     2700:                        }
1.1.1.27  root     2701: #ifdef SUPPORT_XMS
1.1.1.30  root     2702:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root     2703:                                support_xms = true;
                   2704:                        }
1.1.1.30  root     2705: #endif
1.1.1.28  root     2706:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                   2707:                                buf_width  = buffer[1] | (buffer[2] << 8);
                   2708:                                buf_height = buffer[3] | (buffer[4] << 8);
                   2709:                        }
                   2710:                        if(buffer[5] != 0) {
1.1.1.30  root     2711:                                dos_major_version = buffer[5];
                   2712:                                dos_minor_version = buffer[6];
                   2713:                        }
                   2714:                        if(buffer[7] != 0) {
                   2715:                                win_major_version = buffer[7];
                   2716:                                win_minor_version = buffer[8];
1.1.1.28  root     2717:                        }
1.1.1.30  root     2718:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root     2719:                                SetConsoleCP(code_page);
                   2720:                                SetConsoleOutputCP(code_page);
                   2721:                        }
1.1.1.30  root     2722:                        int name_len = buffer[11];
                   2723:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root     2724:                        
                   2725:                        // restore command file name
                   2726:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                   2727:                        fread(dummy_argv_1, name_len, 1, fp);
                   2728:                        
                   2729:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                   2730:                                // if original command file exists, create a temporary file name
1.1.1.60  root     2731:                                if(GetTempFileNameA(".", "DOS", 0, dummy_argv_1) != 0) {
1.1.1.28  root     2732:                                        // create a temporary command file in the current director
1.1.1.60  root     2733:                                        DeleteFileA(dummy_argv_1);
1.1.1.27  root     2734:                                } else {
1.1.1.28  root     2735:                                        // create a temporary command file in the temporary folder
1.1.1.60  root     2736:                                        GetTempPathA(MAX_PATH, path);
                   2737:                                        if(GetTempFileNameA(path, "DOS", 0, dummy_argv_1) != 0) {
                   2738:                                                DeleteFileA(dummy_argv_1);
1.1.1.28  root     2739:                                        } else {
                   2740:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                   2741:                                        }
1.1.1.27  root     2742:                                }
1.1.1.28  root     2743:                                // check the command file type
                   2744:                                fread(buffer, 2, 1, fp);
                   2745:                                fseek(fp, -2, SEEK_CUR);
                   2746:                                if(memcmp(buffer, "MZ", 2) != 0) {
                   2747:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                   2748:                                } else {
                   2749:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root     2750:                                }
                   2751:                        }
1.1.1.28  root     2752:                        
                   2753:                        // restore command file
                   2754:                        FILE* fo = fopen(dummy_argv_1, "wb");
                   2755:                        for(int i = 0; i < file_len; i++) {
                   2756:                                fputc(fgetc(fp), fo);
                   2757:                        }
                   2758:                        fclose(fo);
                   2759:                        
1.1.1.60  root     2760:                        GetFullPathNameA(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
1.1.1.28  root     2761:                        temp_file_created = true;
1.1.1.60  root     2762:                        SetFileAttributesA(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
1.1.1.28  root     2763:                        
                   2764:                        // adjust argc/argv
                   2765:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                   2766:                                dummy_argv[i + 1] = argv[i];
                   2767:                        }
                   2768:                        argc++;
                   2769:                        argv = dummy_argv;
1.1.1.27  root     2770:                }
                   2771:                fclose(fp);
                   2772:        }
1.1.1.9   root     2773:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     2774:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   2775:                        stay_busy = true;
                   2776:                        arg_offset++;
1.1.1.27  root     2777:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   2778:                        if(argv[i][2] != '\0') {
                   2779:                                strcpy(new_exec_file, &argv[i][2]);
                   2780:                        } else {
                   2781:                                strcpy(new_exec_file, "new_exec_file.exe");
                   2782:                        }
                   2783:                        convert_cmd_file = true;
                   2784:                        arg_offset++;
1.1.1.28  root     2785:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   2786:                        if(IS_NUMERIC(argv[i][2])) {
                   2787:                                code_page = atoi(&argv[i][2]);
                   2788:                        } else {
                   2789:                                code_page = GetConsoleCP();
                   2790:                        }
                   2791:                        arg_offset++;
1.1.1.25  root     2792:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   2793:                        no_windows = true;
                   2794:                        arg_offset++;
                   2795:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     2796:                        standard_env = 1;
                   2797:                        arg_offset++;
1.1.1.14  root     2798:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   2799:                        ignore_illegal_insn = true;
                   2800:                        arg_offset++;
                   2801:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   2802:                        limit_max_memory = true;
                   2803:                        arg_offset++;
                   2804:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.51  root     2805:                        int result = sscanf(argv[i] + 2, "%d,%d", &buf_height, &buf_width);
                   2806:                        if(result == 1) {
                   2807:                                buf_width = 0;
                   2808:                        } else if(result != 2) {
1.1.1.17  root     2809:                                buf_width = buf_height = 0;
                   2810:                        }
                   2811:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   2812:                                buf_width = 80;
                   2813:                        }
                   2814:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   2815:                                buf_height = 25;
                   2816:                        }
1.1.1.14  root     2817:                        arg_offset++;
1.1.1.25  root     2818:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     2819:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     2820:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   2821:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   2822:                                        sio_port_number[1] = atoi(p1 + 1);
                   2823:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   2824:                                                sio_port_number[2] = atoi(p2 + 1);
                   2825:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   2826:                                                        sio_port_number[3] = atoi(p3 + 1);
                   2827:                                                }
                   2828:                                        }
1.1.1.25  root     2829:                                }
1.1.1.29  root     2830:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     2831:                        }
1.1.1.29  root     2832:                        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     2833:                                get_sio_port_numbers();
1.1.1.25  root     2834:                        }
                   2835:                        arg_offset++;
1.1.1.9   root     2836:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     2837:                        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     2838:                                dos_major_version = argv[i][2] - '0';
                   2839:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   2840:                        }
                   2841:                        arg_offset++;
                   2842:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   2843:                        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]))) {
                   2844:                                win_major_version = argv[i][2] - '0';
                   2845:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     2846:                        }
                   2847:                        arg_offset++;
1.1.1.25  root     2848:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   2849:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   2850:                        support_ems = true;
                   2851: #ifdef SUPPORT_XMS
                   2852:                        support_xms = true;
                   2853: #endif
                   2854:                        arg_offset++;
1.1.1.9   root     2855:                } else {
                   2856:                        break;
                   2857:                }
                   2858:        }
                   2859:        if(argc < 2 + arg_offset) {
1.1       root     2860: #ifdef _WIN64
1.1.1.14  root     2861:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     2862: #else
1.1.1.14  root     2863:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     2864: #endif
1.1.1.25  root     2865:                fprintf(stderr,
1.1.1.28  root     2866:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     2867:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     2868:                        "\n"
                   2869:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     2870: #ifdef _WIN64
1.1.1.27  root     2871:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     2872: #else
1.1.1.27  root     2873:                        "\t-c\tconvert command file to 32bit execution file\n"
                   2874: #endif
1.1.1.28  root     2875:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     2876:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   2877:                        "\t-e\tuse a reduced environment block\n"
                   2878:                        "\t-i\tignore invalid instructions\n"
                   2879:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   2880:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   2881:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   2882:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     2883:                        "\t-w\tset the Windows version\n"
1.1.1.63  root     2884: #if defined(SUPPORT_VCPI)
                   2885:                        "\t-x\tenable LIM EMS, VCPI, and XMS\n"
                   2886: #elif defined(SUPPORT_XMS)
                   2887:                        "\t-x\tenable LIM EMS and XMS\n"
1.1.1.19  root     2888: #else
1.1.1.28  root     2889:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2890: #endif
                   2891:                );
1.1.1.10  root     2892:                
                   2893:                if(!is_started_from_command_prompt()) {
                   2894:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2895:                        while(!_kbhit()) {
                   2896:                                Sleep(10);
                   2897:                        }
                   2898:                }
1.1.1.20  root     2899: #ifdef _DEBUG
                   2900:                _CrtDumpMemoryLeaks();
                   2901: #endif
1.1       root     2902:                return(EXIT_FAILURE);
                   2903:        }
1.1.1.27  root     2904:        if(convert_cmd_file) {
                   2905:                retval = EXIT_FAILURE;
1.1.1.28  root     2906:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2907:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2908:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2909:                        
1.1.1.28  root     2910:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2911:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2912:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2913:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2914:                        } else {
1.1.1.28  root     2915:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2916:                                if(offset != 0) {
                   2917:                                        UINT8 buffer[14];
                   2918:                                        fseek(fp, offset, SEEK_SET);
                   2919:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2920:                                        memset(path, 0, sizeof(path));
                   2921:                                        fread(path, buffer[9], 1, fp);
                   2922:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2923:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2924:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2925:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2926:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2927:                                } else {
                   2928:                                        // read pe header of msdos.exe
                   2929:                                        UINT8 header[0x400];
                   2930:                                        fseek(fp, 0, SEEK_SET);
                   2931:                                        fread(header, sizeof(header), 1, fp);
                   2932:                                        
                   2933:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2934:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2935:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2936:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2937:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2938:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2939:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2940:                                        
                   2941:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2942:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2943:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2944:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2945:                                        if(dwExtraLastSectionBytes != 0) {
                   2946:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2947:                                                dwLastSectionSize += dwRemain;
                   2948:                                        }
                   2949:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2950:                                        
                   2951:                                        // store msdos.exe
                   2952:                                        fseek(fp, 0, SEEK_SET);
                   2953:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2954:                                                if((data = fgetc(fp)) != EOF) {
                   2955:                                                        fputc(data, fo);
                   2956:                                                } else {
                   2957:                                                        // we should not reach here :-(
                   2958:                                                        fputc(0, fo);
                   2959:                                                }
                   2960:                                        }
                   2961:                                        
                   2962:                                        // store options
                   2963:                                        UINT8 flags = 0;
                   2964:                                        if(stay_busy) {
                   2965:                                                flags |= 0x01;
                   2966:                                        }
                   2967:                                        if(no_windows) {
                   2968:                                                flags |= 0x02;
                   2969:                                        }
                   2970:                                        if(standard_env) {
                   2971:                                                flags |= 0x04;
                   2972:                                        }
                   2973:                                        if(ignore_illegal_insn) {
                   2974:                                                flags |= 0x08;
                   2975:                                        }
                   2976:                                        if(limit_max_memory) {
                   2977:                                                flags |= 0x10;
                   2978:                                        }
1.1.1.29  root     2979:                                        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     2980:                                                flags |= 0x20;
                   2981:                                        }
                   2982:                                        if(support_ems) {
                   2983:                                                flags |= 0x40;
                   2984:                                        }
1.1.1.30  root     2985: #ifdef SUPPORT_XMS
                   2986:                                        if(support_xms) {
                   2987:                                                flags |= 0x80;
                   2988:                                        }
                   2989: #endif
1.1.1.28  root     2990:                                        
                   2991:                                        fputc(flags, fo);
                   2992:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2993:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2994:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2995:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2996:                                        fputc(dos_major_version, fo);
                   2997:                                        fputc(dos_minor_version, fo);
                   2998:                                        fputc(win_major_version, fo);
                   2999:                                        fputc(win_minor_version, fo);
1.1.1.28  root     3000:                                        fputc((code_page >> 0) & 0xff, fo);
                   3001:                                        fputc((code_page >> 8) & 0xff, fo);
                   3002:                                        
                   3003:                                        // store command file info
1.1.1.60  root     3004:                                        GetFullPathNameA(argv[arg_offset + 1], MAX_PATH, full, &name);
1.1.1.28  root     3005:                                        int name_len = strlen(name);
                   3006:                                        fseek(fs, 0, SEEK_END);
                   3007:                                        long file_size = ftell(fs);
                   3008:                                        
                   3009:                                        fputc(name_len, fo);
                   3010:                                        fputc((file_size >>  0) & 0xff, fo);
                   3011:                                        fputc((file_size >>  8) & 0xff, fo);
                   3012:                                        fputc((file_size >> 16) & 0xff, fo);
                   3013:                                        fputc((file_size >> 24) & 0xff, fo);
                   3014:                                        fwrite(name, name_len, 1, fo);
                   3015:                                        
                   3016:                                        // store command file
                   3017:                                        fseek(fs, 0, SEEK_SET);
                   3018:                                        for(int i = 0; i < file_size; i++) {
                   3019:                                                if((data = fgetc(fs)) != EOF) {
                   3020:                                                        fputc(data, fo);
                   3021:                                                } else {
                   3022:                                                        // we should not reach here :-(
                   3023:                                                        fputc(0, fo);
                   3024:                                                }
                   3025:                                        }
                   3026:                                        
                   3027:                                        // store padding data and update pe header
1.1.1.29  root     3028:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   3029:                                        coffHeader->NumberOfSections++;
                   3030:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   3031:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   3032:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   3033:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   3034:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     3035:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   3036:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   3037:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     3038:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     3039:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   3040:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     3041:                                                        if(i < 2) {
                   3042:                                                                fputc(padding[i & 15], fo);
                   3043:                                                        } else {
                   3044:                                                                fputc(padding[(i - 2) & 15], fo);
                   3045:                                                        }
1.1.1.28  root     3046:                                                }
                   3047:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   3048:                                        }
                   3049:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   3050:                                        
                   3051:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   3052:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   3053:                                        if(dwExtraNewSectionBytes != 0) {
                   3054:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   3055:                                                dwNewSectionSize += dwRemain;
                   3056:                                        }
                   3057:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   3058:                                        
                   3059:                                        fseek(fo, 0, SEEK_SET);
                   3060:                                        fwrite(header, sizeof(header), 1, fo);
                   3061:                                        
                   3062:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   3063:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     3064:                                }
                   3065:                        }
                   3066:                        if(fp != NULL) {
                   3067:                                fclose(fp);
                   3068:                        }
                   3069:                        if(fs != NULL) {
                   3070:                                fclose(fs);
                   3071:                        }
                   3072:                        if(fo != NULL) {
                   3073:                                fclose(fo);
                   3074:                        }
                   3075:                }
                   3076: #ifdef _DEBUG
                   3077:                _CrtDumpMemoryLeaks();
                   3078: #endif
                   3079:                return(retval);
                   3080:        }
1.1       root     3081:        
1.1.1.62  root     3082:        is_winxp_or_later = is_greater_windows_version(5, 1, 0, 0);
1.1.1.54  root     3083:        is_xp_64_or_later = is_greater_windows_version(5, 2, 0, 0);
1.1.1.14  root     3084:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   3085:        
1.1.1.23  root     3086:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     3087:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     3088:        CONSOLE_CURSOR_INFO ci;
1.1.1.61  root     3089:        UINT input_cp = GetConsoleCP();
                   3090:        UINT output_cp = GetConsoleOutputCP();
                   3091:        int multibyte_cp = _getmbcp();
1.1.1.23  root     3092:        
1.1.1.28  root     3093:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     3094:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.59  root     3095:        ci_old = ci_new = ci;
1.1.1.24  root     3096:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1.1.61  root     3097:        get_console_font_success = get_console_font_size(&font_width, &font_height);
1.1       root     3098:        
1.1.1.14  root     3099:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   3100:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   3101:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3102:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     3103:                }
                   3104:        }
1.1.1.28  root     3105:        if(get_console_info_success) {
1.1.1.12  root     3106:                scr_width = csbi.dwSize.X;
1.1.1.14  root     3107:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   3108:                
1.1.1.28  root     3109:                // v-text shadow buffer size must be lesser than 0x7fd0
                   3110:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     3111:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   3112:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   3113:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     3114:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     3115:                                scr_width = 80;
                   3116:                                scr_height = 25;
                   3117:                        }
1.1.1.28  root     3118:                        screen_size_changed = true;
1.1.1.14  root     3119:                }
1.1.1.12  root     3120:        } else {
                   3121:                // for a proof (not a console)
                   3122:                scr_width = 80;
                   3123:                scr_height = 25;
                   3124:        }
1.1.1.14  root     3125:        scr_buf_size.X = scr_width;
                   3126:        scr_buf_size.Y = scr_height;
                   3127:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   3128:        scr_top = csbi.srWindow.Top;
1.1       root     3129:        cursor_moved = false;
1.1.1.59  root     3130:        cursor_moved_by_crtc = false;
1.1       root     3131:        
1.1.1.54  root     3132:        SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
                   3133:        
1.1.1.35  root     3134: #ifdef USE_SERVICE_THREAD
                   3135:        InitializeCriticalSection(&input_crit_sect);
                   3136:        InitializeCriticalSection(&key_buf_crit_sect);
                   3137:        InitializeCriticalSection(&putch_crit_sect);
1.1.1.50  root     3138:        main_thread_id = GetCurrentThreadId();
1.1.1.35  root     3139: #endif
1.1.1.50  root     3140:        
1.1.1.25  root     3141:        key_buf_char = new FIFO(256);
                   3142:        key_buf_scan = new FIFO(256);
1.1.1.57  root     3143:        key_buf_data = new FIFO(256);
1.1       root     3144:        
                   3145:        hardware_init();
                   3146:        
1.1.1.33  root     3147: #ifdef USE_DEBUGGER
                   3148:        debugger_init();
                   3149: #endif
                   3150:        
1.1.1.9   root     3151:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     3152:                retval = EXIT_FAILURE;
                   3153:        } else {
1.1.1.27  root     3154: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     3155:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   3156: #endif
                   3157:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   3158:                
1.1.1.28  root     3159:                if(screen_size_changed) {
1.1.1.24  root     3160:                        change_console_size(scr_width, scr_height);
                   3161:                }
1.1.1.8   root     3162:                TIMECAPS caps;
                   3163:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   3164:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.35  root     3165: #ifdef USE_VRAM_THREAD
1.1.1.14  root     3166:                InitializeCriticalSection(&vram_crit_sect);
                   3167:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   3168: #endif
1.1.1.33  root     3169: #ifdef USE_DEBUGGER
                   3170:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   3171:                // wait until telnet client starts and connects to me
                   3172:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   3173:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   3174:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   3175:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   3176:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   3177:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   3178:                                Sleep(100);
                   3179:                        }
                   3180:                }
                   3181: #endif
1.1       root     3182:                hardware_run();
1.1.1.35  root     3183: #ifdef USE_VRAM_THREAD
1.1.1.14  root     3184:                vram_flush();
                   3185:                DeleteCriticalSection(&vram_crit_sect);
                   3186: #endif
1.1.1.24  root     3187:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     3188:                
1.1.1.24  root     3189:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.61  root     3190:                hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   3191:                
                   3192:                // restore console settings
                   3193:                _setmbcp(multibyte_cp);
                   3194:                SetConsoleCP(input_cp);
                   3195:                SetConsoleOutputCP(multibyte_cp);
                   3196:                
1.1.1.28  root     3197:                if(get_console_info_success) {
1.1.1.12  root     3198:                        if(restore_console_on_exit) {
1.1.1.14  root     3199:                                // window can't be bigger than buffer,
                   3200:                                // buffer can't be smaller than window,
                   3201:                                // so make a tiny window,
                   3202:                                // set the required buffer,
                   3203:                                // then set the required window
1.1.1.61  root     3204:                                CONSOLE_SCREEN_BUFFER_INFO cur_csbi;
1.1.1.14  root     3205:                                SMALL_RECT rect;
1.1.1.61  root     3206:                                GetConsoleScreenBufferInfo(hStdout, &cur_csbi);
                   3207:                                int min_width  = min(cur_csbi.srWindow.Right - cur_csbi.srWindow.Left + 1, csbi.srWindow.Right - csbi.srWindow.Left + 1);
                   3208:                                int min_height = min(cur_csbi.srWindow.Bottom - cur_csbi.srWindow.Top + 1, csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
                   3209:                                
                   3210:                                SET_RECT(rect, 0, cur_csbi.srWindow.Top, min_width - 1, cur_csbi.srWindow.Top + min_height - 1);
1.1.1.14  root     3211:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     3212:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     3213:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.61  root     3214:                                if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
                   3215:                                        SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
                   3216:                                        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3217:                                }
                   3218:                        }
                   3219:                }
                   3220:                if(get_console_font_success) {
                   3221:                        set_console_font_size(font_width, font_height);
                   3222:                }
                   3223:                if(get_console_info_success) {
                   3224:                        if(restore_console_on_exit) {
                   3225:                                SMALL_RECT rect;
                   3226:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
                   3227:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
                   3228:                                if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
                   3229:                                        SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
                   3230:                                        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3231:                                }
1.1.1.12  root     3232:                        }
1.1.1.14  root     3233:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   3234:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     3235:                }
1.1.1.64  root     3236:                if(dwConsoleMode & (ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE)) {
                   3237:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_EXTENDED_FLAGS);
                   3238:                } else {
                   3239:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   3240:                }
1.1.1.24  root     3241:                
1.1       root     3242:                msdos_finish();
1.1.1.14  root     3243:                
                   3244:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     3245:        }
1.1.1.35  root     3246:        if(temp_file_created) {
1.1.1.60  root     3247:                DeleteFileA(temp_file_path);
1.1.1.35  root     3248:                temp_file_created = false;
                   3249:        }
1.1.1.10  root     3250:        hardware_finish();
                   3251:        
1.1.1.28  root     3252:        if(key_buf_char != NULL) {
                   3253:                key_buf_char->release();
                   3254:                delete key_buf_char;
                   3255:                key_buf_char = NULL;
                   3256:        }
                   3257:        if(key_buf_scan != NULL) {
                   3258:                key_buf_scan->release();
                   3259:                delete key_buf_scan;
                   3260:                key_buf_scan = NULL;
                   3261:        }
1.1.1.57  root     3262:        if(key_buf_data != NULL) {
                   3263:                key_buf_data->release();
                   3264:                delete key_buf_data;
                   3265:                key_buf_data = NULL;
                   3266:        }
1.1.1.35  root     3267: #ifdef USE_SERVICE_THREAD
                   3268:        DeleteCriticalSection(&input_crit_sect);
                   3269:        DeleteCriticalSection(&key_buf_crit_sect);
                   3270:        DeleteCriticalSection(&putch_crit_sect);
                   3271: #endif
1.1.1.20  root     3272: #ifdef _DEBUG
                   3273:        _CrtDumpMemoryLeaks();
                   3274: #endif
1.1       root     3275:        return(retval);
                   3276: }
                   3277: 
1.1.1.20  root     3278: /* ----------------------------------------------------------------------------
                   3279:        console
                   3280: ---------------------------------------------------------------------------- */
                   3281: 
1.1.1.14  root     3282: void change_console_size(int width, int height)
1.1.1.12  root     3283: {
1.1.1.23  root     3284:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     3285:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   3286:        SMALL_RECT rect;
                   3287:        COORD co;
                   3288:        
                   3289:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     3290:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   3291:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
1.1.1.60  root     3292:                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
1.1.1.14  root     3293:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.60  root     3294:                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     3295:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   3296:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
1.1.1.60  root     3297:                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     3298:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.60  root     3299:                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     3300:                }
                   3301:        }
1.1.1.14  root     3302:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     3303:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     3304:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     3305:                SetConsoleCursorPosition(hStdout, co);
                   3306:                cursor_moved = true;
1.1.1.59  root     3307:                cursor_moved_by_crtc = false;
1.1.1.12  root     3308:        }
1.1.1.14  root     3309:        
                   3310:        // window can't be bigger than buffer,
                   3311:        // buffer can't be smaller than window,
                   3312:        // so make a tiny window,
                   3313:        // set the required buffer,
                   3314:        // then set the required window
1.1.1.61  root     3315:        int min_width  = min(csbi.srWindow.Right - csbi.srWindow.Left + 1, width);
                   3316:        int min_height = min(csbi.srWindow.Bottom - csbi.srWindow.Top + 1, height);
                   3317:        
                   3318:        SET_RECT(rect, 0, csbi.srWindow.Top, min_width - 1, csbi.srWindow.Top + min_height - 1);
1.1.1.12  root     3319:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     3320:        co.X = width;
                   3321:        co.Y = height;
1.1.1.12  root     3322:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     3323:        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.61  root     3324:        if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
                   3325:                SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
                   3326:                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3327:        }
1.1.1.14  root     3328:        
                   3329:        scr_width = scr_buf_size.X = width;
                   3330:        scr_height = scr_buf_size.Y = height;
                   3331:        scr_top = 0;
                   3332:        
                   3333:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   3334:        
                   3335:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     3336:        text_vram_end_address = text_vram_top_address + regen;
                   3337:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   3338:        
1.1.1.14  root     3339:        if(regen > 0x4000) {
                   3340:                regen = 0x8000;
                   3341:                vram_pages = 1;
                   3342:        } else if(regen > 0x2000) {
                   3343:                regen = 0x4000;
                   3344:                vram_pages = 2;
                   3345:        } else if(regen > 0x1000) {
                   3346:                regen = 0x2000;
                   3347:                vram_pages = 4;
                   3348:        } else {
                   3349:                regen = 0x1000;
                   3350:                vram_pages = 8;
                   3351:        }
1.1.1.15  root     3352:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   3353:        *(UINT16 *)(mem + 0x44c) = regen;
                   3354:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   3355:        
1.1.1.24  root     3356:        mouse.min_position.x = 0;
                   3357:        mouse.min_position.y = 0;
1.1.1.34  root     3358:        mouse.max_position.x = 8 * (scr_width  - 1);
                   3359:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     3360:        
1.1.1.15  root     3361:        restore_console_on_exit = true;
1.1.1.14  root     3362: }
                   3363: 
                   3364: void clear_scr_buffer(WORD attr)
                   3365: {
                   3366:        for(int y = 0; y < scr_height; y++) {
                   3367:                for(int x = 0; x < scr_width; x++) {
                   3368:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3369:                        SCR_BUF(y,x).Attributes = attr;
                   3370:                }
                   3371:        }
1.1.1.12  root     3372: }
                   3373: 
1.1.1.24  root     3374: bool update_console_input()
1.1       root     3375: {
1.1.1.35  root     3376: #ifdef USE_SERVICE_THREAD
                   3377:        EnterCriticalSection(&input_crit_sect);
                   3378: #endif
1.1.1.23  root     3379:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     3380:        DWORD dwNumberOfEvents = 0;
1.1       root     3381:        DWORD dwRead;
                   3382:        INPUT_RECORD ir[16];
1.1.1.24  root     3383:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   3384:        bool result = false;
1.1       root     3385:        
1.1.1.8   root     3386:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   3387:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   3388:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     3389:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.59  root     3390:                                        if(ir[i].Event.MouseEvent.dwEventFlags & MOUSE_MOVED) {
                   3391:                                                if(mouse.hidden == 0 || (mouse.call_addr_ps2.dw && mouse.enabled_ps2)) {
                   3392:                                                        // NOTE: if restore_console_on_exit, console is not scrolled
                   3393:                                                        if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   3394:                                                                GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   3395:                                                        }
                   3396:                                                        // FIXME: character size is always 8x8 ???
                   3397:                                                        int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   3398:                                                        int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
                   3399:                                                        
                   3400:                                                        if(mouse.position.x != x || mouse.position.y != y) {
                   3401:                                                                mouse.position.x = x;
                   3402:                                                                mouse.position.y = y;
                   3403:                                                                mouse.status |= 1;
                   3404:                                                                mouse.status_alt |= 1;
                   3405:                                                        }
1.1.1.34  root     3406:                                                }
1.1.1.59  root     3407:                                        } else if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   3408:                                                for(int j = 0; j < MAX_MOUSE_BUTTONS; j++) {
1.1.1.34  root     3409:                                                        static const DWORD bits[] = {
                   3410:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   3411:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
                   3412:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
                   3413:                                                        };
1.1.1.59  root     3414:                                                        bool prev_status = mouse.buttons[j].status;
                   3415:                                                        mouse.buttons[j].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[j]) != 0);
1.1.1.34  root     3416:                                                        
1.1.1.59  root     3417:                                                        if(!prev_status && mouse.buttons[j].status) {
                   3418:                                                                mouse.buttons[j].pressed_times++;
                   3419:                                                                mouse.buttons[j].pressed_position.x = mouse.position.x;
                   3420:                                                                mouse.buttons[j].pressed_position.y = mouse.position.x;
                   3421:                                                                if(j < 2) {
                   3422:                                                                        mouse.status_alt |= 2 << (j * 2);
1.1.1.43  root     3423:                                                                }
1.1.1.59  root     3424:                                                                mouse.status |= 2 << (j * 2);
                   3425:                                                        } else if(prev_status && !mouse.buttons[j].status) {
                   3426:                                                                mouse.buttons[j].released_times++;
                   3427:                                                                mouse.buttons[j].released_position.x = mouse.position.x;
                   3428:                                                                mouse.buttons[j].released_position.y = mouse.position.x;
                   3429:                                                                if(j < 2) {
                   3430:                                                                        mouse.status_alt |= 4 << (j * 2);
1.1.1.43  root     3431:                                                                }
1.1.1.59  root     3432:                                                                mouse.status |= 4 << (j * 2);
1.1.1.14  root     3433:                                                        }
                   3434:                                                }
                   3435:                                        }
1.1.1.24  root     3436:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3437:                                        // update keyboard flags in bios data area
1.1.1.35  root     3438:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
                   3439:                                                mem[0x417] |= 0x40;
1.1.1.33  root     3440:                                        } else {
1.1.1.35  root     3441:                                                mem[0x417] &= ~0x40;
1.1.1.33  root     3442:                                        }
1.1.1.35  root     3443:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
                   3444:                                                mem[0x417] |= 0x20;
1.1.1.33  root     3445:                                        } else {
1.1.1.35  root     3446:                                                mem[0x417] &= ~0x20;
                   3447:                                        }
                   3448:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
                   3449:                                                mem[0x417] |= 0x10;
                   3450:                                        } else {
                   3451:                                                mem[0x417] &= ~0x10;
1.1.1.33  root     3452:                                        }
                   3453:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43  root     3454:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3455:                                                        mouse.status_alt |= 0x80;
                   3456:                                                }
1.1.1.33  root     3457:                                                mem[0x417] |= 0x08;
                   3458:                                        } else {
                   3459:                                                mem[0x417] &= ~0x08;
                   3460:                                        }
1.1.1.35  root     3461:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43  root     3462:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3463:                                                        mouse.status_alt |= 0x40;
                   3464:                                                }
1.1.1.35  root     3465:                                                mem[0x417] |= 0x04;
1.1.1.33  root     3466:                                        } else {
1.1.1.35  root     3467:                                                mem[0x417] &= ~0x04;
1.1.1.33  root     3468:                                        }
                   3469:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43  root     3470:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3471:                                                        mouse.status_alt |= 0x20;
                   3472:                                                }
1.1.1.33  root     3473:                                                if(!(mem[0x417] & 0x03)) {
                   3474:                                                        mem[0x417] |= 0x02; // left shift
                   3475:                                                }
                   3476:                                        } else {
                   3477:                                                mem[0x417] &= ~0x03;
                   3478:                                        }
1.1.1.35  root     3479:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3480:                                                mem[0x418] |= 0x02;
                   3481:                                        } else {
                   3482:                                                mem[0x418] &= ~0x02;
                   3483:                                        }
                   3484:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3485:                                                mem[0x418] |= 0x01;
                   3486:                                        } else {
                   3487:                                                mem[0x418] &= ~0x01;
                   3488:                                        }
1.1.1.33  root     3489:                                        
1.1.1.28  root     3490:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.57  root     3491: //                                     kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
                   3492: //                                     kbd_status |= 1;
                   3493:                                        UINT8 tmp_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3494:                                        
                   3495:                                        // update dos key buffer
                   3496:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3497:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
1.1.1.51  root     3498:                                        UINT8 scn_old = scn;
1.1.1.33  root     3499:                                        
                   3500:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3501:                                                // make
1.1.1.57  root     3502:                                                tmp_data &= 0x7f;
1.1.1.24  root     3503:                                                
1.1.1.33  root     3504:                                                if(chr == 0x00) {
1.1.1.24  root     3505:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3506:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3507:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3508:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3509:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3510:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3511:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3512:                                                                } else if(scn == 0x35) {
                   3513:                                                                        scn = 0xa4;             // keypad /
                   3514:                                                                }
                   3515:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3516:                                                                if(scn == 0x07) {
                   3517:                                                                        chr = 0x1e;     // Ctrl+^
                   3518:                                                                } else if(scn == 0x0c) {
                   3519:                                                                        chr = 0x1f;     // Ctrl+_
                   3520:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3521:                                                                        static const UINT8 ctrl_map[] = {
                   3522:                                                                                0x95,   // keypad /
                   3523:                                                                                0,
                   3524:                                                                                0x96,   // keypad *
                   3525:                                                                                0, 0, 0,
                   3526:                                                                                0x5e,   // F1
                   3527:                                                                                0x5f,   // F2
                   3528:                                                                                0x60,   // F3
                   3529:                                                                                0x61,   // F4
                   3530:                                                                                0x62,   // F5
                   3531:                                                                                0x63,   // F6
                   3532:                                                                                0x64,   // F7
                   3533:                                                                                0x65,   // F8
                   3534:                                                                                0x66,   // F9
                   3535:                                                                                0x67,   // F10
                   3536:                                                                                0,
                   3537:                                                                                0,
                   3538:                                                                                0x77,   // Home
                   3539:                                                                                0x8d,   // Up
                   3540:                                                                                0x84,   // PgUp
                   3541:                                                                                0x8e,   // keypad -
                   3542:                                                                                0x73,   // Left
                   3543:                                                                                0x8f,   // keypad center
                   3544:                                                                                0x74,   // Right
                   3545:                                                                                0x90,   // keyapd +
                   3546:                                                                                0x75,   // End
                   3547:                                                                                0x91,   // Down
                   3548:                                                                                0x76,   // PgDn
                   3549:                                                                                0x92,   // Insert
                   3550:                                                                                0x93,   // Delete
                   3551:                                                                                0, 0, 0,
                   3552:                                                                                0x89,   // F11
                   3553:                                                                                0x8a,   // F12
                   3554:                                                                        };
                   3555:                                                                        scn = ctrl_map[scn - 0x35];
                   3556:                                                                }
                   3557:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3558:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3559:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3560:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3561:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3562:                                                                }
                   3563:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3564:                                                                scn += 0x85 - 0x57;
                   3565:                                                        }
                   3566:                                                        // ignore shift, ctrl, alt, win and menu keys
1.1.1.51  root     3567:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && !(scn >= 0x5b && scn <= 0x5d && scn == scn_old)) {
1.1.1.32  root     3568:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3569: #ifdef USE_SERVICE_THREAD
                   3570:                                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3571: #endif
1.1.1.32  root     3572:                                                                        if(chr == 0) {
1.1.1.51  root     3573:                                                                                pcbios_set_key_buffer(0x00, ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
1.1.1.32  root     3574:                                                                        }
1.1.1.51  root     3575:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3576: #ifdef USE_SERVICE_THREAD
                   3577:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3578: #endif
1.1.1.24  root     3579:                                                                }
                   3580:                                                        }
                   3581:                                                } else {
                   3582:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3583:                                                                chr = 0;
                   3584:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3585:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3586:                                                                }
                   3587:                                                        }
1.1.1.32  root     3588:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3589: #ifdef USE_SERVICE_THREAD
                   3590:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3591: #endif
1.1.1.51  root     3592:                                                                pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3593: #ifdef USE_SERVICE_THREAD
                   3594:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3595: #endif
1.1.1.32  root     3596:                                                        }
1.1.1.24  root     3597:                                                }
1.1.1.57  root     3598:                                        } else {
                   3599:                                                if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
                   3600:                                                        // ctrl-break, ctrl-c
                   3601:                                                        if(scn == 0x46) {
                   3602:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3603: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3604:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3605: #endif
1.1.1.57  root     3606:                                                                        pcbios_set_key_buffer(0x00, 0x00);
1.1.1.35  root     3607: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3608:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3609: #endif
1.1.1.57  root     3610:                                                                }
                   3611:                                                                ctrl_break_pressed = true;
                   3612:                                                                mem[0x471] = 0x80;
                   3613:                                                                raise_int_1bh = true;
                   3614:                                                        } else {
                   3615:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3616: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3617:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3618: #endif
1.1.1.57  root     3619:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3620: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3621:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3622: #endif
1.1.1.57  root     3623:                                                                }
                   3624:                                                                ctrl_c_pressed = (scn == 0x2e);
1.1.1.33  root     3625:                                                        }
                   3626:                                                }
                   3627:                                                // break
1.1.1.57  root     3628:                                                tmp_data |= 0x80;
                   3629:                                        }
                   3630:                                        if(!(kbd_status & 1)) {
                   3631:                                                kbd_data = tmp_data;
                   3632:                                                kbd_status |= 1;
                   3633:                                        } else {
                   3634:                                                if(key_buf_data != NULL) {
                   3635: #ifdef USE_SERVICE_THREAD
                   3636:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3637: #endif
                   3638:                                                        key_buf_data->write(tmp_data);
                   3639: #ifdef USE_SERVICE_THREAD
                   3640:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3641: #endif
                   3642:                                                }
1.1       root     3643:                                        }
1.1.1.24  root     3644:                                        result = key_changed = true;
1.1.1.36  root     3645:                                        // IME may be on and it may causes screen scroll up and cursor position change
                   3646:                                        cursor_moved = true;
1.1       root     3647:                                }
                   3648:                        }
                   3649:                }
                   3650:        }
1.1.1.35  root     3651: #ifdef USE_SERVICE_THREAD
                   3652:        LeaveCriticalSection(&input_crit_sect);
                   3653: #endif
1.1.1.24  root     3654:        return(result);
1.1.1.8   root     3655: }
                   3656: 
1.1.1.14  root     3657: bool update_key_buffer()
1.1.1.8   root     3658: {
1.1.1.35  root     3659:        if(update_console_input()) {
                   3660:                return(true);
                   3661:        }
                   3662:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3663: #ifdef USE_SERVICE_THREAD
                   3664:                EnterCriticalSection(&key_buf_crit_sect);
                   3665: #endif
1.1.1.55  root     3666:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     3667: #ifdef USE_SERVICE_THREAD
                   3668:                LeaveCriticalSection(&key_buf_crit_sect);
                   3669: #endif
                   3670:                if(!empty) return(true);
                   3671:        }
                   3672:        return(false);
1.1.1.8   root     3673: }
                   3674: 
1.1.1.20  root     3675: /* ----------------------------------------------------------------------------
                   3676:        MS-DOS virtual machine
                   3677: ---------------------------------------------------------------------------- */
                   3678: 
1.1.1.32  root     3679: static const struct {
1.1.1.33  root     3680:        char *name;
                   3681:        DWORD lcid;
                   3682:        char *std;
                   3683:        char *dlt;
                   3684: } tz_table[] = {
                   3685:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3686: //     0       GMT             Greenwich Mean Time             GMT0
                   3687:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3688:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3689:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3690:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3691: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3692:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3693:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3694: //     3       BST             Brazil Standard Time            BST3
                   3695:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3696:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3697:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3698: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3699:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3700: //     3       GST             Greenland Standard Time         GST3
                   3701:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3702: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3703:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3704: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3705:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3706: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3707:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3708:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3709: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3710:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3711:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3712:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3713: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3714:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3715: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3716:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3717: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3718:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3719: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3720:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3721:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3722:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3723: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3724:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3725: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3726:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3727:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3728:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3729: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3730:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3731:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3732: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3733: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3734:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3735: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3736:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3737:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3738: //     11      SST             Samoa Standard Time             SST11
                   3739:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3740: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3741:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3742: //     -10     GST             Guam Standard Time              GST-10
                   3743:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3744: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3745:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3746:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3747:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3748: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3749:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3750:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3751: //     -9      JST             Japan Standard Time             JST-9
                   3752:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3753: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3754:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3755:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3756: //     -8      HKT             Hong Kong Time                  HKT-8
                   3757:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3758: //     -8      CCT             China Coast Time                CCT-8
                   3759:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3760:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3761: //     -8      SST             Singapore Standard Time         SST-8
                   3762:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3763: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3764:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3765:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3766: //     -7:30   JT              Java Standard Time              JST-7:30
                   3767: //     -7      NST             North Sumatra Time              NST-7
                   3768:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3769: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3770:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3771: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3772:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3773: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3774:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3775:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3776: //     -2      EET             Eastern Europe Time             EET-2
                   3777:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3778:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3779:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3780:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3781: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3782:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3783: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3784: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3785: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3786: //     -1      CET     CES     Central European Time           CET-1CES
                   3787:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3788:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3789:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3790:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3791: //     -1      WAT             West African Time               WAT-1
                   3792:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3793:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3794: //     0       UTC             Universal Coordinated Time      UTC0
                   3795:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3796:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3797:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3798:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3799:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3800:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3801: };
                   3802: 
1.1.1.53  root     3803: // FIXME: consider to build on non-Japanese environment :-(
                   3804: // message_japanese string must be in shift-jis
                   3805: 
1.1.1.33  root     3806: static const struct {
1.1.1.32  root     3807:        UINT16 code;
                   3808:        char *message_english;
                   3809:        char *message_japanese;
                   3810: } standard_error_table[] = {
                   3811:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3812:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3813:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3814:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3815:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3816:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3817:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3818:        {0x08,  "Insufficient memory", "������������܂���."},
                   3819:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3820:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3821:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3822:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3823:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3824:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3825:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3826:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3827:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3828:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3829:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3830:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3831:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3832:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3833:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3834:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3835:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3836:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3837:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3838:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3839:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3840:        {0x1F,  "General failure", "�G���[�ł�."},
                   3841:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3842:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3843:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3844:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3845:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3846:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3847:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3848:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3849: /*
                   3850:        {0x32,  "Network request not supported", NULL},
                   3851:        {0x33,  "Remote computer not listening", NULL},
                   3852:        {0x34,  "Duplicate name on network", NULL},
                   3853:        {0x35,  "Network name not found", NULL},
                   3854:        {0x36,  "Network busy", NULL},
                   3855:        {0x37,  "Network device no longer exists", NULL},
                   3856:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3857:        {0x39,  "Network adapter hardware error", NULL},
                   3858:        {0x3A,  "Incorrect response from network", NULL},
                   3859:        {0x3B,  "Unexpected network error", NULL},
                   3860:        {0x3C,  "Incompatible remote adapter", NULL},
                   3861:        {0x3D,  "Print queue full", NULL},
                   3862:        {0x3E,  "Queue not full", NULL},
                   3863:        {0x3F,  "Not enough space to print file", NULL},
                   3864:        {0x40,  "Network name was deleted", NULL},
                   3865:        {0x41,  "Network: Access denied", NULL},
                   3866:        {0x42,  "Network device type incorrect", NULL},
                   3867:        {0x43,  "Network name not found", NULL},
                   3868:        {0x44,  "Network name limit exceeded", NULL},
                   3869:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3870:        {0x46,  "Temporarily paused", NULL},
                   3871:        {0x47,  "Network request not accepted", NULL},
                   3872:        {0x48,  "Network print/disk redirection paused", NULL},
                   3873:        {0x49,  "Network software not installed", NULL},
                   3874:        {0x4A,  "Unexpected adapter close", NULL},
                   3875: */
                   3876:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3877:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3878:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3879:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3880:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3881:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3882:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3883:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3884:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3885:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
1.1.1.53  root     3886: #ifdef SUPPORT_MSCDEX
1.1.1.32  root     3887:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3888:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3889:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3890:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3891:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
1.1.1.53  root     3892: #endif
1.1.1.32  root     3893:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3894:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3895:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3896:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3897:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3898:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3899: };
                   3900: 
                   3901: static const struct {
                   3902:        UINT16 code;
                   3903:        char *message_english;
                   3904:        char *message_japanese;
                   3905: } param_error_table[] = {
                   3906:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3907:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3908:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3909:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3910:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3911:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3912:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3913:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3914:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3915:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3916:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3917: };
                   3918: 
                   3919: static const struct {
                   3920:        UINT16 code;
                   3921:        char *message_english;
                   3922:        char *message_japanese;
                   3923: } critical_error_table[] = {
                   3924:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3925:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3926:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3927:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3928:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3929:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3930:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3931:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3932:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3933:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3934:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3935:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3936:        {0x0C,  "General failure", "�G���[�ł�."},
                   3937:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3938:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3939:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3940:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3941:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3942:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3943:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3944:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3945:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3946: };
                   3947: 
1.1.1.20  root     3948: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3949: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3950: void msdos_putch(UINT8 data);
1.1.1.50  root     3951: void msdos_putch_fast(UINT8 data);
1.1.1.35  root     3952: #ifdef USE_SERVICE_THREAD
                   3953: void msdos_putch_tmp(UINT8 data);
                   3954: #endif
1.1.1.45  root     3955: const char *msdos_short_path(const char *path);
1.1.1.44  root     3956: bool msdos_is_valid_drive(int drv);
                   3957: bool msdos_is_removable_drive(int drv);
                   3958: bool msdos_is_cdrom_drive(int drv);
                   3959: bool msdos_is_remote_drive(int drv);
                   3960: bool msdos_is_subst_drive(int drv);
1.1.1.20  root     3961: 
1.1       root     3962: // process info
                   3963: 
                   3964: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3965: {
                   3966:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3967:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3968:                        memset(&process[i], 0, sizeof(process_t));
                   3969:                        process[i].psp = psp_seg;
                   3970:                        return(&process[i]);
                   3971:                }
                   3972:        }
                   3973:        fatalerror("too many processes\n");
                   3974:        return(NULL);
                   3975: }
                   3976: 
1.1.1.52  root     3977: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error = true)
1.1       root     3978: {
                   3979:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3980:                if(process[i].psp == psp_seg) {
                   3981:                        return(&process[i]);
                   3982:                }
                   3983:        }
1.1.1.33  root     3984:        if(show_error) {
                   3985:                fatalerror("invalid psp address\n");
                   3986:        }
1.1       root     3987:        return(NULL);
                   3988: }
                   3989: 
1.1.1.23  root     3990: void msdos_sda_update(int psp_seg)
                   3991: {
                   3992:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3993:        
                   3994:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3995:                if(process[i].psp == psp_seg) {
                   3996:                        sda->switchar = process[i].switchar;
                   3997:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3998:                        sda->current_dta.w.h = process[i].dta.w.h;
                   3999:                        sda->current_psp = process[i].psp;
                   4000:                        break;
                   4001:                }
                   4002:        }
                   4003:        sda->malloc_strategy = malloc_strategy;
                   4004:        sda->return_code = retval;
                   4005:        sda->current_drive = _getdrive();
                   4006: }
                   4007: 
1.1.1.13  root     4008: // dta info
                   4009: 
                   4010: void msdos_dta_info_init()
                   4011: {
1.1.1.14  root     4012:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     4013:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   4014:        }
                   4015: }
                   4016: 
                   4017: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   4018: {
                   4019:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     4020:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   4021:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   4022:                        if(free_dta == NULL) {
1.1.1.13  root     4023:                                free_dta = &dtalist[i];
                   4024:                        }
1.1.1.14  root     4025:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     4026:                        return(&dtalist[i]);
                   4027:                }
                   4028:        }
1.1.1.14  root     4029:        if(free_dta) {
1.1.1.13  root     4030:                free_dta->psp = psp_seg;
                   4031:                free_dta->dta = dta_laddr;
                   4032:                return(free_dta);
                   4033:        }
                   4034:        fatalerror("too many dta\n");
                   4035:        return(NULL);
                   4036: }
                   4037: 
                   4038: void msdos_dta_info_free(UINT16 psp_seg)
                   4039: {
1.1.1.14  root     4040:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   4041:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     4042:                        FindClose(dtalist[i].find_handle);
                   4043:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   4044:                }
                   4045:        }
                   4046: }
                   4047: 
1.1       root     4048: void msdos_cds_update(int drv)
                   4049: {
1.1.1.44  root     4050:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1       root     4051:        
1.1.1.44  root     4052:        memset(cds, 0, 88);
                   4053:        
                   4054:        if(msdos_is_valid_drive(drv)) {
                   4055:                char path[MAX_PATH];
                   4056:                if(msdos_is_remote_drive(drv)) {
                   4057:                        cds->drive_attrib = 0xc000;     // network drive
                   4058:                } else if(msdos_is_subst_drive(drv)) {
                   4059:                        cds->drive_attrib = 0x5000;     // subst drive
                   4060:                } else {
                   4061:                        cds->drive_attrib = 0x4000;     // physical drive
                   4062:                }
                   4063:                if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
                   4064:                        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
                   4065:                }
                   4066:        }
                   4067:        if(cds->path_name[0] == '\0') {
                   4068:                sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   4069:        }
                   4070:        cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   4071:        cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
                   4072:        cds->word_1 = cds->word_2 = 0xffff;
                   4073:        cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
                   4074:        cds->bs_offset = 2;
                   4075: }
                   4076: 
1.1.1.45  root     4077: void msdos_cds_update(int drv, const char *path)
1.1.1.44  root     4078: {
                   4079:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
                   4080:        
                   4081:        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1       root     4082: }
                   4083: 
1.1.1.17  root     4084: // nls information tables
                   4085: 
                   4086: // uppercase table (func 6502h)
                   4087: void msdos_upper_table_update()
                   4088: {
                   4089:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     4090:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     4091:                UINT8 c[4];
1.1.1.33  root     4092:                *(UINT32 *)c = 0;               // reset internal conversion state
                   4093:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     4094:                c[0] = 0x80 + i;
                   4095:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   4096:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   4097:        }
                   4098: }
                   4099: 
1.1.1.23  root     4100: // lowercase table (func 6503h)
                   4101: void msdos_lower_table_update()
                   4102: {
                   4103:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   4104:        for(unsigned i = 0; i < 0x80; ++i) {
                   4105:                UINT8 c[4];
1.1.1.33  root     4106:                *(UINT32 *)c = 0;               // reset internal conversion state
                   4107:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     4108:                c[0] = 0x80 + i;
                   4109:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   4110:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   4111:        }
                   4112: }
                   4113: 
1.1.1.17  root     4114: // filename uppercase table (func 6504h)
                   4115: void msdos_filename_upper_table_init()
                   4116: {
                   4117:        // depended on (file)system, not on active codepage
                   4118:        // temporary solution: just filling data
                   4119:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     4120:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     4121:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   4122:        }
                   4123: }
                   4124: 
                   4125: // filaname terminator table (func 6505h)
                   4126: void msdos_filename_terminator_table_init()
                   4127: {
                   4128:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   4129:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   4130:        
                   4131:        data[2] = 1;            // marker? (permissible character value)
                   4132:        data[3] = 0x00;         // 00h...FFh
                   4133:        data[4] = 0xff;
                   4134:        data[5] = 0;            // marker? (excluded character)
                   4135:        data[6] = 0x00;         // 00h...20h
                   4136:        data[7] = 0x20;
                   4137:        data[8] = 2;            // marker? (illegal characters for filename)
                   4138:        data[9] = (UINT8)strlen(illegal_chars);
                   4139:        memcpy(data + 10, illegal_chars, data[9]);
                   4140:        
                   4141:        // total length
                   4142:        *(UINT16 *)data = (10 - 2) + data[9];
                   4143: }
                   4144: 
                   4145: // collating table (func 6506h)
                   4146: void msdos_collating_table_update()
                   4147: {
                   4148:        // temporary solution: just filling data
                   4149:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     4150:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     4151:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   4152:        }
                   4153: }
                   4154: 
1.1       root     4155: // dbcs
                   4156: 
                   4157: void msdos_dbcs_table_update()
                   4158: {
                   4159:        UINT8 dbcs_data[DBCS_SIZE];
                   4160:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   4161:        
                   4162:        CPINFO info;
                   4163:        GetCPInfo(active_code_page, &info);
                   4164:        
                   4165:        if(info.MaxCharSize != 1) {
                   4166:                for(int i = 0;; i += 2) {
                   4167:                        UINT8 lo = info.LeadByte[i + 0];
                   4168:                        UINT8 hi = info.LeadByte[i + 1];
                   4169:                        dbcs_data[2 + i + 0] = lo;
                   4170:                        dbcs_data[2 + i + 1] = hi;
                   4171:                        if(lo == 0 && hi == 0) {
                   4172:                                dbcs_data[0] = i + 2;
                   4173:                                break;
                   4174:                        }
                   4175:                }
                   4176:        } else {
                   4177:                dbcs_data[0] = 2;       // ???
                   4178:        }
                   4179:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   4180: }
                   4181: 
1.1.1.17  root     4182: void msdos_dbcs_table_finish()
                   4183: {
1.1.1.32  root     4184:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     4185:                _setmbcp(system_code_page);
                   4186:        }
1.1.1.32  root     4187:        if(console_code_page != GetConsoleCP()) {
                   4188:                SetConsoleCP(console_code_page);
                   4189:                SetConsoleOutputCP(console_code_page);
                   4190:        }
1.1.1.17  root     4191: }
                   4192: 
                   4193: void msdos_nls_tables_init()
1.1       root     4194: {
1.1.1.32  root     4195:        active_code_page = console_code_page = GetConsoleCP();
                   4196:        system_code_page = _getmbcp();
                   4197:        
                   4198:        if(active_code_page != system_code_page) {
                   4199:                if(_setmbcp(active_code_page) != 0) {
                   4200:                        active_code_page = system_code_page;
                   4201:                }
                   4202:        }
                   4203:        
1.1.1.17  root     4204:        msdos_upper_table_update();
1.1.1.23  root     4205:        msdos_lower_table_update();
1.1.1.17  root     4206:        msdos_filename_terminator_table_init();
                   4207:        msdos_filename_upper_table_init();
                   4208:        msdos_collating_table_update();
1.1       root     4209:        msdos_dbcs_table_update();
                   4210: }
                   4211: 
1.1.1.17  root     4212: void msdos_nls_tables_update()
1.1       root     4213: {
1.1.1.17  root     4214:        msdos_dbcs_table_update();
                   4215:        msdos_upper_table_update();
1.1.1.23  root     4216:        msdos_lower_table_update();
                   4217: //     msdos_collating_table_update();
1.1       root     4218: }
                   4219: 
                   4220: int msdos_lead_byte_check(UINT8 code)
                   4221: {
                   4222:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   4223:        
                   4224:        for(int i = 0;; i += 2) {
                   4225:                UINT8 lo = dbcs_table[i + 0];
                   4226:                UINT8 hi = dbcs_table[i + 1];
                   4227:                if(lo == 0 && hi == 0) {
                   4228:                        break;
                   4229:                }
                   4230:                if(lo <= code && code <= hi) {
                   4231:                        return(1);
                   4232:                }
                   4233:        }
                   4234:        return(0);
                   4235: }
                   4236: 
1.1.1.20  root     4237: int msdos_ctrl_code_check(UINT8 code)
                   4238: {
1.1.1.22  root     4239:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     4240: }
                   4241: 
1.1.1.36  root     4242: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
                   4243: {
                   4244:        int is_kanji_1st = 0;
                   4245:        int is_kanji_2nd = 0;
                   4246:        
                   4247:        for(int p = 0;; p++) {
                   4248:                if(is_kanji_1st) {
                   4249:                        is_kanji_1st = 0;
                   4250:                        is_kanji_2nd = 1;
                   4251:                } else if(msdos_lead_byte_check(buf[p])) {
                   4252:                        is_kanji_1st = 1;
                   4253:                }
                   4254:                if(p == n) {
                   4255:                        return(is_kanji_2nd);
                   4256:                }
                   4257:                is_kanji_2nd = 0;
                   4258:        }
                   4259: }
                   4260: 
1.1       root     4261: // file control
                   4262: 
1.1.1.45  root     4263: const char *msdos_remove_double_quote(const char *path)
1.1.1.14  root     4264: {
                   4265:        static char tmp[MAX_PATH];
                   4266:        
                   4267:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45  root     4268:                memset(tmp, 0, sizeof(tmp));
1.1.1.14  root     4269:                memcpy(tmp, path + 1, strlen(path) - 2);
                   4270:        } else {
                   4271:                strcpy(tmp, path);
                   4272:        }
                   4273:        return(tmp);
                   4274: }
                   4275: 
1.1.1.45  root     4276: const char *msdos_remove_end_separator(const char *path)
1.1.1.32  root     4277: {
                   4278:        static char tmp[MAX_PATH];
                   4279:        
                   4280:        strcpy(tmp, path);
1.1.1.45  root     4281:        
                   4282:        // for example "C:\" case, the end separator should not be removed
                   4283:        if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
                   4284:                tmp[strlen(tmp) - 1] = '\0';
1.1.1.32  root     4285:        }
                   4286:        return(tmp);
                   4287: }
                   4288: 
1.1.1.45  root     4289: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14  root     4290: {
                   4291:        static char tmp[MAX_PATH];
1.1.1.45  root     4292:        const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14  root     4293:        
                   4294:        if(strlen(tmp_dir) == 0) {
                   4295:                strcpy(tmp, file);
                   4296:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   4297:                sprintf(tmp, "%s%s", tmp_dir, file);
                   4298:        } else {
                   4299:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   4300:        }
                   4301:        return(tmp);
                   4302: }
                   4303: 
1.1.1.45  root     4304: const char *msdos_trimmed_path(const char *path, int lfn)
1.1       root     4305: {
                   4306:        static char tmp[MAX_PATH];
                   4307:        
                   4308:        if(lfn) {
                   4309:                strcpy(tmp, path);
                   4310:        } else {
                   4311:                // remove space in the path
1.1.1.45  root     4312:                const char *src = path;
                   4313:                char *dst = tmp;
1.1       root     4314:                
                   4315:                while(*src != '\0') {
                   4316:                        if(msdos_lead_byte_check(*src)) {
                   4317:                                *dst++ = *src++;
                   4318:                                *dst++ = *src++;
                   4319:                        } else if(*src != ' ') {
                   4320:                                *dst++ = *src++;
                   4321:                        } else {
                   4322:                                src++;  // skip space
                   4323:                        }
                   4324:                }
                   4325:                *dst = '\0';
                   4326:        }
1.1.1.14  root     4327:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   4328:                // redirect C:\COMMAND.COM to comspec_path
                   4329:                strcpy(tmp, comspec_path);
                   4330:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   4331:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   4332:                static int root_drive_protected = -1;
                   4333:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   4334:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   4335:                
1.1.1.60  root     4336:                if(GetFullPathNameA(tmp, MAX_PATH, temp, &name_temp) != 0 &&
1.1.1.14  root     4337:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   4338:                        strcpy(name, name_temp);
                   4339:                        name_temp[0] = '\0';
                   4340:                        
                   4341:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   4342:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   4343:                                if(root_drive_protected == -1) {
                   4344:                                        FILE *fp = NULL;
                   4345:                                        
                   4346:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   4347:                                        root_drive_protected = 1;
                   4348:                                        try {
                   4349:                                                if((fp = fopen(temp, "w")) != NULL) {
                   4350:                                                        if(fprintf(fp, "TEST") == 4) {
                   4351:                                                                root_drive_protected = 0;
                   4352:                                                        }
                   4353:                                                }
                   4354:                                        } catch(...) {
                   4355:                                        }
                   4356:                                        if(fp != NULL) {
                   4357:                                                fclose(fp);
                   4358:                                        }
                   4359:                                        if(_access(temp, 0) == 0) {
                   4360:                                                remove(temp);
                   4361:                                        }
                   4362:                                }
                   4363:                                if(root_drive_protected == 1) {
1.1.1.60  root     4364:                                        if(GetEnvironmentVariableA("TEMP", temp, MAX_PATH) != 0 ||
                   4365:                                           GetEnvironmentVariableA("TMP",  temp, MAX_PATH) != 0) {
1.1.1.14  root     4366:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   4367:                                        }
                   4368:                                }
                   4369:                        }
                   4370:                }
                   4371:        }
1.1       root     4372:        return(tmp);
                   4373: }
                   4374: 
1.1.1.45  root     4375: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28  root     4376: {
1.1.1.32  root     4377:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     4378:        static char env_path[ENV_SIZE];
                   4379:        char tmp[ENV_SIZE], *token;
                   4380:        
                   4381:        memset(env_path, 0, sizeof(env_path));
                   4382:        strcpy(tmp, src);
                   4383:        token = my_strtok(tmp, ";");
                   4384:        
                   4385:        while(token != NULL) {
                   4386:                if(token[0] != '\0') {
1.1.1.45  root     4387:                        const char *path = msdos_remove_double_quote(token);
                   4388:                        char short_path[MAX_PATH];
1.1.1.32  root     4389:                        if(path != NULL && strlen(path) != 0) {
                   4390:                                if(env_path[0] != '\0') {
                   4391:                                        strcat(env_path, ";");
                   4392:                                }
1.1.1.60  root     4393:                                if(GetShortPathNameA(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     4394:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     4395:                                } else {
                   4396:                                        my_strupr(short_path);
1.1.1.32  root     4397:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     4398:                                }
                   4399:                        }
                   4400:                }
                   4401:                token = my_strtok(NULL, ";");
                   4402:        }
                   4403:        return(env_path);
                   4404: }
                   4405: 
1.1.1.45  root     4406: bool match(const char *text, const char *pattern)
1.1       root     4407: {
1.1.1.24  root     4408:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     4409:        switch(*pattern) {
1.1       root     4410:        case '\0':
                   4411:                return !*text;
                   4412:        case '*':
1.1.1.14  root     4413:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     4414:        case '?':
                   4415:                return *text && match(text + 1, pattern + 1);
                   4416:        default:
                   4417:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   4418:        }
                   4419: }
                   4420: 
1.1.1.45  root     4421: bool msdos_match_volume_label(const char *path, const char *volume)
1.1       root     4422: {
1.1.1.45  root     4423:        const char *p = NULL;
1.1       root     4424:        
1.1.1.14  root     4425:        if(!*volume) {
                   4426:                return false;
                   4427:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     4428:                return msdos_match_volume_label(p + 1, volume);
                   4429:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   4430:                return msdos_match_volume_label(p + 1, volume);
                   4431:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     4432:                char tmp[MAX_PATH];
                   4433:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   4434:                return match(volume, tmp);
1.1       root     4435:        } else {
                   4436:                return match(volume, path);
                   4437:        }
                   4438: }
                   4439: 
1.1.1.45  root     4440: const char *msdos_fcb_path(fcb_t *fcb)
1.1       root     4441: {
                   4442:        static char tmp[MAX_PATH];
                   4443:        char name[9], ext[4];
                   4444:        
                   4445:        memset(name, 0, sizeof(name));
                   4446:        memcpy(name, fcb->file_name, 8);
                   4447:        strcpy(name, msdos_trimmed_path(name, 0));
                   4448:        
                   4449:        memset(ext, 0, sizeof(ext));
                   4450:        memcpy(ext, fcb->file_name + 8, 3);
                   4451:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4452:        
                   4453:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4454:                strcpy(name, "*");
                   4455:        }
                   4456:        if(ext[0] == '\0') {
                   4457:                strcpy(tmp, name);
                   4458:        } else {
                   4459:                if(strcmp(ext, "???") == 0) {
                   4460:                        strcpy(ext, "*");
                   4461:                }
                   4462:                sprintf(tmp, "%s.%s", name, ext);
                   4463:        }
                   4464:        return(tmp);
                   4465: }
                   4466: 
1.1.1.45  root     4467: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1       root     4468: {
1.1.1.60  root     4469:        char tmp[MAX_PATH];
                   4470:        strcpy(tmp, path);
                   4471:        char *ext = my_strchr(tmp, '.');
1.1       root     4472:        
                   4473:        memset(fcb->file_name, 0x20, 8 + 3);
1.1.1.60  root     4474:        if(ext != NULL && tmp[0] != '.') {
1.1       root     4475:                *ext = '\0';
                   4476:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4477:        }
1.1.1.60  root     4478:        memcpy(fcb->file_name, tmp, strlen(tmp));
1.1       root     4479: }
                   4480: 
1.1.1.45  root     4481: const char *msdos_short_path(const char *path)
1.1       root     4482: {
                   4483:        static char tmp[MAX_PATH];
                   4484:        
1.1.1.60  root     4485:        if(GetShortPathNameA(path, tmp, MAX_PATH) == 0) {
1.1.1.24  root     4486:                strcpy(tmp, path);
                   4487:        }
1.1       root     4488:        my_strupr(tmp);
                   4489:        return(tmp);
                   4490: }
                   4491: 
1.1.1.60  root     4492: const char *msdos_short_name(WIN32_FIND_DATAA *fd)
1.1.1.13  root     4493: {
                   4494:        static char tmp[MAX_PATH];
1.1.1.45  root     4495:        
1.1.1.14  root     4496:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4497:                strcpy(tmp, fd->cAlternateFileName);
                   4498:        } else {
                   4499:                strcpy(tmp, fd->cFileName);
                   4500:        }
                   4501:        my_strupr(tmp);
                   4502:        return(tmp);
                   4503: }
                   4504: 
1.1.1.45  root     4505: const char *msdos_short_full_path(const char *path)
1.1       root     4506: {
                   4507:        static char tmp[MAX_PATH];
                   4508:        char full[MAX_PATH], *name;
                   4509:        
1.1.1.14  root     4510:        // Full works with non-existent files, but Short does not
1.1.1.60  root     4511:        GetFullPathNameA(path, MAX_PATH, full, &name);
1.1.1.14  root     4512:        *tmp = '\0';
1.1.1.60  root     4513:        if(GetShortPathNameA(full, tmp, MAX_PATH) == 0 && name > path) {
1.1.1.14  root     4514:                name[-1] = '\0';
1.1.1.60  root     4515:                DWORD len = GetShortPathNameA(full, tmp, MAX_PATH);
1.1.1.14  root     4516:                if(len == 0) {
                   4517:                        strcpy(tmp, full);
                   4518:                } else {
                   4519:                        tmp[len++] = '\\';
                   4520:                        strcpy(tmp + len, name);
                   4521:                }
                   4522:        }
1.1       root     4523:        my_strupr(tmp);
                   4524:        return(tmp);
                   4525: }
                   4526: 
1.1.1.45  root     4527: const char *msdos_short_full_dir(const char *path)
1.1       root     4528: {
                   4529:        static char tmp[MAX_PATH];
                   4530:        char full[MAX_PATH], *name;
                   4531:        
1.1.1.60  root     4532:        GetFullPathNameA(path, MAX_PATH, full, &name);
1.1       root     4533:        name[-1] = '\0';
1.1.1.60  root     4534:        if(GetShortPathNameA(full, tmp, MAX_PATH) == 0) {
1.1.1.24  root     4535:                strcpy(tmp, full);
                   4536:        }
1.1       root     4537:        my_strupr(tmp);
                   4538:        return(tmp);
                   4539: }
                   4540: 
1.1.1.45  root     4541: const char *msdos_local_file_path(const char *path, int lfn)
1.1       root     4542: {
1.1.1.45  root     4543:        static char trimmed[MAX_PATH];
                   4544:        
                   4545:        strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14  root     4546: #if 0
                   4547:        // I have forgotten the reason of this routine... :-(
1.1       root     4548:        if(_access(trimmed, 0) != 0) {
                   4549:                process_t *process = msdos_process_info_get(current_psp);
                   4550:                static char tmp[MAX_PATH];
                   4551:                
                   4552:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4553:                if(_access(tmp, 0) == 0) {
                   4554:                        return(tmp);
                   4555:                }
                   4556:        }
1.1.1.14  root     4557: #endif
1.1       root     4558:        return(trimmed);
                   4559: }
                   4560: 
1.1.1.45  root     4561: bool msdos_is_device_path(const char *path)
1.1.1.11  root     4562: {
                   4563:        char full[MAX_PATH], *name;
                   4564:        
1.1.1.60  root     4565:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4566:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4567:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4568:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4569:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4570:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4571:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4572:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4573:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4574:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4575:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4576:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4577:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4578:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4579:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4580:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4581:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4582:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4583:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4584:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4585:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4586:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4587:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4588:                        return(true);
                   4589:                } else if(name != NULL) {
                   4590:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4591:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4592:                           _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43  root     4593: //                        _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30  root     4594:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4595:                                return(true);
                   4596:                        }
                   4597:                }
1.1.1.24  root     4598:        }
                   4599:        return(false);
1.1.1.11  root     4600: }
                   4601: 
1.1.1.45  root     4602: bool msdos_is_con_path(const char *path)
1.1.1.8   root     4603: {
1.1.1.14  root     4604:        char full[MAX_PATH], *name;
1.1.1.8   root     4605:        
1.1.1.60  root     4606:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4607:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4608:        }
                   4609:        return(false);
                   4610: }
                   4611: 
1.1.1.45  root     4612: int msdos_is_comm_path(const char *path)
1.1.1.24  root     4613: {
                   4614:        char full[MAX_PATH], *name;
                   4615:        
1.1.1.60  root     4616:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4617:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4618:                        return(1);
                   4619:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4620:                        return(2);
                   4621:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4622:                        return(3);
                   4623:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4624:                        return(4);
1.1.1.24  root     4625:                }
                   4626:        }
1.1.1.29  root     4627:        return(0);
                   4628: }
                   4629: 
1.1.1.45  root     4630: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37  root     4631: {
                   4632:        // 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     4633:        const char *p = NULL;
1.1.1.37  root     4634:        
                   4635:        if((p = strstr(path, ":")) != NULL) {
                   4636:                UINT8 selector = sio_read(sio_port - 1, 3);
                   4637:                
                   4638:                // baud rate
                   4639:                int baud = max(110, min(9600, atoi(p + 1)));
                   4640:                UINT16 divisor = 115200 / baud;
                   4641:                
                   4642:                if((p = strstr(p + 1, ",")) != NULL) {
                   4643:                        // parity
                   4644:                        if(p[1] == 'N' || p[1] == 'n') {
                   4645:                                selector = (selector & ~0x38) | 0x00;
                   4646:                        } else if(p[1] == 'O' || p[1] == 'o') {
                   4647:                                selector = (selector & ~0x38) | 0x08;
                   4648:                        } else if(p[1] == 'E' || p[1] == 'e') {
                   4649:                                selector = (selector & ~0x38) | 0x18;
                   4650:                        } else if(p[1] == 'M' || p[1] == 'm') {
                   4651:                                selector = (selector & ~0x38) | 0x28;
                   4652:                        } else if(p[1] == 'S' || p[1] == 's') {
                   4653:                                selector = (selector & ~0x38) | 0x38;
                   4654:                        }
                   4655:                        if((p = strstr(p + 1, ",")) != NULL) {
                   4656:                                // word length
                   4657:                                if(p[1] == '8') {
                   4658:                                        selector = (selector & ~0x03) | 0x03;
                   4659:                                } else if(p[1] == '7') {
                   4660:                                        selector = (selector & ~0x03) | 0x02;
                   4661:                                } else if(p[1] == '6') {
                   4662:                                        selector = (selector & ~0x03) | 0x01;
                   4663:                                } else if(p[1] == '5') {
                   4664:                                        selector = (selector & ~0x03) | 0x00;
                   4665:                                }
                   4666:                                if((p = strstr(p + 1, ",")) != NULL) {
                   4667:                                        // stop bits
                   4668:                                        float bits = atof(p + 1);
                   4669:                                        if(bits > 1.0F) {
                   4670:                                                selector |= 0x04;
                   4671:                                        } else {
                   4672:                                                selector &= ~0x04;
                   4673:                                        }
                   4674:                                }
                   4675:                        }
                   4676:                }
                   4677:                sio_write(sio_port - 1, 3, selector | 0x80);
                   4678:                sio_write(sio_port - 1, 0, divisor & 0xff);
                   4679:                sio_write(sio_port - 1, 1, divisor >> 8);
                   4680:                sio_write(sio_port - 1, 3, selector);
                   4681:        }
                   4682: }
                   4683: 
1.1.1.45  root     4684: int msdos_is_prn_path(const char *path)
1.1.1.30  root     4685: {
                   4686:        char full[MAX_PATH], *name;
                   4687:        
1.1.1.60  root     4688:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.30  root     4689:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4690:                        return(1);
                   4691:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4692:                        return(1);
                   4693:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4694:                        return(2);
                   4695:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4696:                        return(3);
                   4697:                }
                   4698:        }
                   4699:        return(0);
                   4700: }
                   4701: 
1.1.1.44  root     4702: bool msdos_is_valid_drive(int drv)
                   4703: {
                   4704:        return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
                   4705: }
                   4706: 
                   4707: bool msdos_is_removable_drive(int drv)
                   4708: {
                   4709:        char volume[] = "A:\\";
                   4710:        
                   4711:        volume[0] = 'A' + drv;
                   4712:        
1.1.1.60  root     4713:        return(GetDriveTypeA(volume) == DRIVE_REMOVABLE);
1.1.1.44  root     4714: }
                   4715: 
                   4716: bool msdos_is_cdrom_drive(int drv)
                   4717: {
                   4718:        char volume[] = "A:\\";
                   4719:        
                   4720:        volume[0] = 'A' + drv;
                   4721:        
1.1.1.60  root     4722:        return(GetDriveTypeA(volume) == DRIVE_CDROM);
1.1.1.44  root     4723: }
                   4724: 
                   4725: bool msdos_is_remote_drive(int drv)
                   4726: {
                   4727:        char volume[] = "A:\\";
                   4728:        
                   4729:        volume[0] = 'A' + drv;
                   4730:        
1.1.1.60  root     4731:        return(GetDriveTypeA(volume) == DRIVE_REMOTE);
1.1.1.44  root     4732: }
                   4733: 
                   4734: bool msdos_is_subst_drive(int drv)
                   4735: {
                   4736:        char device[] = "A:", path[MAX_PATH];
                   4737:        
                   4738:        device[0] = 'A' + drv;
                   4739:        
1.1.1.60  root     4740:        if(QueryDosDeviceA(device, path, MAX_PATH)) {
1.1.1.44  root     4741:                if(strncmp(path, "\\??\\", 4) == 0) {
                   4742:                        return(true);
                   4743:                }
                   4744:        }
                   4745:        return(false);
                   4746: }
                   4747: 
1.1.1.45  root     4748: bool msdos_is_existing_file(const char *path)
1.1.1.24  root     4749: {
                   4750:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
1.1.1.60  root     4751:        WIN32_FIND_DATAA fd;
1.1.1.24  root     4752:        HANDLE hFind;
                   4753:        
1.1.1.60  root     4754:        if((hFind = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.24  root     4755:                FindClose(hFind);
1.1.1.63  root     4756:                return((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0);
                   4757:        }
                   4758:        return(false);
                   4759: }
                   4760: 
                   4761: bool msdos_is_existing_dir(const char *path)
                   4762: {
                   4763:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   4764:        WIN32_FIND_DATAA fd;
                   4765:        HANDLE hFind;
                   4766:        
                   4767:        if((hFind = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
                   4768:                FindClose(hFind);
                   4769:                return((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
1.1.1.24  root     4770:        }
                   4771:        return(false);
1.1.1.8   root     4772: }
                   4773: 
1.1.1.45  root     4774: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9   root     4775: {
                   4776:        static char tmp[MAX_PATH];
1.1.1.28  root     4777:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4778:        
1.1.1.28  root     4779:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.60  root     4780:        if(GetFullPathNameA(command_path, MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4781:                sprintf(file_name, "COMMAND.COM");
                   4782:                if(_access(tmp, 0) == 0) {
                   4783:                        return(tmp);
                   4784:                }
                   4785:        }
1.1.1.28  root     4786:        
                   4787:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.60  root     4788:        if(GetModuleFileNameA(NULL, path, MAX_PATH) != 0 && GetFullPathNameA(path, MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4789:                sprintf(file_name, "COMMAND.COM");
                   4790:                if(_access(tmp, 0) == 0) {
                   4791:                        return(tmp);
                   4792:                }
                   4793:        }
1.1.1.28  root     4794:        
                   4795:        // check if COMMAND.COM is in the current directory
1.1.1.60  root     4796:        if(GetFullPathNameA("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4797:                if(_access(tmp, 0) == 0) {
                   4798:                        return(tmp);
                   4799:                }
                   4800:        }
1.1.1.28  root     4801:        
                   4802:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4803:        strcpy(path, env_path);
                   4804:        char *token = my_strtok(path, ";");
1.1.1.9   root     4805:        while(token != NULL) {
1.1.1.14  root     4806:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4807:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4808:                        if(_access(tmp, 0) == 0) {
                   4809:                                return(tmp);
                   4810:                        }
                   4811:                }
                   4812:                token = my_strtok(NULL, ";");
                   4813:        }
                   4814:        return(NULL);
                   4815: }
                   4816: 
1.1.1.14  root     4817: int msdos_drive_number(const char *path)
1.1       root     4818: {
                   4819:        char tmp[MAX_PATH], *name;
                   4820:        
1.1.1.60  root     4821:        if(GetFullPathNameA(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
1.1.1.45  root     4822:                if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4823:                        return(tmp[0] - 'a');
                   4824:                } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
                   4825:                        return(tmp[0] - 'A');
                   4826:                }
1.1       root     4827:        }
1.1.1.45  root     4828: //     return(msdos_drive_number("."));
                   4829:        return(_getdrive() - 1);
1.1       root     4830: }
                   4831: 
1.1.1.45  root     4832: const char *msdos_volume_label(const char *path)
1.1       root     4833: {
                   4834:        static char tmp[MAX_PATH];
                   4835:        char volume[] = "A:\\";
                   4836:        
                   4837:        if(path[1] == ':') {
                   4838:                volume[0] = path[0];
                   4839:        } else {
                   4840:                volume[0] = 'A' + _getdrive() - 1;
                   4841:        }
1.1.1.60  root     4842:        if(!GetVolumeInformationA(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
1.1       root     4843:                memset(tmp, 0, sizeof(tmp));
                   4844:        }
                   4845:        return(tmp);
                   4846: }
                   4847: 
1.1.1.45  root     4848: const char *msdos_short_volume_label(const char *label)
1.1       root     4849: {
                   4850:        static char tmp[(8 + 1 + 3) + 1];
1.1.1.45  root     4851:        const char *src = label;
1.1       root     4852:        int remain = strlen(label);
                   4853:        char *dst_n = tmp;
                   4854:        char *dst_e = tmp + 9;
                   4855:        
                   4856:        strcpy(tmp, "        .   ");
                   4857:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4858:                if(msdos_lead_byte_check(*src)) {
                   4859:                        if(++i == 8) {
                   4860:                                break;
                   4861:                        }
                   4862:                        *dst_n++ = *src++;
                   4863:                        remain--;
                   4864:                }
                   4865:                *dst_n++ = *src++;
                   4866:                remain--;
                   4867:        }
                   4868:        if(remain > 0) {
                   4869:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4870:                        if(msdos_lead_byte_check(*src)) {
                   4871:                                if(++i == 3) {
                   4872:                                        break;
                   4873:                                }
                   4874:                                *dst_e++ = *src++;
                   4875:                                remain--;
                   4876:                        }
                   4877:                        *dst_e++ = *src++;
                   4878:                        remain--;
                   4879:                }
                   4880:                *dst_e = '\0';
                   4881:        } else {
                   4882:                *dst_n = '\0';
                   4883:        }
                   4884:        my_strupr(tmp);
                   4885:        return(tmp);
                   4886: }
                   4887: 
1.1.1.13  root     4888: errno_t msdos_maperr(unsigned long oserrno)
                   4889: {
                   4890:        _doserrno = oserrno;
1.1.1.14  root     4891:        switch(oserrno) {
1.1.1.13  root     4892:        case ERROR_FILE_NOT_FOUND:         // 2
                   4893:        case ERROR_PATH_NOT_FOUND:         // 3
                   4894:        case ERROR_INVALID_DRIVE:          // 15
                   4895:        case ERROR_NO_MORE_FILES:          // 18
                   4896:        case ERROR_BAD_NETPATH:            // 53
                   4897:        case ERROR_BAD_NET_NAME:           // 67
                   4898:        case ERROR_BAD_PATHNAME:           // 161
                   4899:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4900:                return ENOENT;
                   4901:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4902:                return EMFILE;
                   4903:        case ERROR_ACCESS_DENIED:          // 5
                   4904:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4905:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4906:        case ERROR_CANNOT_MAKE:            // 82
                   4907:        case ERROR_FAIL_I24:               // 83
                   4908:        case ERROR_DRIVE_LOCKED:           // 108
                   4909:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4910:        case ERROR_NOT_LOCKED:             // 158
                   4911:        case ERROR_LOCK_FAILED:            // 167
                   4912:                return EACCES;
                   4913:        case ERROR_INVALID_HANDLE:         // 6
                   4914:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4915:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4916:                return EBADF;
                   4917:        case ERROR_ARENA_TRASHED:          // 7
                   4918:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4919:        case ERROR_INVALID_BLOCK:          // 9
                   4920:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4921:                return ENOMEM;
                   4922:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4923:                return E2BIG;
                   4924:        case ERROR_BAD_FORMAT:             // 11
                   4925:                return ENOEXEC;
                   4926:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4927:                return EXDEV;
                   4928:        case ERROR_FILE_EXISTS:            // 80
                   4929:        case ERROR_ALREADY_EXISTS:         // 183
                   4930:                return EEXIST;
                   4931:        case ERROR_NO_PROC_SLOTS:          // 89
                   4932:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4933:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4934:                return EAGAIN;
                   4935:        case ERROR_BROKEN_PIPE:            // 109
                   4936:                return EPIPE;
                   4937:        case ERROR_DISK_FULL:              // 112
                   4938:                return ENOSPC;
                   4939:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4940:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4941:                return ECHILD;
                   4942:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4943:                return ENOTEMPTY;
                   4944:        }
1.1.1.14  root     4945:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4946:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4947:                return EACCES;
                   4948:        }
1.1.1.14  root     4949:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4950:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4951:                return ENOEXEC;
                   4952:        }
                   4953:        return EINVAL;
                   4954: }
                   4955: 
1.1.1.45  root     4956: int msdos_open(const char *path, int oflag)
1.1.1.13  root     4957: {
1.1.1.14  root     4958:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45  root     4959:                return(_open(path, oflag));
1.1.1.13  root     4960:        }
1.1.1.14  root     4961:        
                   4962:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4963:        DWORD disposition;
1.1.1.14  root     4964:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4965:        default:
1.1.1.13  root     4966:        case _O_EXCL:
                   4967:                disposition = OPEN_EXISTING;
                   4968:                break;
                   4969:        case _O_CREAT:
                   4970:                disposition = OPEN_ALWAYS;
                   4971:                break;
                   4972:        case _O_CREAT | _O_EXCL:
                   4973:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4974:                disposition = CREATE_NEW;
                   4975:                break;
                   4976:        case _O_TRUNC:
                   4977:        case _O_TRUNC | _O_EXCL:
                   4978:                disposition = TRUNCATE_EXISTING;
                   4979:                break;
                   4980:        case _O_CREAT | _O_TRUNC:
                   4981:                disposition = CREATE_ALWAYS;
                   4982:                break;
                   4983:        }
1.1.1.14  root     4984:        
1.1.1.60  root     4985:        HANDLE h = CreateFileA(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
1.1.1.13  root     4986:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4987:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4988:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4989:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4990:                // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.60  root     4991:                h = CreateFileA(path, GENERIC_READ,
1.1.1.13  root     4992:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4993:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4994:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4995:                        errno = msdos_maperr(GetLastError());
1.1.1.45  root     4996:                        return(-1);
1.1.1.13  root     4997:                }
                   4998:        }
1.1.1.14  root     4999:        
1.1.1.13  root     5000:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     5001:        if(fd == -1) {
1.1.1.13  root     5002:                CloseHandle(h);
                   5003:        }
1.1.1.45  root     5004:        return(fd);
                   5005: }
                   5006: 
                   5007: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
                   5008: {
                   5009:        int fd = -1;
                   5010:        
                   5011:        *sio_port = *lpt_port = 0;
                   5012:        
                   5013:        if(msdos_is_con_path(path)) {
                   5014:                // MODE.COM opens CON device with read/write mode :-(
                   5015:                if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
                   5016:                        oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
                   5017:                        oflag |= _O_RDONLY;
                   5018:                }
                   5019:                if((fd = msdos_open("CON", oflag)) == -1) {
                   5020: //                     fd = msdos_open("NUL", oflag);
                   5021:                }
                   5022:        } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
                   5023:                fd = msdos_open("NUL", oflag);
                   5024:                msdos_set_comm_params(*sio_port, path);
                   5025:        } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
                   5026:                fd = msdos_open("NUL", oflag);
                   5027:        } else if(msdos_is_device_path(path)) {
                   5028:                fd = msdos_open("NUL", oflag);
                   5029: //     } else if(oflag & _O_CREAT) {
                   5030: //             fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
                   5031: //     } else {
                   5032: //             fd = _open(path, oflag);
                   5033:        }
                   5034:        return(fd);
                   5035: }
                   5036: 
                   5037: UINT16 msdos_device_info(const char *path)
                   5038: {
                   5039:        if(msdos_is_con_path(path)) {
                   5040:                return(0x80d3);
                   5041:        } else if(msdos_is_comm_path(path)) {
                   5042:                return(0x80a0);
                   5043:        } else if(msdos_is_prn_path(path)) {
                   5044: //             return(0xa8c0);
                   5045:                return(0x80a0);
                   5046:        } else if(msdos_is_device_path(path)) {
                   5047:                if(strstr(path, "EMMXXXX0") != NULL) {
                   5048:                        return(0xc0c0);
                   5049:                } else if(strstr(path, "MSCD001") != NULL) {
                   5050:                        return(0xc880);
                   5051:                } else {
                   5052:                        return(0x8084);
                   5053:                }
                   5054:        } else {
                   5055:                return(msdos_drive_number(path));
                   5056:        }
1.1.1.13  root     5057: }
                   5058: 
1.1.1.52  root     5059: 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     5060: {
                   5061:        static int id = 0;
                   5062:        char full[MAX_PATH], *name;
                   5063:        
1.1.1.60  root     5064:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1       root     5065:                strcpy(file_handler[fd].path, full);
                   5066:        } else {
                   5067:                strcpy(file_handler[fd].path, path);
                   5068:        }
1.1.1.14  root     5069:        // isatty makes no distinction between CON & NUL
                   5070:        // GetFileSize fails on CON, succeeds on NUL
                   5071:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45  root     5072:                if(info == 0x80d3) {
                   5073:                        info = 0x8084;
                   5074:                }
1.1.1.14  root     5075:                atty = 0;
                   5076:        } else if(!atty && info == 0x80d3) {
1.1.1.45  root     5077: //             info = msdos_drive_number(".");
                   5078:                info = msdos_drive_number(path);
1.1.1.14  root     5079:        }
1.1       root     5080:        file_handler[fd].valid = 1;
                   5081:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37  root     5082:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     5083:        file_handler[fd].mode = mode;
                   5084:        file_handler[fd].info = info;
                   5085:        file_handler[fd].psp = psp_seg;
1.1.1.37  root     5086:        file_handler[fd].sio_port = sio_port;
                   5087:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     5088:        
                   5089:        // init system file table
                   5090:        if(fd < 20) {
                   5091:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   5092:                
                   5093:                memset(sft, 0, 0x3b);
                   5094:                
                   5095:                *(UINT16 *)(sft + 0x00) = 1;
                   5096:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
1.1.1.60  root     5097:                *(UINT8  *)(sft + 0x04) = GetFileAttributesA(file_handler[fd].path) & 0xff;
1.1.1.21  root     5098:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   5099:                
                   5100:                if(!(file_handler[fd].info & 0x80)) {
                   5101:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   5102:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   5103:                        
                   5104:                        FILETIME time, local;
                   5105:                        HANDLE hHandle;
                   5106:                        WORD dos_date = 0, dos_time = 0;
                   5107:                        DWORD file_size = 0;
                   5108:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   5109:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   5110:                                        FileTimeToLocalFileTime(&time, &local);
                   5111:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   5112:                                }
                   5113:                                file_size = GetFileSize(hHandle, NULL);
                   5114:                        }
                   5115:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   5116:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   5117:                        *(UINT32 *)(sft + 0x11) = file_size;
                   5118:                }
                   5119:                
                   5120:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   5121:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   5122:                my_strupr(fname);
                   5123:                my_strupr(ext);
                   5124:                memset(sft + 0x20, 0x20, 11);
                   5125:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   5126:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   5127:                
                   5128:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   5129:        }
1.1       root     5130: }
                   5131: 
                   5132: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   5133: {
                   5134:        strcpy(file_handler[dst].path, file_handler[src].path);
                   5135:        file_handler[dst].valid = 1;
                   5136:        file_handler[dst].id = file_handler[src].id;
                   5137:        file_handler[dst].atty = file_handler[src].atty;
                   5138:        file_handler[dst].mode = file_handler[src].mode;
                   5139:        file_handler[dst].info = file_handler[src].info;
                   5140:        file_handler[dst].psp = psp_seg;
1.1.1.37  root     5141:        file_handler[dst].sio_port = file_handler[src].sio_port;
                   5142:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     5143: }
                   5144: 
1.1.1.20  root     5145: void msdos_file_handler_close(int fd)
1.1       root     5146: {
                   5147:        file_handler[fd].valid = 0;
1.1.1.21  root     5148:        
                   5149:        if(fd < 20) {
                   5150:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   5151:        }
1.1       root     5152: }
                   5153: 
1.1.1.14  root     5154: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     5155: {
1.1.1.14  root     5156:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   5157:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   5158:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     5159: }
                   5160: 
                   5161: // find file
                   5162: 
                   5163: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   5164: {
                   5165:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   5166:                return(0);      // search directory only !!!
                   5167:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   5168:                return(0);
                   5169:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   5170:                return(0);
                   5171:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   5172:                return(0);
                   5173:        } else if((attribute & required_mask) != required_mask) {
                   5174:                return(0);
                   5175:        } else {
                   5176:                return(1);
                   5177:        }
                   5178: }
                   5179: 
1.1.1.60  root     5180: int msdos_find_file_has_8dot3name(WIN32_FIND_DATAA *fd)
1.1.1.13  root     5181: {
1.1.1.14  root     5182:        if(fd->cAlternateFileName[0]) {
1.1.1.42  root     5183:                return(1);
1.1.1.13  root     5184:        }
                   5185:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     5186:        if(len > 12) {
1.1.1.42  root     5187:                return(0);
1.1.1.13  root     5188:        }
                   5189:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     5190:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42  root     5191:                return(0);
1.1.1.13  root     5192:        }
1.1.1.42  root     5193:        return(1);
1.1.1.13  root     5194: }
                   5195: 
1.1.1.60  root     5196: void msdos_find_file_conv_local_time(WIN32_FIND_DATAA *fd)
1.1       root     5197: {
                   5198:        FILETIME local;
                   5199:        
                   5200:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   5201:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   5202:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   5203:        
                   5204:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   5205:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   5206:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   5207:        
                   5208:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   5209:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   5210:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   5211: }
                   5212: 
                   5213: // i/o
                   5214: 
                   5215: void msdos_stdio_reopen()
                   5216: {
                   5217:        if(!file_handler[0].valid) {
                   5218:                _dup2(DUP_STDIN, 0);
                   5219:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   5220:        }
                   5221:        if(!file_handler[1].valid) {
                   5222:                _dup2(DUP_STDOUT, 1);
                   5223:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   5224:        }
                   5225:        if(!file_handler[2].valid) {
                   5226:                _dup2(DUP_STDERR, 2);
                   5227:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   5228:        }
1.1.1.21  root     5229:        if(!file_handler[3].valid) {
                   5230:                _dup2(DUP_STDAUX, 3);
                   5231:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   5232:        }
                   5233:        if(!file_handler[4].valid) {
                   5234:                _dup2(DUP_STDPRN, 4);
1.1.1.45  root     5235: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   5236:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     5237:        }
                   5238:        for(int i = 0; i < 5; i++) {
                   5239:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   5240:                        msdos_psp_set_file_table(i, i, current_psp);
                   5241:                }
                   5242:        }
1.1       root     5243: }
                   5244: 
1.1.1.37  root     5245: int msdos_read(int fd, void *buffer, unsigned int count)
                   5246: {
                   5247:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5248:                // read from serial port
                   5249:                int read = 0;
                   5250:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5251:                        UINT8 *buf = (UINT8 *)buffer;
                   5252:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5253:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5254:                        DWORD timeout = timeGetTime() + 1000;
                   5255:                        while(read < count) {
                   5256:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
                   5257:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
                   5258:                                        timeout = timeGetTime() + 1000;
                   5259:                                } else {
                   5260:                                        if(timeGetTime() > timeout) {
                   5261:                                                break;
                   5262:                                        }
                   5263:                                        Sleep(10);
1.1.1.37  root     5264:                                }
                   5265:                        }
                   5266:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5267:                }
                   5268:                return(read);
                   5269:        }
                   5270:        return(_read(fd, buffer, count));
                   5271: }
                   5272: 
1.1       root     5273: int msdos_kbhit()
                   5274: {
                   5275:        msdos_stdio_reopen();
                   5276:        
1.1.1.20  root     5277:        process_t *process = msdos_process_info_get(current_psp);
                   5278:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5279:        
                   5280:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5281:                // stdin is redirected to file
1.1.1.20  root     5282:                return(eof(fd) == 0);
1.1       root     5283:        }
                   5284:        
                   5285:        // check keyboard status
1.1.1.35  root     5286:        if(key_recv != 0) {
1.1       root     5287:                return(1);
                   5288:        }
1.1.1.35  root     5289:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5290: #ifdef USE_SERVICE_THREAD
                   5291:                EnterCriticalSection(&key_buf_crit_sect);
                   5292: #endif
1.1.1.55  root     5293:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5294: #ifdef USE_SERVICE_THREAD
                   5295:                LeaveCriticalSection(&key_buf_crit_sect);
                   5296: #endif
                   5297:                if(!empty) return(1);
                   5298:        }
                   5299:        return(_kbhit());
1.1       root     5300: }
                   5301: 
                   5302: int msdos_getch_ex(int echo)
                   5303: {
                   5304:        static char prev = 0;
                   5305:        
                   5306:        msdos_stdio_reopen();
                   5307:        
1.1.1.20  root     5308:        process_t *process = msdos_process_info_get(current_psp);
                   5309:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5310:        
                   5311:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5312:                // stdin is redirected to file
                   5313: retry:
                   5314:                char data;
1.1.1.37  root     5315:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     5316:                        char tmp = data;
                   5317:                        if(data == 0x0a) {
                   5318:                                if(prev == 0x0d) {
                   5319:                                        goto retry; // CRLF -> skip LF
                   5320:                                } else {
                   5321:                                        data = 0x0d; // LF only -> CR
                   5322:                                }
                   5323:                        }
                   5324:                        prev = tmp;
                   5325:                        return(data);
                   5326:                }
                   5327:                return(EOF);
                   5328:        }
                   5329:        
                   5330:        // input from console
1.1.1.5   root     5331:        int key_char, key_scan;
1.1.1.33  root     5332:        if(key_recv != 0) {
1.1.1.5   root     5333:                key_char = (key_code >> 0) & 0xff;
                   5334:                key_scan = (key_code >> 8) & 0xff;
                   5335:                key_code >>= 16;
1.1.1.33  root     5336:                key_recv >>= 16;
1.1.1.5   root     5337:        } else {
1.1.1.54  root     5338:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_exit) {
1.1.1.35  root     5339:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5340: #ifdef USE_SERVICE_THREAD
                   5341:                                EnterCriticalSection(&key_buf_crit_sect);
                   5342: #endif
1.1.1.55  root     5343:                                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5344: #ifdef USE_SERVICE_THREAD
                   5345:                                LeaveCriticalSection(&key_buf_crit_sect);
                   5346: #endif
                   5347:                                if(!empty) break;
                   5348:                        }
1.1.1.23  root     5349:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   5350:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   5351:                                if(_kbhit()) {
1.1.1.32  root     5352:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5353: #ifdef USE_SERVICE_THREAD
                   5354:                                                EnterCriticalSection(&key_buf_crit_sect);
                   5355: #endif
1.1.1.51  root     5356:                                                pcbios_set_key_buffer(_getch(), 0x00);
1.1.1.35  root     5357: #ifdef USE_SERVICE_THREAD
                   5358:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   5359: #endif
1.1.1.32  root     5360:                                        }
1.1.1.23  root     5361:                                } else {
                   5362:                                        Sleep(10);
                   5363:                                }
                   5364:                        } else {
                   5365:                                if(!update_key_buffer()) {
                   5366:                                        Sleep(10);
                   5367:                                }
1.1.1.14  root     5368:                        }
                   5369:                }
1.1.1.54  root     5370:                if(m_exit) {
1.1.1.33  root     5371:                        // insert CR to terminate input loops
1.1.1.14  root     5372:                        key_char = 0x0d;
                   5373:                        key_scan = 0;
1.1.1.32  root     5374:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5375: #ifdef USE_SERVICE_THREAD
                   5376:                        EnterCriticalSection(&key_buf_crit_sect);
                   5377: #endif
1.1.1.51  root     5378:                        pcbios_get_key_buffer(&key_char, &key_scan);
1.1.1.35  root     5379: #ifdef USE_SERVICE_THREAD
                   5380:                        LeaveCriticalSection(&key_buf_crit_sect);
                   5381: #endif
1.1.1.5   root     5382:                }
1.1       root     5383:        }
                   5384:        if(echo && key_char) {
                   5385:                msdos_putch(key_char);
                   5386:        }
                   5387:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   5388: }
                   5389: 
                   5390: inline int msdos_getch()
                   5391: {
                   5392:        return(msdos_getch_ex(0));
                   5393: }
                   5394: 
                   5395: inline int msdos_getche()
                   5396: {
                   5397:        return(msdos_getch_ex(1));
                   5398: }
                   5399: 
                   5400: int msdos_write(int fd, const void *buffer, unsigned int count)
                   5401: {
1.1.1.37  root     5402:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5403:                // write to serial port
1.1.1.38  root     5404:                int written = 0;
1.1.1.37  root     5405:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5406:                        UINT8 *buf = (UINT8 *)buffer;
                   5407:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5408:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5409:                        DWORD timeout = timeGetTime() + 1000;
                   5410:                        while(written < count) {
                   5411:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
                   5412:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
                   5413:                                        timeout = timeGetTime() + 1000;
                   5414:                                } else {
                   5415:                                        if(timeGetTime() > timeout) {
                   5416:                                                break;
                   5417:                                        }
                   5418:                                        Sleep(10);
                   5419:                                }
1.1.1.37  root     5420:                        }
                   5421:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5422:                }
1.1.1.38  root     5423:                return(written);
1.1.1.37  root     5424:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   5425:                // write to printer port
                   5426:                UINT8 *buf = (UINT8 *)buffer;
                   5427:                for(unsigned int i = 0; i < count; i++) {
                   5428: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5429:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5430:                }
                   5431:                return(count);
                   5432:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     5433:                // CR+LF -> LF
1.1.1.37  root     5434:                static int is_cr = 0;
1.1       root     5435:                UINT8 *buf = (UINT8 *)buffer;
                   5436:                for(unsigned int i = 0; i < count; i++) {
                   5437:                        UINT8 data = buf[i];
                   5438:                        if(is_cr) {
                   5439:                                if(data != 0x0a) {
                   5440:                                        UINT8 tmp = 0x0d;
                   5441:                                        _write(1, &tmp, 1);
                   5442:                                }
                   5443:                                _write(1, &data, 1);
                   5444:                                is_cr = 0;
                   5445:                        } else if(data == 0x0d) {
                   5446:                                is_cr = 1;
                   5447:                        } else {
                   5448:                                _write(1, &data, 1);
                   5449:                        }
                   5450:                }
                   5451:                return(count);
                   5452:        }
1.1.1.14  root     5453:        vram_flush();
1.1       root     5454:        return(_write(fd, buffer, count));
                   5455: }
                   5456: 
                   5457: void msdos_putch(UINT8 data)
1.1.1.50  root     5458: {
                   5459:        msdos_stdio_reopen();
                   5460:        
                   5461:        process_t *process = msdos_process_info_get(current_psp);
                   5462:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5463:        
                   5464:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
                   5465:                // stdout is redirected to file
                   5466:                msdos_write(fd, &data, 1);
                   5467:                return;
                   5468:        }
                   5469:        
                   5470:        // call int 29h ?
1.1.1.58  root     5471:        if(*(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     5472:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   5473:                // int 29h is not hooked, no need to call int 29h
                   5474:                msdos_putch_fast(data);
                   5475: #ifdef USE_SERVICE_THREAD
                   5476:        } else if(in_service && main_thread_id != GetCurrentThreadId()) {
                   5477:                // XXX: in usually we should not reach here
                   5478:                // this is called from service thread to echo the input
                   5479:                // we can not call int 29h because it causes a critial issue to control cpu running in main thread :-(
                   5480:                msdos_putch_fast(data);
                   5481: #endif
1.1.1.51  root     5482:        } else if(in_service_29h) {
1.1.1.50  root     5483:                // disallow reentering call int 29h routine to prevent an infinite loop :-(
                   5484:                msdos_putch_fast(data);
                   5485:        } else {
                   5486:                // this is called from main thread, so we can call int 29h :-)
1.1.1.51  root     5487:                in_service_29h = true;
1.1.1.50  root     5488:                try {
                   5489:                        UINT32 tmp_pc = m_pc;
                   5490:                        UINT16 tmp_ax = REG16(AX);
                   5491:                        UINT32 tmp_bx = REG16(BX); // BX may be destroyed by some versions of DOS 3.3
                   5492:                        
                   5493:                        // call int 29h routine is at fffc:0027
                   5494:                        i386_call_far(DUMMY_TOP >> 4, 0x0027);
                   5495:                        REG8(AL) = data;
                   5496:                        
                   5497:                        // run cpu until call int 29h routine is done
1.1.1.54  root     5498:                        while(!m_exit && tmp_pc != m_pc) {
1.1.1.50  root     5499:                                try {
                   5500:                                        hardware_run_cpu();
                   5501:                                } catch(...) {
                   5502:                                }
                   5503:                        }
                   5504:                        REG16(AX) = tmp_ax;
                   5505:                        REG16(BX) = tmp_bx;
                   5506:                } catch(...) {
                   5507:                }
1.1.1.51  root     5508:                in_service_29h = false;
1.1.1.50  root     5509:        }
                   5510: }
                   5511: 
                   5512: void msdos_putch_fast(UINT8 data)
1.1.1.35  root     5513: #ifdef USE_SERVICE_THREAD
                   5514: {
                   5515:        EnterCriticalSection(&putch_crit_sect);
                   5516:        msdos_putch_tmp(data);
                   5517:        LeaveCriticalSection(&putch_crit_sect);
                   5518: }
                   5519: void msdos_putch_tmp(UINT8 data)
                   5520: #endif
1.1       root     5521: {
1.1.1.34  root     5522:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5523:        SMALL_RECT rect;
                   5524:        COORD co;
1.1       root     5525:        static int p = 0;
                   5526:        static int is_kanji = 0;
                   5527:        static int is_esc = 0;
                   5528:        static int stored_x;
                   5529:        static int stored_y;
                   5530:        static WORD stored_a;
1.1.1.20  root     5531:        static char tmp[64], out[64];
1.1       root     5532:        
1.1.1.23  root     5533:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     5534:        
                   5535:        // output to console
                   5536:        tmp[p++] = data;
                   5537:        
1.1.1.14  root     5538:        vram_flush();
                   5539:        
1.1       root     5540:        if(is_kanji) {
                   5541:                // kanji character
                   5542:                is_kanji = 0;
                   5543:        } else if(is_esc) {
                   5544:                // escape sequense
                   5545:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   5546:                        p = is_esc = 0;
                   5547:                } else if(tmp[1] == '=' && p == 4) {
                   5548:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     5549:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     5550:                        SetConsoleCursorPosition(hStdout, co);
                   5551:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5552:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     5553:                        cursor_moved = false;
1.1.1.59  root     5554:                        cursor_moved_by_crtc = false;
1.1       root     5555:                        p = is_esc = 0;
                   5556:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
1.1.1.59  root     5557:                        if(cursor_moved_by_crtc) {
                   5558:                                if(!restore_console_on_exit) {
                   5559:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5560:                                        scr_top = csbi.srWindow.Top;
                   5561:                                }
                   5562:                                co.X = mem[0x450 + REG8(BH) * 2];
                   5563:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   5564:                                SetConsoleCursorPosition(hStdout, co);
                   5565:                                cursor_moved_by_crtc = false;
                   5566:                        }
1.1       root     5567:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5568:                        co.X = csbi.dwCursorPosition.X;
                   5569:                        co.Y = csbi.dwCursorPosition.Y;
                   5570:                        WORD wAttributes = csbi.wAttributes;
                   5571:                        
                   5572:                        if(tmp[1] == 'D') {
                   5573:                                co.Y++;
                   5574:                        } else if(tmp[1] == 'E') {
                   5575:                                co.X = 0;
                   5576:                                co.Y++;
                   5577:                        } else if(tmp[1] == 'M') {
                   5578:                                co.Y--;
                   5579:                        } else if(tmp[1] == '*') {
1.1.1.14  root     5580:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5581:                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5582:                                co.X = 0;
                   5583:                                co.Y = csbi.srWindow.Top;
1.1       root     5584:                        } else if(tmp[1] == '[') {
                   5585:                                int param[256], params = 0;
                   5586:                                memset(param, 0, sizeof(param));
                   5587:                                for(int i = 2; i < p; i++) {
                   5588:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   5589:                                                param[params] *= 10;
                   5590:                                                param[params] += tmp[i] - '0';
                   5591:                                        } else {
                   5592:                                                params++;
                   5593:                                        }
                   5594:                                }
                   5595:                                if(data == 'A') {
1.1.1.14  root     5596:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     5597:                                } else if(data == 'B') {
1.1.1.14  root     5598:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     5599:                                } else if(data == 'C') {
1.1.1.14  root     5600:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     5601:                                } else if(data == 'D') {
1.1.1.14  root     5602:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     5603:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     5604:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   5605:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     5606:                                } else if(data == 'J') {
1.1.1.14  root     5607:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5608:                                        if(param[0] == 0) {
                   5609:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5610:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5611:                                                if(co.Y < csbi.srWindow.Bottom) {
                   5612:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5613:                                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5614:                                                }
                   5615:                                        } else if(param[0] == 1) {
1.1.1.14  root     5616:                                                if(co.Y > csbi.srWindow.Top) {
                   5617:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
1.1.1.60  root     5618:                                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5619:                                                }
                   5620:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.60  root     5621:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5622:                                        } else if(param[0] == 2) {
1.1.1.14  root     5623:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5624:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5625:                                                co.X = co.Y = 0;
                   5626:                                        }
                   5627:                                } else if(data == 'K') {
1.1.1.14  root     5628:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5629:                                        if(param[0] == 0) {
                   5630:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5631:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5632:                                        } else if(param[0] == 1) {
                   5633:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.60  root     5634:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5635:                                        } else if(param[0] == 2) {
                   5636:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5637:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5638:                                        }
                   5639:                                } else if(data == 'L') {
1.1.1.14  root     5640:                                        if(params == 0) {
                   5641:                                                param[0] = 1;
1.1       root     5642:                                        }
1.1.1.14  root     5643:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5644:                                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5645:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5646:                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5647:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5648:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.60  root     5649:                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5650:                                        co.X = 0;
                   5651:                                } else if(data == 'M') {
1.1.1.14  root     5652:                                        if(params == 0) {
                   5653:                                                param[0] = 1;
                   5654:                                        }
                   5655:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5656:                                                clear_scr_buffer(csbi.wAttributes);
                   5657:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5658:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5659:                                        } else {
1.1.1.14  root     5660:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5661:                                                ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5662:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5663:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5664:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5665:                                        }
                   5666:                                        co.X = 0;
                   5667:                                } else if(data == 'h') {
                   5668:                                        if(tmp[2] == '>' && tmp[3] == '5') {
1.1.1.60  root     5669:                                                ci_new.bVisible = FALSE;
1.1       root     5670:                                        }
                   5671:                                } else if(data == 'l') {
                   5672:                                        if(tmp[2] == '>' && tmp[3] == '5') {
1.1.1.60  root     5673:                                                ci_new.bVisible = TRUE;
1.1       root     5674:                                        }
                   5675:                                } else if(data == 'm') {
                   5676:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5677:                                        int reverse = 0, hidden = 0;
                   5678:                                        for(int i = 0; i < params; i++) {
                   5679:                                                if(param[i] == 1) {
                   5680:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5681:                                                } else if(param[i] == 4) {
                   5682:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5683:                                                } else if(param[i] == 7) {
                   5684:                                                        reverse = 1;
                   5685:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5686:                                                        hidden = 1;
                   5687:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5688:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5689:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5690:                                                                param[i] -= 16;
                   5691:                                                        } else {
                   5692:                                                                param[i] -= 30;
                   5693:                                                        }
                   5694:                                                        if(param[i] & 1) {
                   5695:                                                                wAttributes |= FOREGROUND_RED;
                   5696:                                                        }
                   5697:                                                        if(param[i] & 2) {
                   5698:                                                                wAttributes |= FOREGROUND_GREEN;
                   5699:                                                        }
                   5700:                                                        if(param[i] & 4) {
                   5701:                                                                wAttributes |= FOREGROUND_BLUE;
                   5702:                                                        }
                   5703:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5704:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5705:                                                        if((param[i] - 40) & 1) {
                   5706:                                                                wAttributes |= BACKGROUND_RED;
                   5707:                                                        }
                   5708:                                                        if((param[i] - 40) & 2) {
                   5709:                                                                wAttributes |= BACKGROUND_GREEN;
                   5710:                                                        }
                   5711:                                                        if((param[i] - 40) & 4) {
                   5712:                                                                wAttributes |= BACKGROUND_BLUE;
                   5713:                                                        }
                   5714:                                                }
                   5715:                                        }
                   5716:                                        if(reverse) {
                   5717:                                                wAttributes &= ~0xff;
                   5718:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5719:                                        }
                   5720:                                        if(hidden) {
                   5721:                                                wAttributes &= ~0x0f;
                   5722:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5723:                                        }
                   5724:                                } else if(data == 'n') {
                   5725:                                        if(param[0] == 6) {
                   5726:                                                char tmp[16];
                   5727:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5728:                                                int len = strlen(tmp);
1.1.1.32  root     5729:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5730: #ifdef USE_SERVICE_THREAD
                   5731:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5732: #endif
1.1.1.32  root     5733:                                                        for(int i = 0; i < len; i++) {
1.1.1.51  root     5734:                                                                pcbios_set_key_buffer(tmp[i], 0x00);
1.1.1.32  root     5735:                                                        }
1.1.1.35  root     5736: #ifdef USE_SERVICE_THREAD
                   5737:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5738: #endif
1.1       root     5739:                                                }
                   5740:                                        }
                   5741:                                } else if(data == 's') {
                   5742:                                        stored_x = co.X;
                   5743:                                        stored_y = co.Y;
                   5744:                                        stored_a = wAttributes;
                   5745:                                } else if(data == 'u') {
                   5746:                                        co.X = stored_x;
                   5747:                                        co.Y = stored_y;
                   5748:                                        wAttributes = stored_a;
                   5749:                                }
                   5750:                        }
                   5751:                        if(co.X < 0) {
                   5752:                                co.X = 0;
                   5753:                        } else if(co.X >= csbi.dwSize.X) {
                   5754:                                co.X = csbi.dwSize.X - 1;
                   5755:                        }
1.1.1.14  root     5756:                        if(co.Y < csbi.srWindow.Top) {
                   5757:                                co.Y = csbi.srWindow.Top;
                   5758:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5759:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5760:                        }
                   5761:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5762:                                SetConsoleCursorPosition(hStdout, co);
                   5763:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5764:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5765:                                cursor_moved = false;
                   5766:                        }
                   5767:                        if(wAttributes != csbi.wAttributes) {
                   5768:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5769:                        }
                   5770:                        p = is_esc = 0;
                   5771:                }
                   5772:                return;
                   5773:        } else {
                   5774:                if(msdos_lead_byte_check(data)) {
                   5775:                        is_kanji = 1;
                   5776:                        return;
                   5777:                } else if(data == 0x1b) {
                   5778:                        is_esc = 1;
                   5779:                        return;
                   5780:                }
                   5781:        }
1.1.1.20  root     5782:        
                   5783:        DWORD q = 0, num;
                   5784:        is_kanji = 0;
                   5785:        for(int i = 0; i < p; i++) {
                   5786:                UINT8 c = tmp[i];
                   5787:                if(is_kanji) {
                   5788:                        is_kanji = 0;
                   5789:                } else if(msdos_lead_byte_check(data)) {
                   5790:                        is_kanji = 1;
                   5791:                } else if(msdos_ctrl_code_check(data)) {
                   5792:                        out[q++] = '^';
                   5793:                        c += 'A' - 1;
                   5794:                }
                   5795:                out[q++] = c;
                   5796:        }
1.1.1.59  root     5797:        if(cursor_moved_by_crtc) {
                   5798:                if(!restore_console_on_exit) {
                   5799:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5800:                        scr_top = csbi.srWindow.Top;
                   5801:                }
                   5802:                co.X = mem[0x450 + REG8(BH) * 2];
                   5803:                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   5804:                SetConsoleCursorPosition(hStdout, co);
                   5805:                cursor_moved_by_crtc = false;
                   5806:        }
1.1.1.34  root     5807:        if(q == 1 && out[0] == 0x08) {
                   5808:                // back space
                   5809:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5810:                if(csbi.dwCursorPosition.X > 0) {
                   5811:                        co.X = csbi.dwCursorPosition.X - 1;
                   5812:                        co.Y = csbi.dwCursorPosition.Y;
                   5813:                        SetConsoleCursorPosition(hStdout, co);
                   5814:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5815:                        co.X = csbi.dwSize.X - 1;
                   5816:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5817:                        SetConsoleCursorPosition(hStdout, co);
                   5818:                } else {
1.1.1.60  root     5819:                        WriteConsoleA(hStdout, out, q, &num, NULL); // to make sure
1.1.1.34  root     5820:                }
                   5821:        } else {
1.1.1.60  root     5822:                WriteConsoleA(hStdout, out, q, &num, NULL);
1.1.1.34  root     5823:        }
1.1       root     5824:        p = 0;
1.1.1.14  root     5825:        
1.1.1.15  root     5826:        if(!restore_console_on_exit) {
                   5827:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5828:                scr_top = csbi.srWindow.Top;
                   5829:        }
1.1       root     5830:        cursor_moved = true;
                   5831: }
                   5832: 
                   5833: int msdos_aux_in()
                   5834: {
1.1.1.21  root     5835:        msdos_stdio_reopen();
                   5836:        
1.1.1.20  root     5837:        process_t *process = msdos_process_info_get(current_psp);
                   5838:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5839:        
                   5840:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5841:                char data = 0;
1.1.1.37  root     5842:                msdos_read(fd, &data, 1);
1.1       root     5843:                return(data);
                   5844:        } else {
                   5845:                return(EOF);
                   5846:        }
                   5847: }
                   5848: 
                   5849: void msdos_aux_out(char data)
                   5850: {
1.1.1.21  root     5851:        msdos_stdio_reopen();
                   5852:        
1.1.1.20  root     5853:        process_t *process = msdos_process_info_get(current_psp);
                   5854:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5855:        
                   5856:        if(fd < process->max_files && file_handler[fd].valid) {
                   5857:                msdos_write(fd, &data, 1);
1.1       root     5858:        }
                   5859: }
                   5860: 
                   5861: void msdos_prn_out(char data)
                   5862: {
1.1.1.21  root     5863:        msdos_stdio_reopen();
                   5864:        
1.1.1.20  root     5865:        process_t *process = msdos_process_info_get(current_psp);
                   5866:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5867:        
                   5868:        if(fd < process->max_files && file_handler[fd].valid) {
                   5869:                msdos_write(fd, &data, 1);
1.1       root     5870:        }
                   5871: }
                   5872: 
                   5873: // memory control
                   5874: 
1.1.1.52  root     5875: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name = NULL)
1.1       root     5876: {
                   5877:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5878:        
                   5879:        mcb->mz = mz;
                   5880:        mcb->psp = psp;
1.1.1.30  root     5881:        mcb->paragraphs = paragraphs;
1.1.1.39  root     5882:        
                   5883:        if(prog_name != NULL) {
                   5884:                memset(mcb->prog_name, 0, 8);
                   5885:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
                   5886:        }
1.1       root     5887:        return(mcb);
                   5888: }
                   5889: 
                   5890: void msdos_mcb_check(mcb_t *mcb)
                   5891: {
                   5892:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5893:                #if 0
                   5894:                        // shutdown now !!!
                   5895:                        fatalerror("broken memory control block\n");
                   5896:                #else
                   5897:                        // return error code and continue
                   5898:                        throw(0x07); // broken memory control block
                   5899:                #endif
1.1       root     5900:        }
                   5901: }
                   5902: 
1.1.1.39  root     5903: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5904: {
                   5905:        int mcb_seg = seg - 1;
                   5906:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5907:        msdos_mcb_check(mcb);
                   5908:        
1.1.1.30  root     5909:        if(mcb->paragraphs > paragraphs) {
1.1       root     5910:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5911:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5912:                
                   5913:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5914:                mcb->mz = 'M';
1.1.1.30  root     5915:                mcb->paragraphs = paragraphs;
1.1       root     5916:        }
                   5917: }
                   5918: 
                   5919: void msdos_mem_merge(int seg)
                   5920: {
                   5921:        int mcb_seg = seg - 1;
                   5922:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5923:        msdos_mcb_check(mcb);
                   5924:        
                   5925:        while(1) {
                   5926:                if(mcb->mz == 'Z') {
                   5927:                        break;
                   5928:                }
1.1.1.30  root     5929:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5930:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5931:                msdos_mcb_check(next_mcb);
                   5932:                
                   5933:                if(next_mcb->psp != 0) {
                   5934:                        break;
                   5935:                }
                   5936:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5937:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5938:        }
                   5939: }
                   5940: 
1.1.1.8   root     5941: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5942: {
                   5943:        while(1) {
                   5944:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5945:                bool last_block;
1.1       root     5946:                
1.1.1.14  root     5947:                if(mcb->psp == 0) {
                   5948:                        msdos_mem_merge(mcb_seg + 1);
                   5949:                } else {
                   5950:                        msdos_mcb_check(mcb);
                   5951:                }
1.1.1.33  root     5952:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5953:                        // check if the next is dummy mcb to link to umb
                   5954:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5955:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5956:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5957:                }
                   5958:                if(!(new_process && !last_block)) {
1.1.1.30  root     5959:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5960:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5961:                                mcb->psp = current_psp;
                   5962:                                return(mcb_seg + 1);
                   5963:                        }
                   5964:                }
                   5965:                if(mcb->mz == 'Z') {
                   5966:                        break;
                   5967:                }
1.1.1.30  root     5968:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5969:        }
                   5970:        return(-1);
                   5971: }
                   5972: 
                   5973: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5974: {
                   5975:        int mcb_seg = seg - 1;
                   5976:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5977:        msdos_mcb_check(mcb);
1.1.1.30  root     5978:        int current_paragraphs = mcb->paragraphs;
1.1       root     5979:        
                   5980:        msdos_mem_merge(seg);
1.1.1.30  root     5981:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5982:                if(max_paragraphs) {
1.1.1.30  root     5983:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5984:                }
1.1       root     5985:                msdos_mem_split(seg, current_paragraphs);
                   5986:                return(-1);
                   5987:        }
                   5988:        msdos_mem_split(seg, paragraphs);
                   5989:        return(0);
                   5990: }
                   5991: 
                   5992: void msdos_mem_free(int seg)
                   5993: {
                   5994:        int mcb_seg = seg - 1;
                   5995:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5996:        msdos_mcb_check(mcb);
                   5997:        
                   5998:        mcb->psp = 0;
                   5999:        msdos_mem_merge(seg);
                   6000: }
                   6001: 
1.1.1.8   root     6002: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     6003: {
                   6004:        int max_paragraphs = 0;
                   6005:        
                   6006:        while(1) {
                   6007:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     6008:                bool last_block;
                   6009:                
1.1       root     6010:                msdos_mcb_check(mcb);
                   6011:                
1.1.1.33  root     6012:                if(!(last_block = (mcb->mz == 'Z'))) {
                   6013:                        // check if the next is dummy mcb to link to umb
                   6014:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   6015:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   6016:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   6017:                }
                   6018:                if(!(new_process && !last_block)) {
1.1.1.30  root     6019:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   6020:                                max_paragraphs = mcb->paragraphs;
1.1       root     6021:                        }
                   6022:                }
                   6023:                if(mcb->mz == 'Z') {
                   6024:                        break;
                   6025:                }
1.1.1.30  root     6026:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     6027:        }
1.1.1.14  root     6028:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     6029: }
                   6030: 
1.1.1.8   root     6031: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   6032: {
                   6033:        int last_seg = -1;
                   6034:        
                   6035:        while(1) {
                   6036:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   6037:                msdos_mcb_check(mcb);
                   6038:                
1.1.1.14  root     6039:                if(mcb->psp == psp) {
1.1.1.8   root     6040:                        last_seg = mcb_seg;
                   6041:                }
1.1.1.14  root     6042:                if(mcb->mz == 'Z') {
                   6043:                        break;
                   6044:                }
1.1.1.30  root     6045:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     6046:        }
                   6047:        return(last_seg);
                   6048: }
                   6049: 
1.1.1.19  root     6050: int msdos_mem_get_umb_linked()
                   6051: {
1.1.1.33  root     6052:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   6053:        msdos_mcb_check(mcb);
1.1.1.19  root     6054:        
1.1.1.33  root     6055:        if(mcb->mz == 'M') {
                   6056:                return(-1);
1.1.1.19  root     6057:        }
                   6058:        return(0);
                   6059: }
                   6060: 
1.1.1.33  root     6061: void msdos_mem_link_umb()
1.1.1.19  root     6062: {
1.1.1.33  root     6063:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   6064:        msdos_mcb_check(mcb);
1.1.1.19  root     6065:        
1.1.1.33  root     6066:        mcb->mz = 'M';
                   6067:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39  root     6068:        
                   6069:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     6070: }
                   6071: 
1.1.1.33  root     6072: void msdos_mem_unlink_umb()
1.1.1.19  root     6073: {
1.1.1.33  root     6074:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   6075:        msdos_mcb_check(mcb);
1.1.1.19  root     6076:        
1.1.1.33  root     6077:        mcb->mz = 'Z';
                   6078:        mcb->paragraphs = 0;
1.1.1.39  root     6079:        
                   6080:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     6081: }
                   6082: 
1.1.1.29  root     6083: #ifdef SUPPORT_HMA
                   6084: 
                   6085: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   6086: {
                   6087:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6088:        
                   6089:        mcb->ms[0] = 'M';
                   6090:        mcb->ms[1] = 'S';
                   6091:        mcb->owner = owner;
                   6092:        mcb->size = size;
                   6093:        mcb->next = next;
                   6094:        return(mcb);
                   6095: }
                   6096: 
                   6097: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   6098: {
                   6099:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   6100: }
                   6101: 
                   6102: int msdos_hma_mem_split(int offset, int size)
                   6103: {
                   6104:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6105:        
                   6106:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6107:                return(-1);
                   6108:        }
                   6109:        if(mcb->size >= size + 0x10) {
                   6110:                int new_offset = offset + 0x10 + size;
                   6111:                int new_size = mcb->size - 0x10 - size;
                   6112:                
                   6113:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   6114:                mcb->size = size;
                   6115:                mcb->next = new_offset;
                   6116:                return(0);
                   6117:        }
                   6118:        return(-1);
                   6119: }
                   6120: 
                   6121: void msdos_hma_mem_merge(int offset)
                   6122: {
                   6123:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6124:        
                   6125:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6126:                return;
                   6127:        }
                   6128:        while(1) {
                   6129:                if(mcb->next == 0) {
                   6130:                        break;
                   6131:                }
                   6132:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   6133:                
                   6134:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   6135:                        return;
                   6136:                }
                   6137:                if(next_mcb->owner != 0) {
                   6138:                        break;
                   6139:                }
                   6140:                mcb->size += 0x10 + next_mcb->size;
                   6141:                mcb->next = next_mcb->next;
                   6142:        }
                   6143: }
                   6144: 
                   6145: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   6146: {
                   6147:        int offset = 0x10; // first mcb in HMA
                   6148:        
                   6149:        while(1) {
                   6150:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6151:                
                   6152:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   6153:                        return(-1);
                   6154:                }
                   6155:                if(mcb->owner == 0) {
                   6156:                        msdos_hma_mem_merge(offset);
                   6157:                }
                   6158:                if(mcb->owner == 0 && mcb->size >= size) {
                   6159:                        msdos_hma_mem_split(offset, size);
                   6160:                        mcb->owner = owner;
                   6161:                        return(offset);
                   6162:                }
                   6163:                if(mcb->next == 0) {
                   6164:                        break;
                   6165:                }
                   6166:                offset = mcb->next;
                   6167:        }
                   6168:        return(-1);
                   6169: }
                   6170: 
                   6171: int msdos_hma_mem_realloc(int offset, int size)
                   6172: {
                   6173:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6174:        
                   6175:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6176:                return(-1);
                   6177:        }
                   6178:        if(mcb->size < size) {
                   6179:                return(-1);
                   6180:        }
                   6181:        msdos_hma_mem_split(offset, size);
                   6182:        return(0);
                   6183: }
                   6184: 
                   6185: void msdos_hma_mem_free(int offset)
                   6186: {
                   6187:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6188:        
                   6189:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6190:                return;
                   6191:        }
                   6192:        mcb->owner = 0;
                   6193:        msdos_hma_mem_merge(offset);
                   6194: }
                   6195: 
                   6196: int msdos_hma_mem_get_free(int *available_offset)
                   6197: {
                   6198:        int offset = 0x10; // first mcb in HMA
                   6199:        int size = 0;
                   6200:        
                   6201:        while(1) {
                   6202:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6203:                
                   6204:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   6205:                        return(0);
                   6206:                }
                   6207:                if(mcb->owner == 0 && size < mcb->size) {
                   6208:                        if(available_offset != NULL) {
                   6209:                                *available_offset = offset;
                   6210:                        }
                   6211:                        size = mcb->size;
                   6212:                }
                   6213:                if(mcb->next == 0) {
                   6214:                        break;
                   6215:                }
                   6216:                offset = mcb->next;
                   6217:        }
                   6218:        return(size);
                   6219: }
                   6220: 
                   6221: #endif
                   6222: 
1.1       root     6223: // environment
                   6224: 
1.1.1.45  root     6225: void msdos_env_set_argv(int env_seg, const char *argv)
1.1       root     6226: {
                   6227:        char *dst = (char *)(mem + (env_seg << 4));
                   6228:        
                   6229:        while(1) {
                   6230:                if(dst[0] == 0) {
                   6231:                        break;
                   6232:                }
                   6233:                dst += strlen(dst) + 1;
                   6234:        }
                   6235:        *dst++ = 0; // end of environment
                   6236:        *dst++ = 1; // top of argv[0]
                   6237:        *dst++ = 0;
                   6238:        memcpy(dst, argv, strlen(argv));
                   6239:        dst += strlen(argv);
                   6240:        *dst++ = 0;
                   6241:        *dst++ = 0;
                   6242: }
                   6243: 
1.1.1.45  root     6244: const char *msdos_env_get_argv(int env_seg)
1.1       root     6245: {
                   6246:        static char env[ENV_SIZE];
                   6247:        char *src = env;
                   6248:        
                   6249:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   6250:        while(1) {
                   6251:                if(src[0] == 0) {
                   6252:                        if(src[1] == 1) {
                   6253:                                return(src + 3);
                   6254:                        }
                   6255:                        break;
                   6256:                }
                   6257:                src += strlen(src) + 1;
                   6258:        }
                   6259:        return(NULL);
                   6260: }
                   6261: 
1.1.1.45  root     6262: const char *msdos_env_get(int env_seg, const char *name)
1.1       root     6263: {
                   6264:        static char env[ENV_SIZE];
                   6265:        char *src = env;
                   6266:        
                   6267:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   6268:        while(1) {
                   6269:                if(src[0] == 0) {
                   6270:                        break;
                   6271:                }
                   6272:                int len = strlen(src);
                   6273:                char *n = my_strtok(src, "=");
                   6274:                char *v = src + strlen(n) + 1;
                   6275:                
                   6276:                if(_stricmp(name, n) == 0) {
                   6277:                        return(v);
                   6278:                }
                   6279:                src += len + 1;
                   6280:        }
                   6281:        return(NULL);
                   6282: }
                   6283: 
1.1.1.45  root     6284: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1       root     6285: {
                   6286:        char env[ENV_SIZE];
                   6287:        char *src = env;
                   6288:        char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45  root     6289:        const char *argv = msdos_env_get_argv(env_seg);
1.1       root     6290:        int done = 0;
                   6291:        
                   6292:        memcpy(src, dst, ENV_SIZE);
                   6293:        memset(dst, 0, ENV_SIZE);
                   6294:        while(1) {
                   6295:                if(src[0] == 0) {
                   6296:                        break;
                   6297:                }
                   6298:                int len = strlen(src);
                   6299:                char *n = my_strtok(src, "=");
                   6300:                char *v = src + strlen(n) + 1;
                   6301:                char tmp[1024];
                   6302:                
                   6303:                if(_stricmp(name, n) == 0) {
                   6304:                        sprintf(tmp, "%s=%s", n, value);
                   6305:                        done = 1;
                   6306:                } else {
                   6307:                        sprintf(tmp, "%s=%s", n, v);
                   6308:                }
                   6309:                memcpy(dst, tmp, strlen(tmp));
                   6310:                dst += strlen(tmp) + 1;
                   6311:                src += len + 1;
                   6312:        }
                   6313:        if(!done) {
                   6314:                char tmp[1024];
                   6315:                
                   6316:                sprintf(tmp, "%s=%s", name, value);
                   6317:                memcpy(dst, tmp, strlen(tmp));
                   6318:                dst += strlen(tmp) + 1;
                   6319:        }
                   6320:        if(argv) {
                   6321:                *dst++ = 0; // end of environment
                   6322:                *dst++ = 1; // top of argv[0]
                   6323:                *dst++ = 0;
                   6324:                memcpy(dst, argv, strlen(argv));
                   6325:                dst += strlen(argv);
                   6326:                *dst++ = 0;
                   6327:                *dst++ = 0;
                   6328:        }
                   6329: }
                   6330: 
                   6331: // process
                   6332: 
1.1.1.8   root     6333: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     6334: {
                   6335:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6336:        
                   6337:        memset(psp, 0, PSP_SIZE);
                   6338:        psp->exit[0] = 0xcd;
                   6339:        psp->exit[1] = 0x20;
1.1.1.8   root     6340:        psp->first_mcb = mcb_seg;
1.1.1.46  root     6341: #if 1
1.1.1.49  root     6342:        psp->call5[0] = 0xcd;   // int 30h
                   6343:        psp->call5[1] = 0x30;
1.1.1.46  root     6344:        psp->call5[2] = 0xc3;   // ret
                   6345: #else
                   6346:        psp->call5[0] = 0x8a;   // mov ah, cl
                   6347:        psp->call5[1] = 0xe1;
                   6348:        psp->call5[2] = 0xcd;   // int 21h
                   6349:        psp->call5[3] = 0x21;
                   6350:        psp->call5[4] = 0xc3;   // ret
                   6351: #endif
1.1       root     6352:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   6353:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   6354:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   6355:        psp->parent_psp = parent_psp;
1.1.1.20  root     6356:        if(parent_psp == (UINT16)-1) {
                   6357:                for(int i = 0; i < 20; i++) {
                   6358:                        if(file_handler[i].valid) {
                   6359:                                psp->file_table[i] = i;
                   6360:                        } else {
                   6361:                                psp->file_table[i] = 0xff;
                   6362:                        }
1.1       root     6363:                }
1.1.1.20  root     6364:        } else {
                   6365:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     6366:        }
                   6367:        psp->env_seg = env_seg;
                   6368:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     6369:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     6370:        psp->file_table_size = 20;
                   6371:        psp->file_table_ptr.w.l = 0x18;
                   6372:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     6373:        psp->service[0] = 0xcd;
                   6374:        psp->service[1] = 0x21;
                   6375:        psp->service[2] = 0xcb;
                   6376:        return(psp);
                   6377: }
                   6378: 
1.1.1.20  root     6379: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   6380: {
                   6381:        if(psp_seg && fd < 20) {
                   6382:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6383:                psp->file_table[fd] = value;
                   6384:        }
                   6385: }
                   6386: 
                   6387: int msdos_psp_get_file_table(int fd, int psp_seg)
                   6388: {
                   6389:        if(psp_seg && fd < 20) {
                   6390:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6391:                fd = psp->file_table[fd];
                   6392:        }
                   6393:        return fd;
                   6394: }
                   6395: 
1.1.1.52  root     6396: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al, bool first_process = false)
1.1       root     6397: {
                   6398:        // load command file
                   6399:        int fd = -1;
1.1.1.45  root     6400:        int sio_port = 0;
                   6401:        int lpt_port = 0;
1.1       root     6402:        int dos_command = 0;
1.1.1.24  root     6403:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     6404:        char pipe_stdin_path[MAX_PATH] = {0};
                   6405:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39  root     6406:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     6407:        
                   6408:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6409:        int opt_len = mem[opt_ofs];
                   6410:        memset(opt, 0, sizeof(opt));
                   6411:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6412:        
1.1.1.14  root     6413:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   6414:                // this is a batch file, run command.com
                   6415:                char tmp[MAX_PATH];
                   6416:                if(opt_len != 0) {
                   6417:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   6418:                } else {
                   6419:                        sprintf(tmp, "/C %s", cmd);
                   6420:                }
                   6421:                strcpy(opt, tmp);
                   6422:                opt_len = strlen(opt);
                   6423:                mem[opt_ofs] = opt_len;
                   6424:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6425:                strcpy(command, comspec_path);
                   6426:                strcpy(name_tmp, "COMMAND.COM");
                   6427:        } else {
                   6428:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   6429:                        // redirect C:\COMMAND.COM to comspec_path
                   6430:                        strcpy(command, comspec_path);
                   6431:                } else {
                   6432:                        strcpy(command, cmd);
                   6433:                }
1.1.1.60  root     6434:                if(GetFullPathNameA(command, MAX_PATH, path, &name) == 0) {
1.1.1.24  root     6435:                        return(-1);
                   6436:                }
1.1.1.14  root     6437:                memset(name_tmp, 0, sizeof(name_tmp));
                   6438:                strcpy(name_tmp, name);
                   6439:                
                   6440:                // check command.com
1.1.1.38  root     6441:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   6442:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     6443:                        if(opt_len == 0) {
                   6444: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   6445:                                process_t *current_process = NULL;
                   6446:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   6447:                                        if(process[i].psp == current_psp) {
                   6448:                                                current_process = &process[i];
                   6449:                                                break;
                   6450:                                        }
                   6451:                                }
                   6452:                                if(current_process != NULL) {
                   6453:                                        param->cmd_line.dw = current_process->dta.dw;
                   6454:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6455:                                        opt_len = mem[opt_ofs];
                   6456:                                        memset(opt, 0, sizeof(opt));
                   6457:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6458:                                }
                   6459:                        }
                   6460:                        for(int i = 0; i < opt_len; i++) {
                   6461:                                if(opt[i] == ' ') {
                   6462:                                        continue;
                   6463:                                }
                   6464:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   6465:                                        for(int j = i + 3; j < opt_len; j++) {
                   6466:                                                if(opt[j] == ' ') {
                   6467:                                                        continue;
                   6468:                                                }
                   6469:                                                char *token = my_strtok(opt + j, " ");
                   6470:                                                
1.1.1.38  root     6471:                                                strcpy(command, token);
                   6472:                                                char tmp[MAX_PATH];
                   6473:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39  root     6474:                                                strcpy(opt, "");
                   6475:                                                for(int i = 0; i < strlen(tmp); i++) {
                   6476:                                                        if(tmp[i] != ' ') {
                   6477:                                                                strcpy(opt, tmp + i);
                   6478:                                                                break;
                   6479:                                                        }
                   6480:                                                }
                   6481:                                                strcpy(tmp, opt);
1.1.1.38  root     6482:                                                
                   6483:                                                if(al == 0x00) {
1.1.1.39  root     6484:                                                        #define GET_FILE_PATH() { \
                   6485:                                                                if(token[0] != '>' && token[0] != '<') { \
                   6486:                                                                        token++; \
                   6487:                                                                } \
                   6488:                                                                token++; \
                   6489:                                                                while(*token == ' ') { \
                   6490:                                                                        token++; \
                   6491:                                                                } \
                   6492:                                                                char *ptr = token; \
                   6493:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
                   6494:                                                                        ptr++; \
                   6495:                                                                } \
                   6496:                                                                *ptr = '\0'; \
                   6497:                                                        }
                   6498:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
                   6499:                                                                GET_FILE_PATH();
1.1.1.38  root     6500:                                                                strcpy(pipe_stdin_path, token);
                   6501:                                                                strcpy(opt, tmp);
                   6502:                                                        }
1.1.1.39  root     6503:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
                   6504:                                                                GET_FILE_PATH();
1.1.1.38  root     6505:                                                                strcpy(pipe_stdout_path, token);
                   6506:                                                                strcpy(opt, tmp);
                   6507:                                                        }
1.1.1.39  root     6508:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6509:                                                                GET_FILE_PATH();
                   6510:                                                                strcpy(pipe_stderr_path, token);
                   6511:                                                                strcpy(opt, tmp);
                   6512:                                                        }
                   6513:                                                        #undef GET_FILE_PATH
                   6514:                                                        
                   6515:                                                        if((token = strstr(opt, "0<")) != NULL) {
                   6516:                                                                *token = '\0';
                   6517:                                                        }
                   6518:                                                        if((token = strstr(opt, "1>")) != NULL) {
                   6519:                                                                *token = '\0';
                   6520:                                                        }
                   6521:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6522:                                                                *token = '\0';
                   6523:                                                        }
1.1.1.38  root     6524:                                                        if((token = strstr(opt, "<")) != NULL) {
                   6525:                                                                *token = '\0';
                   6526:                                                        }
                   6527:                                                        if((token = strstr(opt, ">")) != NULL) {
                   6528:                                                                *token = '\0';
                   6529:                                                        }
1.1.1.14  root     6530:                                                }
1.1.1.39  root     6531:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
                   6532:                                                        opt[i] = '\0';
                   6533:                                                }
1.1.1.38  root     6534:                                                opt_len = strlen(opt);
                   6535:                                                mem[opt_ofs] = opt_len;
                   6536:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6537:                                                dos_command = 1;
1.1.1.14  root     6538:                                                break;
1.1       root     6539:                                        }
                   6540:                                }
1.1.1.14  root     6541:                                break;
1.1       root     6542:                        }
                   6543:                }
                   6544:        }
                   6545:        
                   6546:        // load command file
                   6547:        strcpy(path, command);
                   6548:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6549:                sprintf(path, "%s.COM", command);
                   6550:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6551:                        sprintf(path, "%s.EXE", command);
                   6552:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     6553:                                sprintf(path, "%s.BAT", command);
                   6554:                                if(_access(path, 0) == 0) {
                   6555:                                        // this is a batch file, run command.com
                   6556:                                        char tmp[MAX_PATH];
                   6557:                                        if(opt_len != 0) {
                   6558:                                                sprintf(tmp, "/C %s %s", path, opt);
                   6559:                                        } else {
                   6560:                                                sprintf(tmp, "/C %s", path);
                   6561:                                        }
                   6562:                                        strcpy(opt, tmp);
                   6563:                                        opt_len = strlen(opt);
                   6564:                                        mem[opt_ofs] = opt_len;
                   6565:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6566:                                        strcpy(path, comspec_path);
                   6567:                                        strcpy(name_tmp, "COMMAND.COM");
                   6568:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6569:                                } else {
                   6570:                                        // search path in parent environments
                   6571:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45  root     6572:                                        const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14  root     6573:                                        if(env != NULL) {
                   6574:                                                char env_path[4096];
                   6575:                                                strcpy(env_path, env);
                   6576:                                                char *token = my_strtok(env_path, ";");
                   6577:                                                
                   6578:                                                while(token != NULL) {
                   6579:                                                        if(strlen(token) != 0) {
                   6580:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   6581:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6582:                                                                        break;
                   6583:                                                                }
                   6584:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   6585:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6586:                                                                        break;
                   6587:                                                                }
                   6588:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   6589:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6590:                                                                        break;
                   6591:                                                                }
                   6592:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   6593:                                                                if(_access(path, 0) == 0) {
                   6594:                                                                        // this is a batch file, run command.com
                   6595:                                                                        char tmp[MAX_PATH];
                   6596:                                                                        if(opt_len != 0) {
                   6597:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   6598:                                                                        } else {
                   6599:                                                                                sprintf(tmp, "/C %s", path);
                   6600:                                                                        }
                   6601:                                                                        strcpy(opt, tmp);
                   6602:                                                                        opt_len = strlen(opt);
                   6603:                                                                        mem[opt_ofs] = opt_len;
                   6604:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6605:                                                                        strcpy(path, comspec_path);
                   6606:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   6607:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6608:                                                                        break;
                   6609:                                                                }
1.1.1.8   root     6610:                                                        }
1.1.1.14  root     6611:                                                        token = my_strtok(NULL, ";");
1.1       root     6612:                                                }
                   6613:                                        }
                   6614:                                }
                   6615:                        }
                   6616:                }
                   6617:        }
                   6618:        if(fd == -1) {
1.1.1.38  root     6619:                // we can not find command.com in the path, so open comspec_path
                   6620:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6621:                        strcpy(command, comspec_path);
                   6622:                        strcpy(path, command);
                   6623:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6624:                }
                   6625:        }
                   6626:        if(fd == -1) {
1.1.1.52  root     6627:                if(!first_process && al == 0 && dos_command) {
1.1       root     6628:                        // may be dos command
                   6629:                        char tmp[MAX_PATH];
1.1.1.52  root     6630:                        if(opt_len != 0) {
                   6631:                                sprintf(tmp, "%s %s", command, opt);
                   6632:                        } else {
                   6633:                                sprintf(tmp, "%s", command);
                   6634:                        }
                   6635:                        retval = system(tmp);
1.1       root     6636:                        return(0);
                   6637:                } else {
                   6638:                        return(-1);
                   6639:                }
                   6640:        }
1.1.1.52  root     6641:        memset(file_buffer, 0, sizeof(file_buffer));
1.1       root     6642:        _read(fd, file_buffer, sizeof(file_buffer));
                   6643:        _close(fd);
                   6644:        
1.1.1.52  root     6645:        // check if this is win32 program
                   6646:        if(!first_process && al == 0) {
                   6647:                UINT16 sign_dos = *(UINT16 *)(file_buffer + 0x00);
                   6648:                UINT32 e_lfanew = *(UINT32 *)(file_buffer + 0x3c);
                   6649:                if(sign_dos == IMAGE_DOS_SIGNATURE && e_lfanew >= 0x40 && e_lfanew < 0x400) {
                   6650:                        UINT32 sign_nt = *(UINT32 *)(file_buffer + e_lfanew + 0x00);
                   6651:                        UINT16 machine = *(UINT16 *)(file_buffer + e_lfanew + 0x04);
                   6652:                        if(sign_nt == IMAGE_NT_SIGNATURE && (machine == IMAGE_FILE_MACHINE_I386 || machine == IMAGE_FILE_MACHINE_AMD64)) {
                   6653:                                char tmp[MAX_PATH];
                   6654:                                if(opt_len != 0) {
                   6655:                                        sprintf(tmp, "\"%s\" %s", path, opt);
                   6656:                                } else {
                   6657:                                        sprintf(tmp, "\"%s\"", path);
                   6658:                                }
                   6659:                                retval = system(tmp);
                   6660:                                return(0);
                   6661:                        }
                   6662:                }
                   6663:        }
                   6664:        
1.1       root     6665:        // copy environment
1.1.1.29  root     6666:        int umb_linked, env_seg, psp_seg;
1.1       root     6667:        
1.1.1.29  root     6668:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6669:                msdos_mem_unlink_umb();
                   6670:        }
1.1.1.8   root     6671:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6672:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6673:                        if(umb_linked != 0) {
                   6674:                                msdos_mem_link_umb();
                   6675:                        }
                   6676:                        return(-1);
                   6677:                }
1.1       root     6678:        }
                   6679:        if(param->env_seg == 0) {
                   6680:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6681:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6682:        } else {
                   6683:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6684:        }
                   6685:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6686:        
                   6687:        // check exe header
                   6688:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6689:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6690:        UINT16 cs, ss, ip, sp;
                   6691:        
                   6692:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6693:                // memory allocation
                   6694:                int header_size = header->header_size * 16;
                   6695:                int load_size = header->pages * 512 - header_size;
                   6696:                if(header_size + load_size < 512) {
                   6697:                        load_size = 512 - header_size;
                   6698:                }
                   6699:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6700:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6701:                        msdos_mem_free(env_seg);
                   6702:                        return(-1);
                   6703:                }
                   6704:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6705:                if(paragraphs > free_paragraphs) {
                   6706:                        paragraphs = free_paragraphs;
                   6707:                }
1.1.1.8   root     6708:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6709:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6710:                                if(umb_linked != 0) {
                   6711:                                        msdos_mem_link_umb();
                   6712:                                }
                   6713:                                msdos_mem_free(env_seg);
                   6714:                                return(-1);
                   6715:                        }
1.1       root     6716:                }
                   6717:                // relocation
                   6718:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6719:                for(int i = 0; i < header->relocations; i++) {
                   6720:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6721:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6722:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6723:                }
                   6724:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6725:                // segments
                   6726:                cs = header->init_cs + start_seg;
                   6727:                ss = header->init_ss + start_seg;
                   6728:                ip = header->init_ip;
                   6729:                sp = header->init_sp - 2; // for symdeb
                   6730:        } else {
                   6731:                // memory allocation
                   6732:                paragraphs = free_paragraphs;
1.1.1.8   root     6733:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6734:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6735:                                if(umb_linked != 0) {
                   6736:                                        msdos_mem_link_umb();
                   6737:                                }
                   6738:                                msdos_mem_free(env_seg);
                   6739:                                return(-1);
                   6740:                        }
1.1       root     6741:                }
                   6742:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6743:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6744:                // segments
                   6745:                cs = ss = psp_seg;
                   6746:                ip = 0x100;
                   6747:                sp = 0xfffe;
                   6748:        }
1.1.1.29  root     6749:        if(umb_linked != 0) {
                   6750:                msdos_mem_link_umb();
                   6751:        }
1.1       root     6752:        
                   6753:        // create psp
1.1.1.3   root     6754:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6755:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6756:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6757:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6758:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6759:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6760:        
                   6761:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6762:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6763:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6764:        
1.1.1.4   root     6765:        for(int i = 0; i < 8; i++) {
                   6766:                if(name_tmp[i] == '.') {
                   6767:                        mcb_psp->prog_name[i] = '\0';
                   6768:                        break;
                   6769:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6770:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6771:                        i++;
                   6772:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6773:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6774:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6775:                } else {
                   6776:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6777:                }
                   6778:        }
                   6779:        
1.1       root     6780:        // process info
                   6781:        process_t *process = msdos_process_info_create(psp_seg);
                   6782:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6783: #ifdef USE_DEBUGGER
                   6784:        strcpy(process->module_path, path);
                   6785: #endif
1.1       root     6786:        process->dta.w.l = 0x80;
                   6787:        process->dta.w.h = psp_seg;
                   6788:        process->switchar = '/';
                   6789:        process->max_files = 20;
                   6790:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6791:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6792:        process->parent_ds = SREG(DS);
1.1.1.31  root     6793:        process->parent_es = SREG(ES);
1.1       root     6794:        
                   6795:        current_psp = psp_seg;
1.1.1.23  root     6796:        msdos_sda_update(current_psp);
1.1       root     6797:        
                   6798:        if(al == 0x00) {
                   6799:                int_10h_feh_called = int_10h_ffh_called = false;
                   6800:                
1.1.1.38  root     6801:                // pipe
                   6802:                if(pipe_stdin_path[0] != '\0') {
1.1.1.45  root     6803: //                     if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
                   6804:                        if(msdos_is_device_path(pipe_stdin_path)) {
                   6805:                                fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
                   6806:                        } else {
                   6807:                                fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
                   6808:                        }
                   6809:                        if(fd != -1) {
                   6810:                                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     6811:                                psp->file_table[0] = fd;
                   6812:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6813:                        }
                   6814:                }
                   6815:                if(pipe_stdout_path[0] != '\0') {
                   6816:                        if(_access(pipe_stdout_path, 0) == 0) {
1.1.1.60  root     6817:                                SetFileAttributesA(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6818:                                DeleteFileA(pipe_stdout_path);
1.1.1.38  root     6819:                        }
1.1.1.45  root     6820: //                     if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6821:                        if(msdos_is_device_path(pipe_stdout_path)) {
                   6822:                                fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6823:                        } else {
                   6824:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6825:                        }
                   6826:                        if(fd != -1) {
                   6827:                                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     6828:                                psp->file_table[1] = fd;
                   6829:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6830:                        }
                   6831:                }
1.1.1.39  root     6832:                if(pipe_stderr_path[0] != '\0') {
                   6833:                        if(_access(pipe_stderr_path, 0) == 0) {
1.1.1.60  root     6834:                                SetFileAttributesA(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
                   6835:                                DeleteFileA(pipe_stderr_path);
1.1.1.39  root     6836:                        }
1.1.1.45  root     6837: //                     if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6838:                        if(msdos_is_device_path(pipe_stderr_path)) {
                   6839:                                fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6840:                        } else {
                   6841:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6842:                        }
                   6843:                        if(fd != -1) {
                   6844:                                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     6845:                                psp->file_table[2] = fd;
                   6846:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6847:                        }
                   6848:                }
1.1.1.38  root     6849:                
1.1       root     6850:                // registers and segments
                   6851:                REG16(AX) = REG16(BX) = 0x00;
                   6852:                REG16(CX) = 0xff;
                   6853:                REG16(DX) = psp_seg;
                   6854:                REG16(SI) = ip;
                   6855:                REG16(DI) = sp;
                   6856:                REG16(SP) = sp;
1.1.1.3   root     6857:                SREG(DS) = SREG(ES) = psp_seg;
                   6858:                SREG(SS) = ss;
                   6859:                i386_load_segment_descriptor(DS);
                   6860:                i386_load_segment_descriptor(ES);
                   6861:                i386_load_segment_descriptor(SS);
1.1       root     6862:                
                   6863:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6864:                i386_jmp_far(cs, ip);
                   6865:        } else if(al == 0x01) {
                   6866:                // copy ss:sp and cs:ip to param block
                   6867:                param->sp = sp;
                   6868:                param->ss = ss;
                   6869:                param->ip = ip;
                   6870:                param->cs = cs;
1.1.1.31  root     6871:                
                   6872:                // the AX value to be passed to the child program is put on top of the child's stack
                   6873:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6874:        }
                   6875:        return(0);
                   6876: }
                   6877: 
                   6878: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6879: {
                   6880:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6881:        
                   6882:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6883:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6884:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6885:        
1.1.1.3   root     6886:        SREG(SS) = psp->stack.w.h;
                   6887:        i386_load_segment_descriptor(SS);
1.1       root     6888:        REG16(SP) = psp->stack.w.l;
                   6889:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6890:        
1.1.1.28  root     6891: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6892:        process_t *current_process = NULL;
                   6893:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6894:                if(process[i].psp == psp_seg) {
                   6895:                        current_process = &process[i];
                   6896:                        break;
                   6897:                }
                   6898:        }
                   6899:        if(current_process == NULL) {
                   6900:                throw(0x1f); // general failure
                   6901:        }
                   6902:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6903:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6904:        if(current_process->called_by_int2eh) {
                   6905:                REG16(AX) = ret;
                   6906:        }
                   6907:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6908:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6909:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6910:        i386_load_segment_descriptor(ES);
1.1       root     6911:        
                   6912:        if(mem_free) {
1.1.1.8   root     6913:                int mcb_seg;
                   6914:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6915:                        msdos_mem_free(mcb_seg + 1);
                   6916:                }
                   6917:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6918:                        msdos_mem_free(mcb_seg + 1);
                   6919:                }
1.1       root     6920:                
                   6921:                for(int i = 0; i < MAX_FILES; i++) {
                   6922:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6923:                                _close(i);
1.1.1.20  root     6924:                                msdos_file_handler_close(i);
                   6925:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6926:                        }
                   6927:                }
1.1.1.13  root     6928:                msdos_dta_info_free(psp_seg);
1.1       root     6929:        }
1.1.1.14  root     6930:        msdos_stdio_reopen();
1.1       root     6931:        
1.1.1.28  root     6932:        memset(current_process, 0, sizeof(process_t));
1.1       root     6933:        
                   6934:        current_psp = psp->parent_psp;
                   6935:        retval = ret;
1.1.1.23  root     6936:        msdos_sda_update(current_psp);
1.1       root     6937: }
                   6938: 
                   6939: // drive
                   6940: 
1.1.1.42  root     6941: int pcbios_update_drive_param(int drive_num, int force_update);
                   6942: 
1.1       root     6943: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6944: {
1.1.1.41  root     6945:        if(!(drive_num >= 0 && drive_num < 26)) {
                   6946:                return(0);
                   6947:        }
1.1.1.42  root     6948:        pcbios_update_drive_param(drive_num, force_update);
                   6949:        
                   6950:        drive_param_t *drive_param = &drive_params[drive_num];
1.1       root     6951:        *seg = DPB_TOP >> 4;
                   6952:        *ofs = sizeof(dpb_t) * drive_num;
                   6953:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6954:        
                   6955:        memset(dpb, 0, sizeof(dpb_t));
                   6956:        
1.1.1.41  root     6957:        dpb->drive_num = drive_num;
                   6958:        dpb->unit_num = drive_num;
1.1.1.42  root     6959:        
                   6960:        if(drive_param->valid) {
                   6961:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   6962:                
                   6963:                dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
                   6964:                dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
                   6965:                dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6966:                dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6967:                switch(geo->MediaType) {
                   6968:                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6969:                        dpb->media_type = 0xff;
                   6970:                        break;
                   6971:                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6972:                        dpb->media_type = 0xfe;
                   6973:                        break;
                   6974:                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6975:                        dpb->media_type = 0xfd;
                   6976:                        break;
                   6977:                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6978:                        dpb->media_type = 0xfc;
                   6979:                        break;
                   6980:                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6981:                case F3_1Pt2_512:
                   6982:                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6983:                case F5_720_512:
                   6984:                        dpb->media_type = 0xf9;
                   6985:                        break;
                   6986:                case FixedMedia:        // hard disk
                   6987:                case RemovableMedia:
                   6988:                case Unknown:
                   6989:                        dpb->media_type = 0xf8;
                   6990:                        break;
                   6991:                default:
                   6992:                        dpb->media_type = 0xf0;
                   6993:                        break;
                   6994:                }
                   6995:        }
1.1.1.41  root     6996:        dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
                   6997:        dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42  root     6998:        dpb->info_sector = 0xffff;
                   6999:        dpb->backup_boot_sector = 0xffff;
                   7000:        dpb->free_clusters = 0xffff;
                   7001:        dpb->free_search_cluster = 0xffffffff;
                   7002:        
                   7003:        return(drive_param->valid);
1.1       root     7004: }
                   7005: 
                   7006: // pc bios
                   7007: 
1.1.1.35  root     7008: #ifdef USE_SERVICE_THREAD
                   7009: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   7010: {
                   7011: #if defined(HAS_I386)
                   7012:        if(m_SF != 0) {
                   7013:                m_SF = 0;
1.1.1.49  root     7014:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     7015:        } else {
                   7016:                m_SF = 1;
1.1.1.49  root     7017:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     7018:        }
                   7019: #else
                   7020:        if(m_SignVal < 0) {
                   7021:                m_SignVal = 0;
1.1.1.49  root     7022:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     7023:        } else {
                   7024:                m_SignVal = -1;
1.1.1.49  root     7025:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     7026:        }
                   7027: #endif
1.1.1.59  root     7028:        // dummy loop to wait BIOS/DOS service is done is at fffc:0013
1.1.1.49  root     7029:        i386_call_far(DUMMY_TOP >> 4, 0x0013);
1.1.1.35  root     7030:        in_service = true;
                   7031:        service_exit = false;
                   7032:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   7033: }
                   7034: 
                   7035: void finish_service_loop()
                   7036: {
                   7037:        if(in_service && service_exit) {
                   7038: #if defined(HAS_I386)
                   7039:                if(m_SF != 0) {
                   7040:                        m_SF = 0;
                   7041:                } else {
                   7042:                        m_SF = 1;
                   7043:                }
                   7044: #else
                   7045:                if(m_SignVal < 0) {
                   7046:                        m_SignVal = 0;
                   7047:                } else {
                   7048:                        m_SignVal = -1;
                   7049:                }
                   7050: #endif
                   7051:                in_service = false;
                   7052:        }
                   7053: }
                   7054: #endif
                   7055: 
1.1.1.19  root     7056: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   7057: {
                   7058:        static unsigned __int64 start_msec_since_midnight = 0;
                   7059:        static unsigned __int64 start_msec_since_hostboot = 0;
                   7060:        
                   7061:        if(start_msec_since_midnight == 0) {
                   7062:                SYSTEMTIME time;
                   7063:                GetLocalTime(&time);
                   7064:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   7065:                start_msec_since_hostboot = cur_msec;
                   7066:        }
                   7067:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   7068:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   7069:        return (UINT32)tick;
                   7070: }
                   7071: 
                   7072: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   7073: {
                   7074:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   7075:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   7076:        
                   7077:        if(prev_tick > next_tick) {
                   7078:                mem[0x470] = 1;
                   7079:        }
                   7080:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   7081: }
                   7082: 
1.1.1.14  root     7083: inline void pcbios_irq0()
                   7084: {
                   7085:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     7086:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     7087: }
                   7088: 
1.1.1.16  root     7089: int pcbios_get_text_vram_address(int page)
1.1       root     7090: {
                   7091:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7092:                return TEXT_VRAM_TOP;
1.1       root     7093:        } else {
1.1.1.14  root     7094:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     7095:        }
                   7096: }
                   7097: 
1.1.1.16  root     7098: int pcbios_get_shadow_buffer_address(int page)
1.1       root     7099: {
1.1.1.14  root     7100:        if(!int_10h_feh_called) {
1.1.1.16  root     7101:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     7102:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7103:                return SHADOW_BUF_TOP;
                   7104:        } else {
1.1.1.14  root     7105:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     7106:        }
                   7107: }
                   7108: 
1.1.1.16  root     7109: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     7110: {
1.1.1.16  root     7111:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     7112: }
                   7113: 
1.1.1.56  root     7114: bool pcbios_set_font_size(int width, int height)
                   7115: {
1.1.1.61  root     7116:        if(set_console_font_size(width, height)) {
                   7117:                *(UINT16 *)(mem + 0x485) = height;
                   7118:                return(true);
                   7119:        }
                   7120:        return(false);
1.1.1.56  root     7121: }
                   7122: 
1.1.1.16  root     7123: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     7124: {
1.1.1.14  root     7125:        // clear the existing screen, not just the new one
                   7126:        int clr_height = max(height, scr_height);
                   7127:        
1.1.1.16  root     7128:        if(scr_width != width || scr_height != height) {
                   7129:                change_console_size(width, height);
1.1.1.14  root     7130:        }
                   7131:        mem[0x462] = 0;
                   7132:        *(UINT16 *)(mem + 0x44e) = 0;
                   7133:        
1.1.1.16  root     7134:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   7135:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   7136:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   7137:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1.1.51  root     7138:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     7139:        
1.1.1.23  root     7140:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     7141:        if(clr_screen) {
1.1.1.14  root     7142:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   7143:                        mem[ofs++] = 0x20;
                   7144:                        mem[ofs++] = 0x07;
                   7145:                }
                   7146:                
1.1.1.35  root     7147: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7148:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7149: #endif
1.1.1.14  root     7150:                for(int y = 0; y < clr_height; y++) {
                   7151:                        for(int x = 0; x < scr_width; x++) {
                   7152:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7153:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     7154:                        }
                   7155:                }
                   7156:                SMALL_RECT rect;
1.1.1.14  root     7157:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
1.1.1.60  root     7158:                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7159:                vram_length_char = vram_last_length_char = 0;
                   7160:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     7161: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7162:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7163: #endif
1.1       root     7164:        }
1.1.1.14  root     7165:        COORD co;
                   7166:        co.X = 0;
                   7167:        co.Y = scr_top;
                   7168:        SetConsoleCursorPosition(hStdout, co);
                   7169:        cursor_moved = true;
1.1.1.59  root     7170:        cursor_moved_by_crtc = false;
1.1.1.14  root     7171:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     7172: }
                   7173: 
1.1.1.36  root     7174: void pcbios_update_cursor_position()
                   7175: {
                   7176:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7177:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   7178:        if(!restore_console_on_exit) {
                   7179:                scr_top = csbi.srWindow.Top;
                   7180:        }
                   7181:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   7182:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   7183: }
                   7184: 
1.1.1.16  root     7185: inline void pcbios_int_10h_00h()
                   7186: {
                   7187:        switch(REG8(AL) & 0x7f) {
                   7188:        case 0x70: // v-text mode
                   7189:        case 0x71: // extended cga v-text mode
                   7190:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   7191:                break;
1.1.1.61  root     7192:        case 0x73:
                   7193:        case 0x03:
                   7194:                change_console_size(80, 25); // for Windows10
                   7195:                pcbios_set_font_size(font_width, font_height);
1.1.1.16  root     7196:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   7197:                break;
                   7198:        }
                   7199:        if(REG8(AL) & 0x80) {
                   7200:                mem[0x487] |= 0x80;
                   7201:        } else {
                   7202:                mem[0x487] &= ~0x80;
                   7203:        }
                   7204:        mem[0x449] = REG8(AL) & 0x7f;
                   7205: }
                   7206: 
1.1       root     7207: inline void pcbios_int_10h_01h()
                   7208: {
1.1.1.13  root     7209:        mem[0x460] = REG8(CL);
                   7210:        mem[0x461] = REG8(CH);
1.1.1.14  root     7211:        
1.1.1.60  root     7212:        int size = (int)(REG8(CL) & 7) - (int)(REG8(CH) & 7) + 1;
                   7213:        
                   7214:        if(!((REG8(CH) & 0x20) != 0 || size < 0)) {
                   7215:                ci_new.bVisible = TRUE;
                   7216:                ci_new.dwSize = (size + 2) * 100 / (8 + 2);
                   7217:        } else {
                   7218:                ci_new.bVisible = FALSE;
                   7219:        }
1.1       root     7220: }
                   7221: 
                   7222: inline void pcbios_int_10h_02h()
                   7223: {
1.1.1.14  root     7224:        // continuously setting the cursor effectively stops it blinking
1.1.1.59  root     7225:        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     7226:                COORD co;
                   7227:                co.X = REG8(DL);
1.1.1.14  root     7228:                co.Y = REG8(DH) + scr_top;
                   7229:                
                   7230:                // some programs hide the cursor by moving it off screen
                   7231:                static bool hidden = false;
1.1.1.23  root     7232:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7233:                
                   7234:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
1.1.1.59  root     7235:                        if(ci_new.bVisible) {
                   7236:                                ci_new.bVisible = FALSE;
1.1.1.14  root     7237:                                hidden = true;
                   7238:                        }
                   7239:                } else if(hidden) {
1.1.1.59  root     7240:                        if(!ci_new.bVisible) {
                   7241:                                ci_new.bVisible = TRUE;
1.1.1.14  root     7242:                        }
                   7243:                        hidden = false;
                   7244:                }
1.1.1.59  root     7245:                cursor_moved_by_crtc = false;
1.1       root     7246:        }
1.1.1.14  root     7247:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   7248:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     7249: }
                   7250: 
                   7251: inline void pcbios_int_10h_03h()
                   7252: {
1.1.1.14  root     7253:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7254:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7255:        REG8(CL) = mem[0x460];
                   7256:        REG8(CH) = mem[0x461];
                   7257: }
                   7258: 
                   7259: inline void pcbios_int_10h_05h()
                   7260: {
1.1.1.14  root     7261:        if(REG8(AL) >= vram_pages) {
                   7262:                return;
                   7263:        }
                   7264:        if(mem[0x462] != REG8(AL)) {
                   7265:                vram_flush();
                   7266:                
1.1.1.23  root     7267:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7268:                SMALL_RECT rect;
1.1.1.14  root     7269:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7270:                ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7271:                
1.1.1.16  root     7272:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     7273:                        for(int x = 0; x < scr_width; x++) {
                   7274:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7275:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7276:                        }
                   7277:                }
1.1.1.16  root     7278:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     7279:                        for(int x = 0; x < scr_width; x++) {
                   7280:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   7281:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     7282:                        }
                   7283:                }
1.1.1.60  root     7284:                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7285:                
                   7286:                COORD co;
1.1.1.14  root     7287:                co.X = mem[0x450 + REG8(AL) * 2];
                   7288:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   7289:                if(co.Y < scr_top + scr_height) {
                   7290:                        SetConsoleCursorPosition(hStdout, co);
                   7291:                }
1.1.1.59  root     7292:                cursor_moved_by_crtc = false;
1.1       root     7293:        }
1.1.1.14  root     7294:        mem[0x462] = REG8(AL);
                   7295:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   7296:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     7297:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     7298:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     7299:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     7300:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     7301:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     7302: }
                   7303: 
                   7304: inline void pcbios_int_10h_06h()
                   7305: {
1.1.1.14  root     7306:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7307:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7308:                return;
                   7309:        }
                   7310:        vram_flush();
                   7311:        
1.1.1.23  root     7312:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7313:        SMALL_RECT rect;
1.1.1.14  root     7314:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7315:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7316:        
                   7317:        int right = min(REG8(DL), scr_width - 1);
                   7318:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7319:        
                   7320:        if(REG8(AL) == 0) {
1.1.1.14  root     7321:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7322:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7323:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7324:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7325:                        }
                   7326:                }
                   7327:        } else {
1.1.1.14  root     7328:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     7329:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7330:                                if(y2 <= bottom) {
                   7331:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7332:                                } else {
1.1.1.14  root     7333:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7334:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7335:                                }
1.1.1.14  root     7336:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7337:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7338:                        }
                   7339:                }
                   7340:        }
1.1.1.60  root     7341:        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7342: }
                   7343: 
                   7344: inline void pcbios_int_10h_07h()
                   7345: {
1.1.1.14  root     7346:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7347:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7348:                return;
                   7349:        }
                   7350:        vram_flush();
                   7351:        
1.1.1.23  root     7352:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7353:        SMALL_RECT rect;
1.1.1.14  root     7354:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7355:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7356:        
                   7357:        int right = min(REG8(DL), scr_width - 1);
                   7358:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7359:        
                   7360:        if(REG8(AL) == 0) {
1.1.1.14  root     7361:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7362:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7363:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7364:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7365:                        }
                   7366:                }
                   7367:        } else {
1.1.1.14  root     7368:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     7369:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7370:                                if(y2 >= REG8(CH)) {
                   7371:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7372:                                } else {
1.1.1.14  root     7373:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7374:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7375:                                }
1.1.1.14  root     7376:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7377:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7378:                        }
                   7379:                }
                   7380:        }
1.1.1.60  root     7381:        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7382: }
                   7383: 
                   7384: inline void pcbios_int_10h_08h()
                   7385: {
                   7386:        COORD co;
                   7387:        DWORD num;
                   7388:        
1.1.1.14  root     7389:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7390:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7391:        
                   7392:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7393:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7394:                co.Y += scr_top;
                   7395:                vram_flush();
1.1.1.60  root     7396:                ReadConsoleOutputCharacterA(hStdout, scr_char, 1, co, &num);
1.1       root     7397:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   7398:                REG8(AL) = scr_char[0];
                   7399:                REG8(AH) = scr_attr[0];
                   7400:        } else {
1.1.1.16  root     7401:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     7402:        }
                   7403: }
                   7404: 
                   7405: inline void pcbios_int_10h_09h()
                   7406: {
                   7407:        COORD co;
                   7408:        
1.1.1.14  root     7409:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7410:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7411:        
1.1.1.16  root     7412:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7413:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7414:        
                   7415:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7416: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7417:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7418: #endif
1.1.1.16  root     7419:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7420:                while(dest < end) {
                   7421:                        write_text_vram_char(dest - vram, REG8(AL));
                   7422:                        mem[dest++] = REG8(AL);
                   7423:                        write_text_vram_attr(dest - vram, REG8(BL));
                   7424:                        mem[dest++] = REG8(BL);
1.1       root     7425:                }
1.1.1.35  root     7426: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7427:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7428: #endif
1.1       root     7429:        } else {
1.1.1.14  root     7430:                while(dest < end) {
1.1       root     7431:                        mem[dest++] = REG8(AL);
                   7432:                        mem[dest++] = REG8(BL);
                   7433:                }
                   7434:        }
                   7435: }
                   7436: 
                   7437: inline void pcbios_int_10h_0ah()
                   7438: {
                   7439:        COORD co;
                   7440:        
1.1.1.14  root     7441:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7442:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7443:        
1.1.1.16  root     7444:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7445:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7446:        
                   7447:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7448: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7449:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7450: #endif
1.1.1.16  root     7451:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7452:                while(dest < end) {
                   7453:                        write_text_vram_char(dest - vram, REG8(AL));
                   7454:                        mem[dest++] = REG8(AL);
                   7455:                        dest++;
1.1       root     7456:                }
1.1.1.35  root     7457: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7458:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7459: #endif
1.1       root     7460:        } else {
1.1.1.14  root     7461:                while(dest < end) {
1.1       root     7462:                        mem[dest++] = REG8(AL);
                   7463:                        dest++;
                   7464:                }
                   7465:        }
                   7466: }
                   7467: 
1.1.1.40  root     7468: inline void pcbios_int_10h_0ch()
                   7469: {
                   7470:        HDC hdc = get_console_window_device_context();
                   7471:        
                   7472:        if(hdc != NULL) {
                   7473:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
                   7474:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
                   7475:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
                   7476:                
                   7477:                if(REG8(AL) & 0x80) {
                   7478:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7479:                        if(color != CLR_INVALID) {
                   7480:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7481:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7482:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
                   7483:                        }
                   7484:                }
                   7485:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
                   7486:        }
                   7487: }
                   7488: 
                   7489: inline void pcbios_int_10h_0dh()
                   7490: {
                   7491:        HDC hdc = get_console_window_device_context();
                   7492:        BYTE r = 0;
                   7493:        BYTE g = 0;
                   7494:        BYTE b = 0;
                   7495:        
                   7496:        if(hdc != NULL) {
                   7497:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7498:                if(color != CLR_INVALID) {
                   7499:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7500:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7501:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
                   7502:                }
                   7503:        }
                   7504:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
                   7505: }
                   7506: 
1.1       root     7507: inline void pcbios_int_10h_0eh()
                   7508: {
1.1.1.59  root     7509:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   7510:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     7511:        DWORD num;
                   7512:        COORD co;
                   7513:        
1.1.1.59  root     7514:        if(cursor_moved_by_crtc) {
                   7515:                if(!restore_console_on_exit) {
                   7516:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7517:                        scr_top = csbi.srWindow.Top;
                   7518:                }
                   7519:                co.X = mem[0x450 + REG8(BH) * 2];
                   7520:                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   7521:                SetConsoleCursorPosition(hStdout, co);
                   7522:                cursor_moved_by_crtc = false;
                   7523:        }
1.1.1.54  root     7524:        co.X = mem[0x450 + mem[0x462] * 2];
                   7525:        co.Y = mem[0x451 + mem[0x462] * 2];
1.1.1.14  root     7526:        
                   7527:        if(REG8(AL) == 7) {
                   7528:                //MessageBeep(-1);
                   7529:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   7530:                if(REG8(AL) == 10) {
                   7531:                        vram_flush();
                   7532:                }
1.1.1.60  root     7533:                WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     7534:                cursor_moved = true;
                   7535:        } else {
1.1.1.54  root     7536:                int dest = pcbios_get_shadow_buffer_address(mem[0x462], co.X, co.Y);
1.1.1.35  root     7537: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7538:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7539: #endif
1.1.1.54  root     7540:                int vram = pcbios_get_shadow_buffer_address(mem[0x462]);
                   7541:                write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     7542: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7543:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7544: #endif
1.1.1.54  root     7545:                
                   7546:                if(++co.X == scr_width) {
                   7547:                        co.X = 0;
                   7548:                        if(++co.Y == scr_height) {
                   7549:                                vram_flush();
1.1.1.60  root     7550:                                WriteConsoleA(hStdout, "\n", 1, &num, NULL);
1.1.1.14  root     7551:                                cursor_moved = true;
                   7552:                        }
                   7553:                }
1.1.1.54  root     7554:                if(!cursor_moved) {
                   7555:                        co.Y += scr_top;
                   7556:                        SetConsoleCursorPosition(hStdout, co);
                   7557:                        cursor_moved = true;
                   7558:                }
1.1.1.14  root     7559:                mem[dest] = REG8(AL);
                   7560:        }
1.1       root     7561: }
                   7562: 
                   7563: inline void pcbios_int_10h_0fh()
                   7564: {
                   7565:        REG8(AL) = mem[0x449];
                   7566:        REG8(AH) = mem[0x44a];
                   7567:        REG8(BH) = mem[0x462];
                   7568: }
                   7569: 
1.1.1.14  root     7570: inline void pcbios_int_10h_11h()
                   7571: {
                   7572:        switch(REG8(AL)) {
1.1.1.58  root     7573:        case 0x00:
                   7574:        case 0x10:
1.1.1.61  root     7575:                if(REG8(BH)) {
                   7576:                        change_console_size(80, 25); // for Windows10
                   7577:                        if(!pcbios_set_font_size(font_width, (int)REG8(BH))) {
                   7578:                                for(int h = min(font_height, (int)REG8(BH)); h <= max(font_height, (int)REG8(BH)); h++) {
                   7579:                                        if(h != (int)REG8(BH)) {
                   7580:                                                if(pcbios_set_font_size(font_width, h)) {
                   7581:                                                        break;
                   7582:                                                }
                   7583:                                        }
                   7584:                                }
                   7585:                        }
1.1.1.58  root     7586:                        pcbios_set_console_size(80, (25 * 16) / REG8(BH), true);
                   7587:                }
                   7588:                break;
1.1.1.16  root     7589:        case 0x01:
1.1.1.14  root     7590:        case 0x11:
1.1.1.61  root     7591:                change_console_size(80, 28); // for Windows10
                   7592:                if(!pcbios_set_font_size(font_width, 14)) {
                   7593:                        for(int h = min(font_height, 14); h <= max(font_height, 14); h++) {
                   7594:                                if(h != 14) {
                   7595:                                        if(pcbios_set_font_size(font_width, h)) {
                   7596:                                                break;
                   7597:                                        }
                   7598:                                }
                   7599:                        }
1.1.1.56  root     7600:                }
1.1.1.61  root     7601:                pcbios_set_console_size(80, 28, true); // 28 = 25 * 16 / 14
1.1.1.14  root     7602:                break;
1.1.1.16  root     7603:        case 0x02:
1.1.1.14  root     7604:        case 0x12:
1.1.1.61  root     7605:                change_console_size(80, 25); // for Windows10
                   7606:                if(!pcbios_set_font_size(8, 8)) {
                   7607:                        bool success = false;
                   7608:                        for(int y = 8; y <= 14; y++) {
                   7609:                                for(int x = min(font_width, 6); x <= max(font_width, 8); x++) {
                   7610:                                        if(pcbios_set_font_size(x, y)) {
                   7611:                                                success = true;
                   7612:                                                break;
                   7613:                                        }
                   7614:                                }
                   7615:                        }
                   7616:                        if(!success) {
                   7617:                                pcbios_set_font_size(font_width, font_height);
                   7618:                        }
1.1.1.56  root     7619:                }
1.1.1.61  root     7620:                pcbios_set_console_size(80, 50, true); // 50 = 25 * 16 / 8
1.1.1.14  root     7621:                break;
1.1.1.16  root     7622:        case 0x04:
1.1.1.14  root     7623:        case 0x14:
1.1.1.61  root     7624:                change_console_size(80, 25); // for Windows10
                   7625:                pcbios_set_font_size(font_width, font_height);
                   7626:                pcbios_set_console_size(80, 25, true);
1.1.1.58  root     7627:                break;
                   7628:        case 0x18:
1.1.1.61  root     7629:                change_console_size(80, 25); // for Windows10
                   7630:                pcbios_set_font_size(font_width, font_height);
                   7631:                pcbios_set_console_size(80, 25, true);
1.1.1.14  root     7632:                break;
                   7633:        case 0x30:
                   7634:                SREG(ES) = 0;
                   7635:                i386_load_segment_descriptor(ES);
                   7636:                REG16(BP) = 0;
                   7637:                REG16(CX) = mem[0x485];
                   7638:                REG8(DL) = mem[0x484];
                   7639:                break;
1.1.1.54  root     7640:        default:
                   7641:                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));
                   7642:                m_CF = 1;
                   7643:                break;
1.1.1.14  root     7644:        }
                   7645: }
                   7646: 
                   7647: inline void pcbios_int_10h_12h()
                   7648: {
1.1.1.16  root     7649:        switch(REG8(BL)) {
                   7650:        case 0x10:
1.1.1.14  root     7651:                REG16(BX) = 0x0003;
                   7652:                REG16(CX) = 0x0009;
1.1.1.16  root     7653:                break;
1.1.1.14  root     7654:        }
                   7655: }
                   7656: 
1.1       root     7657: inline void pcbios_int_10h_13h()
                   7658: {
1.1.1.3   root     7659:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     7660:        COORD co;
                   7661:        DWORD num;
                   7662:        
                   7663:        co.X = REG8(DL);
1.1.1.14  root     7664:        co.Y = REG8(DH) + scr_top;
                   7665:        
                   7666:        vram_flush();
1.1       root     7667:        
                   7668:        switch(REG8(AL)) {
                   7669:        case 0x00:
                   7670:        case 0x01:
                   7671:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7672:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7673:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7674:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7675:                        SetConsoleCursorPosition(hStdout, co);
                   7676:                        
                   7677:                        if(csbi.wAttributes != REG8(BL)) {
                   7678:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   7679:                        }
1.1.1.60  root     7680:                        WriteConsoleA(hStdout, &mem[ofs], REG16(CX), &num, NULL);
1.1.1.14  root     7681:                        
1.1       root     7682:                        if(csbi.wAttributes != REG8(BL)) {
                   7683:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7684:                        }
                   7685:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     7686:                                if(!restore_console_on_exit) {
                   7687:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7688:                                        scr_top = csbi.srWindow.Top;
                   7689:                                }
1.1.1.14  root     7690:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7691:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7692:                                SetConsoleCursorPosition(hStdout, co);
                   7693:                        } else {
                   7694:                                cursor_moved = true;
                   7695:                        }
1.1.1.59  root     7696:                        cursor_moved_by_crtc = false;
1.1       root     7697:                } else {
1.1.1.3   root     7698:                        m_CF = 1;
1.1       root     7699:                }
                   7700:                break;
                   7701:        case 0x02:
                   7702:        case 0x03:
                   7703:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7704:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7705:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7706:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7707:                        SetConsoleCursorPosition(hStdout, co);
                   7708:                        
                   7709:                        WORD wAttributes = csbi.wAttributes;
                   7710:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7711:                                if(wAttributes != mem[ofs + 1]) {
                   7712:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7713:                                        wAttributes = mem[ofs + 1];
                   7714:                                }
1.1.1.60  root     7715:                                WriteConsoleA(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7716:                        }
                   7717:                        if(csbi.wAttributes != wAttributes) {
                   7718:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7719:                        }
                   7720:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7721:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7722:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7723:                                SetConsoleCursorPosition(hStdout, co);
                   7724:                        } else {
                   7725:                                cursor_moved = true;
                   7726:                        }
1.1.1.59  root     7727:                        cursor_moved_by_crtc = false;
1.1       root     7728:                } else {
1.1.1.3   root     7729:                        m_CF = 1;
1.1       root     7730:                }
                   7731:                break;
                   7732:        case 0x10:
                   7733:        case 0x11:
                   7734:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7735:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.60  root     7736:                        ReadConsoleOutputCharacterA(hStdout, scr_char, REG16(CX), co, &num);
1.1       root     7737:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7738:                        for(int i = 0; i < num; i++) {
                   7739:                                mem[ofs++] = scr_char[i];
                   7740:                                mem[ofs++] = scr_attr[i];
1.1.1.45  root     7741:                                if(REG8(AL) & 0x01) {
1.1       root     7742:                                        mem[ofs++] = 0;
                   7743:                                        mem[ofs++] = 0;
                   7744:                                }
                   7745:                        }
                   7746:                } else {
1.1.1.16  root     7747:                        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     7748:                                mem[ofs++] = mem[src++];
                   7749:                                mem[ofs++] = mem[src++];
1.1.1.45  root     7750:                                if(REG8(AL) & 0x01) {
1.1       root     7751:                                        mem[ofs++] = 0;
                   7752:                                        mem[ofs++] = 0;
                   7753:                                }
1.1.1.14  root     7754:                                if(++co.X == scr_width) {
                   7755:                                        if(++co.Y == scr_height) {
1.1       root     7756:                                                break;
                   7757:                                        }
                   7758:                                        co.X = 0;
                   7759:                                }
                   7760:                        }
                   7761:                }
                   7762:                break;
1.1.1.45  root     7763:        case 0x12: // ???
                   7764:        case 0x13: // ???
1.1       root     7765:        case 0x20:
                   7766:        case 0x21:
                   7767:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7768:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7769:                        int len = min(REG16(CX), scr_width * scr_height);
                   7770:                        for(int i = 0; i < len; i++) {
1.1       root     7771:                                scr_char[i] = mem[ofs++];
                   7772:                                scr_attr[i] = mem[ofs++];
1.1.1.45  root     7773:                                if(REG8(AL) & 0x01) {
1.1       root     7774:                                        ofs += 2;
                   7775:                                }
                   7776:                        }
1.1.1.60  root     7777:                        WriteConsoleOutputCharacterA(hStdout, scr_char, len, co, &num);
1.1.1.14  root     7778:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7779:                } else {
1.1.1.16  root     7780:                        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     7781:                                mem[dest++] = mem[ofs++];
                   7782:                                mem[dest++] = mem[ofs++];
1.1.1.45  root     7783:                                if(REG8(AL) & 0x01) {
1.1       root     7784:                                        ofs += 2;
                   7785:                                }
1.1.1.14  root     7786:                                if(++co.X == scr_width) {
                   7787:                                        if(++co.Y == scr_height) {
1.1       root     7788:                                                break;
                   7789:                                        }
                   7790:                                        co.X = 0;
                   7791:                                }
                   7792:                        }
                   7793:                }
                   7794:                break;
                   7795:        default:
1.1.1.22  root     7796:                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     7797:                m_CF = 1;
1.1       root     7798:                break;
                   7799:        }
                   7800: }
                   7801: 
1.1.1.30  root     7802: inline void pcbios_int_10h_18h()
                   7803: {
                   7804:        switch(REG8(AL)) {
                   7805:        case 0x00:
                   7806:        case 0x01:
                   7807: //             REG8(AL) = 0x86;
                   7808:                REG8(AL) = 0x00;
                   7809:                break;
                   7810:        default:
                   7811:                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));
                   7812:                m_CF = 1;
                   7813:                break;
                   7814:        }
                   7815: }
                   7816: 
1.1.1.14  root     7817: inline void pcbios_int_10h_1ah()
                   7818: {
                   7819:        switch(REG8(AL)) {
                   7820:        case 0x00:
                   7821:                REG8(AL) = 0x1a;
                   7822:                REG8(BL) = 0x08;
                   7823:                REG8(BH) = 0x00;
                   7824:                break;
                   7825:        default:
1.1.1.22  root     7826:                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     7827:                m_CF = 1;
                   7828:                break;
                   7829:        }
                   7830: }
                   7831: 
1.1       root     7832: inline void pcbios_int_10h_1dh()
                   7833: {
                   7834:        switch(REG8(AL)) {
1.1.1.43  root     7835:        case 0x00:
                   7836:                // DOS/V Shift Status Line Control is not supported
                   7837:                m_CF = 1;
                   7838:                break;
1.1       root     7839:        case 0x01:
                   7840:                break;
                   7841:        case 0x02:
                   7842:                REG16(BX) = 0;
                   7843:                break;
                   7844:        default:
1.1.1.22  root     7845:                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));
                   7846:                m_CF = 1;
                   7847:                break;
                   7848:        }
                   7849: }
                   7850: 
                   7851: inline void pcbios_int_10h_4fh()
                   7852: {
                   7853:        switch(REG8(AL)) {
                   7854:        case 0x00:
                   7855:                REG8(AH) = 0x02; // not supported
                   7856:                break;
                   7857:        case 0x01:
                   7858:        case 0x02:
                   7859:        case 0x03:
                   7860:        case 0x04:
                   7861:        case 0x05:
                   7862:        case 0x06:
                   7863:        case 0x07:
                   7864:        case 0x08:
                   7865:        case 0x09:
                   7866:        case 0x0a:
                   7867:        case 0x0b:
                   7868:        case 0x0c:
                   7869:                REG8(AH) = 0x01; // failed
                   7870:                break;
                   7871:        default:
                   7872:                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     7873:                m_CF = 1;
1.1       root     7874:                break;
                   7875:        }
                   7876: }
                   7877: 
                   7878: inline void pcbios_int_10h_82h()
                   7879: {
                   7880:        static UINT8 mode = 0;
                   7881:        
                   7882:        switch(REG8(AL)) {
1.1.1.22  root     7883:        case 0x00:
1.1       root     7884:                if(REG8(BL) != 0xff) {
                   7885:                        mode = REG8(BL);
                   7886:                }
                   7887:                REG8(AL) = mode;
                   7888:                break;
                   7889:        default:
1.1.1.22  root     7890:                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     7891:                m_CF = 1;
1.1       root     7892:                break;
                   7893:        }
                   7894: }
                   7895: 
1.1.1.22  root     7896: inline void pcbios_int_10h_83h()
                   7897: {
                   7898:        static UINT8 mode = 0;
                   7899:        
                   7900:        switch(REG8(AL)) {
                   7901:        case 0x00:
                   7902:                REG16(AX) = 0; // offset???
                   7903:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7904:                i386_load_segment_descriptor(ES);
                   7905:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7906:                break;
                   7907:        default:
                   7908:                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));
                   7909:                m_CF = 1;
                   7910:                break;
                   7911:        }
                   7912: }
                   7913: 
                   7914: inline void pcbios_int_10h_90h()
                   7915: {
                   7916:        REG8(AL) = mem[0x449];
                   7917: }
                   7918: 
                   7919: inline void pcbios_int_10h_91h()
                   7920: {
                   7921:        REG8(AL) = 0x04; // VGA
                   7922: }
                   7923: 
                   7924: inline void pcbios_int_10h_efh()
                   7925: {
                   7926:        REG16(DX) = 0xffff;
                   7927: }
                   7928: 
1.1       root     7929: inline void pcbios_int_10h_feh()
                   7930: {
                   7931:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7932:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7933:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7934:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7935:        }
                   7936:        int_10h_feh_called = true;
                   7937: }
                   7938: 
                   7939: inline void pcbios_int_10h_ffh()
                   7940: {
                   7941:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7942:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7943:                COORD co;
                   7944:                DWORD num;
                   7945:                
1.1.1.14  root     7946:                vram_flush();
                   7947:                
                   7948:                co.X = (REG16(DI) >> 1) % scr_width;
                   7949:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7950:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7951:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7952:                int len;
                   7953:                for(len = 0; ofs < end; len++) {
                   7954:                        scr_char[len] = mem[ofs++];
                   7955:                        scr_attr[len] = mem[ofs++];
                   7956:                }
                   7957:                co.Y += scr_top;
1.1.1.60  root     7958:                WriteConsoleOutputCharacterA(hStdout, scr_char, len, co, &num);
1.1.1.14  root     7959:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7960:        }
                   7961:        int_10h_ffh_called = true;
                   7962: }
                   7963: 
1.1.1.42  root     7964: int pcbios_update_drive_param(int drive_num, int force_update)
                   7965: {
                   7966:        if(drive_num >= 0 && drive_num < 26) {
                   7967:                drive_param_t *drive_param = &drive_params[drive_num];
                   7968:                
                   7969:                if(force_update || !drive_param->initialized) {
                   7970:                        drive_param->valid = 0;
                   7971:                        char dev[64];
                   7972:                        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7973:                        
1.1.1.60  root     7974:                        HANDLE hFile = CreateFileA(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.42  root     7975:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7976:                                DWORD dwSize;
                   7977:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
                   7978:                                        drive_param->valid = 1;
                   7979:                                }
                   7980:                                CloseHandle(hFile);
                   7981:                        }
                   7982:                        drive_param->initialized = 1;
                   7983:                }
                   7984:                return(drive_param->valid);
                   7985:        }
                   7986:        return(0);
                   7987: }
                   7988: 
                   7989: inline void pcbios_int_13h_00h()
                   7990: {
                   7991:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7992:        
                   7993:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7994:                REG8(AH) = 0x00; // successful completion
                   7995:        } else {
                   7996:                if(REG8(DL) & 0x80) {
                   7997:                        REG8(AH) = 0x05; // reset failed (hard disk)
                   7998:                } else {
                   7999:                        REG8(AH) = 0x80; // timeout (not ready)
                   8000:                }
                   8001:                m_CF = 1;
                   8002:        }
                   8003: }
                   8004: 
                   8005: inline void pcbios_int_13h_02h()
                   8006: {
                   8007:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8008:        
                   8009:        if(REG8(AL) == 0) {
                   8010:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   8011:                m_CF = 1;
                   8012:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   8013:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8014:                m_CF = 1;
                   8015:        } else {
                   8016:                drive_param_t *drive_param = &drive_params[drive_num];
                   8017:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8018:                char dev[64];
                   8019:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   8020:                
1.1.1.60  root     8021:                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     8022:                if(hFile == INVALID_HANDLE_VALUE) {
                   8023:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8024:                        m_CF = 1;
                   8025:                } else {
                   8026:                        UINT32 sector_num = REG8(AL);
                   8027:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   8028:                        UINT32 head = REG8(DH);
                   8029:                        UINT32 sector = REG8(CL) & 0x3f;
                   8030:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   8031:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   8032:                        DWORD dwSize;
                   8033:                        
                   8034: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8035: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8036: //                             m_CF = 1;
                   8037: //                     } else 
                   8038:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8039:                                REG8(AH) = 0x04; // sector not found/read error
                   8040:                                m_CF = 1;
                   8041:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   8042:                                REG8(AH) = 0x04; // sector not found/read error
                   8043:                                m_CF = 1;
                   8044:                        } else {
                   8045:                                REG8(AH) = 0x00; // successful completion
                   8046:                        }
                   8047:                        CloseHandle(hFile);
                   8048:                }
                   8049:        }
                   8050: }
                   8051: 
                   8052: inline void pcbios_int_13h_03h()
                   8053: {
                   8054:        // this operation may cause serious damage for drives, so support only floppy disk...
                   8055:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8056:        
                   8057:        if(REG8(AL) == 0) {
                   8058:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   8059:                m_CF = 1;
                   8060:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   8061:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8062:                m_CF = 1;
                   8063:        } else if(!drive_params[drive_num].is_fdd()) {
                   8064:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8065:                m_CF = 1;
                   8066:        } else {
                   8067:                drive_param_t *drive_param = &drive_params[drive_num];
                   8068:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8069:                char dev[64];
                   8070:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   8071:                
1.1.1.60  root     8072:                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     8073:                if(hFile == INVALID_HANDLE_VALUE) {
                   8074:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8075:                        m_CF = 1;
                   8076:                } else {
                   8077:                        UINT32 sector_num = REG8(AL);
                   8078:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   8079:                        UINT32 head = REG8(DH);
                   8080:                        UINT32 sector = REG8(CL) & 0x3f;
                   8081:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   8082:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   8083:                        DWORD dwSize;
                   8084:                        
                   8085: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8086: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8087: //                             m_CF = 1;
                   8088: //                     } else 
                   8089:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8090:                                REG8(AH) = 0x04; // sector not found/read error
                   8091:                                m_CF = 1;
                   8092:                        } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   8093:                                REG8(AH) = 0x04; // sector not found/read error
                   8094:                                m_CF = 1;
                   8095:                        } else {
                   8096:                                REG8(AH) = 0x00; // successful completion
                   8097:                        }
                   8098:                        CloseHandle(hFile);
                   8099:                }
                   8100:        }
                   8101: }
                   8102: 
                   8103: inline void pcbios_int_13h_04h()
                   8104: {
                   8105:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8106:        
                   8107:        if(REG8(AL) == 0) {
                   8108:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   8109:                m_CF = 1;
                   8110:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   8111:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8112:                m_CF = 1;
                   8113:        } else {
                   8114:                drive_param_t *drive_param = &drive_params[drive_num];
                   8115:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8116:                char dev[64];
                   8117:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   8118:                
1.1.1.60  root     8119:                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     8120:                if(hFile == INVALID_HANDLE_VALUE) {
                   8121:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8122:                        m_CF = 1;
                   8123:                } else {
                   8124:                        UINT32 sector_num = REG8(AL);
                   8125:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   8126:                        UINT32 head = REG8(DH);
                   8127:                        UINT32 sector = REG8(CL) & 0x3f;
                   8128:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   8129:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   8130:                        DWORD dwSize;
                   8131:                        UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
                   8132:                        
                   8133: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8134: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8135: //                             m_CF = 1;
                   8136: //                     } else 
                   8137:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8138:                                REG8(AH) = 0x04; // sector not found/read error
                   8139:                                m_CF = 1;
                   8140:                        } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   8141:                                REG8(AH) = 0x04; // sector not found/read error
                   8142:                                m_CF = 1;
                   8143:                        } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
                   8144:                                REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
                   8145:                                m_CF = 1;
                   8146:                        } else {
                   8147:                                REG8(AH) = 0x00; // successful completion
                   8148:                        }
                   8149:                        free(tmp_buffer);
                   8150:                        CloseHandle(hFile);
                   8151:                }
                   8152:        }
                   8153: }
                   8154: 
                   8155: inline void pcbios_int_13h_08h()
                   8156: {
                   8157:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8158:        
                   8159:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8160:                drive_param_t *drive_param = &drive_params[drive_num];
                   8161:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8162:                
                   8163:                REG16(AX) = 0x0000;
                   8164:                switch(geo->MediaType) {
                   8165:                case F5_360_512:
                   8166:                case F5_320_512:
                   8167:                case F5_320_1024:
                   8168:                case F5_180_512:
                   8169:                case F5_160_512:
                   8170:                        REG8(BL) = 0x01; // 320K/360K disk
                   8171:                        break;
                   8172:                case F5_1Pt2_512:
                   8173:                case F3_1Pt2_512:
                   8174:                case F3_1Pt23_1024:
                   8175:                case F5_1Pt23_1024:
                   8176:                        REG8(BL) = 0x02; // 1.2M disk
                   8177:                        break;
                   8178:                case F3_720_512:
                   8179:                case F3_640_512:
                   8180:                case F5_640_512:
                   8181:                case F5_720_512:
                   8182:                        REG8(BL) = 0x03; // 720K disk
                   8183:                        break;
                   8184:                case F3_1Pt44_512:
                   8185:                        REG8(BL) = 0x04; // 1.44M disk
                   8186:                        break;
                   8187:                case F3_2Pt88_512:
                   8188:                        REG8(BL) = 0x06; // 2.88M disk
                   8189:                        break;
                   8190:                case RemovableMedia:
                   8191:                        REG8(BL) = 0x10; // ATAPI Removable Media Device
                   8192:                        break;
                   8193:                default:
                   8194:                        REG8(BL) = 0x00; // unknown
                   8195:                        break;
                   8196:                }
                   8197:                if(REG8(DL) & 0x80) {
                   8198:                        switch(GetLogicalDrives() & 0x0c) {
                   8199:                        case 0x00: REG8(DL) = 0x00; break;
                   8200:                        case 0x04:
                   8201:                        case 0x08: REG8(DL) = 0x01; break;
                   8202:                        case 0x0c: REG8(DL) = 0x02; break;
                   8203:                        }
                   8204:                } else {
                   8205:                        switch(GetLogicalDrives() & 0x03) {
                   8206:                        case 0x00: REG8(DL) = 0x00; break;
                   8207:                        case 0x01:
                   8208:                        case 0x02: REG8(DL) = 0x01; break;
                   8209:                        case 0x03: REG8(DL) = 0x02; break;
                   8210:                        }
                   8211:                }
                   8212:                REG8(DH) = drive_param->head_num();
                   8213:                int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
                   8214:                int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
                   8215:                REG8(CH) = cyl & 0xff;
                   8216:                REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
                   8217:        } else {
                   8218:                REG8(AH) = 0x07;
                   8219:                m_CF = 1;
                   8220:        }
                   8221: }
                   8222: 
                   8223: inline void pcbios_int_13h_10h()
                   8224: {
                   8225:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8226:        
                   8227:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8228:                REG8(AH) = 0x00; // successful completion
                   8229:        } else {
                   8230:                if(REG8(DL) & 0x80) {
                   8231:                        REG8(AH) = 0xaa; // drive not ready (hard disk)
                   8232:                } else {
                   8233:                        REG8(AH) = 0x80; // timeout (not ready)
                   8234:                }
                   8235:                m_CF = 1;
                   8236:        }
                   8237: }
                   8238: 
                   8239: inline void pcbios_int_13h_15h()
                   8240: {
                   8241:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8242:        
                   8243:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8244:                if(REG8(DL) & 0x80) {
                   8245:                        REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
                   8246:                } else {
                   8247:                        REG8(AH) = 0x03; // hard disk
                   8248:                }
                   8249:        } else {
                   8250:                REG8(AH) = 0x00; // no such drive
                   8251:        }
                   8252: }
                   8253: 
1.1.1.43  root     8254: inline void pcbios_int_13h_41h()
                   8255: {
                   8256:        if(REG16(BX) == 0x55aa) {
                   8257:                // IBM/MS INT 13 Extensions is not installed
                   8258:                REG8(AH) = 0x01;
                   8259:                m_CF = 1;
                   8260:        } else {
                   8261:                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));
                   8262:                REG8(AH) = 0x01;
                   8263:                m_CF = 1;
                   8264:        }
                   8265: }
                   8266: 
1.1.1.25  root     8267: inline void pcbios_int_14h_00h()
                   8268: {
1.1.1.29  root     8269:        if(REG16(DX) < 4) {
1.1.1.25  root     8270:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   8271:                UINT8 selector = sio_read(REG16(DX), 3);
                   8272:                selector &= ~0x3f;
                   8273:                selector |= REG8(AL) & 0x1f;
                   8274:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   8275:                sio_write(REG16(DX), 3, selector | 0x80);
                   8276:                sio_write(REG16(DX), 0, divisor & 0xff);
                   8277:                sio_write(REG16(DX), 1, divisor >> 8);
                   8278:                sio_write(REG16(DX), 3, selector);
                   8279:                REG8(AH) = sio_read(REG16(DX), 5);
                   8280:                REG8(AL) = sio_read(REG16(DX), 6);
                   8281:        } else {
                   8282:                REG8(AH) = 0x80;
                   8283:        }
                   8284: }
                   8285: 
                   8286: inline void pcbios_int_14h_01h()
                   8287: {
1.1.1.29  root     8288:        if(REG16(DX) < 4) {
1.1.1.25  root     8289:                UINT8 selector = sio_read(REG16(DX), 3);
                   8290:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8291:                sio_write(REG16(DX), 0, REG8(AL));
                   8292:                sio_write(REG16(DX), 3, selector);
                   8293:                REG8(AH) = sio_read(REG16(DX), 5);
                   8294:        } else {
                   8295:                REG8(AH) = 0x80;
                   8296:        }
                   8297: }
                   8298: 
                   8299: inline void pcbios_int_14h_02h()
                   8300: {
1.1.1.29  root     8301:        if(REG16(DX) < 4) {
1.1.1.25  root     8302:                UINT8 selector = sio_read(REG16(DX), 3);
                   8303:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8304:                REG8(AL) = sio_read(REG16(DX), 0);
                   8305:                sio_write(REG16(DX), 3, selector);
                   8306:                REG8(AH) = sio_read(REG16(DX), 5);
                   8307:        } else {
                   8308:                REG8(AH) = 0x80;
                   8309:        }
                   8310: }
                   8311: 
                   8312: inline void pcbios_int_14h_03h()
                   8313: {
1.1.1.29  root     8314:        if(REG16(DX) < 4) {
1.1.1.25  root     8315:                REG8(AH) = sio_read(REG16(DX), 5);
                   8316:                REG8(AL) = sio_read(REG16(DX), 6);
                   8317:        } else {
                   8318:                REG8(AH) = 0x80;
                   8319:        }
                   8320: }
                   8321: 
                   8322: inline void pcbios_int_14h_04h()
                   8323: {
1.1.1.29  root     8324:        if(REG16(DX) < 4) {
1.1.1.25  root     8325:                UINT8 selector = sio_read(REG16(DX), 3);
                   8326:                if(REG8(CH) <= 0x03) {
                   8327:                        selector = (selector & ~0x03) | REG8(CH);
                   8328:                }
                   8329:                if(REG8(BL) == 0x00) {
                   8330:                        selector &= ~0x04;
                   8331:                } else if(REG8(BL) == 0x01) {
                   8332:                        selector |= 0x04;
                   8333:                }
                   8334:                if(REG8(BH) == 0x00) {
                   8335:                        selector = (selector & ~0x38) | 0x00;
                   8336:                } else if(REG8(BH) == 0x01) {
                   8337:                        selector = (selector & ~0x38) | 0x08;
                   8338:                } else if(REG8(BH) == 0x02) {
                   8339:                        selector = (selector & ~0x38) | 0x18;
                   8340:                } else if(REG8(BH) == 0x03) {
                   8341:                        selector = (selector & ~0x38) | 0x28;
                   8342:                } else if(REG8(BH) == 0x04) {
                   8343:                        selector = (selector & ~0x38) | 0x38;
                   8344:                }
                   8345:                if(REG8(AL) == 0x00) {
                   8346:                        selector |= 0x40;
                   8347:                } else if(REG8(AL) == 0x01) {
                   8348:                        selector &= ~0x40;
                   8349:                }
                   8350:                if(REG8(CL) <= 0x0b) {
                   8351:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   8352:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   8353:                        sio_write(REG16(DX), 3, selector | 0x80);
                   8354:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   8355:                        sio_write(REG16(DX), 1, divisor >> 8);
                   8356:                }
                   8357:                sio_write(REG16(DX), 3, selector);
                   8358:                REG8(AH) = sio_read(REG16(DX), 5);
                   8359:                REG8(AL) = sio_read(REG16(DX), 6);
                   8360:        } else {
                   8361:                REG8(AH) = 0x80;
                   8362:        }
                   8363: }
                   8364: 
                   8365: inline void pcbios_int_14h_05h()
                   8366: {
1.1.1.29  root     8367:        if(REG16(DX) < 4) {
1.1.1.25  root     8368:                if(REG8(AL) == 0x00) {
                   8369:                        REG8(BL) = sio_read(REG16(DX), 4);
                   8370:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8371:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8372:                } else if(REG8(AL) == 0x01) {
                   8373:                        sio_write(REG16(DX), 4, REG8(BL));
                   8374:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8375:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8376:                } else {
                   8377:                        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));
                   8378:                }
                   8379:        } else {
                   8380:                REG8(AH) = 0x80;
                   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.1.64  root     8435: //     REG8(BL) = 0x01; // standard DBCS DOS (hardware DBCS support)
1.1       root     8436: }
                   8437: 
1.1.1.22  root     8438: inline void pcbios_int_15h_50h()
                   8439: {
                   8440:        switch(REG8(AL)) {
                   8441:        case 0x00:
                   8442:        case 0x01:
                   8443:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   8444:                        REG8(AH) = 0x01; // invalid font type in bh
                   8445:                        m_CF = 1;
1.1.1.27  root     8446:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     8447:                        REG8(AH) = 0x02; // bl not zero
                   8448:                        m_CF = 1;
                   8449:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   8450:                        REG8(AH) = 0x04; // invalid code page
                   8451:                        m_CF = 1;
1.1.1.27  root     8452:                } else if(REG8(AL) == 0x01) {
                   8453:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     8454:                        m_CF = 1;
1.1.1.27  root     8455:                } else {
1.1.1.49  root     8456:                        // dummy font read routine is at fffc:000d
                   8457:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.27  root     8458:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     8459:                        REG16(BX) = 0x000d;
1.1.1.27  root     8460:                        REG8(AH) = 0x00; // success
1.1.1.22  root     8461:                }
                   8462:                break;
                   8463:        default:
                   8464:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   8465:                REG8(AH) = 0x86;
                   8466:                m_CF = 1;
                   8467:                break;
                   8468:        }
                   8469: }
                   8470: 
1.1.1.30  root     8471: inline void pcbios_int_15h_53h()
                   8472: {
                   8473:        switch(REG8(AL)) {
                   8474:        case 0x00:
                   8475:                // APM is not installed
                   8476:                REG8(AH) = 0x86;
                   8477:                m_CF = 1;
                   8478:                break;
                   8479:        default:
                   8480:                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));
                   8481:                REG8(AH) = 0x86;
                   8482:                m_CF = 1;
                   8483:                break;
                   8484:        }
                   8485: }
                   8486: 
1.1.1.43  root     8487: inline void pcbios_int_15h_84h()
                   8488: {
                   8489:        // joystick support (from DOSBox)
                   8490:        switch(REG16(DX)) {
                   8491:        case 0x00:
                   8492:                REG16(AX) = 0x00f0;
                   8493:                REG16(DX) = 0x0201;
                   8494:                m_CF = 1;
                   8495:                break;
                   8496:        case 0x01:
                   8497:                REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   8498:                m_CF = 1;
                   8499:                break;
                   8500:        default:
                   8501:                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));
                   8502:                REG8(AH) = 0x86;
                   8503:                m_CF = 1;
                   8504:                break;
                   8505:        }
                   8506: }
1.1.1.35  root     8507: 
                   8508: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     8509: {
                   8510:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     8511:        UINT32 msec = usec / 1000;
                   8512:        
1.1.1.54  root     8513:        while(msec && !m_exit) {
1.1.1.14  root     8514:                UINT32 tmp = min(msec, 100);
                   8515:                if(msec - tmp < 10) {
                   8516:                        tmp = msec;
                   8517:                }
                   8518:                Sleep(tmp);
                   8519:                msec -= tmp;
                   8520:        }
1.1.1.35  root     8521:        
                   8522: #ifdef USE_SERVICE_THREAD
                   8523:        service_exit = true;
                   8524: #endif
                   8525:        return(0);
                   8526: }
                   8527: 
                   8528: inline void pcbios_int_15h_86h()
                   8529: {
                   8530:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   8531: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8532:                if(!in_service && !in_service_29h) {
                   8533:                        start_service_loop(pcbios_int_15h_86h_thread);
                   8534:                } else {
                   8535: #endif
                   8536:                        pcbios_int_15h_86h_thread(NULL);
                   8537:                        REQUEST_HARDWRE_UPDATE();
                   8538: #ifdef USE_SERVICE_THREAD
                   8539:                }
1.1.1.35  root     8540: #endif
                   8541:        }
1.1       root     8542: }
                   8543: 
                   8544: inline void pcbios_int_15h_87h()
                   8545: {
                   8546:        // copy extended memory (from DOSBox)
                   8547:        int len = REG16(CX) * 2;
1.1.1.3   root     8548:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     8549:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   8550:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   8551:        memcpy(mem + dst, mem + src, len);
                   8552:        REG16(AX) = 0x00;
                   8553: }
                   8554: 
                   8555: inline void pcbios_int_15h_88h()
                   8556: {
1.1.1.17  root     8557:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     8558: }
                   8559: 
                   8560: inline void pcbios_int_15h_89h()
                   8561: {
1.1.1.21  root     8562: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     8563:        // switch to protected mode (from DOSBox)
                   8564:        write_io_byte(0x20, 0x10);
                   8565:        write_io_byte(0x21, REG8(BH));
                   8566:        write_io_byte(0x21, 0x00);
1.1.1.64  root     8567:        write_io_byte(0x21, 0xff);
1.1       root     8568:        write_io_byte(0xa0, 0x10);
                   8569:        write_io_byte(0xa1, REG8(BL));
                   8570:        write_io_byte(0xa1, 0x00);
1.1.1.64  root     8571:        write_io_byte(0xa1, 0xff);
1.1.1.3   root     8572:        i386_set_a20_line(1);
1.1.1.64  root     8573:        m_gdtr.limit = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(SI) + 0x08);
                   8574:        m_gdtr.base  = *(UINT32 *)(mem + SREG_BASE(ES) + REG16(SI) + 0x08 + 0x02) & 0xffffff;
                   8575:        m_idtr.limit = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(SI) + 0x10);
                   8576:        m_idtr.base  = *(UINT32 *)(mem + SREG_BASE(ES) + REG16(SI) + 0x10 + 0x02) & 0xffffff;
1.1.1.3   root     8577: #if defined(HAS_I386)
                   8578:        m_cr[0] |= 1;
                   8579: #else
                   8580:        m_msw |= 1;
                   8581: #endif
1.1.1.64  root     8582:        i386_sreg_load(0x18, DS, NULL);
                   8583:        i386_sreg_load(0x20, ES, NULL);
                   8584:        i386_sreg_load(0x28, SS, NULL);
1.1.1.21  root     8585:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1.1.64  root     8586:        REG16(SP) += 6; // clear stack of interrupt frame
                   8587:        UINT32 flags = i386_get_flags();
                   8588:        flags &= ~0x247fd5; // clear CF,PF,AF,ZF,SF,TF,IF,DF,OF,IOPL,NT,AC,ID
                   8589:        i386_set_flags(flags);
1.1       root     8590:        REG16(AX) = 0x00;
1.1.1.21  root     8591:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     8592: #else
1.1.1.21  root     8593:        // i86/i186/v30: protected mode is not supported
1.1       root     8594:        REG8(AH) = 0x86;
1.1.1.3   root     8595:        m_CF = 1;
1.1       root     8596: #endif
                   8597: }
                   8598: 
1.1.1.21  root     8599: inline void pcbios_int_15h_8ah()
                   8600: {
                   8601:        UINT32 size = MAX_MEM - 0x100000;
                   8602:        REG16(AX) = size & 0xffff;
                   8603:        REG16(DX) = size >> 16;
                   8604: }
                   8605: 
1.1.1.54  root     8606: #ifdef EXT_BIOS_TOP
                   8607: inline void pcbios_int_15h_c1h()
                   8608: {
                   8609:        SREG(ES) = EXT_BIOS_TOP >> 4;
                   8610:        i386_load_segment_descriptor(ES);
                   8611: }
                   8612: #endif
                   8613: 
                   8614: void pcbios_read_from_ps2_mouse(UINT16 *data_1st, UINT16 *data_2nd, UINT16 *data_3rd)
                   8615: {
                   8616:        // from DOSBox DoPS2Callback()
                   8617:        UINT16 mdat = 0x08;
                   8618:        INT16 xdiff = mouse.position.x - mouse.prev_position.x;
                   8619:        INT16 ydiff = mouse.prev_position.y - mouse.position.y;
                   8620:        
1.1.1.59  root     8621: #if 1
                   8622:        if(xdiff > +16) xdiff = +16;
                   8623:        if(xdiff < -16) xdiff = -16;
                   8624:        if(ydiff > +16) ydiff = +16;
                   8625:        if(ydiff < -16) ydiff = -16;
                   8626: #endif
                   8627:        
1.1.1.54  root     8628:        if(mouse.buttons[0].status) {
                   8629:                mdat |= 0x01;
                   8630:        }
                   8631:        if(mouse.buttons[1].status) {
                   8632:                mdat |= 0x02;
                   8633:        }
                   8634:        mouse.prev_position.x = mouse.position.x;
                   8635:        mouse.prev_position.y = mouse.position.y;
1.1.1.59  root     8636:        
1.1.1.54  root     8637:        if((xdiff > 0xff) || (xdiff < -0xff)) {
                   8638:                mdat |= 0x40;   // x overflow
                   8639:        }
                   8640:        if((ydiff > 0xff) || (ydiff < -0xff)) {
                   8641:                mdat |= 0x80;   // y overflow
                   8642:        }
                   8643:        xdiff %= 256;
                   8644:        ydiff %= 256;
                   8645:        if(xdiff < 0) {
                   8646:                xdiff = (0x100 + xdiff);
                   8647:                mdat |= 0x10;
                   8648:        }
                   8649:        if(ydiff < 0) {
                   8650:                ydiff = (0x100 + ydiff);
                   8651:                mdat |= 0x20;
                   8652:        }
                   8653:        *data_1st = (UINT16)mdat;
                   8654:        *data_2nd = (UINT16)(xdiff % 256);
                   8655:        *data_3rd = (UINT16)(ydiff % 256);
                   8656: }
                   8657: 
                   8658: inline void pcbios_int_15h_c2h()
                   8659: {
                   8660:        static UINT8 sampling_rate = 5;
                   8661:        static UINT8 resolution = 2;
                   8662:        static UINT8 scaling = 1;
                   8663:        
                   8664:        switch(REG8(AL)) {
                   8665:        case 0x00:
1.1.1.59  root     8666:                if(REG8(BH) == 0x00) {
1.1.1.64  root     8667:                        if(dwConsoleMode & (ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE)) {
                   8668:                                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_EXTENDED_FLAGS);
                   8669:                        } else {
                   8670:                                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   8671:                        }
1.1.1.59  root     8672:                        pic[1].imr |= 0x10; // disable irq12
                   8673:                        mouse.enabled_ps2 = false;
                   8674:                        REG8(AH) = 0x00; // successful
                   8675:                } else if(REG8(BH) == 0x01) {
1.1.1.64  root     8676:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), (dwConsoleMode | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS) & ~ENABLE_QUICK_EDIT_MODE);
1.1.1.59  root     8677:                        pic[1].imr &= ~0x10; // enable irq12
                   8678:                        mouse.enabled_ps2 = true;
1.1.1.54  root     8679:                        REG8(AH) = 0x00; // successful
                   8680:                } else {
                   8681:                        REG8(AH) = 0x01; // invalid function
                   8682:                        m_CF = 1;
                   8683:                }
                   8684:                break;
                   8685:        case 0x01:
                   8686:                REG8(BH) = 0x00; // device id
                   8687:                REG8(BL) = 0xaa; // mouse
                   8688:        case 0x05:
1.1.1.64  root     8689:                if(dwConsoleMode & (ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE)) {
                   8690:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_EXTENDED_FLAGS);
                   8691:                } else {
                   8692:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   8693:                }
1.1.1.59  root     8694:                pic[1].imr |= 0x10; // disable irq12
                   8695:                mouse.enabled_ps2 = false;
1.1.1.54  root     8696:                sampling_rate = 5;
                   8697:                resolution = 2;
                   8698:                scaling = 1;
                   8699:                REG8(AH) = 0x00; // successful
                   8700:                break;
                   8701:        case 0x02:
                   8702:                sampling_rate = REG8(BH);
                   8703:                REG8(AH) = 0x00; // successful
                   8704:                break;
                   8705:        case 0x03:
                   8706:                resolution = REG8(BH);
                   8707:                REG8(AH) = 0x00; // successful
                   8708:                break;
                   8709:        case 0x04:
                   8710:                REG8(BH) = 0x00; // device id
                   8711:                REG8(AH) = 0x00; // successful
                   8712:                break;
                   8713:        case 0x06:
                   8714:                switch(REG8(BH)) {
                   8715:                case 0x00:
                   8716:                        REG8(BL) = 0x00;
                   8717:                        if(mouse.buttons[1].status) {
                   8718:                                REG8(BL) |= 0x01;
                   8719:                        }
                   8720:                        if(mouse.buttons[0].status) {
                   8721:                                REG8(BL) |= 0x04;
                   8722:                        }
                   8723:                        if(scaling == 2) {
                   8724:                                REG8(BL) |= 0x10;
                   8725:                        }
                   8726:                        REG8(CL) = resolution;
                   8727:                        switch(sampling_rate) {
                   8728:                        case 0:  REG8(DL) =  10; break;
                   8729:                        case 1:  REG8(DL) =  20; break;
                   8730:                        case 2:  REG8(DL) =  40; break;
                   8731:                        case 3:  REG8(DL) =  60; break;
                   8732:                        case 4:  REG8(DL) =  80; break;
                   8733: //                     case 5:  REG8(DL) = 100; break;
                   8734:                        case 6:  REG8(DL) = 200; break;
                   8735:                        default: REG8(DL) = 100; break;
                   8736:                        }
                   8737:                        REG8(AH) = 0x00; // successful
                   8738:                        break;
                   8739:                case 0x01:
                   8740:                case 0x02:
                   8741:                        scaling = REG8(BH);
                   8742:                        REG8(AH) = 0x00; // successful
                   8743:                        break;
                   8744:                default:
                   8745:                        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));
                   8746:                        REG8(AH) = 0x01; // invalid function
                   8747:                        m_CF = 1;
                   8748:                        break;
                   8749:                }
                   8750:                break;
                   8751:        case 0x07: // set device handler addr
                   8752:                mouse.call_addr_ps2.w.l = REG16(BX);
                   8753:                mouse.call_addr_ps2.w.h = SREG(ES);
                   8754:                REG8(AH) = 0x00; // successful
                   8755:                break;
                   8756:        case 0x08:
                   8757:                REG8(AH) = 0x00; // successful
                   8758:                break;
                   8759:        case 0x09:
                   8760:                {
                   8761:                        UINT16 data_1st, data_2nd, data_3rd;
                   8762:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   8763:                        REG8(BL) = (UINT8)(data_1st & 0xff);
                   8764:                        REG8(CL) = (UINT8)(data_2nd & 0xff);
                   8765:                        REG8(DL) = (UINT8)(data_3rd & 0xff);
                   8766:                }
                   8767:                REG8(AH) = 0x00; // successful
                   8768:                break;
                   8769:        default:
                   8770:                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));
                   8771: //             REG8(AH) = 0x86;
                   8772:                REG8(AH) = 0x01; // invalid function
                   8773:                m_CF = 1;
                   8774:                break;
                   8775:        }
                   8776: }
                   8777: 
1.1.1.3   root     8778: #if defined(HAS_I386)
1.1       root     8779: inline void pcbios_int_15h_c9h()
                   8780: {
                   8781:        REG8(AH) = 0x00;
                   8782:        REG8(CH) = cpu_type;
                   8783:        REG8(CL) = cpu_step;
                   8784: }
1.1.1.3   root     8785: #endif
1.1       root     8786: 
                   8787: inline void pcbios_int_15h_cah()
                   8788: {
                   8789:        switch(REG8(AL)) {
1.1.1.22  root     8790:        case 0x00:
1.1       root     8791:                if(REG8(BL) > 0x3f) {
                   8792:                        REG8(AH) = 0x03;
1.1.1.3   root     8793:                        m_CF = 1;
1.1       root     8794:                } else if(REG8(BL) < 0x0e) {
                   8795:                        REG8(AH) = 0x04;
1.1.1.3   root     8796:                        m_CF = 1;
1.1       root     8797:                } else {
1.1.1.8   root     8798:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     8799:                }
                   8800:                break;
1.1.1.22  root     8801:        case 0x01:
1.1       root     8802:                if(REG8(BL) > 0x3f) {
                   8803:                        REG8(AH) = 0x03;
1.1.1.3   root     8804:                        m_CF = 1;
1.1       root     8805:                } else if(REG8(BL) < 0x0e) {
                   8806:                        REG8(AH) = 0x04;
1.1.1.3   root     8807:                        m_CF = 1;
1.1       root     8808:                } else {
1.1.1.8   root     8809:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     8810:                }
                   8811:                break;
                   8812:        default:
1.1.1.22  root     8813:                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     8814:                REG8(AH) = 0x86;
1.1.1.3   root     8815:                m_CF = 1;
1.1       root     8816:                break;
                   8817:        }
                   8818: }
                   8819: 
1.1.1.22  root     8820: inline void pcbios_int_15h_e8h()
1.1.1.17  root     8821: {
1.1.1.22  root     8822:        switch(REG8(AL)) {
                   8823:        case 0x01:
1.1.1.64  root     8824:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x0100000) >> 10);
1.1.1.22  root     8825:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8826:                break;
1.1.1.64  root     8827: #if defined(HAS_I386)
                   8828:        case 0x20:
1.1.1.65! root     8829:                if(REG32(EDX) == 0x534d4150 && REG32(ECX) >= 20) {
1.1.1.64  root     8830:                        if(REG32(EBX) < 3) {
                   8831:                                UINT32 base = 0, len = 0, type = 0;
                   8832:                                switch(REG32(EBX)) {
                   8833:                                case 0:
                   8834:                                        base = 0x000000;
                   8835:                                        len  = MEMORY_END;
                   8836:                                        type = 1;
                   8837:                                        break;
                   8838:                                case 1:
                   8839:                                        base = DUMMY_TOP;
                   8840:                                        len  = 0x100000 - DUMMY_TOP;
                   8841:                                        type = 2;
                   8842:                                        break;
                   8843:                                case 2:
                   8844:                                        base = 0x100000;
                   8845:                                        len  = MAX_MEM - 0x100000;
                   8846:                                        type = 1;
                   8847:                                        break;
                   8848:                                }
                   8849:                                *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 0x00) = base;
                   8850:                                *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 0x04) = 0;
                   8851:                                *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 0x08) = len;
                   8852:                                *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 0x0c) = 0;
                   8853:                                *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 0x10) = type;
                   8854:                                
                   8855:                                if(++REG32(EBX) >= 3) {
                   8856:                                        REG32(EBX) = 0;
                   8857:                                }
                   8858:                                REG32(ECX) = 20;
                   8859:                        } else {
                   8860:                                m_CF = 1;
                   8861:                        }
                   8862:                        REG32(EAX) = 0x534d4150;
                   8863:                        break;
                   8864:                }
                   8865:        case 0x81:
                   8866:                REG32(EAX) = REG32(ECX) = ((min(MAX_MEM, 0x1000000) - 0x0100000) >> 10);
                   8867:                REG32(EBX) = REG32(EDX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8868:                break;
1.1.1.17  root     8869: #endif
1.1.1.22  root     8870:        default:
                   8871:                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));
                   8872:                REG8(AH) = 0x86;
                   8873:                m_CF = 1;
                   8874:                break;
                   8875:        }
                   8876: }
1.1.1.17  root     8877: 
1.1.1.55  root     8878: bool pcbios_is_key_buffer_empty()
                   8879: {
                   8880:        return(*(UINT16 *)(mem + 0x41a) == *(UINT16 *)(mem + 0x41c));
                   8881: }
                   8882: 
1.1.1.51  root     8883: void pcbios_clear_key_buffer()
                   8884: {
                   8885:        key_buf_char->clear();
                   8886:        key_buf_scan->clear();
                   8887:        
                   8888:        // update key buffer
                   8889:        *(UINT16 *)(mem + 0x41a) = *(UINT16 *)(mem + 0x41c); // head = tail
                   8890: }
                   8891: 
                   8892: void pcbios_set_key_buffer(int key_char, int key_scan)
                   8893: {
                   8894:        // update key buffer
                   8895:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8896:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8897:        UINT16 next = tail + 2;
                   8898:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8899:                next = *(UINT16 *)(mem + 0x480);
                   8900:        }
                   8901:        if(next != head) {
                   8902:                *(UINT16 *)(mem + 0x41c) = next;
                   8903:                mem[0x400 + (tail++)] = key_char;
                   8904:                mem[0x400 + (tail++)] = key_scan;
1.1.1.55  root     8905:        } else {
                   8906:                // store to extra key buffer
                   8907:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8908:                        key_buf_char->write(key_char);
                   8909:                        key_buf_scan->write(key_scan);
                   8910:                }
1.1.1.51  root     8911:        }
                   8912: }
                   8913: 
                   8914: bool pcbios_get_key_buffer(int *key_char, int *key_scan)
                   8915: {
                   8916:        // update key buffer
                   8917:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8918:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8919:        UINT16 next = head + 2;
                   8920:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8921:                next = *(UINT16 *)(mem + 0x480);
                   8922:        }
                   8923:        if(head != tail) {
                   8924:                *(UINT16 *)(mem + 0x41a) = next;
1.1.1.55  root     8925:                *key_char = mem[0x400 + (head++)];
                   8926:                *key_scan = mem[0x400 + (head++)];
                   8927:                
                   8928:                // restore from extra key buffer
                   8929:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8930:                        if(!key_buf_char->empty()) {
                   8931:                                pcbios_set_key_buffer(key_buf_char->read(), key_buf_scan->read());
                   8932:                        }
                   8933:                }
                   8934:                return(true);
                   8935:        } else {
                   8936:                *key_char = 0x00;
                   8937:                *key_scan = 0x00;
                   8938:                return(false);
1.1.1.51  root     8939:        }
                   8940: }
                   8941: 
1.1.1.60  root     8942: bool pcbios_check_key_buffer(int *key_char, int *key_scan)
                   8943: {
                   8944:        // do not remove from key buffer
                   8945:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8946:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8947:        if(head != tail) {
                   8948:                *key_char = mem[0x400 + (head++)];
                   8949:                *key_scan = mem[0x400 + (head++)];
                   8950:                return(true);
                   8951:        } else {
                   8952:                *key_char = 0x00;
                   8953:                *key_scan = 0x00;
                   8954:                return(false);
                   8955:        }
                   8956: }
                   8957: 
1.1.1.33  root     8958: void pcbios_update_key_code(bool wait)
1.1       root     8959: {
1.1.1.32  root     8960:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8961: #ifdef USE_SERVICE_THREAD
                   8962:                EnterCriticalSection(&key_buf_crit_sect);
                   8963: #endif
1.1.1.55  root     8964:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     8965: #ifdef USE_SERVICE_THREAD
                   8966:                LeaveCriticalSection(&key_buf_crit_sect);
                   8967: #endif
                   8968:                if(empty) {
1.1.1.32  root     8969:                        if(!update_key_buffer()) {
1.1.1.33  root     8970:                                if(wait) {
1.1.1.32  root     8971:                                        Sleep(10);
                   8972:                                } else {
                   8973:                                        maybe_idle();
                   8974:                                }
1.1.1.14  root     8975:                        }
                   8976:                }
1.1.1.34  root     8977:        }
                   8978:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8979: #ifdef USE_SERVICE_THREAD
                   8980:                EnterCriticalSection(&key_buf_crit_sect);
                   8981: #endif
1.1.1.51  root     8982:                int key_char, key_scan;
                   8983:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8984:                        key_code  = key_char << 0;
                   8985:                        key_code |= key_scan << 8;
1.1.1.35  root     8986:                        key_recv  = 0x0000ffff;
1.1.1.51  root     8987:                }
                   8988:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8989:                        key_code |= key_char << 16;
                   8990:                        key_code |= key_scan << 24;
1.1.1.33  root     8991:                        key_recv |= 0xffff0000;
1.1.1.32  root     8992:                }
1.1.1.35  root     8993: #ifdef USE_SERVICE_THREAD
                   8994:                LeaveCriticalSection(&key_buf_crit_sect);
                   8995: #endif
1.1       root     8996:        }
                   8997: }
                   8998: 
1.1.1.35  root     8999: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     9000: {
1.1.1.54  root     9001:        while(key_recv == 0 && !m_exit) {
1.1.1.33  root     9002:                pcbios_update_key_code(true);
1.1       root     9003:        }
1.1.1.33  root     9004:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   9005:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   9006:                        if(REG8(AH) == 0x10) {
                   9007:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   9008:                        } else {
                   9009:                                key_code = ((key_code >> 16) & 0xff00);
                   9010:                        }
                   9011:                        key_recv >>= 16;
1.1       root     9012:                }
                   9013:        }
                   9014:        REG16(AX) = key_code & 0xffff;
                   9015:        key_code >>= 16;
1.1.1.33  root     9016:        key_recv >>= 16;
1.1.1.35  root     9017:        
                   9018: #ifdef USE_SERVICE_THREAD
                   9019:        service_exit = true;
                   9020: #endif
                   9021:        return(0);
                   9022: }
                   9023: 
                   9024: inline void pcbios_int_16h_00h()
                   9025: {
                   9026: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9027:        if(!in_service && !in_service_29h) {
                   9028:                start_service_loop(pcbios_int_16h_00h_thread);
                   9029:        } else {
                   9030: #endif
                   9031:                pcbios_int_16h_00h_thread(NULL);
                   9032:                REQUEST_HARDWRE_UPDATE();
                   9033: #ifdef USE_SERVICE_THREAD
                   9034:        }
1.1.1.35  root     9035: #endif
1.1       root     9036: }
                   9037: 
                   9038: inline void pcbios_int_16h_01h()
                   9039: {
1.1.1.33  root     9040:        if(key_recv == 0) {
                   9041:                pcbios_update_key_code(false);
1.1.1.5   root     9042:        }
1.1.1.33  root     9043:        if(key_recv != 0) {
                   9044:                UINT32 key_code_tmp = key_code;
                   9045:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   9046:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   9047:                                if(REG8(AH) == 0x11) {
                   9048:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   9049:                                } else {
                   9050:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   9051:                                }
                   9052:                        }
1.1       root     9053:                }
1.1.1.5   root     9054:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     9055: #if defined(HAS_I386)
1.1.1.33  root     9056:                m_ZF = 0;
                   9057: #else
                   9058:                m_ZeroVal = 1;
                   9059: #endif
                   9060:        } else {
                   9061: #if defined(HAS_I386)
                   9062:                m_ZF = 1;
1.1.1.3   root     9063: #else
1.1.1.33  root     9064:                m_ZeroVal = 0;
1.1.1.3   root     9065: #endif
1.1.1.33  root     9066:        }
1.1       root     9067: }
                   9068: 
                   9069: inline void pcbios_int_16h_02h()
                   9070: {
                   9071:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   9072:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   9073:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   9074:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   9075:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   9076:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   9077:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   9078:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   9079: }
                   9080: 
                   9081: inline void pcbios_int_16h_03h()
                   9082: {
                   9083:        static UINT16 status = 0;
                   9084:        
                   9085:        switch(REG8(AL)) {
                   9086:        case 0x05:
                   9087:                status = REG16(BX);
                   9088:                break;
                   9089:        case 0x06:
                   9090:                REG16(BX) = status;
                   9091:                break;
                   9092:        default:
1.1.1.3   root     9093:                m_CF = 1;
1.1       root     9094:                break;
                   9095:        }
                   9096: }
                   9097: 
                   9098: inline void pcbios_int_16h_05h()
                   9099: {
1.1.1.32  root     9100:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     9101: #ifdef USE_SERVICE_THREAD
                   9102:                EnterCriticalSection(&key_buf_crit_sect);
                   9103: #endif
1.1.1.51  root     9104:                pcbios_set_key_buffer(REG8(CL), REG8(CH));
1.1.1.35  root     9105: #ifdef USE_SERVICE_THREAD
                   9106:                LeaveCriticalSection(&key_buf_crit_sect);
                   9107: #endif
1.1.1.32  root     9108:        }
1.1       root     9109:        REG8(AL) = 0x00;
                   9110: }
                   9111: 
1.1.1.60  root     9112: inline void pcbios_int_16h_09h()
                   9113: {
                   9114:        REG8(AL)  = 0x00;
                   9115: //     REG8(AL) |= 0x01;       // INT 16/AX=0300h supported    (set default delay and rate (PCjr and some PS/2))
                   9116: //     REG8(AL) |= 0x02;       // INT 16/AX=0304h supported    (turn off typematic repeat (PCjr and some PS/2))
                   9117:        REG8(AL) |= 0x04;       // INT 16/AX=0305h supported    (set repeat rate and delay (AT,PS))
                   9118:        REG8(AL) |= 0x08;       // INT 16/AX=0306h supported    (get current typematic rate and delay (newer PS/2s))
                   9119:        REG8(AL) |= 0x10;       // INT 16/AH=0Ah supported      (get keyboard id)
                   9120:        REG8(AL) |= 0x20;       // INT 16/AH=10h-12h supported  (enhanced keyboard support)
                   9121: //     REG8(AL) |= 0x40;       // INT 16/AH=20h-22h supported  (122-key keyboard support)
                   9122: //     REG8(AL) |= 0x80;       // reserved
                   9123: }
                   9124: 
                   9125: inline void pcbios_int_16h_0ah()
                   9126: {
                   9127: //     REG16(BX) = 0x41ab;     // MF2 Keyboard (usually in translate mode)
                   9128:        REG16(BX) = 0x83ab;     // MF2 Keyboard (pass-through mode)
                   9129: }
                   9130: 
                   9131: inline void pcbios_int_16h_11h()
                   9132: {
                   9133:        int key_char, key_scan;
                   9134:        
                   9135: #ifdef USE_SERVICE_THREAD
                   9136:        EnterCriticalSection(&key_buf_crit_sect);
                   9137: #endif
                   9138:        if(pcbios_check_key_buffer(&key_char, &key_scan)) {
                   9139:                REG8(AL) = key_char;
                   9140:                REG8(AH) = key_scan;
                   9141: #if defined(HAS_I386)
                   9142:                m_ZF = 0;
                   9143: #else
                   9144:                m_ZeroVal = 1;
                   9145: #endif
                   9146:        } else {
                   9147: #if defined(HAS_I386)
                   9148:                m_ZF = 1;
                   9149: #else
                   9150:                m_ZeroVal = 0;
                   9151: #endif
                   9152:        }
                   9153: #ifdef USE_SERVICE_THREAD
                   9154:        LeaveCriticalSection(&key_buf_crit_sect);
                   9155: #endif
                   9156: }
                   9157: 
1.1       root     9158: inline void pcbios_int_16h_12h()
                   9159: {
                   9160:        pcbios_int_16h_02h();
                   9161:        
                   9162:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   9163:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   9164:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   9165:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   9166:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   9167:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   9168:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   9169:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   9170: }
                   9171: 
                   9172: inline void pcbios_int_16h_13h()
                   9173: {
                   9174:        static UINT16 status = 0;
                   9175:        
                   9176:        switch(REG8(AL)) {
                   9177:        case 0x00:
                   9178:                status = REG16(DX);
                   9179:                break;
                   9180:        case 0x01:
                   9181:                REG16(DX) = status;
                   9182:                break;
                   9183:        default:
1.1.1.22  root     9184:                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     9185:                m_CF = 1;
1.1       root     9186:                break;
                   9187:        }
                   9188: }
                   9189: 
                   9190: inline void pcbios_int_16h_14h()
                   9191: {
                   9192:        static UINT8 status = 0;
                   9193:        
                   9194:        switch(REG8(AL)) {
                   9195:        case 0x00:
                   9196:        case 0x01:
                   9197:                status = REG8(AL);
                   9198:                break;
                   9199:        case 0x02:
                   9200:                REG8(AL) = status;
                   9201:                break;
                   9202:        default:
1.1.1.22  root     9203:                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     9204:                m_CF = 1;
1.1       root     9205:                break;
                   9206:        }
                   9207: }
                   9208: 
1.1.1.24  root     9209: inline void pcbios_int_16h_55h()
                   9210: {
                   9211:        switch(REG8(AL)) {
                   9212:        case 0x00:
                   9213:                // keyboard tsr is not present
                   9214:                break;
                   9215:        case 0xfe:
                   9216:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   9217:                break;
                   9218:        case 0xff:
                   9219:                break;
                   9220:        default:
                   9221:                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));
                   9222:                m_CF = 1;
                   9223:                break;
                   9224:        }
                   9225: }
                   9226: 
1.1.1.30  root     9227: inline void pcbios_int_16h_6fh()
                   9228: {
                   9229:        switch(REG8(AL)) {
                   9230:        case 0x00:
                   9231:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   9232:                break;
                   9233:        default:
                   9234:                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));
                   9235:                m_CF = 1;
                   9236:                break;
                   9237:        }
                   9238: }
                   9239: 
1.1.1.37  root     9240: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   9241: {
                   9242:        UINT8 hi = jis >> 8;
                   9243:        UINT8 lo = jis & 0xff;
                   9244:        
                   9245:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   9246:        hi = (hi - 0x21) / 2 + 0x81;
                   9247:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   9248:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   9249:        
                   9250:        return((hi << 8) + lo);
                   9251: }
                   9252: 
                   9253: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   9254: {
                   9255:        UINT8 hi = sjis >> 8;
                   9256:        UINT8 lo = sjis & 0xff;
                   9257:        
                   9258:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   9259:                return(0x2121);
                   9260:        }
                   9261:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   9262:                return(0x2121);
                   9263:        }
                   9264:        if(hi >= 0xf0 && hi <= 0xf3) {
                   9265:                // gaiji
                   9266:                if(lo >= 0x40 && lo <= 0x7e) {
                   9267:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   9268:                }
                   9269:                if(lo >= 0x80 && lo <= 0x9e) {
                   9270:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   9271:                }
                   9272:                if(lo >= 0x9f && lo <= 0xfc) {
                   9273:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   9274:                }
                   9275:        }
                   9276:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   9277:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   9278:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   9279:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   9280:        
                   9281:        return((hi << 8) + lo);
                   9282: }
                   9283: 
1.1.1.38  root     9284: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   9285: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   9286: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     9287: 
                   9288: void pcbios_printer_out(int c, UINT8 data)
                   9289: {
                   9290:        if(pio[c].conv_mode) {
                   9291:                if(pio[c].sjis_hi != 0) {
                   9292:                        if(!pio[c].jis_mode) {
                   9293:                                printer_out(c, 0x1c);
                   9294:                                printer_out(c, 0x26);
                   9295:                                pio[c].jis_mode = true;
                   9296:                        }
                   9297:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   9298:                        printer_out(c, jis >> 8);
                   9299:                        printer_out(c, jis & 0xff);
                   9300:                        pio[c].sjis_hi = 0;
                   9301:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   9302:                        printer_out(c, data);
                   9303:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   9304:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   9305:                        }
                   9306:                        pio[c].esc_len++;
                   9307:                        
                   9308:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     9309:                        case 0x33: // 1Bh 33h XX
                   9310:                        case 0x4a: // 1Bh 4Ah XX
                   9311:                        case 0x4e: // 1Bh 4Eh XX
                   9312:                        case 0x51: // 1Bh 51h XX
                   9313:                        case 0x55: // 1Bh 55h XX
                   9314:                        case 0x6c: // 1Bh 6Ch XX
                   9315:                        case 0x71: // 1Bh 71h XX
                   9316:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     9317:                                if(pio[c].esc_len == 3) {
                   9318:                                        pio[c].esc_buf[0] = 0x00;
                   9319:                                }
                   9320:                                break;
1.1.1.38  root     9321:                        case 0x24: // 1Bh 24h XX XX
                   9322:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     9323:                                if(pio[c].esc_len == 4) {
                   9324:                                        pio[c].esc_buf[0] = 0x00;
                   9325:                                }
                   9326:                                break;
1.1.1.38  root     9327:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     9328:                                if(pio[c].esc_len >= 3) {
                   9329:                                        switch(pio[c].esc_buf[2]) {
                   9330:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   9331:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   9332:                                                        pio[c].esc_buf[0] = 0x00;
                   9333:                                                }
                   9334:                                                break;
                   9335:                                        case 32: case 33: case 38: case 39: case 40:
                   9336:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   9337:                                                        pio[c].esc_buf[0] = 0x00;
                   9338:                                                }
                   9339:                                                break;
1.1.1.38  root     9340:                                        case 71: case 72: case 73:
                   9341:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   9342:                                                        pio[c].esc_buf[0] = 0x00;
                   9343:                                                }
                   9344:                                                break;
1.1.1.37  root     9345:                                        default:
                   9346:                                                pio[c].esc_buf[0] = 0x00;
                   9347:                                                break;
                   9348:                                        }
                   9349:                                }
                   9350:                                break;
1.1.1.38  root     9351:                        case 0x40: // 1Bh 40h
1.1.1.37  root     9352:                                if(pio[c].jis_mode) {
                   9353:                                        printer_out(c, 0x1c);
                   9354:                                        printer_out(c, 0x2e);
                   9355:                                        pio[c].jis_mode = false;
                   9356:                                }
                   9357:                                pio[c].esc_buf[0] = 0x00;
                   9358:                                break;
1.1.1.38  root     9359:                        case 0x42: // 1Bh 42h data 00h
                   9360:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     9361:                                if(pio[c].esc_len >= 3 && data == 0) {
                   9362:                                        pio[c].esc_buf[0] = 0x00;
                   9363:                                }
                   9364:                                break;
1.1.1.38  root     9365:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     9366:                                if(pio[c].esc_len >= 3 && data != 0) {
                   9367:                                        pio[c].esc_buf[0] = 0x00;
                   9368:                                }
                   9369:                                break;
1.1.1.38  root     9370:                        default: // 1Bh XX
1.1.1.37  root     9371:                                pio[c].esc_buf[0] = 0x00;
                   9372:                                break;
                   9373:                        }
                   9374:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   9375:                        printer_out(c, data);
                   9376:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   9377:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   9378:                        }
                   9379:                        pio[c].esc_len++;
                   9380:                        
                   9381:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     9382:                        case 0x21: // 1Ch 21h XX
                   9383:                        case 0x2d: // 1Ch 2Dh XX
                   9384:                        case 0x57: // 1Ch 57h XX
                   9385:                        case 0x6b: // 1Ch 6Bh XX
                   9386:                        case 0x72: // 1Ch 72h XX
                   9387:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     9388:                                if(pio[c].esc_len == 3) {
                   9389:                                        pio[c].esc_buf[0] = 0x00;
                   9390:                                }
                   9391:                                break;
1.1.1.38  root     9392:                        case 0x26: // 1Ch 26h
1.1.1.37  root     9393:                                pio[c].jis_mode = true;
                   9394:                                pio[c].esc_buf[0] = 0x00;
                   9395:                                break;
1.1.1.38  root     9396:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     9397:                                pio[c].jis_mode = false;
                   9398:                                pio[c].esc_buf[0] = 0x00;
                   9399:                                break;
1.1.1.38  root     9400:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     9401:                                if(pio[c].esc_len == 76) {
                   9402:                                        pio[c].esc_buf[0] = 0x00;
                   9403:                                }
                   9404:                                break;
1.1.1.38  root     9405:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     9406:                                if(pio[c].esc_len == 6) {
                   9407:                                        pio[c].esc_buf[0] = 0x00;
                   9408:                                }
                   9409:                                break;
1.1.1.38  root     9410:                        case 0x53: // 1Ch 53h XX XX
                   9411:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     9412:                                if(pio[c].esc_len == 4) {
                   9413:                                        pio[c].esc_buf[0] = 0x00;
                   9414:                                }
                   9415:                                break;
1.1.1.38  root     9416:                        default: // 1Ch XX
1.1.1.37  root     9417:                                pio[c].esc_buf[0] = 0x00;
                   9418:                                break;
                   9419:                        }
                   9420:                } else if(data == 0x1b || data == 0x1c) {
                   9421:                        printer_out(c, data);
                   9422:                        pio[c].esc_buf[0] = data;
                   9423:                        pio[c].esc_len = 1;
                   9424:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   9425:                        pio[c].sjis_hi = data;
                   9426:                } else {
                   9427:                        if(pio[c].jis_mode) {
                   9428:                                printer_out(c, 0x1c);
                   9429:                                printer_out(c, 0x2e);
                   9430:                                pio[c].jis_mode = false;
                   9431:                        }
                   9432:                        printer_out(c, data);
                   9433:                }
                   9434:        } else {
                   9435:                if(pio[c].jis_mode) {
                   9436:                        printer_out(c, 0x1c);
                   9437:                        printer_out(c, 0x2e);
                   9438:                        pio[c].jis_mode = false;
                   9439:                }
                   9440:                printer_out(c, data);
                   9441:        }
                   9442: }
                   9443: 
                   9444: inline void pcbios_int_17h_00h()
                   9445: {
                   9446:        if(REG16(DX) < 3) {
                   9447:                pcbios_printer_out(REG16(DX), REG8(AL));
                   9448:                REG8(AH) = 0xd0;
                   9449:        }
                   9450: }
                   9451: 
                   9452: inline void pcbios_int_17h_01h()
                   9453: {
                   9454:        if(REG16(DX) < 3) {
                   9455:                REG8(AH) = 0xd0;
                   9456:        }
                   9457: }
                   9458: 
                   9459: inline void pcbios_int_17h_02h()
                   9460: {
                   9461:        if(REG16(DX) < 3) {
                   9462:                REG8(AH) = 0xd0;
                   9463:        }
                   9464: }
                   9465: 
                   9466: inline void pcbios_int_17h_03h()
                   9467: {
                   9468:        switch(REG8(AL)) {
                   9469:        case 0x00:
                   9470:                if(REG16(DX) < 3) {
                   9471:                        if(pio[REG16(DX)].jis_mode) {
                   9472:                                printer_out(REG16(DX), 0x1c);
                   9473:                                printer_out(REG16(DX), 0x2e);
                   9474:                                pio[REG16(DX)].jis_mode = false;
                   9475:                        }
                   9476:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   9477:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   9478:                        }
                   9479:                        REG16(CX) = 0x0000;
                   9480:                        REG8(AH) = 0xd0;
                   9481:                }
                   9482:                break;
                   9483:        default:
                   9484:                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));
                   9485:                break;
                   9486:        }
                   9487: }
                   9488: 
                   9489: inline void pcbios_int_17h_50h()
                   9490: {
                   9491:        switch(REG8(AL)) {
                   9492:        case 0x00:
                   9493:                if(REG16(DX) < 3) {
                   9494:                        if(REG16(BX) = 0x0001) {
                   9495:                                pio[REG16(DX)].conv_mode = false;
                   9496:                                REG8(AL) = 0x00;
                   9497:                        } else if(REG16(BX) = 0x0051) {
                   9498:                                pio[REG16(DX)].conv_mode = true;
                   9499:                                REG8(AL) = 0x00;
                   9500:                        } else {
                   9501:                                REG8(AL) = 0x01;
                   9502:                        }
                   9503:                } else {
                   9504:                        REG8(AL) = 0x02;
                   9505:                }
                   9506:                break;
                   9507:        case 0x01:
                   9508:                if(REG16(DX) < 3) {
                   9509:                        if(pio[REG16(DX)].conv_mode) {
                   9510:                                REG16(BX) = 0x0051;
                   9511:                        } else {
                   9512:                                REG16(BX) = 0x0001;
                   9513:                        }
                   9514:                        REG8(AL) = 0x00;
                   9515:                } else {
                   9516:                        REG8(AL) = 0x02;
                   9517:                }
                   9518:                break;
                   9519:        default:
                   9520:                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));
                   9521:                break;
                   9522:        }
                   9523: }
                   9524: 
                   9525: inline void pcbios_int_17h_51h()
                   9526: {
                   9527:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   9528:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   9529:        } else {
                   9530:                REG16(DX) = 0x0000;
                   9531:        }
                   9532: }
                   9533: 
                   9534: inline void pcbios_int_17h_52h()
                   9535: {
                   9536:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   9537:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   9538:        } else {
                   9539:                REG16(DX) = 0x0000;
                   9540:        }
                   9541: }
                   9542: 
                   9543: inline void pcbios_int_17h_84h()
                   9544: {
                   9545:        if(REG16(DX) < 3) {
                   9546:                if(pio[REG16(DX)].jis_mode) {
                   9547:                        printer_out(REG16(DX), 0x1c);
                   9548:                        printer_out(REG16(DX), 0x2e);
                   9549:                        pio[REG16(DX)].jis_mode = false;
                   9550:                }
                   9551:                printer_out(REG16(DX), REG8(AL));
                   9552:                REG8(AH) = 0xd0;
                   9553:        }
                   9554: }
                   9555: 
                   9556: inline void pcbios_int_17h_85h()
                   9557: {
                   9558:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   9559: }
                   9560: 
1.1       root     9561: inline void pcbios_int_1ah_00h()
                   9562: {
1.1.1.19  root     9563:        pcbios_update_daily_timer_counter(timeGetTime());
                   9564:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   9565:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   9566:        REG8(AL) = mem[0x470];
                   9567:        mem[0x470] = 0;
1.1       root     9568: }
                   9569: 
                   9570: inline int to_bcd(int t)
                   9571: {
                   9572:        int u = (t % 100) / 10;
                   9573:        return (u << 4) | (t % 10);
                   9574: }
                   9575: 
                   9576: inline void pcbios_int_1ah_02h()
                   9577: {
                   9578:        SYSTEMTIME time;
                   9579:        
                   9580:        GetLocalTime(&time);
                   9581:        REG8(CH) = to_bcd(time.wHour);
                   9582:        REG8(CL) = to_bcd(time.wMinute);
                   9583:        REG8(DH) = to_bcd(time.wSecond);
                   9584:        REG8(DL) = 0x00;
                   9585: }
                   9586: 
                   9587: inline void pcbios_int_1ah_04h()
                   9588: {
                   9589:        SYSTEMTIME time;
                   9590:        
                   9591:        GetLocalTime(&time);
                   9592:        REG8(CH) = to_bcd(time.wYear / 100);
                   9593:        REG8(CL) = to_bcd(time.wYear);
                   9594:        REG8(DH) = to_bcd(time.wMonth);
                   9595:        REG8(DL) = to_bcd(time.wDay);
                   9596: }
                   9597: 
                   9598: inline void pcbios_int_1ah_0ah()
                   9599: {
                   9600:        SYSTEMTIME time;
                   9601:        FILETIME file_time;
                   9602:        WORD dos_date, dos_time;
                   9603:        
                   9604:        GetLocalTime(&time);
                   9605:        SystemTimeToFileTime(&time, &file_time);
                   9606:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   9607:        REG16(CX) = dos_date;
                   9608: }
                   9609: 
                   9610: // msdos system call
                   9611: 
1.1.1.43  root     9612: inline void msdos_int_21h_56h(int lfn);
                   9613: 
1.1       root     9614: inline void msdos_int_21h_00h()
                   9615: {
1.1.1.3   root     9616:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     9617: }
                   9618: 
1.1.1.35  root     9619: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     9620: {
                   9621:        REG8(AL) = msdos_getche();
1.1.1.33  root     9622:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9623:        
1.1.1.35  root     9624: #ifdef USE_SERVICE_THREAD
                   9625:        service_exit = true;
                   9626: #endif
                   9627:        return(0);
                   9628: }
                   9629: 
                   9630: inline void msdos_int_21h_01h()
                   9631: {
                   9632: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9633:        if(!in_service && !in_service_29h &&
1.1.1.58  root     9634:           *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     9635:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9636:                // msdos_putch() will be used in this service
                   9637:                // if int 29h is hooked, run this service in main thread to call int 29h
                   9638:                start_service_loop(msdos_int_21h_01h_thread);
                   9639:        } else {
                   9640: #endif
                   9641:                msdos_int_21h_01h_thread(NULL);
                   9642:                REQUEST_HARDWRE_UPDATE();
                   9643: #ifdef USE_SERVICE_THREAD
                   9644:        }
1.1.1.35  root     9645: #endif
1.1       root     9646: }
                   9647: 
                   9648: inline void msdos_int_21h_02h()
                   9649: {
1.1.1.33  root     9650:        UINT8 data = REG8(DL);
                   9651:        msdos_putch(data);
                   9652:        REG8(AL) = data;
                   9653:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9654: }
                   9655: 
                   9656: inline void msdos_int_21h_03h()
                   9657: {
                   9658:        REG8(AL) = msdos_aux_in();
                   9659: }
                   9660: 
                   9661: inline void msdos_int_21h_04h()
                   9662: {
                   9663:        msdos_aux_out(REG8(DL));
                   9664: }
                   9665: 
                   9666: inline void msdos_int_21h_05h()
                   9667: {
                   9668:        msdos_prn_out(REG8(DL));
                   9669: }
                   9670: 
                   9671: inline void msdos_int_21h_06h()
                   9672: {
                   9673:        if(REG8(DL) == 0xff) {
                   9674:                if(msdos_kbhit()) {
                   9675:                        REG8(AL) = msdos_getch();
1.1.1.3   root     9676: #if defined(HAS_I386)
                   9677:                        m_ZF = 0;
                   9678: #else
                   9679:                        m_ZeroVal = 1;
                   9680: #endif
1.1       root     9681:                } else {
                   9682:                        REG8(AL) = 0;
1.1.1.3   root     9683: #if defined(HAS_I386)
                   9684:                        m_ZF = 1;
                   9685: #else
                   9686:                        m_ZeroVal = 0;
                   9687: #endif
1.1.1.14  root     9688:                        maybe_idle();
1.1       root     9689:                }
                   9690:        } else {
1.1.1.33  root     9691:                UINT8 data = REG8(DL);
                   9692:                msdos_putch(data);
                   9693:                REG8(AL) = data;
1.1       root     9694:        }
                   9695: }
                   9696: 
1.1.1.35  root     9697: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     9698: {
                   9699:        REG8(AL) = msdos_getch();
1.1.1.26  root     9700:        
1.1.1.35  root     9701: #ifdef USE_SERVICE_THREAD
                   9702:        service_exit = true;
                   9703: #endif
                   9704:        return(0);
1.1       root     9705: }
                   9706: 
1.1.1.35  root     9707: inline void msdos_int_21h_07h()
                   9708: {
                   9709: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9710:        if(!in_service && !in_service_29h) {
                   9711:                start_service_loop(msdos_int_21h_07h_thread);
                   9712:        } else {
                   9713: #endif
                   9714:                msdos_int_21h_07h_thread(NULL);
                   9715:                REQUEST_HARDWRE_UPDATE();
                   9716: #ifdef USE_SERVICE_THREAD
                   9717:        }
1.1.1.35  root     9718: #endif
                   9719: }
                   9720: 
                   9721: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     9722: {
                   9723:        REG8(AL) = msdos_getch();
1.1.1.33  root     9724:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9725:        
1.1.1.35  root     9726: #ifdef USE_SERVICE_THREAD
                   9727:        service_exit = true;
                   9728: #endif
                   9729:        return(0);
                   9730: }
                   9731: 
                   9732: inline void msdos_int_21h_08h()
                   9733: {
                   9734: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9735:        if(!in_service && !in_service_29h) {
                   9736:                start_service_loop(msdos_int_21h_08h_thread);
                   9737:        } else {
                   9738: #endif
                   9739:                msdos_int_21h_08h_thread(NULL);
                   9740:                REQUEST_HARDWRE_UPDATE();
                   9741: #ifdef USE_SERVICE_THREAD
                   9742:        }
1.1.1.35  root     9743: #endif
1.1       root     9744: }
                   9745: 
                   9746: inline void msdos_int_21h_09h()
                   9747: {
1.1.1.21  root     9748:        msdos_stdio_reopen();
                   9749:        
1.1.1.20  root     9750:        process_t *process = msdos_process_info_get(current_psp);
                   9751:        int fd = msdos_psp_get_file_table(1, current_psp);
                   9752:        
1.1.1.14  root     9753:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   9754:        int len = 0;
1.1       root     9755:        
1.1.1.14  root     9756:        while(str[len] != '$' && len < 0x10000) {
                   9757:                len++;
                   9758:        }
1.1.1.20  root     9759:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9760:                // stdout is redirected to file
1.1.1.20  root     9761:                msdos_write(fd, str, len);
1.1       root     9762:        } else {
                   9763:                for(int i = 0; i < len; i++) {
1.1.1.14  root     9764:                        msdos_putch(str[i]);
1.1       root     9765:                }
                   9766:        }
1.1.1.33  root     9767:        REG8(AL) = '$';
                   9768:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9769: }
                   9770: 
1.1.1.35  root     9771: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     9772: {
1.1.1.3   root     9773:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     9774:        int max = mem[ofs] - 1;
                   9775:        UINT8 *buf = mem + ofs + 2;
                   9776:        int chr, p = 0;
                   9777:        
                   9778:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     9779:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     9780:                        p = 0;
1.1.1.33  root     9781:                        msdos_putch(0x03);
                   9782:                        msdos_putch(0x0d);
                   9783:                        msdos_putch(0x0a);
1.1.1.26  root     9784:                        break;
1.1.1.33  root     9785:                } else if(ctrl_break_pressed) {
                   9786:                        // skip this byte
1.1.1.26  root     9787:                } else if(chr == 0x00) {
1.1       root     9788:                        // skip 2nd byte
                   9789:                        msdos_getch();
                   9790:                } else if(chr == 0x08) {
                   9791:                        // back space
                   9792:                        if(p > 0) {
                   9793:                                p--;
1.1.1.20  root     9794:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     9795:                                        msdos_putch(0x08);
                   9796:                                        msdos_putch(0x08);
                   9797:                                        msdos_putch(0x20);
                   9798:                                        msdos_putch(0x20);
                   9799:                                        msdos_putch(0x08);
                   9800:                                        msdos_putch(0x08);
1.1.1.36  root     9801:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9802:                                        p--;
                   9803:                                        msdos_putch(0x08);
                   9804:                                        msdos_putch(0x08);
                   9805:                                        msdos_putch(0x20);
                   9806:                                        msdos_putch(0x20);
                   9807:                                        msdos_putch(0x08);
                   9808:                                        msdos_putch(0x08);
1.1.1.34  root     9809:                                } else {
                   9810:                                        msdos_putch(0x08);
                   9811:                                        msdos_putch(0x20);
                   9812:                                        msdos_putch(0x08);
                   9813:                                }
                   9814:                        }
                   9815:                } else if(chr == 0x1b) {
                   9816:                        // escape
                   9817:                        while(p > 0) {
                   9818:                                p--;
                   9819:                                if(msdos_ctrl_code_check(buf[p])) {
                   9820:                                        msdos_putch(0x08);
                   9821:                                        msdos_putch(0x08);
                   9822:                                        msdos_putch(0x20);
                   9823:                                        msdos_putch(0x20);
                   9824:                                        msdos_putch(0x08);
                   9825:                                        msdos_putch(0x08);
1.1.1.20  root     9826:                                } else {
1.1.1.34  root     9827:                                        msdos_putch(0x08);
                   9828:                                        msdos_putch(0x20);
                   9829:                                        msdos_putch(0x08);
1.1.1.20  root     9830:                                }
1.1       root     9831:                        }
                   9832:                } else if(p < max) {
                   9833:                        buf[p++] = chr;
                   9834:                        msdos_putch(chr);
                   9835:                }
                   9836:        }
                   9837:        buf[p] = 0x0d;
                   9838:        mem[ofs + 1] = p;
1.1.1.33  root     9839:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9840:        
1.1.1.35  root     9841: #ifdef USE_SERVICE_THREAD
                   9842:        service_exit = true;
                   9843: #endif
                   9844:        return(0);
                   9845: }
                   9846: 
                   9847: inline void msdos_int_21h_0ah()
                   9848: {
                   9849:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   9850: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9851:                if(!in_service && !in_service_29h &&
1.1.1.58  root     9852:                   *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     9853:                   *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9854:                        // msdos_putch() will be used in this service
                   9855:                        // if int 29h is hooked, run this service in main thread to call int 29h
                   9856:                        start_service_loop(msdos_int_21h_0ah_thread);
                   9857:                } else {
                   9858: #endif
                   9859:                        msdos_int_21h_0ah_thread(NULL);
                   9860:                        REQUEST_HARDWRE_UPDATE();
                   9861: #ifdef USE_SERVICE_THREAD
                   9862:                }
1.1.1.35  root     9863: #endif
                   9864:        }
1.1       root     9865: }
                   9866: 
                   9867: inline void msdos_int_21h_0bh()
                   9868: {
                   9869:        if(msdos_kbhit()) {
                   9870:                REG8(AL) = 0xff;
                   9871:        } else {
                   9872:                REG8(AL) = 0x00;
1.1.1.14  root     9873:                maybe_idle();
1.1       root     9874:        }
1.1.1.33  root     9875:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9876: }
                   9877: 
                   9878: inline void msdos_int_21h_0ch()
                   9879: {
                   9880:        // clear key buffer
1.1.1.21  root     9881:        msdos_stdio_reopen();
                   9882:        
1.1.1.20  root     9883:        process_t *process = msdos_process_info_get(current_psp);
                   9884:        int fd = msdos_psp_get_file_table(0, current_psp);
                   9885:        
                   9886:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9887:                // stdin is redirected to file
                   9888:        } else {
                   9889:                while(msdos_kbhit()) {
                   9890:                        msdos_getch();
                   9891:                }
                   9892:        }
                   9893:        
                   9894:        switch(REG8(AL)) {
                   9895:        case 0x01:
                   9896:                msdos_int_21h_01h();
                   9897:                break;
                   9898:        case 0x06:
                   9899:                msdos_int_21h_06h();
                   9900:                break;
                   9901:        case 0x07:
                   9902:                msdos_int_21h_07h();
                   9903:                break;
                   9904:        case 0x08:
                   9905:                msdos_int_21h_08h();
                   9906:                break;
                   9907:        case 0x0a:
                   9908:                msdos_int_21h_0ah();
                   9909:                break;
                   9910:        default:
1.1.1.48  root     9911:                // the buffer is flushed but no input is attempted
1.1       root     9912:                break;
                   9913:        }
                   9914: }
                   9915: 
                   9916: inline void msdos_int_21h_0dh()
                   9917: {
                   9918: }
                   9919: 
                   9920: inline void msdos_int_21h_0eh()
                   9921: {
                   9922:        if(REG8(DL) < 26) {
                   9923:                _chdrive(REG8(DL) + 1);
                   9924:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9925:                msdos_sda_update(current_psp);
1.1       root     9926:        }
                   9927:        REG8(AL) = 26; // zdrive
                   9928: }
                   9929: 
1.1.1.14  root     9930: inline void msdos_int_21h_0fh()
                   9931: {
                   9932:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9933:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9934:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     9935:        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     9936:        
1.1.1.14  root     9937:        if(hFile == INVALID_HANDLE_VALUE) {
                   9938:                REG8(AL) = 0xff;
                   9939:        } else {
                   9940:                REG8(AL) = 0;
                   9941:                fcb->current_block = 0;
                   9942:                fcb->record_size = 128;
                   9943:                fcb->file_size = GetFileSize(hFile, NULL);
                   9944:                fcb->handle = hFile;
                   9945:        }
                   9946: }
                   9947: 
                   9948: inline void msdos_int_21h_10h()
                   9949: {
                   9950:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9951:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9952:        
                   9953:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9954: }
                   9955: 
1.1       root     9956: inline void msdos_int_21h_11h()
                   9957: {
1.1.1.3   root     9958:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9959:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9960:        
                   9961:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9962:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9963:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9964:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45  root     9965:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     9966:        WIN32_FIND_DATAA fd;
1.1       root     9967:        
1.1.1.13  root     9968:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9969:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9970:                FindClose(dtainfo->find_handle);
                   9971:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9972:        }
                   9973:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9974:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9975:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9976:        
1.1.1.14  root     9977:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9978:                dtainfo->allowable_mask &= ~8;
1.1       root     9979:        }
1.1.1.60  root     9980:        if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14  root     9981:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9982:                      !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     9983:                        if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     9984:                                FindClose(dtainfo->find_handle);
                   9985:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9986:                                break;
                   9987:                        }
                   9988:                }
                   9989:        }
1.1.1.13  root     9990:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9991:                if(ext_fcb->flag == 0xff) {
                   9992:                        ext_find->flag = 0xff;
                   9993:                        memset(ext_find->reserved, 0, 5);
                   9994:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9995:                }
                   9996:                find->drive = _getdrive();
1.1.1.13  root     9997:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9998:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9999:                find->nt_res = 0;
                   10000:                msdos_find_file_conv_local_time(&fd);
                   10001:                find->create_time_ms = 0;
                   10002:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   10003:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   10004:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   10005:                find->cluster_hi = find->cluster_lo = 0;
                   10006:                find->file_size = fd.nFileSizeLow;
                   10007:                REG8(AL) = 0x00;
1.1.1.14  root     10008:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     10009:                if(ext_fcb->flag == 0xff) {
                   10010:                        ext_find->flag = 0xff;
                   10011:                        memset(ext_find->reserved, 0, 5);
                   10012:                        ext_find->attribute = 8;
                   10013:                }
                   10014:                find->drive = _getdrive();
                   10015:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   10016:                find->attribute = 8;
                   10017:                find->nt_res = 0;
                   10018:                msdos_find_file_conv_local_time(&fd);
                   10019:                find->create_time_ms = 0;
                   10020:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   10021:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   10022:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   10023:                find->cluster_hi = find->cluster_lo = 0;
                   10024:                find->file_size = 0;
1.1.1.14  root     10025:                dtainfo->allowable_mask &= ~8;
1.1       root     10026:                REG8(AL) = 0x00;
                   10027:        } else {
                   10028:                REG8(AL) = 0xff;
                   10029:        }
                   10030: }
                   10031: 
                   10032: inline void msdos_int_21h_12h()
                   10033: {
1.1.1.3   root     10034:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     10035: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     10036:        
                   10037:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     10038:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10039:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   10040:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.60  root     10041:        WIN32_FIND_DATAA fd;
1.1       root     10042:        
1.1.1.13  root     10043:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   10044:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60  root     10045:                if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14  root     10046:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     10047:                              !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     10048:                                if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     10049:                                        FindClose(dtainfo->find_handle);
                   10050:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10051:                                        break;
                   10052:                                }
                   10053:                        }
                   10054:                } else {
1.1.1.13  root     10055:                        FindClose(dtainfo->find_handle);
                   10056:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10057:                }
                   10058:        }
1.1.1.13  root     10059:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     10060:                if(ext_fcb->flag == 0xff) {
                   10061:                        ext_find->flag = 0xff;
                   10062:                        memset(ext_find->reserved, 0, 5);
                   10063:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   10064:                }
                   10065:                find->drive = _getdrive();
1.1.1.13  root     10066:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     10067:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   10068:                find->nt_res = 0;
                   10069:                msdos_find_file_conv_local_time(&fd);
                   10070:                find->create_time_ms = 0;
                   10071:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   10072:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   10073:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   10074:                find->cluster_hi = find->cluster_lo = 0;
                   10075:                find->file_size = fd.nFileSizeLow;
                   10076:                REG8(AL) = 0x00;
1.1.1.14  root     10077:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     10078:                if(ext_fcb->flag == 0xff) {
                   10079:                        ext_find->flag = 0xff;
                   10080:                        memset(ext_find->reserved, 0, 5);
                   10081:                        ext_find->attribute = 8;
                   10082:                }
                   10083:                find->drive = _getdrive();
                   10084:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   10085:                find->attribute = 8;
                   10086:                find->nt_res = 0;
                   10087:                msdos_find_file_conv_local_time(&fd);
                   10088:                find->create_time_ms = 0;
                   10089:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   10090:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   10091:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   10092:                find->cluster_hi = find->cluster_lo = 0;
                   10093:                find->file_size = 0;
1.1.1.14  root     10094:                dtainfo->allowable_mask &= ~8;
1.1       root     10095:                REG8(AL) = 0x00;
                   10096:        } else {
                   10097:                REG8(AL) = 0xff;
                   10098:        }
                   10099: }
                   10100: 
                   10101: inline void msdos_int_21h_13h()
                   10102: {
1.1.1.3   root     10103:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     10104:                REG8(AL) = 0xff;
                   10105:        } else {
                   10106:                REG8(AL) = 0x00;
                   10107:        }
                   10108: }
                   10109: 
1.1.1.16  root     10110: inline void msdos_int_21h_14h()
                   10111: {
                   10112:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10113:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10114:        process_t *process = msdos_process_info_get(current_psp);
                   10115:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10116:        DWORD num = 0;
                   10117:        
                   10118:        memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.65! root     10119:        SetFilePointer(fcb->handle, fcb->record_size * (fcb->current_block * 128 + fcb->cur_record), NULL, FILE_BEGIN);
        !          10120:        
1.1.1.16  root     10121:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   10122:                REG8(AL) = 1;
                   10123:        } else {
1.1.1.65! root     10124:                if(++fcb->cur_record >= 128) {
        !          10125:                        fcb->current_block += fcb->cur_record / 128;
        !          10126:                        fcb->cur_record %= 128;
        !          10127:                }
1.1.1.16  root     10128:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   10129:        }
                   10130: }
                   10131: 
                   10132: inline void msdos_int_21h_15h()
1.1.1.14  root     10133: {
                   10134:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10135:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     10136:        process_t *process = msdos_process_info_get(current_psp);
                   10137:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10138:        DWORD num = 0;
1.1.1.14  root     10139:        
1.1.1.65! root     10140:        SetFilePointer(fcb->handle, fcb->record_size * (fcb->current_block * 128 + fcb->cur_record), NULL, FILE_BEGIN);
        !          10141:        WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
        !          10142:        fcb->file_size = GetFileSize(fcb->handle, NULL);
        !          10143:        
        !          10144:        if(num != fcb->record_size) {
1.1.1.16  root     10145:                REG8(AL) = 1;
                   10146:        } else {
1.1.1.65! root     10147:                if(++fcb->cur_record >= 128) {
        !          10148:                        fcb->current_block += fcb->cur_record / 128;
        !          10149:                        fcb->cur_record %= 128;
        !          10150:                }
        !          10151:                REG8(AL) = 0;
1.1.1.16  root     10152:        }
                   10153: }
                   10154: 
                   10155: inline void msdos_int_21h_16h()
                   10156: {
                   10157:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10158:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     10159:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     10160:        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     10161:        
1.1.1.14  root     10162:        if(hFile == INVALID_HANDLE_VALUE) {
                   10163:                REG8(AL) = 0xff;
                   10164:        } else {
                   10165:                REG8(AL) = 0;
                   10166:                fcb->current_block = 0;
                   10167:                fcb->record_size = 128;
                   10168:                fcb->file_size = 0;
                   10169:                fcb->handle = hFile;
                   10170:        }
                   10171: }
                   10172: 
1.1.1.16  root     10173: inline void msdos_int_21h_17h()
                   10174: {
                   10175:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10176:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45  root     10177: //     const char *path_src = msdos_fcb_path(fcb_src);
                   10178:        char path_src[MAX_PATH];
                   10179:        strcpy(path_src, msdos_fcb_path(fcb_src));
                   10180:        
1.1.1.65! root     10181:        fcb_t *fcb_dst = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16 + (ext_fcb_src->flag == 0xff ? 7 : 0));
1.1.1.45  root     10182: //     const char *path_dst = msdos_fcb_path(fcb_dst);
                   10183:        char path_dst[MAX_PATH];
                   10184:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     10185:        
                   10186:        if(rename(path_src, path_dst)) {
                   10187:                REG8(AL) = 0xff;
                   10188:        } else {
                   10189:                REG8(AL) = 0;
                   10190:        }
                   10191: }
                   10192: 
1.1       root     10193: inline void msdos_int_21h_18h()
                   10194: {
                   10195:        REG8(AL) = 0x00;
                   10196: }
                   10197: 
                   10198: inline void msdos_int_21h_19h()
                   10199: {
                   10200:        REG8(AL) = _getdrive() - 1;
                   10201: }
                   10202: 
                   10203: inline void msdos_int_21h_1ah()
                   10204: {
                   10205:        process_t *process = msdos_process_info_get(current_psp);
                   10206:        
                   10207:        process->dta.w.l = REG16(DX);
1.1.1.3   root     10208:        process->dta.w.h = SREG(DS);
1.1.1.23  root     10209:        msdos_sda_update(current_psp);
1.1       root     10210: }
                   10211: 
                   10212: inline void msdos_int_21h_1bh()
                   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:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   10219:                REG8(AL) = dpb->highest_sector_num + 1;
                   10220:                REG16(CX) = dpb->bytes_per_sector;
                   10221:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     10222:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     10223:        } else {
                   10224:                REG8(AL) = 0xff;
1.1.1.3   root     10225:                m_CF = 1;
1.1       root     10226:        }
                   10227: 
                   10228: }
                   10229: 
                   10230: inline void msdos_int_21h_1ch()
                   10231: {
1.1.1.41  root     10232:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     10233:        UINT16 seg, ofs;
                   10234:        
                   10235:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10236:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   10237:                REG8(AL) = dpb->highest_sector_num + 1;
                   10238:                REG16(CX) = dpb->bytes_per_sector;
                   10239:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     10240:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     10241:        } else {
                   10242:                REG8(AL) = 0xff;
1.1.1.3   root     10243:                m_CF = 1;
1.1       root     10244:        }
                   10245: 
                   10246: }
                   10247: 
                   10248: inline void msdos_int_21h_1dh()
                   10249: {
                   10250:        REG8(AL) = 0;
                   10251: }
                   10252: 
                   10253: inline void msdos_int_21h_1eh()
                   10254: {
                   10255:        REG8(AL) = 0;
                   10256: }
                   10257: 
                   10258: inline void msdos_int_21h_1fh()
                   10259: {
                   10260:        int drive_num = _getdrive() - 1;
                   10261:        UINT16 seg, ofs;
                   10262:        
                   10263:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10264:                REG8(AL) = 0;
1.1.1.3   root     10265:                SREG(DS) = seg;
                   10266:                i386_load_segment_descriptor(DS);
1.1       root     10267:                REG16(BX) = ofs;
                   10268:        } else {
                   10269:                REG8(AL) = 0xff;
1.1.1.3   root     10270:                m_CF = 1;
1.1       root     10271:        }
                   10272: }
                   10273: 
                   10274: inline void msdos_int_21h_20h()
                   10275: {
                   10276:        REG8(AL) = 0;
                   10277: }
                   10278: 
1.1.1.14  root     10279: inline void msdos_int_21h_21h()
                   10280: {
                   10281:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10282:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.65! root     10283:        UINT32 rec = (fcb->record_size >= 64) ? fcb->rand_record & 0xffffff : fcb->rand_record;
1.1.1.14  root     10284:        
1.1.1.65! root     10285:        fcb->current_block = rec / 128;
        !          10286:        fcb->cur_record = rec % 128;
        !          10287:        
        !          10288:        if(SetFilePointer(fcb->handle, fcb->record_size * rec, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1.1.14  root     10289:                REG8(AL) = 1;
                   10290:        } else {
                   10291:                process_t *process = msdos_process_info_get(current_psp);
                   10292:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10293:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     10294:                DWORD num = 0;
1.1.1.14  root     10295:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   10296:                        REG8(AL) = 1;
                   10297:                } else {
1.1.1.16  root     10298:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     10299:                }
                   10300:        }
                   10301: }
                   10302: 
                   10303: inline void msdos_int_21h_22h()
                   10304: {
                   10305:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10306:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.65! root     10307:        UINT32 rec = (fcb->record_size >= 64) ? fcb->rand_record & 0xffffff : fcb->rand_record;
1.1.1.14  root     10308:        
1.1.1.65! root     10309:        fcb->current_block = rec / 128;
        !          10310:        fcb->cur_record = rec % 128;
        !          10311:        
        !          10312:        if(SetFilePointer(fcb->handle, fcb->record_size * rec, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
        !          10313:                REG8(AL) = 1;
1.1.1.14  root     10314:        } else {
                   10315:                process_t *process = msdos_process_info_get(current_psp);
                   10316:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     10317:                DWORD num = 0;
1.1.1.14  root     10318:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   10319:                fcb->file_size = GetFileSize(fcb->handle, NULL);
1.1.1.16  root     10320:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     10321:        }
                   10322: }
                   10323: 
1.1.1.16  root     10324: inline void msdos_int_21h_23h()
                   10325: {
                   10326:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10327:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     10328:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     10329:        HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16  root     10330:        
                   10331:        if(hFile == INVALID_HANDLE_VALUE) {
                   10332:                REG8(AL) = 0xff;
                   10333:        } else {
                   10334:                UINT32 size = GetFileSize(hFile, NULL);
1.1.1.65! root     10335:                UINT32 rec = size / fcb->record_size + ((size % fcb->record_size) != 0);
        !          10336:                fcb->rand_record = (fcb->record_size >= 64) ? (fcb->rand_record & 0xff000000) | (rec & 0xffffff) : rec;
        !          10337:                CloseHandle(hFile);
1.1.1.16  root     10338:                REG8(AL) = 0;
                   10339:        }
                   10340: }
                   10341: 
                   10342: inline void msdos_int_21h_24h()
                   10343: {
                   10344:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10345:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.65! root     10346:        UINT32 rec = fcb->current_block * 128 + fcb->cur_record;
1.1.1.16  root     10347:        
1.1.1.65! root     10348:        fcb->rand_record = (fcb->record_size >= 64) ? (fcb->rand_record & 0xff000000) | (rec & 0xffffff) : rec;
1.1.1.16  root     10349: }
                   10350: 
1.1       root     10351: inline void msdos_int_21h_25h()
                   10352: {
                   10353:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     10354:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     10355: }
                   10356: 
                   10357: inline void msdos_int_21h_26h()
                   10358: {
                   10359:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   10360:        
                   10361:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
1.1.1.48  root     10362:        psp->first_mcb = REG16(DX) + (PSP_SIZE >> 4);
1.1       root     10363:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   10364:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   10365:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   10366:        psp->parent_psp = 0;
                   10367: }
                   10368: 
1.1.1.16  root     10369: inline void msdos_int_21h_27h()
                   10370: {
                   10371:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10372:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.65! root     10373:        UINT32 rec = (fcb->record_size >= 64) ? fcb->rand_record & 0xffffff : fcb->rand_record;
1.1.1.16  root     10374:        
1.1.1.65! root     10375:        if(SetFilePointer(fcb->handle, fcb->record_size * rec, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1.1.16  root     10376:                REG8(AL) = 1;
1.1.1.65! root     10377:        } else if(REG16(CX) == 0) {
        !          10378:                REG8(AL) = 0;
1.1.1.16  root     10379:        } else {
                   10380:                process_t *process = msdos_process_info_get(current_psp);
                   10381:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.65! root     10382:                UINT32 len = fcb->record_size * REG16(CX);
        !          10383:                memset(mem + dta_laddr, 0, len);
1.1.1.16  root     10384:                DWORD num = 0;
1.1.1.65! root     10385:                if(!ReadFile(fcb->handle, mem + dta_laddr, len, &num, NULL) || num == 0) {
1.1.1.16  root     10386:                        REG8(AL) = 1;
                   10387:                } else {
1.1.1.65! root     10388:                        UINT16 nrec = num / fcb->record_size + ((num % fcb->record_size) != 0);
        !          10389:                        rec += nrec;
        !          10390:                        fcb->rand_record = (fcb->record_size >= 64) ? (fcb->rand_record & 0xff000000) | (rec & 0xffffff) : rec;
        !          10391:                        REG8(AL) = (num == len) ? 0 : 3;
        !          10392:                        REG16(CX) = nrec;
1.1.1.16  root     10393:                }
                   10394:        }
1.1.1.65! root     10395:        fcb->current_block = rec / 128;
        !          10396:        fcb->cur_record = rec % 128;
1.1.1.16  root     10397: }
                   10398: 
                   10399: inline void msdos_int_21h_28h()
                   10400: {
                   10401:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10402:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.65! root     10403:        UINT32 rec = (fcb->record_size >= 64) ? fcb->rand_record & 0xffffff : fcb->rand_record;
1.1.1.16  root     10404:        
1.1.1.65! root     10405:        if(SetFilePointer(fcb->handle, fcb->record_size * rec, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
        !          10406:                REG8(AL) = 1;
        !          10407:        } else if(REG16(CX) == 0) {
        !          10408:                if(!SetEndOfFile(fcb->handle)) {
        !          10409:                        REG8(AL) = 1;
        !          10410:                } else {
        !          10411:                        fcb->file_size = GetFileSize(fcb->handle, NULL);
        !          10412:                        REG8(AL) = 0;
        !          10413:                }
1.1.1.16  root     10414:        } else {
                   10415:                process_t *process = msdos_process_info_get(current_psp);
                   10416:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.65! root     10417:                UINT32 len = fcb->record_size * REG16(CX);
1.1.1.16  root     10418:                DWORD num = 0;
1.1.1.65! root     10419:                WriteFile(fcb->handle, mem + dta_laddr, len, &num, NULL);
1.1.1.16  root     10420:                fcb->file_size = GetFileSize(fcb->handle, NULL);
1.1.1.65! root     10421:                UINT16 nrec = num / fcb->record_size + ((num % fcb->record_size) != 0);
        !          10422:                rec += nrec;
        !          10423:                fcb->rand_record = (fcb->record_size >= 64) ? (fcb->rand_record & 0xff000000) | (rec & 0xffffff) : rec;
        !          10424:                REG8(AL) = (num == len) ? 0 : 1;
        !          10425:                REG16(CX) = nrec;
1.1.1.16  root     10426:        }
1.1.1.65! root     10427:        fcb->current_block = rec / 128;
        !          10428:        fcb->cur_record = rec % 128;
1.1.1.16  root     10429: }
                   10430: 
1.1       root     10431: inline void msdos_int_21h_29h()
                   10432: {
1.1.1.20  root     10433:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   10434:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     10435:        UINT8 drv = 0;
                   10436:        char sep_chars[] = ":.;,=+";
                   10437:        char end_chars[] = "\\<>|/\"[]";
                   10438:        char spc_chars[] = " \t";
                   10439:        
1.1.1.20  root     10440:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   10441:        buffer[1023] = 0;
                   10442:        memset(name, 0x20, sizeof(name));
                   10443:        memset(ext, 0x20, sizeof(ext));
                   10444:        
1.1       root     10445:        if(REG8(AL) & 1) {
1.1.1.20  root     10446:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   10447:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     10448:                        ofs++;
                   10449:                }
                   10450:        }
1.1.1.20  root     10451:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     10452:        
1.1.1.24  root     10453:        if(buffer[ofs + 1] == ':') {
                   10454:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   10455:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     10456:                        ofs += 2;
1.1.1.24  root     10457:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10458:                                ofs++;
                   10459:                        }
                   10460:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   10461:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     10462:                        ofs += 2;
1.1.1.24  root     10463:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10464:                                ofs++;
                   10465:                        }
1.1       root     10466:                }
                   10467:        }
1.1.1.20  root     10468:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10469:                UINT8 c = buffer[ofs];
                   10470:                if(is_kanji) {
                   10471:                        is_kanji = 0;
                   10472:                } else if(msdos_lead_byte_check(c)) {
                   10473:                        is_kanji = 1;
                   10474:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10475:                        break;
                   10476:                } else if(c >= 'a' && c <= 'z') {
                   10477:                        c -= 0x20;
                   10478:                }
                   10479:                ofs++;
                   10480:                name[i] = c;
                   10481:        }
1.1.1.20  root     10482:        if(buffer[ofs] == '.') {
1.1       root     10483:                ofs++;
1.1.1.20  root     10484:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10485:                        UINT8 c = buffer[ofs];
                   10486:                        if(is_kanji) {
                   10487:                                is_kanji = 0;
                   10488:                        } else if(msdos_lead_byte_check(c)) {
                   10489:                                is_kanji = 1;
                   10490:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10491:                                break;
                   10492:                        } else if(c >= 'a' && c <= 'z') {
                   10493:                                c -= 0x20;
                   10494:                        }
                   10495:                        ofs++;
                   10496:                        ext[i] = c;
                   10497:                }
                   10498:        }
1.1.1.20  root     10499:        int si = REG16(SI) + ofs;
1.1.1.3   root     10500:        int ds = SREG(DS);
1.1       root     10501:        while(si > 0xffff) {
                   10502:                si -= 0x10;
                   10503:                ds++;
                   10504:        }
                   10505:        REG16(SI) = si;
1.1.1.3   root     10506:        SREG(DS) = ds;
                   10507:        i386_load_segment_descriptor(DS);
1.1       root     10508:        
1.1.1.3   root     10509:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     10510:        if(!(REG8(AL) & 2) || drv != 0) {
                   10511:                fcb[0] = drv;
                   10512:        }
                   10513:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   10514:                memcpy(fcb + 1, name, 8);
                   10515:        }
                   10516:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   10517:                memcpy(fcb + 9, ext, 3);
                   10518:        }
                   10519:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     10520:                if(fcb[i] == '*') {
                   10521:                        found_star = 1;
                   10522:                }
                   10523:                if(found_star) {
                   10524:                        fcb[i] = '?';
                   10525:                }
                   10526:        }
1.1.1.20  root     10527:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     10528:                if(fcb[i] == '*') {
                   10529:                        found_star = 1;
                   10530:                }
                   10531:                if(found_star) {
                   10532:                        fcb[i] = '?';
                   10533:                }
                   10534:        }
                   10535:        
1.1.1.44  root     10536:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     10537:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   10538:                        REG8(AL) = 0x01;
1.1.1.20  root     10539:                } else {
                   10540:                        REG8(AL) = 0x00;
1.1       root     10541:                }
                   10542:        } else {
                   10543:                REG8(AL) = 0xff;
                   10544:        }
                   10545: }
                   10546: 
                   10547: inline void msdos_int_21h_2ah()
                   10548: {
                   10549:        SYSTEMTIME sTime;
                   10550:        
                   10551:        GetLocalTime(&sTime);
                   10552:        REG16(CX) = sTime.wYear;
                   10553:        REG8(DH) = (UINT8)sTime.wMonth;
                   10554:        REG8(DL) = (UINT8)sTime.wDay;
                   10555:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   10556: }
                   10557: 
                   10558: inline void msdos_int_21h_2bh()
                   10559: {
1.1.1.14  root     10560:        REG8(AL) = 0xff;
1.1       root     10561: }
                   10562: 
                   10563: inline void msdos_int_21h_2ch()
                   10564: {
                   10565:        SYSTEMTIME sTime;
                   10566:        
                   10567:        GetLocalTime(&sTime);
                   10568:        REG8(CH) = (UINT8)sTime.wHour;
                   10569:        REG8(CL) = (UINT8)sTime.wMinute;
                   10570:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     10571:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     10572: }
                   10573: 
                   10574: inline void msdos_int_21h_2dh()
                   10575: {
                   10576:        REG8(AL) = 0x00;
                   10577: }
                   10578: 
                   10579: inline void msdos_int_21h_2eh()
                   10580: {
                   10581:        process_t *process = msdos_process_info_get(current_psp);
                   10582:        
                   10583:        process->verify = REG8(AL);
                   10584: }
                   10585: 
                   10586: inline void msdos_int_21h_2fh()
                   10587: {
                   10588:        process_t *process = msdos_process_info_get(current_psp);
                   10589:        
                   10590:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     10591:        SREG(ES) = process->dta.w.h;
                   10592:        i386_load_segment_descriptor(ES);
1.1       root     10593: }
                   10594: 
                   10595: inline void msdos_int_21h_30h()
                   10596: {
                   10597:        // Version Flag / OEM
1.1.1.27  root     10598:        if(REG8(AL) == 0x01) {
1.1.1.29  root     10599: #ifdef SUPPORT_HMA
                   10600:                REG16(BX) = 0x0000;
                   10601: #else
                   10602:                REG16(BX) = 0x1000; // DOS is in HMA
                   10603: #endif
1.1       root     10604:        } else {
1.1.1.27  root     10605:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   10606:                // but this is not correct on Windows 98 SE
                   10607: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   10608:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     10609:        }
1.1.1.27  root     10610:        REG16(CX) = 0x0000;
1.1.1.30  root     10611:        REG8(AL) = dos_major_version;   // 7
                   10612:        REG8(AH) = dos_minor_version;   // 10
1.1       root     10613: }
                   10614: 
                   10615: inline void msdos_int_21h_31h()
                   10616: {
1.1.1.29  root     10617:        try {
                   10618:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10619:        } catch(...) {
                   10620:                // recover the broken mcb
                   10621:                int mcb_seg = current_psp - 1;
                   10622:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     10623:                
1.1.1.29  root     10624:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     10625:                        mcb->mz = 'M';
                   10626:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   10627:                        
1.1.1.29  root     10628:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     10629:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     10630:                        } else {
1.1.1.39  root     10631:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     10632:                        }
                   10633:                } else {
                   10634:                        mcb->mz = 'Z';
1.1.1.30  root     10635:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     10636:                }
                   10637:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10638:        }
1.1       root     10639:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   10640: }
                   10641: 
                   10642: inline void msdos_int_21h_32h()
                   10643: {
                   10644:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   10645:        UINT16 seg, ofs;
                   10646:        
                   10647:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10648:                REG8(AL) = 0;
1.1.1.3   root     10649:                SREG(DS) = seg;
                   10650:                i386_load_segment_descriptor(DS);
1.1       root     10651:                REG16(BX) = ofs;
                   10652:        } else {
                   10653:                REG8(AL) = 0xff;
1.1.1.3   root     10654:                m_CF = 1;
1.1       root     10655:        }
                   10656: }
                   10657: 
                   10658: inline void msdos_int_21h_33h()
                   10659: {
                   10660:        char path[MAX_PATH];
1.1.1.48  root     10661:        char drive = 3; // C:
1.1       root     10662:        
                   10663:        switch(REG8(AL)) {
                   10664:        case 0x00:
1.1.1.33  root     10665:                REG8(DL) = ctrl_break_checking;
1.1       root     10666:                break;
                   10667:        case 0x01:
1.1.1.33  root     10668:                ctrl_break_checking = REG8(DL);
                   10669:                break;
                   10670:        case 0x02:
                   10671:                {
                   10672:                        UINT8 old = ctrl_break_checking;
                   10673:                        ctrl_break_checking = REG8(DL);
                   10674:                        REG8(DL) = old;
                   10675:                }
                   10676:                break;
                   10677:        case 0x03:
                   10678:        case 0x04:
                   10679:                // DOS 4.0+ - Unused
1.1       root     10680:                break;
                   10681:        case 0x05:
1.1.1.60  root     10682:                if(GetSystemDirectoryA(path, MAX_PATH) != 0) {
1.1.1.48  root     10683:                        if(path[0] >= 'a' && path[0] <= 'z') {
                   10684:                                drive = path[0] - 'a' + 1;
                   10685:                        } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   10686:                                drive = path[0] - 'A' + 1;
                   10687:                        }
1.1       root     10688:                }
1.1.1.48  root     10689:                REG8(DL) = (UINT8)drive;
1.1       root     10690:                break;
                   10691:        case 0x06:
1.1.1.2   root     10692:                // MS-DOS version (7.10)
1.1       root     10693:                REG8(BL) = 7;
1.1.1.2   root     10694:                REG8(BH) = 10;
1.1       root     10695:                REG8(DL) = 0;
1.1.1.29  root     10696: #ifdef SUPPORT_HMA
                   10697:                REG8(DH) = 0x00;
                   10698: #else
                   10699:                REG8(DH) = 0x10; // DOS is in HMA
                   10700: #endif
1.1       root     10701:                break;
1.1.1.6   root     10702:        case 0x07:
                   10703:                if(REG8(DL) == 0) {
                   10704:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   10705:                } else if(REG8(DL) == 1) {
                   10706:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   10707:                }
                   10708:                break;
1.1       root     10709:        default:
1.1.1.22  root     10710:                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     10711: //             REG16(AX) = 0x01;
                   10712: //             m_CF = 1;
                   10713:                REG8(AL) = 0xff;
1.1       root     10714:                break;
                   10715:        }
                   10716: }
                   10717: 
1.1.1.23  root     10718: inline void msdos_int_21h_34h()
                   10719: {
                   10720:        SREG(ES) = SDA_TOP >> 4;
                   10721:        i386_load_segment_descriptor(ES);
1.1.1.65! root     10722:        REG16(BX) = offsetof(sda_t, indos_flag);
1.1.1.23  root     10723: }
                   10724: 
1.1       root     10725: inline void msdos_int_21h_35h()
                   10726: {
                   10727:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     10728:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   10729:        i386_load_segment_descriptor(ES);
1.1       root     10730: }
                   10731: 
                   10732: inline void msdos_int_21h_36h()
                   10733: {
                   10734:        struct _diskfree_t df = {0};
                   10735:        
                   10736:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   10737:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   10738:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     10739:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   10740:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     10741:        } else {
                   10742:                REG16(AX) = 0xffff;
                   10743:        }
                   10744: }
                   10745: 
                   10746: inline void msdos_int_21h_37h()
                   10747: {
1.1.1.22  root     10748:        static UINT8 dev_flag = 0xff;
1.1       root     10749:        
                   10750:        switch(REG8(AL)) {
                   10751:        case 0x00:
1.1.1.22  root     10752:                {
                   10753:                        process_t *process = msdos_process_info_get(current_psp);
                   10754:                        REG8(AL) = 0x00;
                   10755:                        REG8(DL) = process->switchar;
                   10756:                }
1.1       root     10757:                break;
                   10758:        case 0x01:
1.1.1.22  root     10759:                {
                   10760:                        process_t *process = msdos_process_info_get(current_psp);
                   10761:                        REG8(AL) = 0x00;
                   10762:                        process->switchar = REG8(DL);
1.1.1.23  root     10763:                        msdos_sda_update(current_psp);
1.1.1.22  root     10764:                }
                   10765:                break;
                   10766:        case 0x02:
                   10767:                REG8(DL) = dev_flag;
                   10768:                break;
                   10769:        case 0x03:
                   10770:                dev_flag = REG8(DL);
                   10771:                break;
                   10772:        case 0xd0:
                   10773:        case 0xd1:
                   10774:        case 0xd2:
                   10775:        case 0xd3:
                   10776:        case 0xd4:
                   10777:        case 0xd5:
                   10778:        case 0xd6:
                   10779:        case 0xd7:
                   10780:        case 0xdc:
                   10781:        case 0xdd:
                   10782:        case 0xde:
                   10783:        case 0xdf:
1.1.1.48  root     10784:                // DIET v1.43e
                   10785: //             REG16(AX) = 1;
                   10786:                REG8(AL) = 0xff;
1.1       root     10787:                break;
                   10788:        default:
1.1.1.22  root     10789:                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     10790: //             REG16(AX) = 1;
                   10791:                REG8(AL) = 0xff;
1.1       root     10792:                break;
                   10793:        }
                   10794: }
                   10795: 
1.1.1.52  root     10796: int get_country_info(country_info_t *ci, LCID locale = LOCALE_USER_DEFAULT)
1.1.1.17  root     10797: {
                   10798:        char LCdata[80];
                   10799:        
1.1.1.19  root     10800:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.60  root     10801:        GetLocaleInfoA(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     10802:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.60  root     10803:        GetLocaleInfoA(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10804:        ci->currency_format = *LCdata - '0';
1.1.1.60  root     10805:        GetLocaleInfoA(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10806:        ci->date_format = *LCdata - '0';
1.1.1.60  root     10807:        GetLocaleInfoA(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10808:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.60  root     10809:        GetLocaleInfoA(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10810:        *ci->date_sep = *LCdata;
1.1.1.60  root     10811:        GetLocaleInfoA(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     10812:        *ci->dec_sep = *LCdata;
1.1.1.60  root     10813:        GetLocaleInfoA(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     10814:        *ci->list_sep = *LCdata;
1.1.1.60  root     10815:        GetLocaleInfoA(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     10816:        *ci->thou_sep = *LCdata;
1.1.1.60  root     10817:        GetLocaleInfoA(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     10818:        *ci->time_sep = *LCdata;
1.1.1.60  root     10819:        GetLocaleInfoA(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     10820:        if(strchr(LCdata, 'H') != NULL) {
                   10821:                ci->time_format = 1;
                   10822:        }
1.1.1.49  root     10823:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffc:000a
                   10824:        ci->case_map.w.h = DUMMY_TOP >> 4;
1.1.1.60  root     10825:        GetLocaleInfoA(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     10826:        return atoi(LCdata);
                   10827: }
                   10828: 
1.1.1.42  root     10829: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   10830: {
                   10831:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   10832: }
                   10833: 
1.1.1.43  root     10834: void set_country_info(country_info_t *ci, int size)
                   10835: {
                   10836:        char LCdata[80];
                   10837:        
                   10838:        if(size >= 0x00 + 2) {
                   10839:                memset(LCdata, 0, sizeof(LCdata));
                   10840:                *LCdata = '0' + ci->date_format;
1.1.1.60  root     10841:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
1.1.1.43  root     10842:        }
                   10843:        if(size >= 0x02 + 5) {
                   10844:                memset(LCdata, 0, sizeof(LCdata));
                   10845:                memcpy(LCdata, &ci->currency_symbol, 4);
1.1.1.60  root     10846:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
1.1.1.43  root     10847:        }
                   10848:        if(size >= 0x07 + 2) {
                   10849:                memset(LCdata, 0, sizeof(LCdata));
                   10850:                *LCdata = *ci->thou_sep;
1.1.1.60  root     10851:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
1.1.1.43  root     10852:        }
                   10853:        if(size >= 0x09 + 2) {
                   10854:                memset(LCdata, 0, sizeof(LCdata));
                   10855:                *LCdata = *ci->dec_sep;
1.1.1.60  root     10856:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
1.1.1.43  root     10857:        }
                   10858:        if(size >= 0x0b + 2) {
                   10859:                memset(LCdata, 0, sizeof(LCdata));
                   10860:                *LCdata = *ci->date_sep;
1.1.1.60  root     10861:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
1.1.1.43  root     10862:        }
                   10863:        if(size >= 0x0d + 2) {
                   10864:                memset(LCdata, 0, sizeof(LCdata));
                   10865:                *LCdata = *ci->time_sep;
1.1.1.60  root     10866:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
1.1.1.43  root     10867:        }
                   10868:        if(size >= 0x0f + 1) {
                   10869:                memset(LCdata, 0, sizeof(LCdata));
                   10870:                *LCdata = '0' + ci->currency_format;
1.1.1.60  root     10871:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
1.1.1.43  root     10872:        }
                   10873:        if(size >= 0x10 + 1) {
                   10874:                sprintf(LCdata, "%d", ci->currency_dec_digits);
1.1.1.60  root     10875:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
1.1.1.43  root     10876:        }
                   10877:        if(size >= 0x11 + 1) {
                   10878:                // FIXME: is time format always H/h:mm:ss ???
                   10879:                if(ci->time_format & 1) {
                   10880:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   10881:                } else {
                   10882:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   10883:                }
1.1.1.60  root     10884:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
1.1.1.43  root     10885:        }
                   10886:        if(size >= 0x12 + 4) {
                   10887:                // 12h  DWORD   address of case map routine
                   10888:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   10889:        }
                   10890:        if(size >= 0x16 + 2) {
                   10891:                memset(LCdata, 0, sizeof(LCdata));
                   10892:                *LCdata = *ci->list_sep;
1.1.1.60  root     10893:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
1.1.1.43  root     10894:        }
                   10895: }
                   10896: 
1.1.1.42  root     10897: #ifndef SUBLANG_SWAHILI
                   10898:        #define SUBLANG_SWAHILI 0x01
                   10899: #endif
                   10900: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10901:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10902: #endif
                   10903: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10904:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10905: #endif
                   10906: #ifndef LANG_BANGLA
                   10907:        #define LANG_BANGLA 0x45
                   10908: #endif
                   10909: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10910:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10911: #endif
                   10912: 
                   10913: static const struct {
                   10914:        int code;
                   10915:        USHORT usPrimaryLanguage;
                   10916:        USHORT usSubLanguage;
                   10917: } country_table[] = {
                   10918:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10919:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10920:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10921:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10922:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10923:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10924: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10925: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10926: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10927: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10928: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10929:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10930:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10931: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10932:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10933: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10934:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10935:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10936:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10937:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10938:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10939:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10940:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10941: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10942: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10943:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10944:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10945:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10946:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10947:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10948: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10949: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10950: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10951:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10952: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10953: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10954: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10955: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10956:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10957:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10958:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10959: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10960:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10961:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10962:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10963:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10964:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10965:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10966:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10967:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10968:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10969:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10970:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10971:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10972:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10973: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10974:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10975:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10976:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10977:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10978:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10979:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10980:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10981:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10982:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10983:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10984: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10985:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10986: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10987:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10988:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10989:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10990:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10991:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10992:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10993:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10994:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10995: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10996:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10997: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10998: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10999:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   11000: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   11001:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   11002:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   11003:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   11004:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   11005:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   11006:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   11007:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   11008: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   11009: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   11010:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   11011: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   11012:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   11013:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   11014:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   11015:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   11016: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   11017: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   11018: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   11019:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   11020:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   11021:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   11022:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   11023:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   11024:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   11025:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   11026:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   11027:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   11028:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   11029:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   11030:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   11031:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   11032:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   11033:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   11034:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   11035:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   11036:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   11037:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   11038:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   11039:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     11040: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     11041:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   11042: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   11043:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   11044:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   11045:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   11046:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   11047:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   11048:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   11049:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   11050:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   11051:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   11052:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   11053:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   11054:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   11055:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   11056:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   11057:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   11058:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   11059:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   11060:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   11061:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   11062:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   11063:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   11064:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   11065:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   11066:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   11067:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   11068: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   11069:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   11070:        {-1, 0, 0},
                   11071: };
                   11072: 
1.1.1.14  root     11073: inline void msdos_int_21h_38h()
                   11074: {
                   11075:        switch(REG8(AL)) {
                   11076:        case 0x00:
1.1.1.19  root     11077:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     11078:                break;
                   11079:        default:
1.1.1.42  root     11080:                for(int i = 0;; i++) {
                   11081:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   11082:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   11083:                                break;
                   11084:                        } else if(country_table[i].code == -1) {
                   11085: //                             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));
                   11086: //                             REG16(AX) = 2;
                   11087: //                             m_CF = 1;
1.1.1.48  root     11088:                                // get current coutry info
1.1.1.42  root     11089:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   11090:                                break;
                   11091:                        }
                   11092:                }
1.1.1.14  root     11093:                break;
                   11094:        }
                   11095: }
                   11096: 
1.1       root     11097: inline void msdos_int_21h_39h(int lfn)
                   11098: {
1.1.1.3   root     11099:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     11100:                REG16(AX) = errno;
1.1.1.3   root     11101:                m_CF = 1;
1.1       root     11102:        }
                   11103: }
                   11104: 
                   11105: inline void msdos_int_21h_3ah(int lfn)
                   11106: {
1.1.1.3   root     11107:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     11108:                REG16(AX) = errno;
1.1.1.3   root     11109:                m_CF = 1;
1.1       root     11110:        }
                   11111: }
                   11112: 
                   11113: inline void msdos_int_21h_3bh(int lfn)
                   11114: {
1.1.1.45  root     11115:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     11116:        
                   11117:        if(_chdir(path)) {
1.1.1.17  root     11118:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     11119:                m_CF = 1;
1.1.1.44  root     11120:        } else {
                   11121:                int drv = _getdrive() - 1;
                   11122:                if(path[1] == ':') {
                   11123:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   11124:                                drv = path[0] - 'A';
                   11125:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   11126:                                drv = path[0] - 'a';
                   11127:                        }
                   11128:                }
                   11129:                msdos_cds_update(drv, path);
1.1       root     11130:        }
                   11131: }
                   11132: 
                   11133: inline void msdos_int_21h_3ch()
                   11134: {
1.1.1.45  root     11135:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1.1.60  root     11136:        int attr = GetFileAttributesA(path);
1.1.1.37  root     11137:        int fd = -1;
                   11138:        int sio_port = 0;
                   11139:        int lpt_port = 0;
1.1       root     11140:        
1.1.1.45  root     11141:        if(msdos_is_device_path(path)) {
                   11142:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     11143:        } else {
                   11144:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11145:        }
                   11146:        if(fd != -1) {
                   11147:                if(attr == -1) {
                   11148:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   11149:                }
1.1.1.60  root     11150:                SetFileAttributesA(path, attr);
1.1       root     11151:                REG16(AX) = fd;
1.1.1.45  root     11152:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     11153:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11154:        } else {
                   11155:                REG16(AX) = errno;
1.1.1.3   root     11156:                m_CF = 1;
1.1       root     11157:        }
                   11158: }
                   11159: 
                   11160: inline void msdos_int_21h_3dh()
                   11161: {
1.1.1.45  root     11162:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11163:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     11164:        int fd = -1;
                   11165:        int sio_port = 0;
                   11166:        int lpt_port = 0;
1.1       root     11167:        
                   11168:        if(mode < 0x03) {
1.1.1.45  root     11169:                if(msdos_is_device_path(path)) {
                   11170:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     11171:                } else {
1.1.1.13  root     11172:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     11173:                }
1.1       root     11174:                if(fd != -1) {
                   11175:                        REG16(AX) = fd;
1.1.1.45  root     11176:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     11177:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11178:                } else {
                   11179:                        REG16(AX) = errno;
1.1.1.3   root     11180:                        m_CF = 1;
1.1       root     11181:                }
                   11182:        } else {
                   11183:                REG16(AX) = 0x0c;
1.1.1.3   root     11184:                m_CF = 1;
1.1       root     11185:        }
                   11186: }
                   11187: 
                   11188: inline void msdos_int_21h_3eh()
                   11189: {
                   11190:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11191:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11192:        
1.1.1.20  root     11193:        if(fd < process->max_files && file_handler[fd].valid) {
                   11194:                _close(fd);
                   11195:                msdos_file_handler_close(fd);
                   11196:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     11197:        } else {
                   11198:                REG16(AX) = 0x06;
1.1.1.3   root     11199:                m_CF = 1;
1.1       root     11200:        }
                   11201: }
                   11202: 
1.1.1.35  root     11203: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   11204: {
                   11205:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   11206:        int max = REG16(CX);
                   11207:        int p = 0;
                   11208:        
                   11209:        while(max > p) {
                   11210:                int chr = msdos_getch();
                   11211:                
                   11212:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   11213:                        p = 0;
                   11214:                        buf[p++] = 0x0d;
                   11215:                        if(max > p) {
                   11216:                                buf[p++] = 0x0a;
                   11217:                        }
                   11218:                        msdos_putch(0x03);
                   11219:                        msdos_putch(0x0d);
                   11220:                        msdos_putch(0x0a);
                   11221:                        break;
                   11222:                } else if(ctrl_break_pressed) {
                   11223:                        // skip this byte
                   11224:                } else if(chr == 0x00) {
                   11225:                        // skip 2nd byte
                   11226:                        msdos_getch();
                   11227:                } else if(chr == 0x0d) {
                   11228:                        // carriage return
                   11229:                        buf[p++] = 0x0d;
                   11230:                        if(max > p) {
                   11231:                                buf[p++] = 0x0a;
                   11232:                        }
                   11233:                        msdos_putch('\n');
                   11234:                        break;
                   11235:                } else if(chr == 0x08) {
                   11236:                        // back space
                   11237:                        if(p > 0) {
                   11238:                                p--;
                   11239:                                if(msdos_ctrl_code_check(buf[p])) {
                   11240:                                        msdos_putch(0x08);
                   11241:                                        msdos_putch(0x08);
                   11242:                                        msdos_putch(0x20);
                   11243:                                        msdos_putch(0x20);
                   11244:                                        msdos_putch(0x08);
                   11245:                                        msdos_putch(0x08);
1.1.1.36  root     11246:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   11247:                                        p--;
                   11248:                                        msdos_putch(0x08);
                   11249:                                        msdos_putch(0x08);
                   11250:                                        msdos_putch(0x20);
                   11251:                                        msdos_putch(0x20);
                   11252:                                        msdos_putch(0x08);
                   11253:                                        msdos_putch(0x08);
1.1.1.35  root     11254:                                } else {
                   11255:                                        msdos_putch(0x08);
                   11256:                                        msdos_putch(0x20);
                   11257:                                        msdos_putch(0x08);
                   11258:                                }
                   11259:                        }
                   11260:                } else if(chr == 0x1b) {
                   11261:                        // escape
                   11262:                        while(p > 0) {
                   11263:                                p--;
                   11264:                                if(msdos_ctrl_code_check(buf[p])) {
                   11265:                                        msdos_putch(0x08);
                   11266:                                        msdos_putch(0x08);
                   11267:                                        msdos_putch(0x20);
                   11268:                                        msdos_putch(0x20);
                   11269:                                        msdos_putch(0x08);
                   11270:                                        msdos_putch(0x08);
                   11271:                                } else {
                   11272:                                        msdos_putch(0x08);
                   11273:                                        msdos_putch(0x20);
                   11274:                                        msdos_putch(0x08);
                   11275:                                }
                   11276:                        }
                   11277:                } else {
                   11278:                        buf[p++] = chr;
                   11279:                        msdos_putch(chr);
                   11280:                }
                   11281:        }
                   11282:        REG16(AX) = p;
                   11283:        
                   11284: #ifdef USE_SERVICE_THREAD
                   11285:        service_exit = true;
                   11286: #endif
                   11287:        return(0);
                   11288: }
                   11289: 
1.1       root     11290: inline void msdos_int_21h_3fh()
                   11291: {
                   11292:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11293:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11294:        
1.1.1.20  root     11295:        if(fd < process->max_files && file_handler[fd].valid) {
                   11296:                if(file_mode[file_handler[fd].mode].in) {
                   11297:                        if(file_handler[fd].atty) {
1.1       root     11298:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     11299:                                if(REG16(CX) != 0) {
                   11300: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     11301:                                        if(!in_service && !in_service_29h &&
1.1.1.58  root     11302:                                           *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     11303:                                           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   11304:                                                // msdos_putch() will be used in this service
                   11305:                                                // if int 29h is hooked, run this service in main thread to call int 29h
                   11306:                                                start_service_loop(msdos_int_21h_3fh_thread);
                   11307:                                        } else {
                   11308: #endif
                   11309:                                                msdos_int_21h_3fh_thread(NULL);
                   11310:                                                REQUEST_HARDWRE_UPDATE();
                   11311: #ifdef USE_SERVICE_THREAD
                   11312:                                        }
1.1.1.35  root     11313: #endif
                   11314:                                } else {
                   11315:                                        REG16(AX) = 0;
1.1       root     11316:                                }
                   11317:                        } else {
1.1.1.37  root     11318:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     11319:                        }
                   11320:                } else {
                   11321:                        REG16(AX) = 0x05;
1.1.1.3   root     11322:                        m_CF = 1;
1.1       root     11323:                }
                   11324:        } else {
                   11325:                REG16(AX) = 0x06;
1.1.1.3   root     11326:                m_CF = 1;
1.1       root     11327:        }
                   11328: }
                   11329: 
                   11330: inline void msdos_int_21h_40h()
                   11331: {
                   11332:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11333:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11334:        
1.1.1.20  root     11335:        if(fd < process->max_files && file_handler[fd].valid) {
                   11336:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     11337:                        if(REG16(CX)) {
1.1.1.20  root     11338:                                if(file_handler[fd].atty) {
1.1       root     11339:                                        // BX is stdout/stderr or is redirected to stdout
                   11340:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     11341:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     11342:                                        }
                   11343:                                        REG16(AX) = REG16(CX);
                   11344:                                } else {
1.1.1.20  root     11345:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     11346:                                }
                   11347:                        } else {
1.1.1.20  root     11348:                                UINT32 pos = _tell(fd);
                   11349:                                _lseek(fd, 0, SEEK_END);
                   11350:                                UINT32 size = _tell(fd);
1.1.1.12  root     11351:                                if(pos < size) {
1.1.1.20  root     11352:                                        _lseek(fd, pos, SEEK_SET);
                   11353:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     11354:                                } else {
                   11355:                                        for(UINT32 i = size; i < pos; i++) {
                   11356:                                                UINT8 tmp = 0;
1.1.1.23  root     11357:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     11358:                                        }
1.1.1.20  root     11359:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     11360:                                }
1.1.1.23  root     11361:                                REG16(AX) = 0;
1.1       root     11362:                        }
                   11363:                } else {
                   11364:                        REG16(AX) = 0x05;
1.1.1.3   root     11365:                        m_CF = 1;
1.1       root     11366:                }
                   11367:        } else {
                   11368:                REG16(AX) = 0x06;
1.1.1.3   root     11369:                m_CF = 1;
1.1       root     11370:        }
                   11371: }
                   11372: 
                   11373: inline void msdos_int_21h_41h(int lfn)
                   11374: {
1.1.1.3   root     11375:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     11376:                REG16(AX) = errno;
1.1.1.3   root     11377:                m_CF = 1;
1.1       root     11378:        }
                   11379: }
                   11380: 
                   11381: inline void msdos_int_21h_42h()
                   11382: {
                   11383:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11384:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11385:        
1.1.1.20  root     11386:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     11387:                if(REG8(AL) < 0x03) {
1.1.1.35  root     11388:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     11389:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   11390:                        UINT32 pos = _tell(fd);
1.1       root     11391:                        REG16(AX) = pos & 0xffff;
                   11392:                        REG16(DX) = (pos >> 16);
                   11393:                } else {
                   11394:                        REG16(AX) = 0x01;
1.1.1.3   root     11395:                        m_CF = 1;
1.1       root     11396:                }
                   11397:        } else {
                   11398:                REG16(AX) = 0x06;
1.1.1.3   root     11399:                m_CF = 1;
1.1       root     11400:        }
                   11401: }
                   11402: 
                   11403: inline void msdos_int_21h_43h(int lfn)
                   11404: {
1.1.1.45  root     11405:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     11406:        int attr;
                   11407:        
1.1.1.14  root     11408:        if(!lfn && REG8(AL) > 2) {
                   11409:                REG16(AX) = 0x01;
                   11410:                m_CF = 1;
                   11411:                return;
                   11412:        }
                   11413:        switch(REG8(lfn ? BL : AL)) {
1.1       root     11414:        case 0x00:
1.1.1.60  root     11415:                if((attr = GetFileAttributesA(path)) != -1) {
1.1.1.14  root     11416:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   11417:                } else {
                   11418:                        REG16(AX) = (UINT16)GetLastError();
                   11419:                        m_CF = 1;
                   11420:                }
                   11421:                break;
                   11422:        case 0x01:
1.1.1.60  root     11423:                if(!SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)))) {
1.1.1.14  root     11424:                        REG16(AX) = (UINT16)GetLastError();
                   11425:                        m_CF = 1;
                   11426:                }
                   11427:                break;
                   11428:        case 0x02:
                   11429:                {
1.1.1.60  root     11430:                        DWORD compressed_size = GetCompressedFileSizeA(path, NULL), file_size = 0;
1.1.1.45  root     11431:                        if(compressed_size != INVALID_FILE_SIZE) {
                   11432:                                if(compressed_size != 0) {
1.1.1.60  root     11433:                                        HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.45  root     11434:                                        if(hFile != INVALID_HANDLE_VALUE) {
                   11435:                                                file_size = GetFileSize(hFile, NULL);
                   11436:                                                CloseHandle(hFile);
                   11437:                                        }
                   11438:                                        if(compressed_size == file_size) {
                   11439:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   11440:                                                // this isn't correct if the file is in the NTFS MFT
1.1.1.60  root     11441:                                                if(GetDiskFreeSpaceA(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
1.1.1.45  root     11442:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   11443:                                                }
1.1.1.14  root     11444:                                        }
                   11445:                                }
1.1.1.45  root     11446:                                REG16(AX) = LOWORD(compressed_size);
                   11447:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     11448:                        } else {
                   11449:                                REG16(AX) = (UINT16)GetLastError();
                   11450:                                m_CF = 1;
1.1       root     11451:                        }
1.1.1.14  root     11452:                }
                   11453:                break;
                   11454:        case 0x03:
                   11455:        case 0x05:
                   11456:        case 0x07:
1.1.1.48  root     11457:                if(lfn) {
1.1.1.60  root     11458:                        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     11459:                        if(hFile != INVALID_HANDLE_VALUE) {
                   11460:                                FILETIME local, time;
                   11461:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   11462:                                if(REG8(BL) == 7) {
                   11463:                                        ULARGE_INTEGER hund;
                   11464:                                        hund.LowPart = local.dwLowDateTime;
                   11465:                                        hund.HighPart = local.dwHighDateTime;
                   11466:                                        hund.QuadPart += REG16(SI) * 100000;
                   11467:                                        local.dwLowDateTime = hund.LowPart;
                   11468:                                        local.dwHighDateTime = hund.HighPart;
                   11469:                                }
                   11470:                                LocalFileTimeToFileTime(&local, &time);
                   11471:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   11472:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   11473:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   11474:                                        REG16(AX) = (UINT16)GetLastError();
                   11475:                                        m_CF = 1;
                   11476:                                }
                   11477:                                CloseHandle(hFile);
                   11478:                        } else {
                   11479:                                REG16(AX) = (UINT16)GetLastError();
                   11480:                                m_CF = 1;
1.1       root     11481:                        }
1.1.1.48  root     11482:                } else {
                   11483:                        // 214303 DR DOS 3.41+ internal - Set Access Rights And Password
                   11484:                        // 214305 DR DOS 5.0-6.0 internal - Set Extended File Attributes
                   11485:                        // 214307 DR DOS 6.0 - Set File Owner
                   11486: //                     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));
                   11487:                        REG16(AX) = 0x01;
                   11488:                        m_CF = 1;
1.1.1.14  root     11489:                }
                   11490:                break;
                   11491:        case 0x04:
                   11492:        case 0x06:
                   11493:        case 0x08:
1.1.1.48  root     11494:                if(lfn) {
1.1.1.14  root     11495:                        WIN32_FILE_ATTRIBUTE_DATA fad;
1.1.1.60  root     11496:                        if(GetFileAttributesExA(path, GetFileExInfoStandard, (LPVOID)&fad)) {
1.1.1.14  root     11497:                                FILETIME *time, local;
                   11498:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   11499:                                                   0x06 ? &fad.ftLastAccessTime :
                   11500:                                                          &fad.ftCreationTime;
                   11501:                                FileTimeToLocalFileTime(time, &local);
                   11502:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   11503:                                if(REG8(BL) == 0x08) {
                   11504:                                        ULARGE_INTEGER hund;
                   11505:                                        hund.LowPart = local.dwLowDateTime;
                   11506:                                        hund.HighPart = local.dwHighDateTime;
                   11507:                                        hund.QuadPart /= 100000;
                   11508:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   11509:                                }
                   11510:                        } else {
                   11511:                                REG16(AX) = (UINT16)GetLastError();
                   11512:                                m_CF = 1;
1.1       root     11513:                        }
1.1.1.48  root     11514:                } else {
                   11515:                        // 214304 DR DOS 5.0-6.0 internal - Get Encrypted Password
                   11516:                        // 214306 DR DOS 6.0 - Get File Owner
                   11517: //                     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));
                   11518:                        REG16(AX) = 0x01;
                   11519:                        m_CF = 1;
1.1.1.14  root     11520:                }
                   11521:                break;
1.1.1.43  root     11522:        case 0xff:
1.1.1.48  root     11523:                if(!lfn && REG16(BP) == 0x5053) {
1.1.1.43  root     11524:                        if(REG8(CL) == 0x39) {
                   11525:                                msdos_int_21h_39h(1);
                   11526:                                break;
                   11527:                        } else if(REG8(CL) == 0x56) {
                   11528:                                msdos_int_21h_56h(1);
                   11529:                                break;
                   11530:                        }
                   11531:                }
1.1.1.14  root     11532:        default:
1.1.1.22  root     11533:                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     11534:                REG16(AX) = lfn ? 0x7100 : 0x01;
1.1.1.14  root     11535:                m_CF = 1;
                   11536:                break;
                   11537:        }
                   11538: }
                   11539: 
                   11540: inline void msdos_int_21h_44h()
                   11541: {
1.1.1.22  root     11542:        static UINT16 iteration_count = 0;
                   11543:        
1.1.1.44  root     11544:        process_t *process;
                   11545:        int fd, drv;
1.1.1.14  root     11546:        
                   11547:        switch(REG8(AL)) {
                   11548:        case 0x00:
                   11549:        case 0x01:
                   11550:        case 0x02:
                   11551:        case 0x03:
                   11552:        case 0x04:
                   11553:        case 0x05:
                   11554:        case 0x06:
                   11555:        case 0x07:
1.1.1.44  root     11556:                process = msdos_process_info_get(current_psp);
                   11557:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     11558:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   11559:                        REG16(AX) = 0x06;
                   11560:                        m_CF = 1;
                   11561:                        return;
1.1.1.14  root     11562:                }
                   11563:                break;
                   11564:        case 0x08:
                   11565:        case 0x09:
1.1.1.44  root     11566:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11567:                if(!msdos_is_valid_drive(drv)) {
                   11568:                        // invalid drive
1.1.1.14  root     11569:                        REG16(AX) = 0x0f;
                   11570:                        m_CF = 1;
                   11571:                        return;
1.1       root     11572:                }
                   11573:                break;
                   11574:        }
                   11575:        switch(REG8(AL)) {
1.1.1.48  root     11576:        case 0x00: // Get Device Information
1.1.1.20  root     11577:                REG16(DX) = file_handler[fd].info;
1.1       root     11578:                break;
1.1.1.48  root     11579:        case 0x01: // Set Device Information
1.1.1.45  root     11580:                if(REG8(DH) != 0) {
                   11581: //                     REG16(AX) = 0x0d; // data invalid
                   11582: //                     m_CF = 1;
                   11583:                        file_handler[fd].info = REG16(DX);
                   11584:                } else {
                   11585:                        file_handler[fd].info &= 0xff00;
                   11586:                        file_handler[fd].info |= REG8(DL);
                   11587:                }
1.1       root     11588:                break;
1.1.1.48  root     11589:        case 0x02: // Read From Character Device Control Channel
1.1.1.45  root     11590:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
                   11591:                        // from DOSBox
                   11592:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
                   11593:                        case 0x00:
                   11594:                                if(REG16(CX) >= 6) {
                   11595:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
                   11596:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
                   11597:                                        REG16(AX) = 6; // number of bytes actually read
                   11598:                                } else {
                   11599:                                        REG16(AX) = 0x0d; // data invalid
                   11600:                                        m_CF = 1;
                   11601:                                }
                   11602:                                break;
                   11603:                        case 0x01:
                   11604:                                if(REG16(CX) >= 6) {
                   11605:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
                   11606:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
                   11607:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
                   11608:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
                   11609:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
                   11610:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
                   11611:                                                        int page = (addr - EMS_TOP) / 0x4000;
                   11612:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
                   11613:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11614:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
                   11615:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
                   11616:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
                   11617:                                                } else {
                   11618:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
                   11619:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11620:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
                   11621:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
                   11622:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
                   11623:                                                }
                   11624:                                        }
                   11625:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
                   11626:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
                   11627:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
                   11628:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
                   11629:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
                   11630:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
                   11631:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
                   11632:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
                   11633:                                        
                   11634:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
1.1.1.65! root     11635:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;
1.1.1.45  root     11636:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
                   11637:                                        REG16(AX) = 6; // number of bytes actually read
                   11638:                                } else {
                   11639:                                        REG16(AX) = 0x0d; // data invalid
                   11640:                                        m_CF = 1;
                   11641:                                }
                   11642:                                break;
                   11643:                        case 0x02:
                   11644:                                if(REG16(CX) >= 2) {
                   11645:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
                   11646:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
                   11647:                                        REG16(AX) = 2; // number of bytes actually read
                   11648:                                } else {
                   11649:                                        REG16(AX) = 0x0d; // data invalid
                   11650:                                        m_CF = 1;
                   11651:                                }
                   11652:                                break;
                   11653:                        case 0x03:
                   11654:                                if(REG16(CX) >= 4) {
                   11655:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
                   11656:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
                   11657:                                        REG16(AX) = 4; // number of bytes actually read
                   11658:                                } else {
                   11659:                                        REG16(AX) = 0x0d; // data invalid
                   11660:                                        m_CF = 1;
                   11661:                                }
                   11662:                                break;
                   11663:                        default:
                   11664:                                REG16(AX) = 0x01; // function number invalid
                   11665:                                m_CF = 1;
                   11666:                        }
                   11667:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
                   11668:                        if(REG16(CX) >= 5) {
                   11669:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
                   11670:                                REG16(AX) = 5; // number of bytes actually read
                   11671:                        } else {
                   11672:                                REG16(AX) = 0x0d; // data invalid
                   11673:                                m_CF = 1;
                   11674:                        }
                   11675:                } else {
                   11676: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
                   11677: //                     REG16(AX) = REG16(CX);
                   11678:                        REG16(AX) = 0x05; // access denied
                   11679:                        m_CF = 1;
                   11680:                }
                   11681:                break;
1.1.1.48  root     11682:        case 0x03: // Write To Character Device Control Channel
1.1.1.45  root     11683: //             REG16(AX) = 0x05;
                   11684: //             m_CF = 1;
                   11685:                REG16(AX) = 0x00; // success
                   11686:                break;
1.1.1.48  root     11687:        case 0x04: // Read From Block Device Control Channel
                   11688:        case 0x05: // Write To Block Device Control Channel
1.1       root     11689:                REG16(AX) = 0x05;
1.1.1.3   root     11690:                m_CF = 1;
1.1       root     11691:                break;
1.1.1.48  root     11692:        case 0x06: // Get Input Status
1.1.1.20  root     11693:                if(file_mode[file_handler[fd].mode].in) {
                   11694:                        if(file_handler[fd].atty) {
1.1.1.14  root     11695:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     11696:                        } else {
1.1.1.20  root     11697:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     11698:                        }
1.1.1.14  root     11699:                } else {
                   11700:                        REG8(AL) = 0x00;
1.1       root     11701:                }
                   11702:                break;
1.1.1.48  root     11703:        case 0x07: // Get Output Status
1.1.1.20  root     11704:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     11705:                        REG8(AL) = 0xff;
                   11706:                } else {
                   11707:                        REG8(AL) = 0x00;
1.1       root     11708:                }
                   11709:                break;
1.1.1.48  root     11710:        case 0x08: // Check If Block Device Removable
1.1.1.44  root     11711:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     11712:                        // removable drive
                   11713:                        REG16(AX) = 0x00;
1.1       root     11714:                } else {
1.1.1.14  root     11715:                        // fixed drive
                   11716:                        REG16(AX) = 0x01;
1.1       root     11717:                }
                   11718:                break;
1.1.1.48  root     11719:        case 0x09: // Check If Block Device Remote
1.1.1.44  root     11720:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     11721:                        // remote drive
                   11722:                        REG16(DX) = 0x1000;
1.1.1.44  root     11723:                } else if(msdos_is_subst_drive(drv)) {
                   11724:                        // subst drive
                   11725:                        REG16(DX) = 0x8000;
1.1       root     11726:                } else {
1.1.1.14  root     11727:                        // local drive
1.1.1.44  root     11728:                        REG16(DX) = 0x0000;
1.1       root     11729:                }
                   11730:                break;
1.1.1.48  root     11731:        case 0x0a: // Check If Handle Is Remote
1.1.1.45  root     11732:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
                   11733:                        REG16(DX) = 0x8000;
                   11734:                } else {
                   11735:                        REG16(DX) = 0x0000;
                   11736:                }
1.1.1.21  root     11737:                break;
1.1.1.48  root     11738:        case 0x0b: // Set Sharing Retry Count
1.1       root     11739:                break;
1.1.1.48  root     11740:        case 0x0c: // Generic Character Device Request
1.1.1.22  root     11741:                if(REG8(CL) == 0x45) {
                   11742:                        // set iteration (retry) count
                   11743:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   11744:                } else if(REG8(CL) == 0x4a) {
                   11745:                        // select code page
                   11746:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   11747:                        msdos_nls_tables_update();
1.1.1.44  root     11748:                } else if(REG8(CL) == 0x4c) {
                   11749:                        // start code-page preparation
                   11750:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11751:                        int count = 1, offset = 0;
                   11752:                        if(active_code_page != 437) {
                   11753:                                ids[count++] = active_code_page;
                   11754:                        }
                   11755:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11756:                                ids[count++] = system_code_page;
                   11757:                        }
                   11758:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   11759:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   11760:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11761:                        for(int i = 0; i < count; i++) {
                   11762:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11763:                        }
                   11764:                } else if(REG8(CL) == 0x4d) {
                   11765:                        // end code-page preparation
                   11766:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   11767:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.50  root     11768:                } else if(REG8(CL) == 0x5f) {
                   11769:                        // set display information
                   11770:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   11771:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   11772:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   11773:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   11774:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   11775:                                
                   11776:                                if(cur_width != new_width || cur_height != new_height) {
                   11777:                                        pcbios_set_console_size(new_width, new_height, true);
                   11778:                                }
                   11779:                        }
1.1.1.22  root     11780:                } else if(REG8(CL) == 0x65) {
                   11781:                        // get iteration (retry) count
                   11782:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   11783:                } else if(REG8(CL) == 0x6a) {
                   11784:                        // query selected code page
                   11785:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   11786:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   11787:                        
                   11788:                        CPINFO info;
                   11789:                        GetCPInfo(active_code_page, &info);
                   11790:                        
                   11791:                        if(info.MaxCharSize != 1) {
                   11792:                                for(int i = 0;; i++) {
                   11793:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   11794:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   11795:                                        
                   11796:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   11797:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   11798:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   11799:                                        
                   11800:                                        if(lo == 0 && hi == 0) {
                   11801:                                                break;
                   11802:                                        }
                   11803:                                }
                   11804:                        }
1.1.1.44  root     11805:                } else if(REG8(CL) == 0x6b) {
                   11806:                        // query prepare list
                   11807:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11808:                        int count = 1, offset = 0;
                   11809:                        if(active_code_page != 437) {
                   11810:                                ids[count++] = active_code_page;
                   11811:                        }
                   11812:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11813:                                ids[count++] = system_code_page;
                   11814:                        }
                   11815:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   11816:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11817:                        for(int i = 0; i < count; i++) {
                   11818:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11819:                        }
                   11820:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11821:                        for(int i = 0; i < count; i++) {
                   11822:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11823:                        }
1.1.1.22  root     11824:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     11825:                        // get display information
1.1.1.50  root     11826:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   11827:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   11828:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   11829:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   11830:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   11831:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   11832:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   11833:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   11834:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   11835:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   11836:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.22  root     11837:                } else {
                   11838:                        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));
                   11839:                        REG16(AX) = 0x01; // invalid function
                   11840:                        m_CF = 1;
                   11841:                }
                   11842:                break;
1.1.1.48  root     11843:        case 0x0d: // Generic Block Device Request
1.1.1.22  root     11844:                if(REG8(CL) == 0x40) {
                   11845:                        // set device parameters
1.1.1.48  root     11846: //             } else if(REG8(CL) == 0x41) {
                   11847: //                     // write logical device track
                   11848: //             } else if(REG8(CL) == 0x42) {
                   11849: //                     // format and verify logical device track
1.1.1.22  root     11850:                } else if(REG8(CL) == 0x46) {
                   11851:                        // set volume serial number
1.1.1.48  root     11852:                } else if(REG8(CL) == 0x47) {
                   11853:                        // set access flag
                   11854: //             } else if(REG8(CL) == 0x48) {
                   11855: //                     // set media lock state
                   11856: //             } else if(REG8(CL) == 0x49) {
                   11857: //                     // eject media in drive
1.1.1.22  root     11858:                } else if(REG8(CL) == 0x4a) {
                   11859:                        // lock logical volume
                   11860:                } else if(REG8(CL) == 0x4b) {
                   11861:                        // lock physical volume
                   11862:                } else if(REG8(CL) == 0x60) {
                   11863:                        // get device parameters
1.1.1.42  root     11864:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11865:                        
1.1.1.42  root     11866:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11867:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11868:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11869:                                
                   11870:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   11871:                                switch(geo->MediaType) {
                   11872:                                case F5_360_512:
                   11873:                                case F5_320_512:
                   11874:                                case F5_320_1024:
                   11875:                                case F5_180_512:
                   11876:                                case F5_160_512:
                   11877:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   11878:                                        break;
                   11879:                                case F5_1Pt2_512:
                   11880:                                case F3_1Pt2_512:
                   11881:                                case F3_1Pt23_1024:
                   11882:                                case F5_1Pt23_1024:
                   11883:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   11884:                                        break;
                   11885:                                case F3_720_512:
                   11886:                                case F3_640_512:
                   11887:                                case F5_640_512:
                   11888:                                case F5_720_512:
                   11889:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   11890:                                        break;
                   11891:                                case F8_256_128:
                   11892:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   11893:                                        break;
                   11894:                                case FixedMedia:
                   11895:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11896:                                        break;
                   11897:                                case F3_1Pt44_512:
                   11898:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11899:                                        break;
                   11900:                                case F3_2Pt88_512:
                   11901:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   11902:                                        break;
                   11903:                                default:
                   11904:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11905: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11906:                                        break;
1.1.1.22  root     11907:                                }
1.1.1.42  root     11908:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   11909:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   11910:                                switch(geo->MediaType) {
                   11911:                                case F5_360_512:
                   11912:                                case F5_320_512:
                   11913:                                case F5_320_1024:
                   11914:                                case F5_180_512:
                   11915:                                case F5_160_512:
                   11916:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   11917:                                        break;
                   11918:                                default:
                   11919:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   11920:                                        break;
                   11921:                                }
                   11922:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   11923:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   11924:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   11925:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   11926:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   11927:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   11928:                                switch(geo->MediaType) {
                   11929:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   11930:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   11931:                                        break;
                   11932:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   11933:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   11934:                                        break;
                   11935:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   11936:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11937:                                        break;
                   11938:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11939:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11940:                                        break;
                   11941:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11942:                                case F3_1Pt2_512:
                   11943:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11944:                                case F5_720_512:
                   11945:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11946:                                        break;
                   11947:                                case FixedMedia:        // hard disk
                   11948:                                case RemovableMedia:
                   11949:                                case Unknown:
                   11950:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11951:                                        break;
                   11952:                                default:
                   11953:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11954:                                        break;
                   11955:                                }
                   11956:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11957:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11958:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11959:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11960:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11961:                                // 21h  BYTE    device type
                   11962:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11963:                        } else {
                   11964:                                REG16(AX) = 0x0f; // invalid drive
                   11965:                                m_CF = 1;
                   11966:                        }
1.1.1.48  root     11967: //             } else if(REG8(CL) == 0x61) {
                   11968: //                     // read logical device track
                   11969: //             } else if(REG8(CL) == 0x62) {
                   11970: //                     // verify logical device track
1.1.1.22  root     11971:                } else if(REG8(CL) == 0x66) {
                   11972:                        // get volume serial number
                   11973:                        char path[] = "A:\\";
                   11974:                        char volume_label[MAX_PATH];
                   11975:                        DWORD serial_number = 0;
                   11976:                        char file_system[MAX_PATH];
                   11977:                        
                   11978:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11979:                        
1.1.1.60  root     11980:                        if(GetVolumeInformationA(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
1.1.1.22  root     11981:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11982:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11983:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11984:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11985:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11986:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11987:                        } else {
                   11988:                                REG16(AX) = 0x0f; // invalid drive
                   11989:                                m_CF = 1;
                   11990:                        }
                   11991:                } else if(REG8(CL) == 0x67) {
                   11992:                        // get access flag
                   11993:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11994:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11995:                } else if(REG8(CL) == 0x68) {
                   11996:                        // sense media type
1.1.1.42  root     11997:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11998:                        
1.1.1.42  root     11999:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   12000:                                drive_param_t *drive_param = &drive_params[drive_num];
                   12001:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   12002:                                
                   12003:                                switch(geo->MediaType) {
                   12004:                                case F3_720_512:
                   12005:                                case F5_720_512:
                   12006:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   12007:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   12008:                                        break;
                   12009:                                case F3_1Pt44_512:
                   12010:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   12011:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   12012:                                        break;
                   12013:                                case F3_2Pt88_512:
                   12014:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   12015:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   12016:                                        break;
                   12017:                                default:
                   12018:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   12019:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   12020:                                        break;
1.1.1.22  root     12021:                                }
                   12022:                        } else {
                   12023:                                REG16(AX) = 0x0f; // invalid drive
                   12024:                                m_CF = 1;
                   12025:                        }
                   12026:                } else if(REG8(CL) == 0x6a) {
                   12027:                        // unlock logical volume
                   12028:                } else if(REG8(CL) == 0x6b) {
                   12029:                        // unlock physical volume
1.1.1.48  root     12030: //             } else if(REG8(CL) == 0x6c) {
                   12031: //                     // get lock flag
                   12032: //             } else if(REG8(CL) == 0x6d) {
                   12033: //                     // enumerate open files
                   12034: //             } else if(REG8(CL) == 0x6e) {
                   12035: //                     // find swap file
                   12036: //             } else if(REG8(CL) == 0x6f) {
                   12037: //                     // get drive map information
                   12038: //             } else if(REG8(CL) == 0x70) {
                   12039: //                     // get current lock state
                   12040: //             } else if(REG8(CL) == 0x71) {
                   12041: //                     // get first cluster
1.1.1.22  root     12042:                } else {
                   12043:                        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));
                   12044:                        REG16(AX) = 0x01; // invalid function
                   12045:                        m_CF = 1;
                   12046:                }
                   12047:                break;
1.1.1.48  root     12048:        case 0x0e: // Get Lgical Drive Map
1.1.1.44  root     12049:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12050:                        REG16(AX) = 0x0f; // invalid drive
                   12051:                        m_CF = 1;
                   12052:                } else {
                   12053:                        REG8(AL) = 0;
1.1.1.22  root     12054:                }
                   12055:                break;
1.1.1.48  root     12056:        case 0x0f: // Set Logical Drive Map
1.1.1.44  root     12057:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12058:                        REG16(AX) = 0x0f; // invalid drive
                   12059:                        m_CF = 1;
1.1.1.22  root     12060:                }
                   12061:                break;
1.1.1.48  root     12062:        case 0x10: // Query Generic IOCTRL Capability (Handle)
1.1.1.22  root     12063:                switch(REG8(CL)) {
                   12064:                case 0x45:
                   12065:                case 0x4a:
1.1.1.48  root     12066:                case 0x4c:
                   12067:                case 0x4d:
1.1.1.22  root     12068:                case 0x65:
                   12069:                case 0x6a:
1.1.1.48  root     12070:                case 0x6b:
1.1.1.22  root     12071:                case 0x7f:
                   12072:                        REG16(AX) = 0x0000; // supported
                   12073:                        break;
                   12074:                default:
                   12075:                        REG8(AL) = 0x01; // ioctl capability not available
                   12076:                        m_CF = 1;
                   12077:                        break;
                   12078:                }
                   12079:                break;
1.1.1.48  root     12080:        case 0x11: // Query Generic IOCTRL Capability (Drive)
1.1.1.22  root     12081:                switch(REG8(CL)) {
                   12082:                case 0x40:
                   12083:                case 0x46:
                   12084:                case 0x4a:
                   12085:                case 0x4b:
                   12086:                case 0x60:
                   12087:                case 0x66:
                   12088:                case 0x67:
                   12089:                case 0x68:
                   12090:                case 0x6a:
                   12091:                case 0x6b:
1.1.1.48  root     12092:                        if(REG8(CH) == 0x00 || REG8(CH) == 0x01 || REG8(CH) == 0x03 || REG8(CH) == 0x05) {
                   12093:                                // CH = 00h     Unknown
                   12094:                                // CH = 01h     COMn:
                   12095:                                // CH = 03h     CON
                   12096:                                // CH = 05h     LPTn:
                   12097:                                REG16(AX) = 0x0000; // supported
                   12098:                                break;
                   12099:                        }
1.1.1.22  root     12100:                default:
                   12101:                        REG8(AL) = 0x01; // ioctl capability not available
                   12102:                        m_CF = 1;
                   12103:                        break;
                   12104:                }
                   12105:                break;
1.1.1.48  root     12106:        case 0x12: // DR DOS 5.0-6.0 - Determine DOS Type
                   12107:        case 0x14: // DR DOS 5.0-6.0 - Set Global Password
                   12108:        case 0x16: // DR DOS 5.0-6.0 - History Buffer, Share, And Hiload Control
                   12109:        case 0x51: // Concurrent DOS v3.2+ - Installation Check
                   12110:        case 0x52: // DR DOS 3.41+ - Determine DOS tTpe/Get DR DOS Version
                   12111:        case 0x54: // DR DOS 3.41+ - Set Global Password
                   12112:        case 0x56: // DR DOS 5.0+ - History Buffer Control
                   12113:        case 0x57: // DR DOS 5.0-6.0 - Share/Hiload Control
                   12114:        case 0x58: // DR DOS 5.0+ internal - Get Pointer To Internal Variable Table
                   12115:        case 0x59: // DR Multiuser DOS 5.0 - API
                   12116:                REG16(AX) = 0x01; // this is not DR-DOS
1.1.1.22  root     12117:                m_CF = 1;
                   12118:                break;
1.1       root     12119:        default:
1.1.1.22  root     12120:                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     12121:                REG16(AX) = 0x01;
1.1.1.3   root     12122:                m_CF = 1;
1.1       root     12123:                break;
                   12124:        }
                   12125: }
                   12126: 
                   12127: inline void msdos_int_21h_45h()
                   12128: {
                   12129:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12130:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12131:        
1.1.1.20  root     12132:        if(fd < process->max_files && file_handler[fd].valid) {
                   12133:                int dup_fd = _dup(fd);
                   12134:                if(dup_fd != -1) {
                   12135:                        REG16(AX) = dup_fd;
                   12136:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   12137: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   12138:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     12139:                } else {
                   12140:                        REG16(AX) = errno;
1.1.1.3   root     12141:                        m_CF = 1;
1.1       root     12142:                }
                   12143:        } else {
                   12144:                REG16(AX) = 0x06;
1.1.1.3   root     12145:                m_CF = 1;
1.1       root     12146:        }
                   12147: }
                   12148: 
                   12149: inline void msdos_int_21h_46h()
                   12150: {
                   12151:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12152:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   12153:        int dup_fd = REG16(CX);
                   12154:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     12155:        
1.1.1.20  root     12156:        if(REG16(BX) == REG16(CX)) {
                   12157:                REG16(AX) = 0x06;
                   12158:                m_CF = 1;
                   12159:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   12160:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   12161:                        _close(tmp_fd);
                   12162:                        msdos_file_handler_close(tmp_fd);
                   12163:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   12164:                }
                   12165:                if(_dup2(fd, dup_fd) != -1) {
                   12166:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   12167: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   12168:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     12169:                } else {
                   12170:                        REG16(AX) = errno;
1.1.1.3   root     12171:                        m_CF = 1;
1.1       root     12172:                }
                   12173:        } else {
                   12174:                REG16(AX) = 0x06;
1.1.1.3   root     12175:                m_CF = 1;
1.1       root     12176:        }
                   12177: }
                   12178: 
                   12179: inline void msdos_int_21h_47h(int lfn)
                   12180: {
                   12181:        char path[MAX_PATH];
                   12182:        
                   12183:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45  root     12184:                if(!lfn) {
                   12185:                        strcpy(path, msdos_short_path(path));
                   12186:                }
1.1       root     12187:                if(path[1] == ':') {
                   12188:                        // the returned path does not include a drive or the initial backslash
1.1.1.45  root     12189:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     12190:                } else {
1.1.1.45  root     12191:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     12192:                }
                   12193:        } else {
                   12194:                REG16(AX) = errno;
1.1.1.3   root     12195:                m_CF = 1;
1.1       root     12196:        }
                   12197: }
                   12198: 
                   12199: inline void msdos_int_21h_48h()
                   12200: {
1.1.1.19  root     12201:        int seg, umb_linked;
1.1       root     12202:        
1.1.1.8   root     12203:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     12204:                // unlink umb not to allocate memory in umb
                   12205:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   12206:                        msdos_mem_unlink_umb();
                   12207:                }
1.1.1.8   root     12208:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   12209:                        REG16(AX) = seg;
                   12210:                } else {
                   12211:                        REG16(AX) = 0x08;
                   12212:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   12213:                        m_CF = 1;
                   12214:                }
1.1.1.19  root     12215:                if(umb_linked != 0) {
                   12216:                        msdos_mem_link_umb();
                   12217:                }
1.1.1.8   root     12218:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   12219:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   12220:                        REG16(AX) = seg;
                   12221:                } else {
                   12222:                        REG16(AX) = 0x08;
                   12223:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   12224:                        m_CF = 1;
                   12225:                }
                   12226:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   12227:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   12228:                        REG16(AX) = seg;
                   12229:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   12230:                        REG16(AX) = seg;
                   12231:                } else {
                   12232:                        REG16(AX) = 0x08;
                   12233:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   12234:                        m_CF = 1;
                   12235:                }
1.1       root     12236:        }
                   12237: }
                   12238: 
                   12239: inline void msdos_int_21h_49h()
                   12240: {
1.1.1.14  root     12241:        int mcb_seg = SREG(ES) - 1;
                   12242:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   12243:        
                   12244:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   12245:                msdos_mem_free(SREG(ES));
                   12246:        } else {
1.1.1.33  root     12247:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     12248:                m_CF = 1;
                   12249:        }
1.1       root     12250: }
                   12251: 
                   12252: inline void msdos_int_21h_4ah()
                   12253: {
1.1.1.14  root     12254:        int mcb_seg = SREG(ES) - 1;
                   12255:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     12256:        int max_paragraphs;
                   12257:        
1.1.1.14  root     12258:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   12259:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   12260:                        REG16(AX) = 0x08;
                   12261:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   12262:                        m_CF = 1;
                   12263:                }
                   12264:        } else {
1.1.1.33  root     12265:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     12266:                m_CF = 1;
1.1       root     12267:        }
                   12268: }
                   12269: 
                   12270: inline void msdos_int_21h_4bh()
                   12271: {
1.1.1.3   root     12272:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12273:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     12274:        
                   12275:        switch(REG8(AL)) {
                   12276:        case 0x00:
                   12277:        case 0x01:
                   12278:                if(msdos_process_exec(command, param, REG8(AL))) {
                   12279:                        REG16(AX) = 0x02;
1.1.1.3   root     12280:                        m_CF = 1;
1.1       root     12281:                }
                   12282:                break;
1.1.1.14  root     12283:        case 0x03:
                   12284:                {
                   12285:                        int fd;
                   12286:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   12287:                                REG16(AX) = 0x02;
                   12288:                                m_CF = 1;
                   12289:                                break;
                   12290:                        }
                   12291:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   12292:                        _close(fd);
                   12293:                        
                   12294:                        UINT16 *overlay = (UINT16 *)param;
                   12295:                        
                   12296:                        // check exe header
                   12297:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   12298:                        int header_size = 0;
                   12299:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   12300:                                header_size = header->header_size * 16;
                   12301:                                // relocation
                   12302:                                int start_seg = overlay[1];
                   12303:                                for(int i = 0; i < header->relocations; i++) {
                   12304:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   12305:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   12306:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   12307:                                }
                   12308:                        }
                   12309:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   12310:                }
                   12311:                break;
1.1.1.48  root     12312:        case 0x04:
                   12313:                // Load And Execute In Background (European MS-DOS 4.0 only)
                   12314: //     case 0x05:
                   12315: //             // DOS 5+ - Set Execution State
                   12316:        case 0x80:
                   12317:                // DR DOS v3.41 - Run Already-Loaded Kernel File
                   12318:        case 0xf0:
                   12319:        case 0xf1:
                   12320:                // DIET v1.10+
1.1.1.43  root     12321:        case 0xfd:
                   12322:        case 0xfe:
                   12323:                // unknown function called in FreeCOM
                   12324:                REG16(AX) = 0x01;
                   12325:                m_CF = 1;
                   12326:                break;
1.1       root     12327:        default:
1.1.1.22  root     12328:                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     12329:                REG16(AX) = 0x01;
1.1.1.3   root     12330:                m_CF = 1;
1.1       root     12331:                break;
                   12332:        }
                   12333: }
                   12334: 
                   12335: inline void msdos_int_21h_4ch()
                   12336: {
                   12337:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   12338: }
                   12339: 
                   12340: inline void msdos_int_21h_4dh()
                   12341: {
                   12342:        REG16(AX) = retval;
                   12343: }
                   12344: 
                   12345: inline void msdos_int_21h_4eh()
                   12346: {
                   12347:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     12348:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   12349:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45  root     12350:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1.1.60  root     12351:        WIN32_FIND_DATAA fd;
1.1       root     12352:        
1.1.1.14  root     12353:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12354:        find->find_magic = FIND_MAGIC;
                   12355:        find->dta_index = dtainfo - dtalist;
1.1       root     12356:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12357:        dtainfo->allowable_mask = REG8(CL);
1.1.1.58  root     12358:        // note: SO1 dir command sets 0x3f, but only directories and volue label are found if bit3 is set :-(
                   12359:        if((dtainfo->allowable_mask & 0x3f) == 0x3f) {
                   12360:                dtainfo->allowable_mask &= ~0x08;
                   12361:        }
1.1.1.14  root     12362:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12363:        
1.1.1.14  root     12364:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12365:                dtainfo->allowable_mask &= ~8;
1.1       root     12366:        }
1.1.1.60  root     12367:        if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14  root     12368:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     12369:                      !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     12370:                        if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     12371:                                FindClose(dtainfo->find_handle);
                   12372:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12373:                                break;
                   12374:                        }
                   12375:                }
                   12376:        }
1.1.1.13  root     12377:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12378:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   12379:                msdos_find_file_conv_local_time(&fd);
                   12380:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   12381:                find->size = fd.nFileSizeLow;
1.1.1.13  root     12382:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     12383:                REG16(AX) = 0;
1.1.1.14  root     12384:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12385:                find->attrib = 8;
                   12386:                find->size = 0;
                   12387:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12388:                dtainfo->allowable_mask &= ~8;
1.1       root     12389:                REG16(AX) = 0;
                   12390:        } else {
                   12391:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12392:                m_CF = 1;
1.1       root     12393:        }
                   12394: }
                   12395: 
                   12396: inline void msdos_int_21h_4fh()
                   12397: {
                   12398:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     12399:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   12400:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.60  root     12401:        WIN32_FIND_DATAA fd;
1.1       root     12402:        
1.1.1.14  root     12403:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   12404:                REG16(AX) = 0x12;
                   12405:                m_CF = 1;
                   12406:                return;
                   12407:        }
                   12408:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     12409:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60  root     12410:                if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14  root     12411:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     12412:                              !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     12413:                                if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     12414:                                        FindClose(dtainfo->find_handle);
                   12415:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12416:                                        break;
                   12417:                                }
                   12418:                        }
                   12419:                } else {
1.1.1.13  root     12420:                        FindClose(dtainfo->find_handle);
                   12421:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12422:                }
                   12423:        }
1.1.1.13  root     12424:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12425:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   12426:                msdos_find_file_conv_local_time(&fd);
                   12427:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   12428:                find->size = fd.nFileSizeLow;
1.1.1.13  root     12429:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     12430:                REG16(AX) = 0;
1.1.1.14  root     12431:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12432:                find->attrib = 8;
                   12433:                find->size = 0;
                   12434:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12435:                dtainfo->allowable_mask &= ~8;
1.1       root     12436:                REG16(AX) = 0;
                   12437:        } else {
                   12438:                REG16(AX) = 0x12;
1.1.1.3   root     12439:                m_CF = 1;
1.1       root     12440:        }
                   12441: }
                   12442: 
                   12443: inline void msdos_int_21h_50h()
                   12444: {
1.1.1.8   root     12445:        if(current_psp != REG16(BX)) {
                   12446:                process_t *process = msdos_process_info_get(current_psp);
                   12447:                if(process != NULL) {
                   12448:                        process->psp = REG16(BX);
                   12449:                }
                   12450:                current_psp = REG16(BX);
1.1.1.23  root     12451:                msdos_sda_update(current_psp);
1.1.1.8   root     12452:        }
1.1       root     12453: }
                   12454: 
                   12455: inline void msdos_int_21h_51h()
                   12456: {
                   12457:        REG16(BX) = current_psp;
                   12458: }
                   12459: 
                   12460: inline void msdos_int_21h_52h()
                   12461: {
1.1.1.25  root     12462:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     12463:        i386_load_segment_descriptor(ES);
1.1.1.25  root     12464:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     12465: }
                   12466: 
1.1.1.43  root     12467: inline void msdos_int_21h_53h()
                   12468: {
                   12469:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   12470:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   12471:        
                   12472:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   12473:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   12474:        dpb->shift_count = 0;
                   12475:        dpb->reserved_sectors = 0;
                   12476:        dpb->fat_num = bpb->fat_num;
                   12477:        dpb->root_entries = bpb->root_entries;
                   12478:        dpb->first_data_sector = 0;
                   12479:        if(bpb->sectors_per_cluster != 0) {
                   12480:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   12481:        } else {
                   12482:                dpb->highest_cluster_num = 0;
                   12483:        }
                   12484:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   12485:        dpb->first_dir_sector = 0;
                   12486:        dpb->device_driver_header = 0;
                   12487:        dpb->media_type = bpb->media_type;
                   12488:        dpb->drive_accessed = 0;
                   12489:        dpb->next_dpb_ofs = 0xffff;
                   12490:        dpb->next_dpb_seg = 0xffff;
                   12491:        dpb->first_free_cluster = 0;
                   12492:        dpb->free_clusters = 0xffff;
                   12493: }
                   12494: 
1.1       root     12495: inline void msdos_int_21h_54h()
                   12496: {
                   12497:        process_t *process = msdos_process_info_get(current_psp);
                   12498:        
                   12499:        REG8(AL) = process->verify;
                   12500: }
                   12501: 
                   12502: inline void msdos_int_21h_55h()
                   12503: {
                   12504:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   12505:        
                   12506:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   12507:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   12508:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   12509:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   12510:        psp->parent_psp = current_psp;
                   12511: }
                   12512: 
                   12513: inline void msdos_int_21h_56h(int lfn)
                   12514: {
                   12515:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     12516:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   12517:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     12518:        
1.1.1.63  root     12519:        if(msdos_is_existing_file(dst) || msdos_is_existing_dir(dst)) {
                   12520:                REG16(AX) = 0x05; // access denied
                   12521:                m_CF = 1;
                   12522:        } else if(rename(src, dst)) {
1.1       root     12523:                REG16(AX) = errno;
1.1.1.3   root     12524:                m_CF = 1;
1.1       root     12525:        }
                   12526: }
                   12527: 
                   12528: inline void msdos_int_21h_57h()
                   12529: {
                   12530:        FILETIME time, local;
1.1.1.14  root     12531:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     12532:        HANDLE hHandle;
1.1       root     12533:        
1.1.1.21  root     12534:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     12535:                REG16(AX) = (UINT16)GetLastError();
                   12536:                m_CF = 1;
                   12537:                return;
                   12538:        }
                   12539:        ctime = atime = mtime = NULL;
                   12540:        
1.1       root     12541:        switch(REG8(AL)) {
                   12542:        case 0x00:
1.1.1.6   root     12543:        case 0x01:
1.1.1.14  root     12544:                mtime = &time;
1.1.1.6   root     12545:                break;
1.1.1.65! root     12546: //     case 0x02: // DOS 4.x only - Get Extended Attributes For File
        !          12547: //     case 0x03: // DOS 4.x only - Get Extended Attribute Properties
        !          12548: //             break;
1.1.1.6   root     12549:        case 0x04:
                   12550:        case 0x05:
1.1.1.14  root     12551:                atime = &time;
1.1       root     12552:                break;
1.1.1.6   root     12553:        case 0x06:
                   12554:        case 0x07:
1.1.1.14  root     12555:                ctime = &time;
                   12556:                break;
                   12557:        default:
1.1.1.22  root     12558:                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     12559:                REG16(AX) = 0x01;
                   12560:                m_CF = 1;
                   12561:                return;
                   12562:        }
                   12563:        if(REG8(AL) & 1) {
1.1       root     12564:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   12565:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     12566:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     12567:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12568:                        m_CF = 1;
1.1       root     12569:                }
1.1.1.14  root     12570:        } else {
1.1.1.21  root     12571:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     12572:                        // assume a device and use the current time
                   12573:                        GetSystemTimeAsFileTime(&time);
                   12574:                }
                   12575:                FileTimeToLocalFileTime(&time, &local);
                   12576:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     12577:        }
                   12578: }
                   12579: 
                   12580: inline void msdos_int_21h_58h()
                   12581: {
                   12582:        switch(REG8(AL)) {
                   12583:        case 0x00:
1.1.1.7   root     12584:                REG16(AX) = malloc_strategy;
                   12585:                break;
                   12586:        case 0x01:
1.1.1.24  root     12587: //             switch(REG16(BX)) {
                   12588:                switch(REG8(BL)) {
1.1.1.7   root     12589:                case 0x0000:
                   12590:                case 0x0001:
                   12591:                case 0x0002:
                   12592:                case 0x0040:
                   12593:                case 0x0041:
                   12594:                case 0x0042:
                   12595:                case 0x0080:
                   12596:                case 0x0081:
                   12597:                case 0x0082:
                   12598:                        malloc_strategy = REG16(BX);
1.1.1.23  root     12599:                        msdos_sda_update(current_psp);
1.1.1.7   root     12600:                        break;
                   12601:                default:
1.1.1.22  root     12602:                        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     12603:                        REG16(AX) = 0x01;
                   12604:                        m_CF = 1;
                   12605:                        break;
                   12606:                }
                   12607:                break;
                   12608:        case 0x02:
1.1.1.19  root     12609:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     12610:                break;
                   12611:        case 0x03:
1.1.1.24  root     12612: //             switch(REG16(BX)) {
                   12613:                switch(REG8(BL)) {
1.1.1.7   root     12614:                case 0x0000:
1.1.1.19  root     12615:                        msdos_mem_unlink_umb();
                   12616:                        break;
1.1.1.7   root     12617:                case 0x0001:
1.1.1.19  root     12618:                        msdos_mem_link_umb();
1.1.1.7   root     12619:                        break;
                   12620:                default:
1.1.1.22  root     12621:                        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     12622:                        REG16(AX) = 0x01;
                   12623:                        m_CF = 1;
                   12624:                        break;
                   12625:                }
1.1       root     12626:                break;
                   12627:        default:
1.1.1.22  root     12628:                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     12629:                REG16(AX) = 0x01;
1.1.1.3   root     12630:                m_CF = 1;
1.1       root     12631:                break;
                   12632:        }
                   12633: }
                   12634: 
                   12635: inline void msdos_int_21h_59h()
                   12636: {
1.1.1.47  root     12637:        if(REG16(BX) == 0x0000) {
                   12638:                sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   12639:                
                   12640:                REG16(AX) = sda->extended_error_code;
                   12641:                REG8(BH)  = sda->error_class;
                   12642:                REG8(BL)  = sda->suggested_action;
                   12643:                REG8(CH)  = sda->locus_of_last_error;
                   12644:                // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
                   12645:                if(sda->int21h_5d0ah_called != 0) {
                   12646:                        REG8(CL)  = sda->int21h_5d0ah_cl;
                   12647:                        REG16(DX) = sda->int21h_5d0ah_dx;
                   12648: //                     REG16(SI) = sda->int21h_5d0ah_si;
                   12649:                        REG16(DI) = sda->last_error_pointer.w.l;
                   12650: //                     SREG(DS)  = sda->int21h_5d0ah_ds;
                   12651: //                     i386_load_segment_descriptor(DS);
                   12652:                        SREG(ES)  = sda->last_error_pointer.w.h;
                   12653:                        i386_load_segment_descriptor(ES);
                   12654:                }
                   12655:                sda->int21h_5d0ah_called = 0;
                   12656: //     } else if(REG16(BX) == 0x0001) {
                   12657: //             // European MS-DOS 4.0 - Get Hard Error Information
                   12658:        } else {
                   12659:                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));
                   12660:                REG16(AX) = 0x01;
                   12661:                m_CF = 1;
                   12662:        }
1.1       root     12663: }
                   12664: 
                   12665: inline void msdos_int_21h_5ah()
                   12666: {
1.1.1.3   root     12667:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12668:        int len = strlen(path);
                   12669:        char tmp[MAX_PATH];
                   12670:        
1.1.1.60  root     12671:        if(GetTempFileNameA(path, "TMP", 0, tmp)) {
1.1       root     12672:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12673:                
1.1.1.60  root     12674:                SetFileAttributesA(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     12675:                REG16(AX) = fd;
                   12676:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12677:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12678:                
                   12679:                strcpy(path, tmp);
                   12680:                int dx = REG16(DX) + len;
1.1.1.3   root     12681:                int ds = SREG(DS);
1.1       root     12682:                while(dx > 0xffff) {
                   12683:                        dx -= 0x10;
                   12684:                        ds++;
                   12685:                }
                   12686:                REG16(DX) = dx;
1.1.1.3   root     12687:                SREG(DS) = ds;
                   12688:                i386_load_segment_descriptor(DS);
1.1       root     12689:        } else {
                   12690:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12691:                m_CF = 1;
1.1       root     12692:        }
                   12693: }
                   12694: 
                   12695: inline void msdos_int_21h_5bh()
                   12696: {
1.1.1.45  root     12697:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     12698:        
1.1.1.45  root     12699:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12700:                // already exists
                   12701:                REG16(AX) = 0x50;
1.1.1.3   root     12702:                m_CF = 1;
1.1       root     12703:        } else {
                   12704:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12705:                
                   12706:                if(fd != -1) {
1.1.1.60  root     12707:                        SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     12708:                        REG16(AX) = fd;
                   12709:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12710:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12711:                } else {
                   12712:                        REG16(AX) = errno;
1.1.1.3   root     12713:                        m_CF = 1;
1.1       root     12714:                }
                   12715:        }
                   12716: }
                   12717: 
                   12718: inline void msdos_int_21h_5ch()
                   12719: {
                   12720:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12721:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12722:        
1.1.1.20  root     12723:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     12724:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     12725:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     12726:                        UINT32 pos = _tell(fd);
                   12727:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   12728:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     12729:                                REG16(AX) = errno;
1.1.1.3   root     12730:                                m_CF = 1;
1.1       root     12731:                        }
1.1.1.20  root     12732:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     12733:                        
1.1       root     12734:                        // some seconds may be passed in _locking()
1.1.1.35  root     12735:                        REQUEST_HARDWRE_UPDATE();
1.1       root     12736:                } else {
                   12737:                        REG16(AX) = 0x01;
1.1.1.3   root     12738:                        m_CF = 1;
1.1       root     12739:                }
                   12740:        } else {
                   12741:                REG16(AX) = 0x06;
1.1.1.3   root     12742:                m_CF = 1;
1.1       root     12743:        }
                   12744: }
                   12745: 
1.1.1.22  root     12746: inline void msdos_int_21h_5dh()
                   12747: {
                   12748:        switch(REG8(AL)) {
1.1.1.65! root     12749:        case 0x00: // DOS 3.1+ internal - Server Function Call
1.1.1.45  root     12750:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
                   12751:                        // current system
                   12752:                        static bool reenter = false;
                   12753:                        if(!reenter) {
                   12754:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
                   12755:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
                   12756:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
                   12757:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
                   12758:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
                   12759:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
                   12760:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
                   12761:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
                   12762:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
                   12763:                                i386_load_segment_descriptor(DS);
                   12764:                                i386_load_segment_descriptor(ES);
                   12765:                                reenter = true;
                   12766:                                try {
                   12767:                                        msdos_syscall(0x21);
                   12768:                                } catch(...) {
                   12769:                                }
                   12770:                                reenter = false;
                   12771:                        }
                   12772:                } else {
                   12773:                        REG16(AX) = 0x49; //  network software not installed
                   12774:                        m_CF = 1;
                   12775:                }
                   12776:                break;
1.1.1.65! root     12777: //     case 0x01: // DOS 3.1+ internal - Commit All Files For Specified Computer/Process
        !          12778: //     case 0x02: // DOS 3.1+ internal - SHARE.EXE - Close File By Name
        !          12779: //     case 0x03: // DOS 3.1+ internal - SHARE.EXE - Close ALL Files For Given Computer
        !          12780: //     case 0x04: // DOS 3.1+ internal - SHARE.EXE - Close ALL Files For Given Process
        !          12781: //     case 0x05: // DOS 3.1+ internal - SHARE.EXE - Get Open File List Entry
        !          12782:        case 0x06: // DOS 3.0+ internal - Get Address Of DOS Swappable Data Area
1.1.1.23  root     12783:                SREG(DS) = (SDA_TOP >> 4);
                   12784:                i386_load_segment_descriptor(DS);
                   12785:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   12786:                REG16(CX) = 0x80;
                   12787:                REG16(DX) = 0x1a;
                   12788:                break;
1.1.1.65! root     12789:        case 0x07: // DOS 3.1+ network - Get Redirected Printer Mode
        !          12790:        case 0x08: // DOS 3.1+ network - Set Redirected Printer Mode
        !          12791:        case 0x09: // DOS 3.1+ network - Flush Redirected Printer Output
1.1.1.45  root     12792:                REG16(AX) = 0x49; //  network software not installed
                   12793:                m_CF = 1;
                   12794:                break;
1.1.1.65! root     12795:        case 0x0a: // DOS 3.1+ - Set Extended Error Information
1.1.1.43  root     12796:                {
                   12797:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     12798:                        sda->int21h_5d0ah_called    = 1;
1.1.1.43  root     12799:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47  root     12800:                        // XXX: which one is correct ???
                   12801: #if 1
                   12802:                        // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43  root     12803:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   12804:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47  root     12805:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43  root     12806:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47  root     12807: #else
                   12808:                        // PC DOS 7 Technical Update
                   12809:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
                   12810:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
                   12811:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
                   12812:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
                   12813: #endif
                   12814:                        sda->int21h_5d0ah_dx        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   12815: //                     sda->int21h_5d0ah_si        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43  root     12816:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47  root     12817: //                     sda->int21h_5d0ah_ds        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43  root     12818:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   12819:                }
                   12820:                break;
1.1.1.65! root     12821:        case 0x0b: // DOS 4.x only - internal - Get DOS Swappable Data Areas
1.1.1.22  root     12822:                REG16(AX) = 0x01;
                   12823:                m_CF = 1;
                   12824:                break;
                   12825:        default:
                   12826:                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));
                   12827:                REG16(AX) = 0x01;
                   12828:                m_CF = 1;
                   12829:                break;
                   12830:        }
                   12831: }
                   12832: 
1.1.1.42  root     12833: inline void msdos_int_21h_5eh()
                   12834: {
                   12835:        switch(REG8(AL)) {
1.1.1.65! root     12836:        case 0x00: // DOS 3.1+ network - Get Machine Name
1.1.1.42  root     12837:                {
                   12838:                        char name[256] = {0};
                   12839:                        DWORD dwSize = 256;
                   12840:                        
1.1.1.60  root     12841:                        if(GetComputerNameA(name, &dwSize)) {
1.1.1.42  root     12842:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12843:                                for(int i = 0; i < 15; i++) {
                   12844:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   12845:                                }
                   12846:                                dest[15] = '\0';
                   12847:                                REG8(CH) = 0x01; // nonzero valid
                   12848:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   12849:                        } else {
                   12850:                                REG16(AX) = 0x01;
                   12851:                                m_CF = 1;
                   12852:                        }
                   12853:                }
                   12854:                break;
1.1.1.65! root     12855: //     case 0x01: // DOS 3.1+ network - Set Machine Name
        !          12856: //     case 0x02: // DOS 3.1+ network - Set Network Printer Setup String
        !          12857: //     case 0x03: // DOS 3.1+ network - Get Network Printer Setup String
        !          12858: //     case 0x04: // DOS 3.1+ network - Set Printer Mode
        !          12859: //     case 0x05: // DOS 3.1+ network - Get Printer Mode
1.1.1.42  root     12860:        default:
1.1.1.45  root     12861: //             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));
                   12862: //             REG16(AX) = 0x01;
                   12863:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     12864:                m_CF = 1;
                   12865:                break;
                   12866:        }
                   12867: }
                   12868: 
1.1.1.30  root     12869: inline void msdos_int_21h_5fh()
                   12870: {
                   12871:        switch(REG8(AL)) {
1.1.1.65! root     12872: //     case 0x00: // DOS 3.1+ network - Get Redirection Mode
        !          12873: //     case 0x01: // DOS 3.1+ network - Set Redirection Mode
        !          12874:        case 0x05: // DOS 4.0+ network - Get Extended Redirection List Entry
1.1.1.44  root     12875:                REG16(BP) = 0;
                   12876:                for(int i = 0; i < 26; i++) {
                   12877:                        if(msdos_is_remote_drive(i)) {
                   12878:                                REG16(BP)++;
1.1.1.42  root     12879:                        }
                   12880:                }
1.1.1.65! root     12881:        case 0x02: // DOS 3.1+ network - Get Redirection List Entry
1.1.1.44  root     12882:                for(int i = 0, index = 0; i < 26; i++) {
                   12883:                        if(msdos_is_remote_drive(i)) {
                   12884:                                if(index == REG16(BX)) {
                   12885:                                        char volume[] = "A:";
1.1.1.30  root     12886:                                        volume[0] = 'A' + i;
1.1.1.44  root     12887:                                        DWORD dwSize = 128;
                   12888:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
1.1.1.60  root     12889:                                        WNetGetConnectionA(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
1.1.1.44  root     12890:                                        REG8(BH) = 0x00; // valid
                   12891:                                        REG8(BL) = 0x04; // disk drive
                   12892:                                        REG16(CX) = 0x00; // LANtastic
                   12893:                                        return;
1.1.1.30  root     12894:                                }
1.1.1.44  root     12895:                                index++;
1.1.1.30  root     12896:                        }
                   12897:                }
                   12898:                REG16(AX) = 0x12; // no more files
                   12899:                m_CF = 1;
                   12900:                break;
1.1.1.65! root     12901: //     case 0x03: // DOS 3.1+ network - Redirect Device
        !          12902: //     case 0x04: // DOS 3.1+ network - Cancel Redirection
        !          12903: //     case 0x06: // Network - Get Full Redirection List
        !          12904:        case 0x07: // DOS 5+ - Enable Drive
1.1.1.44  root     12905:                if(msdos_is_valid_drive(REG8(DL))) {
                   12906:                        msdos_cds_update(REG8(DL));
                   12907:                } else {
                   12908:                        REG16(AX) = 0x0f; // invalid drive
                   12909:                        m_CF = 1;
                   12910:                }
                   12911:                break;
1.1.1.65! root     12912:        case 0x08: // DOS 5+ - Disable Drive
1.1.1.44  root     12913:                if(msdos_is_valid_drive(REG8(DL))) {
                   12914:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   12915:                        cds->drive_attrib = 0x0000;
                   12916:                } else {
                   12917:                        REG16(AX) = 0x0f; // invalid drive
                   12918:                        m_CF = 1;
                   12919:                }
                   12920:                break;
1.1.1.30  root     12921:        default:
1.1.1.45  root     12922: //             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));
                   12923: //             REG16(AX) = 0x01;
                   12924:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     12925:                m_CF = 1;
                   12926:                break;
                   12927:        }
                   12928: }
                   12929: 
1.1       root     12930: inline void msdos_int_21h_60h(int lfn)
                   12931: {
1.1.1.45  root     12932:        char full[MAX_PATH];
                   12933:        const char *path = NULL;
1.1.1.14  root     12934:        
1.1       root     12935:        if(lfn) {
1.1.1.14  root     12936:                char *name;
                   12937:                *full = '\0';
1.1.1.60  root     12938:                GetFullPathNameA((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     12939:                switch(REG8(CL)) {
                   12940:                case 1:
1.1.1.60  root     12941:                        GetShortPathNameA(full, full, MAX_PATH);
1.1.1.14  root     12942:                        my_strupr(full);
                   12943:                        break;
                   12944:                case 2:
1.1.1.60  root     12945:                        GetLongPathNameA(full, full, MAX_PATH);
1.1.1.14  root     12946:                        break;
                   12947:                }
                   12948:                path = full;
                   12949:        } else {
                   12950:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12951:        }
                   12952:        if(*path != '\0') {
                   12953:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     12954:        } else {
1.1.1.14  root     12955:                REG16(AX) = (UINT16)GetLastError();
                   12956:                m_CF = 1;
1.1       root     12957:        }
                   12958: }
                   12959: 
                   12960: inline void msdos_int_21h_61h()
                   12961: {
                   12962:        REG8(AL) = 0;
                   12963: }
                   12964: 
                   12965: inline void msdos_int_21h_62h()
                   12966: {
                   12967:        REG16(BX) = current_psp;
                   12968: }
                   12969: 
                   12970: inline void msdos_int_21h_63h()
                   12971: {
                   12972:        switch(REG8(AL)) {
                   12973:        case 0x00:
1.1.1.3   root     12974:                SREG(DS) = (DBCS_TABLE >> 4);
                   12975:                i386_load_segment_descriptor(DS);
1.1       root     12976:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   12977:                REG8(AL) = 0x00;
                   12978:                break;
1.1.1.22  root     12979:        case 0x01: // set korean input mode
                   12980:        case 0x02: // get korean input mode
                   12981:                REG8(AL) = 0xff; // not supported
                   12982:                break;
1.1       root     12983:        default:
1.1.1.22  root     12984:                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     12985:                REG16(AX) = 0x01;
1.1.1.3   root     12986:                m_CF = 1;
1.1       root     12987:                break;
                   12988:        }
                   12989: }
                   12990: 
1.1.1.25  root     12991: UINT16 get_extended_country_info(UINT8 func)
1.1       root     12992: {
1.1.1.25  root     12993:        switch(func) {
1.1.1.17  root     12994:        case 0x01:
                   12995:                if(REG16(CX) >= 5) {
1.1.1.19  root     12996:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     12997:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   12998:                                REG16(CX) = sizeof(data);
                   12999:                        ZeroMemory(data, sizeof(data));
                   13000:                        data[0] = 0x01;
                   13001:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     13002:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     13003:                        *(UINT16 *)(data + 5) = active_code_page;
                   13004:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     13005: //                     REG16(AX) = active_code_page;
1.1.1.17  root     13006:                } else {
1.1.1.25  root     13007:                        return(0x08); // insufficient memory
1.1.1.17  root     13008:                }
                   13009:                break;
                   13010:        case 0x02:
                   13011:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   13012:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   13013:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     13014: //             REG16(AX) = active_code_page;
1.1.1.17  root     13015:                REG16(CX) = 0x05;
                   13016:                break;
1.1.1.23  root     13017:        case 0x03:
                   13018:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   13019:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   13020:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     13021: //             REG16(AX) = active_code_page;
1.1.1.23  root     13022:                REG16(CX) = 0x05;
                   13023:                break;
1.1.1.17  root     13024:        case 0x04:
                   13025:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   13026:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   13027:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     13028: //             REG16(AX) = active_code_page;
1.1.1.17  root     13029:                REG16(CX) = 0x05;
                   13030:                break;
                   13031:        case 0x05:
                   13032:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   13033:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   13034:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     13035: //             REG16(AX) = active_code_page;
1.1.1.17  root     13036:                REG16(CX) = 0x05;
                   13037:                break;
                   13038:        case 0x06:
                   13039:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   13040:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   13041:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     13042: //             REG16(AX) = active_code_page;
1.1.1.17  root     13043:                REG16(CX) = 0x05;
                   13044:                break;
1.1       root     13045:        case 0x07:
1.1.1.3   root     13046:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   13047:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   13048:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     13049: //             REG16(AX) = active_code_page;
1.1       root     13050:                REG16(CX) = 0x05;
                   13051:                break;
1.1.1.25  root     13052:        default:
                   13053:                return(0x01); // function number invalid
                   13054:        }
                   13055:        return(0x00);
                   13056: }
                   13057: 
                   13058: inline void msdos_int_21h_65h()
                   13059: {
                   13060:        char tmp[0x10000];
                   13061:        
                   13062:        switch(REG8(AL)) {
1.1.1.43  root     13063:        case 0x00:
                   13064:                if(REG16(CX) >= 7) {
                   13065:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   13066:                        REG16(AX) = system_code_page;
                   13067:                } else {
                   13068:                        REG16(AX) = 0x0c;
                   13069:                        m_CF = 1;
                   13070:                }
                   13071:                break;
1.1.1.25  root     13072:        case 0x01:
                   13073:        case 0x02:
                   13074:        case 0x03:
                   13075:        case 0x04:
                   13076:        case 0x05:
                   13077:        case 0x06:
                   13078:        case 0x07:
                   13079:                {
                   13080:                        UINT16 result = get_extended_country_info(REG8(AL));
                   13081:                        if(result) {
                   13082:                                REG16(AX) = result;
                   13083:                                m_CF = 1;
                   13084:                        } else {
                   13085:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   13086:                        }
                   13087:                }
                   13088:                break;
1.1       root     13089:        case 0x20:
1.1.1.25  root     13090:        case 0xa0:
1.1.1.19  root     13091:                memset(tmp, 0, sizeof(tmp));
                   13092:                tmp[0] = REG8(DL);
1.1       root     13093:                my_strupr(tmp);
                   13094:                REG8(DL) = tmp[0];
                   13095:                break;
                   13096:        case 0x21:
1.1.1.25  root     13097:        case 0xa1:
1.1       root     13098:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13099:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     13100:                my_strupr(tmp);
1.1.1.3   root     13101:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     13102:                break;
                   13103:        case 0x22:
1.1.1.25  root     13104:        case 0xa2:
1.1.1.3   root     13105:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     13106:                break;
1.1.1.25  root     13107:        case 0x23:
                   13108:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45  root     13109:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     13110:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45  root     13111:                        REG16(AX) = 0x00;
                   13112:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   13113:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     13114:                        REG16(AX) = 0x01;
                   13115:                } else {
                   13116:                        REG16(AX) = 0x02;
                   13117:                }
                   13118:                break;
1.1       root     13119:        default:
1.1.1.22  root     13120:                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     13121:                REG16(AX) = 0x01;
1.1.1.3   root     13122:                m_CF = 1;
1.1       root     13123:                break;
                   13124:        }
                   13125: }
                   13126: 
                   13127: inline void msdos_int_21h_66h()
                   13128: {
                   13129:        switch(REG8(AL)) {
                   13130:        case 0x01:
                   13131:                REG16(BX) = active_code_page;
                   13132:                REG16(DX) = system_code_page;
                   13133:                break;
                   13134:        case 0x02:
                   13135:                if(active_code_page == REG16(BX)) {
                   13136:                        REG16(AX) = 0xeb41;
                   13137:                } else if(_setmbcp(REG16(BX)) == 0) {
                   13138:                        active_code_page = REG16(BX);
1.1.1.17  root     13139:                        msdos_nls_tables_update();
1.1       root     13140:                        REG16(AX) = 0xeb41;
1.1.1.32  root     13141:                        SetConsoleCP(active_code_page);
                   13142:                        SetConsoleOutputCP(active_code_page);
1.1       root     13143:                } else {
                   13144:                        REG16(AX) = 0x25;
1.1.1.3   root     13145:                        m_CF = 1;
1.1       root     13146:                }
                   13147:                break;
                   13148:        default:
1.1.1.22  root     13149:                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     13150:                REG16(AX) = 0x01;
1.1.1.3   root     13151:                m_CF = 1;
1.1       root     13152:                break;
                   13153:        }
                   13154: }
                   13155: 
                   13156: inline void msdos_int_21h_67h()
                   13157: {
                   13158:        process_t *process = msdos_process_info_get(current_psp);
                   13159:        
                   13160:        if(REG16(BX) <= MAX_FILES) {
                   13161:                process->max_files = max(REG16(BX), 20);
                   13162:        } else {
                   13163:                REG16(AX) = 0x08;
1.1.1.3   root     13164:                m_CF = 1;
1.1       root     13165:        }
                   13166: }
                   13167: 
                   13168: inline void msdos_int_21h_68h()
                   13169: {
                   13170:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     13171:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     13172:        
1.1.1.20  root     13173:        if(fd < process->max_files && file_handler[fd].valid) {
                   13174:                // fflush(_fdopen(fd, ""));
1.1       root     13175:        } else {
                   13176:                REG16(AX) = 0x06;
1.1.1.3   root     13177:                m_CF = 1;
1.1       root     13178:        }
                   13179: }
                   13180: 
                   13181: inline void msdos_int_21h_69h()
                   13182: {
1.1.1.3   root     13183:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13184:        char path[] = "A:\\";
                   13185:        char volume_label[MAX_PATH];
                   13186:        DWORD serial_number = 0;
                   13187:        char file_system[MAX_PATH];
                   13188:        
                   13189:        if(REG8(BL) == 0) {
                   13190:                path[0] = 'A' + _getdrive() - 1;
                   13191:        } else {
                   13192:                path[0] = 'A' + REG8(BL) - 1;
                   13193:        }
                   13194:        
                   13195:        switch(REG8(AL)) {
                   13196:        case 0x00:
1.1.1.60  root     13197:                if(GetVolumeInformationA(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
1.1       root     13198:                        info->info_level = 0;
                   13199:                        info->serial_number = serial_number;
                   13200:                        memset(info->volume_label, 0x20, 11);
                   13201:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   13202:                        memset(info->file_system, 0x20, 8);
                   13203:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   13204:                } else {
                   13205:                        REG16(AX) = errno;
1.1.1.3   root     13206:                        m_CF = 1;
1.1       root     13207:                }
                   13208:                break;
                   13209:        case 0x01:
                   13210:                REG16(AX) = 0x03;
1.1.1.3   root     13211:                m_CF = 1;
1.1.1.45  root     13212:                break;
                   13213:        default:
                   13214:                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));
                   13215:                REG16(AX) = 0x01;
                   13216:                m_CF = 1;
                   13217:                break;
1.1       root     13218:        }
                   13219: }
                   13220: 
                   13221: inline void msdos_int_21h_6ah()
                   13222: {
                   13223:        REG8(AH) = 0x68;
                   13224:        msdos_int_21h_68h();
                   13225: }
                   13226: 
                   13227: inline void msdos_int_21h_6bh()
                   13228: {
1.1.1.45  root     13229:        REG8(AL) = 0x00;
1.1       root     13230: }
                   13231: 
                   13232: inline void msdos_int_21h_6ch(int lfn)
                   13233: {
1.1.1.45  root     13234:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     13235:        int mode = REG8(BL) & 0x03;
                   13236:        
                   13237:        if(mode < 0x03) {
1.1.1.29  root     13238:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     13239:                        // file exists
                   13240:                        if(REG8(DL) & 1) {
1.1.1.37  root     13241:                                int fd = -1;
                   13242:                                int sio_port = 0;
                   13243:                                int lpt_port = 0;
1.1       root     13244:                                
1.1.1.45  root     13245:                                if(msdos_is_device_path(path)) {
                   13246:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     13247:                                } else {
1.1.1.13  root     13248:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     13249:                                }
1.1       root     13250:                                if(fd != -1) {
                   13251:                                        REG16(AX) = fd;
                   13252:                                        REG16(CX) = 1;
1.1.1.45  root     13253:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     13254:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13255:                                } else {
                   13256:                                        REG16(AX) = errno;
1.1.1.3   root     13257:                                        m_CF = 1;
1.1       root     13258:                                }
                   13259:                        } else if(REG8(DL) & 2) {
1.1.1.60  root     13260:                                int attr = GetFileAttributesA(path);
1.1.1.37  root     13261:                                int fd = -1;
                   13262:                                int sio_port = 0;
                   13263:                                int lpt_port = 0;
1.1       root     13264:                                
1.1.1.45  root     13265:                                if(msdos_is_device_path(path)) {
                   13266:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     13267:                                } else {
                   13268:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   13269:                                }
                   13270:                                if(fd != -1) {
                   13271:                                        if(attr == -1) {
                   13272:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   13273:                                        }
1.1.1.60  root     13274:                                        SetFileAttributesA(path, attr);
1.1       root     13275:                                        REG16(AX) = fd;
                   13276:                                        REG16(CX) = 3;
1.1.1.45  root     13277:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     13278:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13279:                                } else {
                   13280:                                        REG16(AX) = errno;
1.1.1.3   root     13281:                                        m_CF = 1;
1.1       root     13282:                                }
                   13283:                        } else {
                   13284:                                REG16(AX) = 0x50;
1.1.1.3   root     13285:                                m_CF = 1;
1.1       root     13286:                        }
                   13287:                } else {
                   13288:                        // file not exists
                   13289:                        if(REG8(DL) & 0x10) {
                   13290:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   13291:                                
                   13292:                                if(fd != -1) {
1.1.1.60  root     13293:                                        SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     13294:                                        REG16(AX) = fd;
                   13295:                                        REG16(CX) = 2;
                   13296:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     13297:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13298:                                } else {
                   13299:                                        REG16(AX) = errno;
1.1.1.3   root     13300:                                        m_CF = 1;
1.1       root     13301:                                }
                   13302:                        } else {
                   13303:                                REG16(AX) = 0x02;
1.1.1.3   root     13304:                                m_CF = 1;
1.1       root     13305:                        }
                   13306:                }
                   13307:        } else {
                   13308:                REG16(AX) = 0x0c;
1.1.1.3   root     13309:                m_CF = 1;
1.1       root     13310:        }
                   13311: }
                   13312: 
1.1.1.43  root     13313: inline void msdos_int_21h_70h()
                   13314: {
                   13315:        switch(REG8(AL)) {
1.1.1.48  root     13316:        case 0x00: // get ??? info
                   13317:        case 0x01: // set above info
                   13318: //             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));
                   13319:                REG16(AX) = 0x7000;
                   13320:                m_CF = 1;
                   13321:                break;
                   13322:        case 0x02: // set general internationalization info
1.1.1.43  root     13323:                if(REG16(CX) >= 7) {
                   13324:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   13325:                        msdos_nls_tables_update();
                   13326:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   13327:                        REG16(AX) = system_code_page;
                   13328:                } else {
                   13329:                        REG16(AX) = 0x0c;
                   13330:                        m_CF = 1;
                   13331:                }
                   13332:                break;
                   13333:        default:
                   13334:                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     13335:                REG16(AX) = 0x7000;
1.1.1.43  root     13336:                m_CF = 1;
                   13337:                break;
                   13338:        }
                   13339: }
                   13340: 
1.1       root     13341: inline void msdos_int_21h_710dh()
                   13342: {
                   13343:        // reset drive
                   13344: }
                   13345: 
1.1.1.48  root     13346: inline void msdos_int_21h_7141h()
1.1.1.17  root     13347: {
                   13348:        if(REG16(SI) == 0) {
1.1.1.48  root     13349:                msdos_int_21h_41h(1);
1.1.1.17  root     13350:                return;
                   13351:        }
                   13352:        if(REG16(SI) != 1) {
                   13353:                REG16(AX) = 5;
                   13354:                m_CF = 1;
                   13355:        }
                   13356:        /* wild card and matching attributes... */
                   13357:        char tmp[MAX_PATH * 2];
                   13358:        // copy search pathname (and quick check overrun)
                   13359:        ZeroMemory(tmp, sizeof(tmp));
                   13360:        tmp[MAX_PATH - 1] = '\0';
                   13361:        tmp[MAX_PATH] = 1;
                   13362:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   13363:        
                   13364:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   13365:                REG16(AX) = 1;
                   13366:                m_CF = 1;
                   13367:                return;
                   13368:        }
                   13369:        for(char *s = tmp; *s; ++s) {
                   13370:                if(*s == '/') {
                   13371:                        *s = '\\';
                   13372:                }
                   13373:        }
1.1.1.60  root     13374:        char *tmp_name = my_strrchr(tmp, '\\');
1.1.1.17  root     13375:        if(tmp_name) {
                   13376:                ++tmp_name;
                   13377:        } else {
                   13378:                tmp_name = strchr(tmp, ':');
                   13379:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   13380:        }
                   13381:        
                   13382:        WIN32_FIND_DATAA fd;
                   13383:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   13384:        if(fh == INVALID_HANDLE_VALUE) {
                   13385:                REG16(AX) = 2;
                   13386:                m_CF = 1;
                   13387:                return;
                   13388:        }
                   13389:        do {
                   13390:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   13391:                        strcpy(tmp_name, fd.cFileName);
1.1.1.48  root     13392:                        if(remove(msdos_trimmed_path(tmp, 1))) {
1.1.1.17  root     13393:                                REG16(AX) = 5;
                   13394:                                m_CF = 1;
                   13395:                                break;
                   13396:                        }
                   13397:                }
                   13398:        } while(FindNextFileA(fh, &fd));
                   13399:        if(!m_CF) {
                   13400:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   13401:                        m_CF = 1;
                   13402:                        REG16(AX) = 2;
                   13403:                }
                   13404:        }
                   13405:        FindClose(fh);
                   13406: }
                   13407: 
1.1       root     13408: inline void msdos_int_21h_714eh()
                   13409: {
                   13410:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13411:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   13412:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.60  root     13413:        WIN32_FIND_DATAA fd;
1.1       root     13414:        
1.1.1.13  root     13415:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   13416:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13417:                FindClose(dtainfo->find_handle);
                   13418:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13419:        }
                   13420:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     13421:        dtainfo->allowable_mask = REG8(CL);
                   13422:        dtainfo->required_mask = REG8(CH);
                   13423:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     13424:        
1.1.1.14  root     13425:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   13426:                dtainfo->allowable_mask &= ~8;
1.1       root     13427:        }
1.1.1.60  root     13428:        if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14  root     13429:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.60  root     13430:                        if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     13431:                                FindClose(dtainfo->find_handle);
                   13432:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13433:                                break;
                   13434:                        }
                   13435:                }
                   13436:        }
1.1.1.13  root     13437:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13438:                find->attrib = fd.dwFileAttributes;
                   13439:                msdos_find_file_conv_local_time(&fd);
                   13440:                if(REG16(SI) == 0) {
                   13441:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13442:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13443:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13444:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13445:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13446:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13447:                } else {
                   13448:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13449:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13450:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13451:                }
                   13452:                find->size_hi = fd.nFileSizeHigh;
                   13453:                find->size_lo = fd.nFileSizeLow;
                   13454:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13455:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13456:                REG16(AX) = dtainfo - dtalist + 1;
                   13457:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13458:                // volume label
                   13459:                find->attrib = 8;
                   13460:                find->size_hi = find->size_lo = 0;
                   13461:                strcpy(find->full_name, process->volume_label);
                   13462:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13463:                dtainfo->allowable_mask &= ~8;
                   13464:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     13465:        } else {
                   13466:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     13467:                m_CF = 1;
1.1       root     13468:        }
                   13469: }
                   13470: 
                   13471: inline void msdos_int_21h_714fh()
                   13472: {
                   13473:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13474:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1.1.60  root     13475:        WIN32_FIND_DATAA fd;
1.1       root     13476:        
1.1.1.14  root     13477:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13478:                REG16(AX) = 6;
1.1.1.13  root     13479:                m_CF = 1;
                   13480:                return;
                   13481:        }
1.1.1.14  root     13482:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13483:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60  root     13484:                if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14  root     13485:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.60  root     13486:                                if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     13487:                                        FindClose(dtainfo->find_handle);
                   13488:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13489:                                        break;
                   13490:                                }
                   13491:                        }
                   13492:                } else {
1.1.1.13  root     13493:                        FindClose(dtainfo->find_handle);
                   13494:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13495:                }
                   13496:        }
1.1.1.13  root     13497:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13498:                find->attrib = fd.dwFileAttributes;
                   13499:                msdos_find_file_conv_local_time(&fd);
                   13500:                if(REG16(SI) == 0) {
                   13501:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13502:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13503:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13504:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13505:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13506:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13507:                } else {
                   13508:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13509:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13510:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13511:                }
                   13512:                find->size_hi = fd.nFileSizeHigh;
                   13513:                find->size_lo = fd.nFileSizeLow;
                   13514:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13515:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13516:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13517:                // volume label
                   13518:                find->attrib = 8;
                   13519:                find->size_hi = find->size_lo = 0;
                   13520:                strcpy(find->full_name, process->volume_label);
                   13521:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13522:                dtainfo->allowable_mask &= ~8;
1.1       root     13523:        } else {
                   13524:                REG16(AX) = 0x12;
1.1.1.3   root     13525:                m_CF = 1;
1.1       root     13526:        }
                   13527: }
                   13528: 
                   13529: inline void msdos_int_21h_71a0h()
                   13530: {
                   13531:        DWORD max_component_len, file_sys_flag;
                   13532:        
1.1.1.60  root     13533:        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     13534:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   13535:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     13536:                REG16(CX) = (UINT16)max_component_len;          // 255
                   13537:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   13538:        } else {
                   13539:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13540:                m_CF = 1;
1.1       root     13541:        }
                   13542: }
                   13543: 
                   13544: inline void msdos_int_21h_71a1h()
                   13545: {
1.1.1.14  root     13546:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13547:                REG16(AX) = 6;
1.1.1.13  root     13548:                m_CF = 1;
                   13549:                return;
                   13550:        }
1.1.1.14  root     13551:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13552:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13553:                FindClose(dtainfo->find_handle);
                   13554:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13555:        }
                   13556: }
                   13557: 
                   13558: inline void msdos_int_21h_71a6h()
                   13559: {
                   13560:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     13561:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13562:        
1.1.1.3   root     13563:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13564:        struct _stat64 status;
                   13565:        DWORD serial_number = 0;
                   13566:        
1.1.1.20  root     13567:        if(fd < process->max_files && file_handler[fd].valid) {
                   13568:                if(_fstat64(fd, &status) == 0) {
                   13569:                        if(file_handler[fd].path[1] == ':') {
1.1       root     13570:                                // NOTE: we need to consider the network file path "\\host\share\"
                   13571:                                char volume[] = "A:\\";
1.1.1.20  root     13572:                                volume[0] = file_handler[fd].path[1];
1.1.1.60  root     13573:                                GetVolumeInformationA(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
1.1       root     13574:                        }
1.1.1.60  root     13575:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributesA(file_handler[fd].path);
1.1       root     13576:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   13577:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   13578:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   13579:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   13580:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   13581:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   13582:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   13583:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   13584:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   13585:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     13586:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     13587:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     13588:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     13589:                } else {
                   13590:                        REG16(AX) = errno;
1.1.1.3   root     13591:                        m_CF = 1;
1.1       root     13592:                }
                   13593:        } else {
                   13594:                REG16(AX) = 0x06;
1.1.1.3   root     13595:                m_CF = 1;
1.1       root     13596:        }
                   13597: }
                   13598: 
                   13599: inline void msdos_int_21h_71a7h()
                   13600: {
                   13601:        switch(REG8(BL)) {
                   13602:        case 0x00:
1.1.1.3   root     13603:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     13604:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13605:                        m_CF = 1;
1.1       root     13606:                }
                   13607:                break;
                   13608:        case 0x01:
                   13609:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     13610:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     13611:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13612:                        m_CF = 1;
1.1       root     13613:                }
                   13614:                break;
                   13615:        default:
1.1.1.22  root     13616:                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     13617:                REG16(AX) = 0x7100;
1.1.1.3   root     13618:                m_CF = 1;
1.1       root     13619:                break;
                   13620:        }
                   13621: }
                   13622: 
                   13623: inline void msdos_int_21h_71a8h()
                   13624: {
                   13625:        if(REG8(DH) == 0) {
                   13626:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     13627:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13628:                memset(fcb, 0x20, sizeof(fcb));
                   13629:                int len = strlen(tmp);
1.1.1.21  root     13630:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     13631:                        if(tmp[i] == '.') {
                   13632:                                pos = 8;
                   13633:                        } else {
                   13634:                                if(msdos_lead_byte_check(tmp[i])) {
                   13635:                                        fcb[pos++] = tmp[i++];
                   13636:                                }
                   13637:                                fcb[pos++] = tmp[i];
                   13638:                        }
                   13639:                }
1.1.1.3   root     13640:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     13641:        } else {
1.1.1.3   root     13642:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13643:        }
                   13644: }
                   13645: 
1.1.1.22  root     13646: inline void msdos_int_21h_71aah()
                   13647: {
                   13648:        char drv[] = "A:", path[MAX_PATH];
                   13649:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13650:        
                   13651:        if(REG8(BL) == 0) {
                   13652:                drv[0] = 'A' + _getdrive() - 1;
                   13653:        } else {
                   13654:                drv[0] = 'A' + REG8(BL) - 1;
                   13655:        }
                   13656:        switch(REG8(BH)) {
                   13657:        case 0x00:
1.1.1.44  root     13658:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13659:                        REG16(AX) = 0x0f; // invalid drive
                   13660:                        m_CF = 1;
1.1.1.60  root     13661:                } else if(DefineDosDeviceA(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
1.1.1.44  root     13662:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     13663:                        m_CF = 1;
                   13664:                }
                   13665:                break;
                   13666:        case 0x01:
1.1.1.44  root     13667:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13668:                        REG16(AX) = 0x0f; // invalid drive
                   13669:                        m_CF = 1;
1.1.1.60  root     13670:                } else if(DefineDosDeviceA(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     13671:                        REG16(AX) = 0x0f; // invalid drive
                   13672:                        m_CF = 1;
                   13673:                }
                   13674:                break;
                   13675:        case 0x02:
1.1.1.44  root     13676:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13677:                        REG16(AX) = 0x0f; // invalid drive
                   13678:                        m_CF = 1;
1.1.1.60  root     13679:                } else if(QueryDosDeviceA(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     13680:                        REG16(AX) = 0x0f; // invalid drive
                   13681:                        m_CF = 1;
                   13682:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   13683:                        REG16(AX) = 0x0f; // invalid drive
                   13684:                        m_CF = 1;
                   13685:                } else {
                   13686:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   13687:                }
                   13688:                break;
                   13689:        default:
                   13690:                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     13691:                REG16(AX) = 0x7100;
1.1.1.22  root     13692:                m_CF = 1;
                   13693:                break;
                   13694:        }
                   13695: }
                   13696: 
1.1.1.14  root     13697: inline void msdos_int_21h_7300h()
                   13698: {
1.1.1.44  root     13699:        REG8(AL) = REG8(CL);
                   13700:        REG8(AH) = 0;
1.1.1.14  root     13701: }
                   13702: 
                   13703: inline void msdos_int_21h_7302h()
                   13704: {
                   13705:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   13706:        UINT16 seg, ofs;
                   13707:        
                   13708:        if(REG16(CX) < 0x3f) {
                   13709:                REG8(AL) = 0x18;
                   13710:                m_CF = 1;
                   13711:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   13712:                REG8(AL) = 0xff;
                   13713:                m_CF = 1;
                   13714:        } else {
                   13715:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   13716:        }
                   13717: }
                   13718: 
1.1       root     13719: inline void msdos_int_21h_7303h()
                   13720: {
1.1.1.3   root     13721:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13722:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     13723:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   13724:        
1.1.1.60  root     13725:        if(GetDiskFreeSpaceA(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
1.1       root     13726:                info->size_of_structure = sizeof(ext_space_info_t);
                   13727:                info->structure_version = 0;
                   13728:                info->sectors_per_cluster = sectors_per_cluster;
                   13729:                info->bytes_per_sector = bytes_per_sector;
                   13730:                info->available_clusters_on_drive = free_clusters;
                   13731:                info->total_clusters_on_drive = total_clusters;
                   13732:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   13733:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   13734:                info->available_allocation_units = free_clusters;       // ???
                   13735:                info->total_allocation_units = total_clusters;          // ???
                   13736:        } else {
                   13737:                REG16(AX) = errno;
1.1.1.3   root     13738:                m_CF = 1;
1.1       root     13739:        }
                   13740: }
                   13741: 
1.1.1.30  root     13742: inline void msdos_int_21h_dbh()
                   13743: {
                   13744:        // Novell NetWare - Workstation - Get Number of Local Drives
                   13745:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   13746:        REG8(AL) = dos_info->last_drive;
                   13747: }
                   13748: 
                   13749: inline void msdos_int_21h_dch()
                   13750: {
                   13751:        // Novell NetWare - Connection Services - Get Connection Number
                   13752:        REG8(AL) = 0x00;
                   13753: }
                   13754: 
1.1.1.32  root     13755: inline void msdos_int_24h()
                   13756: {
                   13757:        const char *message = NULL;
                   13758:        int key = 0;
                   13759:        
                   13760:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   13761:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   13762:                        if(active_code_page == 932) {
                   13763:                                message = critical_error_table[i].message_japanese;
                   13764:                        }
                   13765:                        if(message == NULL) {
                   13766:                                message = critical_error_table[i].message_english;
                   13767:                        }
                   13768:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   13769:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   13770:                        
                   13771:                        SREG(ES) = WORK_TOP >> 4;
                   13772:                        i386_load_segment_descriptor(ES);
                   13773:                        REG16(DI) = 0x0000;
                   13774:                        break;
                   13775:                }
                   13776:        }
                   13777:        fprintf(stderr, "\n%s", message);
                   13778:        if(!(REG8(AH) & 0x80)) {
                   13779:                if(REG8(AH) & 0x01) {
                   13780:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   13781:                } else {
                   13782:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   13783:                }
                   13784:        }
                   13785:        fprintf(stderr, "\n");
                   13786:        
1.1.1.33  root     13787:        {
1.1.1.32  root     13788:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     13789:        }
1.1.1.32  root     13790:        if(REG8(AH) & 0x10) {
                   13791:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   13792:        }
                   13793:        if(REG8(AH) & 0x20) {
                   13794:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   13795:        }
                   13796:        if(REG8(AH) & 0x08) {
                   13797:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   13798:        }
                   13799:        fprintf(stderr, "? ");
                   13800:        
                   13801:        while(1) {
                   13802:                while(!_kbhit()) {
                   13803:                        Sleep(10);
                   13804:                }
                   13805:                key = _getch();
                   13806:                
                   13807:                if(key == 'I' || key == 'i') {
                   13808:                        if(REG8(AH) & 0x20) {
                   13809:                                REG8(AL) = 0;
                   13810:                                break;
                   13811:                        }
                   13812:                } else if(key == 'R' || key == 'r') {
                   13813:                        if(REG8(AH) & 0x10) {
                   13814:                                REG8(AL) = 1;
                   13815:                                break;
                   13816:                        }
                   13817:                } else if(key == 'A' || key == 'a') {
                   13818:                        REG8(AL) = 2;
                   13819:                        break;
                   13820:                } else if(key == 'F' || key == 'f') {
                   13821:                        if(REG8(AH) & 0x08) {
                   13822:                                REG8(AL) = 3;
                   13823:                                break;
                   13824:                        }
                   13825:                }
                   13826:        }
                   13827:        fprintf(stderr, "%c\n", key);
                   13828: }
                   13829: 
1.1       root     13830: inline void msdos_int_25h()
                   13831: {
                   13832:        UINT16 seg, ofs;
                   13833:        DWORD dwSize;
                   13834:        
1.1.1.3   root     13835: #if defined(HAS_I386)
                   13836:        I386OP(pushf)();
                   13837: #else
                   13838:        PREFIX86(_pushf());
                   13839: #endif
1.1       root     13840:        
                   13841:        if(!(REG8(AL) < 26)) {
                   13842:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13843:                m_CF = 1;
1.1       root     13844:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13845:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13846:                m_CF = 1;
1.1       root     13847:        } else {
                   13848:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13849:                char dev[64];
                   13850:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13851:                
1.1.1.60  root     13852:                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     13853:                if(hFile == INVALID_HANDLE_VALUE) {
                   13854:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13855:                        m_CF = 1;
1.1       root     13856:                } else {
1.1.1.19  root     13857:                        UINT32 top_sector  = REG16(DX);
                   13858:                        UINT16 sector_num  = REG16(CX);
                   13859:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13860:                        
                   13861:                        if(sector_num == 0xffff) {
                   13862:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13863:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13864:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13865:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13866:                                buffer_addr = (seg << 4) + ofs;
                   13867:                        }
                   13868: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13869: //                             REG8(AL) = 0x02; // drive not ready
                   13870: //                             m_CF = 1;
                   13871: //                     } else 
                   13872:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13873:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13874:                                m_CF = 1;
1.1.1.19  root     13875:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13876:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     13877:                                m_CF = 1;
1.1       root     13878:                        }
                   13879:                        CloseHandle(hFile);
                   13880:                }
                   13881:        }
                   13882: }
                   13883: 
                   13884: inline void msdos_int_26h()
                   13885: {
1.1.1.42  root     13886:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     13887:        UINT16 seg, ofs;
                   13888:        DWORD dwSize;
                   13889:        
1.1.1.3   root     13890: #if defined(HAS_I386)
                   13891:        I386OP(pushf)();
                   13892: #else
                   13893:        PREFIX86(_pushf());
                   13894: #endif
1.1       root     13895:        
                   13896:        if(!(REG8(AL) < 26)) {
                   13897:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13898:                m_CF = 1;
1.1       root     13899:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13900:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13901:                m_CF = 1;
1.1       root     13902:        } else {
                   13903:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13904:                char dev[64];
                   13905:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13906:                
                   13907:                if(dpb->media_type == 0xf8) {
                   13908:                        // this drive is not a floppy
1.1.1.6   root     13909: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   13910: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   13911: //                     }
1.1       root     13912:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13913:                        m_CF = 1;
1.1       root     13914:                } else {
1.1.1.60  root     13915:                        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     13916:                        if(hFile == INVALID_HANDLE_VALUE) {
                   13917:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13918:                                m_CF = 1;
1.1       root     13919:                        } else {
1.1.1.19  root     13920:                                UINT32 top_sector  = REG16(DX);
                   13921:                                UINT16 sector_num  = REG16(CX);
                   13922:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13923:                                
                   13924:                                if(sector_num == 0xffff) {
                   13925:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13926:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13927:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13928:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13929:                                        buffer_addr = (seg << 4) + ofs;
                   13930:                                }
1.1       root     13931:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13932:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13933:                                        m_CF = 1;
1.1.1.19  root     13934:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13935:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13936:                                        m_CF = 1;
1.1.1.19  root     13937:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13938:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     13939:                                        m_CF = 1;
1.1       root     13940:                                }
                   13941:                                CloseHandle(hFile);
                   13942:                        }
                   13943:                }
                   13944:        }
                   13945: }
                   13946: 
                   13947: inline void msdos_int_27h()
                   13948: {
1.1.1.29  root     13949:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   13950:        try {
                   13951:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13952:        } catch(...) {
                   13953:                // recover the broken mcb
                   13954:                int mcb_seg = SREG(CS) - 1;
                   13955:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     13956:                
1.1.1.29  root     13957:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     13958:                        mcb->mz = 'M';
                   13959:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   13960:                        
1.1.1.29  root     13961:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     13962:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     13963:                        } else {
1.1.1.39  root     13964:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     13965:                        }
                   13966:                } else {
                   13967:                        mcb->mz = 'Z';
1.1.1.30  root     13968:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     13969:                }
                   13970:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13971:        }
1.1.1.3   root     13972:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     13973: }
                   13974: 
                   13975: inline void msdos_int_29h()
                   13976: {
1.1.1.50  root     13977:        msdos_putch_fast(REG8(AL));
1.1       root     13978: }
                   13979: 
                   13980: inline void msdos_int_2eh()
                   13981: {
                   13982:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   13983:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13984:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     13985:        char *token = my_strtok(tmp, " ");
                   13986:        strcpy(command, token);
                   13987:        strcpy(opt, token + strlen(token) + 1);
                   13988:        
                   13989:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   13990:        param->env_seg = 0;
                   13991:        param->cmd_line.w.l = 44;
                   13992:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13993:        param->fcb1.w.l = 24;
                   13994:        param->fcb1.w.h = (WORK_TOP >> 4);
                   13995:        param->fcb2.w.l = 24;
                   13996:        param->fcb2.w.h = (WORK_TOP >> 4);
                   13997:        
                   13998:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   13999:        
                   14000:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   14001:        cmd_line->len = strlen(opt);
                   14002:        strcpy(cmd_line->cmd, opt);
                   14003:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   14004:        
1.1.1.28  root     14005:        try {
                   14006:                if(msdos_process_exec(command, param, 0)) {
                   14007:                        REG16(AX) = 0xffff; // error before processing command
                   14008:                } else {
                   14009:                        // set flag to set retval to ax when the started process is terminated
                   14010:                        process_t *process = msdos_process_info_get(current_psp);
                   14011:                        process->called_by_int2eh = true;
                   14012:                }
                   14013:        } catch(...) {
                   14014:                REG16(AX) = 0xffff; // error before processing command
                   14015:        }
1.1       root     14016: }
                   14017: 
1.1.1.29  root     14018: inline void msdos_int_2fh_05h()
                   14019: {
                   14020:        switch(REG8(AL)) {
                   14021:        case 0x00:
1.1.1.49  root     14022:                // critical error handler is installed
1.1.1.32  root     14023:                REG8(AL) = 0xff;
                   14024:                break;
                   14025:        case 0x01:
                   14026:        case 0x02:
                   14027:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   14028:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   14029:                                const char *message = NULL;
                   14030:                                if(active_code_page == 932) {
                   14031:                                        message = standard_error_table[i].message_japanese;
                   14032:                                }
                   14033:                                if(message == NULL) {
                   14034:                                        message = standard_error_table[i].message_english;
                   14035:                                }
                   14036:                                strcpy((char *)(mem + WORK_TOP), message);
                   14037:                                
                   14038:                                SREG(ES) = WORK_TOP >> 4;
                   14039:                                i386_load_segment_descriptor(ES);
                   14040:                                REG16(DI) = 0x0000;
                   14041:                                REG8(AL) = 0x01;
                   14042:                                break;
                   14043:                        }
                   14044:                }
1.1.1.29  root     14045:                break;
                   14046:        default:
                   14047:                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     14048:                REG16(AX) = 0x01;
1.1.1.29  root     14049:                m_CF = 1;
                   14050:        }
                   14051: }
                   14052: 
1.1.1.44  root     14053: inline void msdos_int_2fh_06h()
                   14054: {
                   14055:        switch(REG8(AL)) {
                   14056:        case 0x00:
                   14057:                // ASSIGN is not installed
1.1.1.49  root     14058: //             REG8(AL) = 0x00;
1.1.1.44  root     14059:                break;
                   14060:        case 0x01:
                   14061:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   14062:                REG16(AX) = 0x01;
                   14063:                m_CF = 1;
                   14064:                break;
                   14065:        default:
                   14066:                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));
                   14067:                REG16(AX) = 0x01;
                   14068:                m_CF = 1;
                   14069:                break;
                   14070:        }
                   14071: }
                   14072: 
1.1.1.22  root     14073: inline void msdos_int_2fh_11h()
                   14074: {
                   14075:        switch(REG8(AL)) {
                   14076:        case 0x00:
1.1.1.29  root     14077:                if(i386_read_stack() == 0xdada) {
1.1.1.53  root     14078: #ifdef SUPPORT_MSCDEX
                   14079:                        // MSCDEX is installed
                   14080:                        REG8(AL) = 0xff;
                   14081:                        i386_write_stack(0xadad);
                   14082: #else
1.1.1.29  root     14083:                        // MSCDEX is not installed
                   14084: //                     REG8(AL) = 0x00;
1.1.1.53  root     14085: #endif
1.1.1.29  root     14086:                } else {
                   14087:                        // Network Redirector is not installed
                   14088: //                     REG8(AL) = 0x00;
                   14089:                }
1.1.1.22  root     14090:                break;
                   14091:        default:
1.1.1.43  root     14092: //             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     14093:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     14094:                m_CF = 1;
                   14095:                break;
                   14096:        }
                   14097: }
                   14098: 
1.1.1.21  root     14099: inline void msdos_int_2fh_12h()
                   14100: {
                   14101:        switch(REG8(AL)) {
1.1.1.22  root     14102:        case 0x00:
1.1.1.29  root     14103:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     14104:                REG8(AL) = 0xff;
                   14105:                break;
1.1.1.29  root     14106: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   14107:        case 0x02:
                   14108:                {
                   14109:                        UINT16 stack = i386_read_stack();
                   14110:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   14111:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   14112:                        i386_load_segment_descriptor(ES);
                   14113:                }
                   14114:                break;
1.1.1.30  root     14115:        case 0x03:
                   14116:                SREG(DS) = (DEVICE_TOP >> 4);
                   14117:                i386_load_segment_descriptor(DS);
                   14118:                break;
1.1.1.29  root     14119:        case 0x04:
                   14120:                {
                   14121:                        UINT16 stack = i386_read_stack();
                   14122:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   14123: #if defined(HAS_I386)
                   14124:                        m_ZF = (REG8(AL) == '\\');
                   14125: #else
                   14126:                        m_ZeroVal = (REG8(AL) != '\\');
                   14127: #endif
                   14128:                }
                   14129:                break;
                   14130:        case 0x05:
1.1.1.49  root     14131:                {
                   14132:                        UINT16 c = i386_read_stack();
                   14133:                        if((c >> 0) & 0xff) {
                   14134:                                msdos_putch((c >> 0) & 0xff);
                   14135:                        }
                   14136:                        if((c >> 8) & 0xff) {
                   14137:                                msdos_putch((c >> 8) & 0xff);
                   14138:                        }
                   14139:                }
1.1.1.29  root     14140:                break;
1.1.1.49  root     14141: //     case 0x06: // DOS 3.0+ internal - Invoke Critical Error
1.1.1.29  root     14142: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   14143: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
1.1.1.49  root     14144: //     case 0x09: // DOS 3.0+ internal - Flush and Free Disk Buffer
1.1.1.29  root     14145: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   14146: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   14147: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   14148:        case 0x0d:
                   14149:                {
                   14150:                        SYSTEMTIME time;
                   14151:                        FILETIME file_time;
                   14152:                        WORD dos_date, dos_time;
                   14153:                        GetLocalTime(&time);
                   14154:                        SystemTimeToFileTime(&time, &file_time);
                   14155:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   14156:                        REG16(AX) = dos_date;
                   14157:                        REG16(DX) = dos_time;
                   14158:                }
                   14159:                break;
                   14160: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   14161: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   14162: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   14163:        case 0x11:
                   14164:                {
                   14165:                        char path[MAX_PATH], *p;
                   14166:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   14167:                        my_strupr(path);
                   14168:                        while((p = my_strchr(path, '/')) != NULL) {
                   14169:                                *p = '\\';
                   14170:                        }
                   14171:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   14172:                }
                   14173:                break;
                   14174:        case 0x12:
                   14175:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   14176:                break;
                   14177:        case 0x13:
                   14178:                {
                   14179:                        char tmp[2] = {0};
                   14180:                        tmp[0] = i386_read_stack();
                   14181:                        my_strupr(tmp);
                   14182:                        REG8(AL) = tmp[0];
                   14183:                }
                   14184:                break;
                   14185:        case 0x14:
                   14186: #if defined(HAS_I386)
                   14187:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   14188: #else
                   14189:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   14190: #endif
                   14191:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   14192:                break;
                   14193: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     14194:        case 0x16:
                   14195:                if(REG16(BX) < 20) {
                   14196:                        SREG(ES) = SFT_TOP >> 4;
                   14197:                        i386_load_segment_descriptor(ES);
                   14198:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   14199:                        
                   14200:                        // update system file table
                   14201:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   14202:                        if(file_handler[REG16(BX)].valid) {
                   14203:                                int count = 0;
                   14204:                                for(int i = 0; i < 20; i++) {
                   14205:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   14206:                                                count++;
                   14207:                                        }
                   14208:                                }
                   14209:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   14210:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   14211:                                _lseek(REG16(BX), 0, SEEK_END);
                   14212:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   14213:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   14214:                        } else {
                   14215:                                memset(sft, 0, 0x3b);
                   14216:                        }
                   14217:                } else {
                   14218:                        REG16(AX) = 0x06;
                   14219:                        m_CF = 1;
                   14220:                }
                   14221:                break;
1.1.1.49  root     14222:        case 0x17:
                   14223:                {
                   14224:                        UINT16 drive = i386_read_stack();
                   14225:                        if(msdos_is_valid_drive(drive)) {
                   14226:                                msdos_cds_update(drive);
                   14227:                        }
                   14228:                        REG16(SI) = 88 * drive;
                   14229:                        SREG(DS) = (CDS_TOP >> 4);
                   14230:                        i386_load_segment_descriptor(DS);
                   14231:                }
                   14232:                break;
1.1.1.29  root     14233: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   14234: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   14235:        case 0x1a:
                   14236:                {
                   14237:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   14238:                        if(path[1] == ':') {
                   14239:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   14240:                                        REG8(AL) = path[0] - 'a' + 1;
                   14241:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   14242:                                        REG8(AL) = path[0] - 'A' + 1;
                   14243:                                } else {
                   14244:                                        REG8(AL) = 0xff; // invalid
                   14245:                                }
                   14246:                                strcpy(full, path);
                   14247:                                strcpy(path, full + 2);
1.1.1.60  root     14248:                        } else if(GetFullPathNameA(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
1.1.1.29  root     14249:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   14250:                                        REG8(AL) = full[0] - 'a' + 1;
                   14251:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   14252:                                        REG8(AL) = full[0] - 'A' + 1;
                   14253:                                } else {
                   14254:                                        REG8(AL) = 0xff; // invalid
                   14255:                                }
                   14256:                        } else {
                   14257:                                REG8(AL) = 0x00; // default
                   14258:                        }
                   14259:                }
                   14260:                break;
                   14261:        case 0x1b:
                   14262:                {
                   14263:                        int year = REG16(CX) + 1980;
                   14264:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   14265:                }
                   14266:                break;
                   14267: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   14268: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   14269:        case 0x1e:
                   14270:                {
                   14271:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   14272:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
1.1.1.60  root     14273:                        if(GetFullPathNameA(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathNameA(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
1.1.1.29  root     14274: #if defined(HAS_I386)
                   14275:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   14276: #else
                   14277:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   14278: #endif
                   14279:                        } else {
                   14280: #if defined(HAS_I386)
                   14281:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   14282: #else
                   14283:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   14284: #endif
                   14285:                        }
                   14286:                }
                   14287:                break;
1.1.1.49  root     14288:        case 0x1f:
                   14289:                {
                   14290:                        UINT16 drive = i386_read_stack();
                   14291:                        if(msdos_is_valid_drive(drive)) {
                   14292:                                msdos_cds_update(drive);
                   14293:                        }
                   14294:                        REG16(SI) = 88 * drive;
                   14295:                        SREG(ES) = (CDS_TOP >> 4);
                   14296:                        i386_load_segment_descriptor(ES);
                   14297:                }
                   14298:                break;
1.1.1.21  root     14299:        case 0x20:
                   14300:                {
                   14301:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   14302:                        
                   14303:                        if(fd < 20) {
                   14304:                                SREG(ES) = current_psp;
                   14305:                                i386_load_segment_descriptor(ES);
                   14306:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   14307:                        } else {
                   14308:                                REG16(AX) = 0x06;
                   14309:                                m_CF = 1;
                   14310:                        }
                   14311:                }
                   14312:                break;
1.1.1.29  root     14313:        case 0x21:
                   14314:                msdos_int_21h_60h(0);
                   14315:                break;
1.1.1.49  root     14316:        case 0x22:
                   14317:                {
                   14318:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14319:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00) != 0xff) {
                   14320:                                sda->extended_error_code = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00);
                   14321:                        }
                   14322:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01) != 0xff) {
                   14323:                                sda->error_class         = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01);
                   14324:                        }
                   14325:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02) != 0xff) {
                   14326:                                sda->suggested_action    = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02);
                   14327:                        }
                   14328:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03) != 0xff) {
                   14329:                                sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03);
                   14330:                        }
                   14331:                }
                   14332:                break;
1.1.1.29  root     14333: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   14334: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   14335:        case 0x25:
                   14336:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   14337:                break;
                   14338:        case 0x26:
                   14339:                REG8(AL) = REG8(CL);
                   14340:                msdos_int_21h_3dh();
                   14341:                break;
                   14342:        case 0x27:
                   14343:                msdos_int_21h_3eh();
                   14344:                break;
                   14345:        case 0x28:
                   14346:                REG16(AX) = REG16(BP);
                   14347:                msdos_int_21h_42h();
                   14348:                break;
                   14349:        case 0x29:
                   14350:                msdos_int_21h_3fh();
                   14351:                break;
                   14352: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   14353:        case 0x2b:
                   14354:                REG16(AX) = REG16(BP);
                   14355:                msdos_int_21h_44h();
                   14356:                break;
                   14357:        case 0x2c:
                   14358:                REG16(BX) = DEVICE_TOP >> 4;
                   14359:                REG16(AX) = 22;
                   14360:                break;
                   14361:        case 0x2d:
                   14362:                {
                   14363:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14364:                        REG16(AX) = sda->extended_error_code;
                   14365:                }
                   14366:                break;
                   14367:        case 0x2e:
                   14368:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     14369:                        SREG(ES) = 0x0001;
                   14370:                        i386_load_segment_descriptor(ES);
                   14371:                        REG16(DI) = 0x00;
                   14372:                } else if(REG8(DL) == 0x08) {
1.1.1.49  root     14373:                        // dummy parameter error message read routine is at fffc:0010
                   14374:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.22  root     14375:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     14376:                        REG16(DI) = 0x0010;
1.1.1.22  root     14377:                }
                   14378:                break;
1.1.1.29  root     14379:        case 0x2f:
                   14380:                if(REG16(DX) != 0) {
1.1.1.30  root     14381:                        dos_major_version = REG8(DL);
                   14382:                        dos_minor_version = REG8(DH);
1.1.1.29  root     14383:                } else {
                   14384:                        REG8(DL) = 7;
                   14385:                        REG8(DH) = 10;
                   14386:                }
                   14387:                break;
                   14388: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   14389: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     14390:        default:
                   14391:                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));
                   14392:                REG16(AX) = 0x01;
                   14393:                m_CF = 1;
                   14394:                break;
                   14395:        }
                   14396: }
                   14397: 
1.1.1.30  root     14398: inline void msdos_int_2fh_13h()
                   14399: {
                   14400:        static UINT16 prevDS = 0, prevDX = 0;
                   14401:        static UINT16 prevES = 0, prevBX = 0;
                   14402:        UINT16 tmp;
                   14403:        
                   14404:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   14405:        i386_load_segment_descriptor(DS);
                   14406:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   14407:        
                   14408:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   14409:        i386_load_segment_descriptor(ES);
                   14410:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   14411: }
                   14412: 
1.1.1.22  root     14413: inline void msdos_int_2fh_14h()
                   14414: {
                   14415:        switch(REG8(AL)) {
                   14416:        case 0x00:
1.1.1.29  root     14417:                // NLSFUNC.COM is installed
                   14418:                REG8(AL) = 0xff;
1.1.1.25  root     14419:                break;
                   14420:        case 0x01:
                   14421:        case 0x03:
                   14422:                REG8(AL) = 0x00;
                   14423:                active_code_page = REG16(BX);
                   14424:                msdos_nls_tables_update();
                   14425:                break;
                   14426:        case 0x02:
                   14427:                REG8(AL) = get_extended_country_info(REG16(BP));
                   14428:                break;
                   14429:        case 0x04:
1.1.1.42  root     14430:                for(int i = 0;; i++) {
                   14431:                        if(country_table[i].code == REG16(DX)) {
                   14432:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   14433:                                break;
                   14434:                        } else if(country_table[i].code == -1) {
                   14435:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   14436:                                break;
                   14437:                        }
                   14438:                }
1.1.1.25  root     14439:                REG8(AL) = 0x00;
1.1.1.22  root     14440:                break;
                   14441:        default:
                   14442:                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));
                   14443:                REG16(AX) = 0x01;
                   14444:                m_CF = 1;
                   14445:                break;
                   14446:        }
                   14447: }
                   14448: 
                   14449: inline void msdos_int_2fh_15h()
                   14450: {
                   14451:        switch(REG8(AL)) {
1.1.1.29  root     14452:        case 0x00: // CD-ROM - Installation Check
                   14453:                if(REG16(BX) == 0x0000) {
1.1.1.53  root     14454: #ifdef SUPPORT_MSCDEX
1.1.1.43  root     14455:                        // MSCDEX is installed
                   14456:                        REG16(BX) = 0;
                   14457:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     14458:                                if(msdos_is_cdrom_drive(i)) {
                   14459:                                        if(REG16(BX) == 0) {
                   14460:                                                REG16(CX) = i;
1.1.1.43  root     14461:                                        }
1.1.1.44  root     14462:                                        REG16(BX)++;
1.1.1.43  root     14463:                                }
                   14464:                        }
                   14465: #else
1.1.1.29  root     14466:                        // MSCDEX is not installed
                   14467: //                     REG8(AL) = 0x00;
1.1.1.43  root     14468: #endif
1.1.1.29  root     14469:                } else {
                   14470:                        // GRAPHICS.COM is not installed
                   14471: //                     REG8(AL) = 0x00;
                   14472:                }
1.1.1.22  root     14473:                break;
1.1.1.43  root     14474:        case 0x0b:
1.1.1.44  root     14475:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   14476:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   14477:                REG16(BX) = 0xadad;
1.1.1.43  root     14478:                break;
                   14479:        case 0x0d:
1.1.1.44  root     14480:                for(int i = 0, n = 0; i < 26; i++) {
                   14481:                        if(msdos_is_cdrom_drive(i)) {
                   14482:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     14483:                        }
                   14484:                }
                   14485:                break;
1.1.1.22  root     14486:        case 0xff:
1.1.1.29  root     14487:                if(REG16(BX) == 0x0000) {
                   14488:                        // CORELCDX is not installed
                   14489:                } else {
                   14490:                        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));
                   14491:                        REG16(AX) = 0x01;
                   14492:                        m_CF = 1;
                   14493:                }
1.1.1.22  root     14494:                break;
1.1.1.21  root     14495:        default:
1.1.1.22  root     14496:                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     14497:                REG16(AX) = 0x01;
                   14498:                m_CF = 1;
                   14499:                break;
                   14500:        }
                   14501: }
                   14502: 
1.1       root     14503: inline void msdos_int_2fh_16h()
                   14504: {
                   14505:        switch(REG8(AL)) {
                   14506:        case 0x00:
1.1.1.14  root     14507:                if(no_windows) {
1.1.1.29  root     14508:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   14509: //                     REG8(AL) = 0x00;
1.1.1.14  root     14510:                } else {
1.1.1.30  root     14511:                        REG8(AL) = win_major_version;
                   14512:                        REG8(AH) = win_minor_version;
1.1       root     14513:                }
                   14514:                break;
1.1.1.43  root     14515:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     14516:                // from DOSBox
                   14517:                i386_set_a20_line(1);
                   14518:                break;
1.1.1.49  root     14519:        case 0x06: // Windows Enhanced Mode & 286 DOSX Exit Broadcast
1.1.1.43  root     14520:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   14521:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   14522:                break;
                   14523:        case 0x07:
                   14524:                // Virtual Device Call API
                   14525:                break;
1.1.1.22  root     14526:        case 0x0a:
                   14527:                if(!no_windows) {
                   14528:                        REG16(AX) = 0x0000;
1.1.1.30  root     14529:                        REG8(BH) = win_major_version;
                   14530:                        REG8(BL) = win_minor_version;
1.1.1.49  root     14531: //                     REG16(CX) = 0x0002; // standard
1.1.1.22  root     14532:                        REG16(CX) = 0x0003; // enhanced
                   14533:                }
                   14534:                break;
1.1.1.30  root     14535:        case 0x0b:
                   14536:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     14537:        case 0x0e:
                   14538:        case 0x0f:
1.1.1.30  root     14539:        case 0x10:
1.1.1.22  root     14540:        case 0x11:
                   14541:        case 0x12:
                   14542:        case 0x13:
                   14543:        case 0x14:
1.1.1.30  root     14544:        case 0x15:
1.1.1.43  root     14545:        case 0x81:
                   14546:        case 0x82:
1.1.1.44  root     14547:        case 0x84:
1.1.1.49  root     14548:        case 0x85:
1.1.1.33  root     14549:        case 0x86:
1.1.1.22  root     14550:        case 0x87:
1.1.1.30  root     14551:        case 0x89:
1.1.1.33  root     14552:        case 0x8a:
1.1.1.22  root     14553:                // function not supported, do not clear AX
                   14554:                break;
1.1.1.14  root     14555:        case 0x80:
                   14556:                Sleep(10);
1.1.1.35  root     14557:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     14558:                REG8(AL) = 0x00;
1.1.1.14  root     14559:                break;
1.1.1.33  root     14560:        case 0x83:
                   14561:                REG16(BX) = 0x01; // system vm id
                   14562:                break;
1.1.1.22  root     14563:        case 0x8e:
                   14564:                REG16(AX) = 0x00; // failed
                   14565:                break;
1.1.1.20  root     14566:        case 0x8f:
                   14567:                switch(REG8(DH)) {
                   14568:                case 0x01:
1.1.1.49  root     14569: //                     REG16(AX) = 0x0000; // close command selected but not yet acknowledged
                   14570: //                     REG16(AX) = 0x0001; // close command issued and acknowledged
                   14571:                        REG16(AX) = 0x168f; // close command not selected -- application should continue
                   14572:                        break;
                   14573:                default:
                   14574:                        REG16(AX) = 0x0000; // successful
1.1.1.20  root     14575:                        break;
                   14576:                }
                   14577:                break;
1.1       root     14578:        default:
1.1.1.22  root     14579:                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));
                   14580:                REG16(AX) = 0x01;
                   14581:                m_CF = 1;
                   14582:                break;
                   14583:        }
                   14584: }
                   14585: 
                   14586: inline void msdos_int_2fh_19h()
                   14587: {
                   14588:        switch(REG8(AL)) {
                   14589:        case 0x00:
1.1.1.29  root     14590:                // SHELLB.COM is not installed
                   14591: //             REG8(AL) = 0x00;
1.1.1.22  root     14592:                break;
                   14593:        case 0x01:
                   14594:        case 0x02:
                   14595:        case 0x03:
                   14596:        case 0x04:
                   14597:                REG16(AX) = 0x01;
                   14598:                m_CF = 1;
                   14599:                break;
1.1.1.29  root     14600:        case 0x80:
                   14601:                // IBM ROM-DOS v4.0 is not installed
                   14602: //             REG8(AL) = 0x00;
                   14603:                break;
1.1.1.22  root     14604:        default:
                   14605:                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     14606:                REG16(AX) = 0x01;
1.1.1.3   root     14607:                m_CF = 1;
1.1       root     14608:                break;
                   14609:        }
                   14610: }
                   14611: 
                   14612: inline void msdos_int_2fh_1ah()
                   14613: {
                   14614:        switch(REG8(AL)) {
                   14615:        case 0x00:
1.1.1.29  root     14616:                // ANSI.SYS is installed
1.1       root     14617:                REG8(AL) = 0xff;
                   14618:                break;
1.1.1.49  root     14619:        case 0x01:
1.1.1.50  root     14620:                if(REG8(CL) == 0x5f) {
                   14621:                        // set display information
                   14622:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   14623:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   14624:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   14625:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   14626:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   14627:                                
                   14628:                                if(cur_width != new_width || cur_height != new_height) {
                   14629:                                        pcbios_set_console_size(new_width, new_height, true);
                   14630:                                }
                   14631:                        }
                   14632:                } else if(REG8(CL) == 0x7f) {
1.1.1.49  root     14633:                        // get display information
1.1.1.50  root     14634:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   14635:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   14636:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   14637:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   14638:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   14639:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   14640:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   14641:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   14642:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   14643:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   14644:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.49  root     14645:                } else {
                   14646:                        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));
                   14647:                        REG16(AX) = 0x01;
                   14648:                        m_CF = 1;
                   14649:                }
                   14650:                break;
1.1       root     14651:        default:
1.1.1.22  root     14652:                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));
                   14653:                REG16(AX) = 0x01;
                   14654:                m_CF = 1;
                   14655:                break;
                   14656:        }
                   14657: }
                   14658: 
1.1.1.30  root     14659: inline void msdos_int_2fh_40h()
1.1.1.22  root     14660: {
                   14661:        switch(REG8(AL)) {
                   14662:        case 0x00:
1.1.1.30  root     14663:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   14664:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     14665:                break;
1.1.1.43  root     14666:        case 0x10:
                   14667:                // OS/2 v2.0+ - Installation Check
                   14668:                REG16(AX) = 0x01;
                   14669:                m_CF = 1;
                   14670:                break;
1.1.1.22  root     14671:        default:
                   14672:                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     14673:                REG16(AX) = 0x01;
1.1.1.3   root     14674:                m_CF = 1;
1.1       root     14675:                break;
                   14676:        }
                   14677: }
                   14678: 
                   14679: inline void msdos_int_2fh_43h()
                   14680: {
                   14681:        switch(REG8(AL)) {
                   14682:        case 0x00:
1.1.1.29  root     14683:                // XMS is installed ?
1.1.1.19  root     14684: #ifdef SUPPORT_XMS
                   14685:                if(support_xms) {
                   14686:                        REG8(AL) = 0x80;
1.1.1.44  root     14687:                }
                   14688: #endif
                   14689:                break;
                   14690:        case 0x08:
                   14691: #ifdef SUPPORT_XMS
                   14692:                if(support_xms) {
                   14693:                        REG8(AL) = 0x43;
                   14694:                        REG8(BL) = 0x01; // IBM PC/AT
                   14695:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   14696:                }
1.1.1.19  root     14697: #endif
                   14698:                break;
                   14699:        case 0x10:
                   14700:                SREG(ES) = XMS_TOP >> 4;
                   14701:                i386_load_segment_descriptor(ES);
1.1.1.26  root     14702:                REG16(BX) = 0x15;
1.1       root     14703:                break;
1.1.1.44  root     14704:        case 0xe0:
                   14705:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   14706:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   14707:                        break;
                   14708:                }
1.1       root     14709:        default:
1.1.1.22  root     14710:                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));
                   14711:                REG16(AX) = 0x01;
                   14712:                m_CF = 1;
                   14713:                break;
                   14714:        }
                   14715: }
                   14716: 
                   14717: inline void msdos_int_2fh_46h()
                   14718: {
                   14719:        switch(REG8(AL)) {
                   14720:        case 0x80:
1.1.1.29  root     14721:                // Windows v3.0 is not installed
                   14722: //             REG8(AL) = 0x00;
1.1.1.22  root     14723:                break;
                   14724:        default:
                   14725:                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));
                   14726:                REG16(AX) = 0x01;
                   14727:                m_CF = 1;
                   14728:                break;
                   14729:        }
                   14730: }
                   14731: 
                   14732: inline void msdos_int_2fh_48h()
                   14733: {
                   14734:        switch(REG8(AL)) {
                   14735:        case 0x00:
1.1.1.29  root     14736:                // DOSKEY is not installed
                   14737: //             REG8(AL) = 0x00;
1.1.1.22  root     14738:                break;
                   14739:        case 0x10:
                   14740:                msdos_int_21h_0ah();
                   14741:                REG16(AX) = 0x00;
                   14742:                break;
                   14743:        default:
                   14744:                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     14745:                REG16(AX) = 0x01;
1.1.1.3   root     14746:                m_CF = 1;
1.1       root     14747:                break;
                   14748:        }
                   14749: }
                   14750: 
                   14751: inline void msdos_int_2fh_4ah()
                   14752: {
                   14753:        switch(REG8(AL)) {
1.1.1.29  root     14754: #ifdef SUPPORT_HMA
                   14755:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   14756:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14757:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14758:                                // restore first free mcb in high memory area
                   14759:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14760:                        }
                   14761:                        int offset = 0xffff;
                   14762:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   14763:                                REG16(DI) = offset + 0x10;
                   14764:                        } else {
                   14765:                                REG16(DI) = 0xffff;
                   14766:                        }
                   14767:                } else {
                   14768:                        // HMA is already used
                   14769:                        REG16(BX) = 0;
                   14770:                        REG16(DI) = 0xffff;
                   14771:                }
                   14772:                SREG(ES) = 0xffff;
                   14773:                i386_load_segment_descriptor(ES);
                   14774:                break;
                   14775:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   14776:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14777:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14778:                                // restore first free mcb in high memory area
                   14779:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14780:                        }
                   14781:                        int size = REG16(BX), offset;
                   14782:                        if((size % 16) != 0) {
                   14783:                                size &= ~15;
                   14784:                                size += 16;
                   14785:                        }
                   14786:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   14787:                                REG16(BX) = size;
                   14788:                                REG16(DI) = offset + 0x10;
                   14789:                                is_hma_used_by_int_2fh = true;
                   14790:                        } else {
                   14791:                                REG16(BX) = 0;
                   14792:                                REG16(DI) = 0xffff;
                   14793:                        }
                   14794:                } else {
                   14795:                        // HMA is already used
                   14796:                        REG16(BX) = 0;
                   14797:                        REG16(DI) = 0xffff;
                   14798:                }
                   14799:                SREG(ES) = 0xffff;
                   14800:                i386_load_segment_descriptor(ES);
                   14801:                break;
                   14802:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   14803:                if(REG8(DL) == 0x00) {
                   14804:                        if(!is_hma_used_by_xms) {
                   14805:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14806:                                        // restore first free mcb in high memory area
                   14807:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14808:                                        is_hma_used_by_int_2fh = false;
                   14809:                                }
                   14810:                                int size = REG16(BX), offset;
                   14811:                                if((size % 16) != 0) {
                   14812:                                        size &= ~15;
                   14813:                                        size += 16;
                   14814:                                }
                   14815:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   14816: //                                     REG16(BX) = size;
                   14817:                                        SREG(ES) = 0xffff;
                   14818:                                        i386_load_segment_descriptor(ES);
                   14819:                                        REG16(DI) = offset + 0x10;
                   14820:                                        is_hma_used_by_int_2fh = true;
                   14821:                                } else {
                   14822:                                        REG16(DI) = 0xffff;
                   14823:                                }
                   14824:                        } else {
                   14825:                                REG16(DI) = 0xffff;
                   14826:                        }
                   14827:                } else if(REG8(DL) == 0x01) {
                   14828:                        if(!is_hma_used_by_xms) {
                   14829:                                int size = REG16(BX);
                   14830:                                if((size % 16) != 0) {
                   14831:                                        size &= ~15;
                   14832:                                        size += 16;
                   14833:                                }
                   14834:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   14835:                                        // memory block address is not changed
                   14836:                                } else {
                   14837:                                        REG16(DI) = 0xffff;
                   14838:                                }
                   14839:                        } else {
                   14840:                                REG16(DI) = 0xffff;
                   14841:                        }
                   14842:                } else if(REG8(DL) == 0x02) {
                   14843:                        if(!is_hma_used_by_xms) {
                   14844:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14845:                                        // restore first free mcb in high memory area
                   14846:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14847:                                        is_hma_used_by_int_2fh = false;
                   14848:                                } else {
                   14849:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   14850:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   14851:                                                is_hma_used_by_int_2fh = false;
                   14852:                                        }
                   14853:                                }
                   14854:                        }
                   14855:                } else {
                   14856:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14857:                        REG16(AX) = 0x01;
                   14858:                        m_CF = 1;
                   14859:                }
                   14860:                break;
                   14861:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   14862:                if(!is_hma_used_by_xms) {
                   14863:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14864:                                // restore first free mcb in high memory area
                   14865:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14866:                                is_hma_used_by_int_2fh = false;
                   14867:                        }
                   14868:                        REG16(AX) = 0x0000;
                   14869:                        SREG(ES) = 0xffff;
                   14870:                        i386_load_segment_descriptor(ES);
                   14871:                        REG16(DI) = 0x10;
                   14872:                }
                   14873:                break;
                   14874: #else
1.1       root     14875:        case 0x01:
                   14876:        case 0x02:
1.1.1.29  root     14877:                // HMA is already used
1.1.1.27  root     14878:                REG16(BX) = 0x0000;
1.1.1.3   root     14879:                SREG(ES) = 0xffff;
                   14880:                i386_load_segment_descriptor(ES);
1.1       root     14881:                REG16(DI) = 0xffff;
                   14882:                break;
1.1.1.19  root     14883:        case 0x03:
                   14884:                // unable to allocate
                   14885:                REG16(DI) = 0xffff;
                   14886:                break;
                   14887:        case 0x04:
                   14888:                // function not supported, do not clear AX
                   14889:                break;
1.1.1.29  root     14890: #endif
                   14891:        case 0x10:
1.1.1.42  root     14892:                switch(REG16(BX)) {
                   14893:                case 0x0000:
                   14894:                case 0x0001:
                   14895:                case 0x0002:
                   14896:                case 0x0003:
                   14897:                case 0x0004:
                   14898:                case 0x0005:
                   14899:                case 0x0006:
                   14900:                case 0x0007:
                   14901:                case 0x0008:
                   14902:                case 0x000a:
                   14903:                case 0x1234:
                   14904:                        // SMARTDRV v4.00+ is not installed
                   14905:                        break;
                   14906:                default:
1.1.1.29  root     14907:                        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));
                   14908:                        REG16(AX) = 0x01;
                   14909:                        m_CF = 1;
1.1.1.42  root     14910:                        break;
1.1.1.29  root     14911:                }
                   14912:                break;
                   14913:        case 0x11:
1.1.1.42  root     14914:                switch(REG16(BX)) {
                   14915:                case 0x0000:
                   14916:                case 0x0001:
                   14917:                case 0x0002:
                   14918:                case 0x0003:
                   14919:                case 0x0004:
                   14920:                case 0x0005:
                   14921:                case 0x0006:
                   14922:                case 0x0007:
                   14923:                case 0x0008:
                   14924:                case 0x0009:
                   14925:                case 0x000a:
                   14926:                case 0x000b:
                   14927:                case 0xfffe:
                   14928:                case 0xffff:
1.1.1.29  root     14929:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     14930:                        break;
                   14931:                default:
                   14932:                        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));
                   14933:                        REG16(AX) = 0x01;
                   14934:                        m_CF = 1;
                   14935:                        break;
                   14936:                }
                   14937:                break;
                   14938:        case 0x12:
                   14939:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   14940:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   14941:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   14942:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     14943:                } else {
                   14944:                        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));
                   14945:                        REG16(AX) = 0x01;
                   14946:                        m_CF = 1;
                   14947:                }
1.1.1.22  root     14948:                break;
1.1.1.42  root     14949:        case 0x13:
                   14950:                // DBLSPACE.BIN is not installed
                   14951:                break;
1.1.1.22  root     14952:        default:
                   14953:                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));
                   14954:                REG16(AX) = 0x01;
                   14955:                m_CF = 1;
                   14956:                break;
                   14957:        }
                   14958: }
                   14959: 
                   14960: inline void msdos_int_2fh_4bh()
                   14961: {
                   14962:        switch(REG8(AL)) {
1.1.1.24  root     14963:        case 0x01:
1.1.1.22  root     14964:        case 0x02:
1.1.1.29  root     14965:                // Task Switcher is not installed
1.1.1.24  root     14966:                break;
                   14967:        case 0x03:
                   14968:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   14969:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     14970:                break;
1.1.1.30  root     14971:        case 0x04:
                   14972:                REG16(BX) = 0x0000; // free switcher id successfully
                   14973:                break;
1.1.1.43  root     14974:        case 0x05:
                   14975:                REG16(BX) = 0x0000; // no instance data chain
                   14976:                SREG(ES) = 0x0000;
                   14977:                i386_load_segment_descriptor(ES);
                   14978:                break;
1.1       root     14979:        default:
1.1.1.22  root     14980:                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     14981:                REG16(AX) = 0x01;
1.1.1.3   root     14982:                m_CF = 1;
1.1       root     14983:                break;
                   14984:        }
                   14985: }
                   14986: 
1.1.1.44  root     14987: inline void msdos_int_2fh_4dh()
                   14988: {
                   14989:        switch(REG8(AL)) {
                   14990:        case 0x00:
                   14991:                // KKCFUNC is not installed ???
                   14992:                break;
                   14993:        default:
                   14994: //             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));
                   14995:                REG16(AX) = 0x01; // invalid function
                   14996:                m_CF = 1;
                   14997:                break;
                   14998:        }
                   14999: }
                   15000: 
1.1       root     15001: inline void msdos_int_2fh_4fh()
                   15002: {
                   15003:        switch(REG8(AL)) {
                   15004:        case 0x00:
1.1.1.29  root     15005:                // BILING is installed
1.1.1.27  root     15006:                REG16(AX) = 0x0000;
                   15007:                REG8(DL) = 0x01;        // major version
                   15008:                REG8(DH) = 0x00;        // minor version
1.1       root     15009:                break;
                   15010:        case 0x01:
1.1.1.27  root     15011:                REG16(AX) = 0x0000;
1.1       root     15012:                REG16(BX) = active_code_page;
                   15013:                break;
                   15014:        default:
1.1.1.22  root     15015:                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));
                   15016:                REG16(AX) = 0x01;
                   15017:                m_CF = 1;
                   15018:                break;
                   15019:        }
                   15020: }
                   15021: 
                   15022: inline void msdos_int_2fh_55h()
                   15023: {
                   15024:        switch(REG8(AL)) {
                   15025:        case 0x00:
                   15026:        case 0x01:
                   15027: //             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));
                   15028:                break;
                   15029:        default:
                   15030:                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     15031:                REG16(AX) = 0x01;
1.1.1.3   root     15032:                m_CF = 1;
1.1       root     15033:                break;
                   15034:        }
                   15035: }
                   15036: 
1.1.1.44  root     15037: inline void msdos_int_2fh_56h()
                   15038: {
                   15039:        switch(REG8(AL)) {
                   15040:        case 0x00:
                   15041:                // INTERLNK is not installed
                   15042:                break;
                   15043:        case 0x01:
                   15044:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   15045: //             if(msdos_is_remote_drive(REG8(BH))) {
                   15046: //                     REG8(AL) = 0x00;
                   15047: //             }
                   15048:                break;
                   15049:        default:
                   15050:                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));
                   15051:                REG16(AX) = 0x01;
                   15052:                m_CF = 1;
                   15053:                break;
                   15054:        }
                   15055: }
                   15056: 
1.1.1.24  root     15057: inline void msdos_int_2fh_adh()
                   15058: {
                   15059:        switch(REG8(AL)) {
                   15060:        case 0x00:
1.1.1.29  root     15061:                // DISPLAY.SYS is installed
1.1.1.24  root     15062:                REG8(AL) = 0xff;
                   15063:                REG16(BX) = 0x100; // ???
                   15064:                break;
                   15065:        case 0x01:
                   15066:                active_code_page = REG16(BX);
                   15067:                msdos_nls_tables_update();
                   15068:                REG16(AX) = 0x01;
                   15069:                break;
                   15070:        case 0x02:
                   15071:                REG16(BX) = active_code_page;
                   15072:                break;
                   15073:        case 0x03:
                   15074:                // FIXME
                   15075:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   15076:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   15077:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   15078:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   15079:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   15080:                break;
                   15081:        case 0x80:
1.1.1.49  root     15082:                // KEYB.COM is not installed
                   15083:                break;
1.1.1.24  root     15084:        default:
                   15085:                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));
                   15086:                REG16(AX) = 0x01;
                   15087:                m_CF = 1;
                   15088:                break;
                   15089:        }
                   15090: }
                   15091: 
1.1       root     15092: inline void msdos_int_2fh_aeh()
                   15093: {
                   15094:        switch(REG8(AL)) {
                   15095:        case 0x00:
1.1.1.28  root     15096:                // FIXME: we need to check the given command line
                   15097:                REG8(AL) = 0x00; // the command should be executed as usual
                   15098: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     15099:                break;
                   15100:        case 0x01:
                   15101:                {
                   15102:                        char command[MAX_PATH];
                   15103:                        memset(command, 0, sizeof(command));
1.1.1.3   root     15104:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     15105:                        
                   15106:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   15107:                        param->env_seg = 0;
                   15108:                        param->cmd_line.w.l = 44;
                   15109:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   15110:                        param->fcb1.w.l = 24;
                   15111:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   15112:                        param->fcb2.w.l = 24;
                   15113:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   15114:                        
                   15115:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   15116:                        
                   15117:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     15118:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   15119:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     15120:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   15121:                        
1.1.1.28  root     15122:                        try {
                   15123:                                msdos_process_exec(command, param, 0);
                   15124:                        } catch(...) {
                   15125:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     15126:                        }
                   15127:                }
                   15128:                break;
                   15129:        default:
1.1.1.22  root     15130:                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     15131:                REG16(AX) = 0x01;
1.1.1.3   root     15132:                m_CF = 1;
1.1       root     15133:                break;
                   15134:        }
                   15135: }
                   15136: 
1.1.1.34  root     15137: inline void msdos_int_2fh_b7h()
                   15138: {
                   15139:        switch(REG8(AL)) {
                   15140:        case 0x00:
                   15141:                // APPEND is not installed
                   15142: //             REG8(AL) = 0x00;
                   15143:                break;
1.1.1.44  root     15144:        case 0x06:
                   15145:                REG16(BX) = 0x0000;
                   15146:                break;
1.1.1.34  root     15147:        case 0x07:
1.1.1.43  root     15148:        case 0x11:
1.1.1.34  root     15149:                // COMMAND.COM calls this service without checking APPEND is installed
                   15150:                break;
                   15151:        default:
                   15152:                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));
                   15153:                REG16(AX) = 0x01;
                   15154:                m_CF = 1;
                   15155:                break;
                   15156:        }
                   15157: }
                   15158: 
1.1.1.24  root     15159: inline void msdos_int_33h_0000h()
                   15160: {
                   15161:        REG16(AX) = 0xffff; // hardware/driver installed
                   15162:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15163: }
                   15164: 
                   15165: inline void msdos_int_33h_0001h()
                   15166: {
1.1.1.34  root     15167:        if(mouse.hidden > 0) {
                   15168:                mouse.hidden--;
                   15169:        }
                   15170:        if(mouse.hidden == 0) {
1.1.1.64  root     15171:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), (dwConsoleMode | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS) & ~ENABLE_QUICK_EDIT_MODE);
1.1.1.59  root     15172:                pic[1].imr &= ~0x10; // enable irq12
1.1.1.24  root     15173:        }
                   15174: }
                   15175: 
                   15176: inline void msdos_int_33h_0002h()
                   15177: {
1.1.1.34  root     15178:        mouse.hidden++;
1.1.1.64  root     15179:        if(dwConsoleMode & (ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE)) {
                   15180:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_EXTENDED_FLAGS);
                   15181:        } else {
                   15182:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   15183:        }
1.1.1.59  root     15184:        pic[1].imr |= 0x10; // disable irq12
1.1.1.24  root     15185: }
                   15186: 
                   15187: inline void msdos_int_33h_0003h()
                   15188: {
1.1.1.34  root     15189: //     if(mouse.hidden > 0) {
                   15190:                update_console_input();
                   15191: //     }
1.1.1.24  root     15192:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     15193:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   15194:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   15195: }
                   15196: 
                   15197: inline void msdos_int_33h_0004h()
                   15198: {
                   15199:        mouse.position.x = REG16(CX);
                   15200:        mouse.position.x = REG16(DX);
1.1.1.24  root     15201: }
                   15202: 
                   15203: inline void msdos_int_33h_0005h()
                   15204: {
1.1.1.34  root     15205: //     if(mouse.hidden > 0) {
                   15206:                update_console_input();
                   15207: //     }
1.1.1.24  root     15208:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   15209:                int idx = REG16(BX);
1.1.1.34  root     15210:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   15211:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   15212:                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     15213:                mouse.buttons[idx].pressed_times = 0;
                   15214:        } else {
                   15215:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   15216:        }
                   15217:        REG16(AX) = mouse.get_buttons();
                   15218: }
                   15219: 
                   15220: inline void msdos_int_33h_0006h()
                   15221: {
1.1.1.34  root     15222: //     if(mouse.hidden > 0) {
                   15223:                update_console_input();
                   15224: //     }
1.1.1.24  root     15225:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   15226:                int idx = REG16(BX);
1.1.1.34  root     15227:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   15228:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   15229:                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     15230:                mouse.buttons[idx].released_times = 0;
                   15231:        } else {
                   15232:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   15233:        }
                   15234:        REG16(AX) = mouse.get_buttons();
                   15235: }
                   15236: 
                   15237: inline void msdos_int_33h_0007h()
                   15238: {
                   15239:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   15240:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   15241: }
                   15242: 
                   15243: inline void msdos_int_33h_0008h()
                   15244: {
                   15245:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   15246:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   15247: }
                   15248: 
                   15249: inline void msdos_int_33h_0009h()
                   15250: {
                   15251:        mouse.hot_spot[0] = REG16(BX);
                   15252:        mouse.hot_spot[1] = REG16(CX);
                   15253: }
                   15254: 
1.1.1.49  root     15255: inline void msdos_int_33h_000ah()
                   15256: {
                   15257:        mouse.screen_mask = REG16(CX);
                   15258:        mouse.cursor_mask = REG16(DX);
                   15259: }
                   15260: 
1.1.1.24  root     15261: inline void msdos_int_33h_000bh()
                   15262: {
1.1.1.34  root     15263: //     if(mouse.hidden > 0) {
                   15264:                update_console_input();
                   15265: //     }
1.1.1.24  root     15266:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   15267:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   15268:        mouse.prev_position.x = mouse.position.x;
                   15269:        mouse.prev_position.y = mouse.position.y;
                   15270:        REG16(CX) = dx;
                   15271:        REG16(DX) = dy;
                   15272: }
                   15273: 
                   15274: inline void msdos_int_33h_000ch()
                   15275: {
                   15276:        mouse.call_mask = REG16(CX);
                   15277:        mouse.call_addr.w.l = REG16(DX);
                   15278:        mouse.call_addr.w.h = SREG(ES);
                   15279: }
                   15280: 
                   15281: inline void msdos_int_33h_000fh()
                   15282: {
                   15283:        mouse.mickey.x = REG16(CX);
                   15284:        mouse.mickey.y = REG16(DX);
                   15285: }
                   15286: 
                   15287: inline void msdos_int_33h_0011h()
                   15288: {
                   15289:        REG16(AX) = 0xffff;
                   15290:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15291: }
                   15292: 
                   15293: inline void msdos_int_33h_0014h()
                   15294: {
                   15295:        UINT16 old_mask = mouse.call_mask;
                   15296:        UINT16 old_ofs = mouse.call_addr.w.l;
                   15297:        UINT16 old_seg = mouse.call_addr.w.h;
                   15298:        
                   15299:        mouse.call_mask = REG16(CX);
                   15300:        mouse.call_addr.w.l = REG16(DX);
                   15301:        mouse.call_addr.w.h = SREG(ES);
                   15302:        
                   15303:        REG16(CX) = old_mask;
                   15304:        REG16(DX) = old_ofs;
                   15305:        SREG(ES) = old_seg;
                   15306:        i386_load_segment_descriptor(ES);
                   15307: }
                   15308: 
                   15309: inline void msdos_int_33h_0015h()
                   15310: {
                   15311:        REG16(BX) = sizeof(mouse);
                   15312: }
                   15313: 
                   15314: inline void msdos_int_33h_0016h()
                   15315: {
                   15316:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   15317: }
                   15318: 
                   15319: inline void msdos_int_33h_0017h()
                   15320: {
                   15321:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   15322: }
                   15323: 
1.1.1.43  root     15324: inline void msdos_int_33h_0018h()
                   15325: {
                   15326:        for(int i = 0; i < 8; i++) {
                   15327:                if(REG16(CX) & (1 << i)) {
                   15328:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   15329:                                // event handler already exists
                   15330:                                REG16(AX) = 0xffff;
                   15331:                                break;
                   15332:                        }
                   15333:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   15334:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   15335:                }
                   15336:        }
                   15337: }
                   15338: 
                   15339: inline void msdos_int_33h_0019h()
                   15340: {
                   15341:        UINT16 call_mask = REG16(CX);
                   15342:        
                   15343:        REG16(CX) = 0;
                   15344:        
                   15345:        for(int i = 0; i < 8; i++) {
                   15346:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   15347:                        for(int j = 0; j < 8; j++) {
                   15348:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   15349:                                        REG16(CX) |= (1 << j);
                   15350:                                }
                   15351:                        }
                   15352:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   15353:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   15354:                        break;
                   15355:                }
                   15356:        }
                   15357: }
                   15358: 
1.1.1.24  root     15359: inline void msdos_int_33h_001ah()
                   15360: {
                   15361:        mouse.sensitivity[0] = REG16(BX);
                   15362:        mouse.sensitivity[1] = REG16(CX);
                   15363:        mouse.sensitivity[2] = REG16(DX);
                   15364: }
                   15365: 
                   15366: inline void msdos_int_33h_001bh()
                   15367: {
                   15368:        REG16(BX) = mouse.sensitivity[0];
                   15369:        REG16(CX) = mouse.sensitivity[1];
                   15370:        REG16(DX) = mouse.sensitivity[2];
                   15371: }
                   15372: 
                   15373: inline void msdos_int_33h_001dh()
                   15374: {
                   15375:        mouse.display_page = REG16(BX);
                   15376: }
                   15377: 
                   15378: inline void msdos_int_33h_001eh()
                   15379: {
                   15380:        REG16(BX) = mouse.display_page;
                   15381: }
                   15382: 
1.1.1.34  root     15383: inline void msdos_int_33h_001fh()
                   15384: {
                   15385:        // from DOSBox
                   15386:        REG16(BX) = 0x0000;
                   15387:        SREG(ES) = 0x0000;
                   15388:        i386_load_segment_descriptor(ES);
                   15389:        mouse.enabled = false;
                   15390:        mouse.old_hidden = mouse.hidden;
                   15391:        mouse.hidden = 1;
                   15392: }
                   15393: 
                   15394: inline void msdos_int_33h_0020h()
                   15395: {
                   15396:        // from DOSBox
                   15397:        mouse.enabled = true;
                   15398:        mouse.hidden = mouse.old_hidden;
                   15399: }
                   15400: 
1.1.1.24  root     15401: inline void msdos_int_33h_0021h()
                   15402: {
                   15403:        REG16(AX) = 0xffff;
                   15404:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15405: }
                   15406: 
                   15407: inline void msdos_int_33h_0022h()
                   15408: {
                   15409:        mouse.language = REG16(BX);
                   15410: }
                   15411: 
                   15412: inline void msdos_int_33h_0023h()
                   15413: {
                   15414:        REG16(BX) = mouse.language;
                   15415: }
                   15416: 
                   15417: inline void msdos_int_33h_0024h()
                   15418: {
                   15419:        REG16(BX) = 0x0805; // V8.05
                   15420:        REG16(CX) = 0x0400; // PS/2
                   15421: }
                   15422: 
1.1.1.49  root     15423: inline void msdos_int_33h_0025h()
                   15424: {
                   15425:        REG16(AX) = 0x8000; // driver (not TSR), software text cursor
                   15426: }
                   15427: 
1.1.1.24  root     15428: inline void msdos_int_33h_0026h()
                   15429: {
                   15430:        REG16(BX) = 0x0000;
                   15431:        REG16(CX) = mouse.max_position.x;
                   15432:        REG16(DX) = mouse.max_position.y;
                   15433: }
                   15434: 
1.1.1.49  root     15435: inline void msdos_int_33h_0027h()
                   15436: {
                   15437: //     if(mouse.hidden > 0) {
                   15438:                update_console_input();
                   15439: //     }
                   15440:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   15441:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   15442:        mouse.prev_position.x = mouse.position.x;
                   15443:        mouse.prev_position.y = mouse.position.y;
                   15444:        REG16(AX) = mouse.screen_mask;
                   15445:        REG16(BX) = mouse.cursor_mask;
                   15446:        REG16(CX) = dx;
                   15447:        REG16(DX) = dy;
                   15448: }
                   15449: 
                   15450: inline void msdos_int_33h_0028h()
                   15451: {
                   15452:        if(REG16(CX) != 0) {
                   15453:                UINT8 tmp = REG8(AL);
                   15454:                REG8(AL) = REG8(CL);
                   15455:                pcbios_int_10h_00h();
                   15456:                REG8(AL) = tmp;
                   15457:        }
                   15458:        REG8(CL) = 0x00; // successful
                   15459: }
                   15460: 
                   15461: inline void msdos_int_33h_0029h()
                   15462: {
                   15463:        switch(REG16(CX)) {
                   15464:        case 0x0000:
                   15465:                REG16(CX) = 0x0003;
                   15466:                sprintf((char *)(mem + WORK_TOP), "TEXT Mode (80x25)$");
                   15467:                break;
                   15468:        case 0x0003:
                   15469:                REG16(CX) = 0x0070;
                   15470:                sprintf((char *)(mem + WORK_TOP), "V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15471:                break;
                   15472:        case 0x0070:
                   15473:                REG16(CX) = 0x0071;
                   15474:                sprintf((char *)(mem + WORK_TOP), "Extended CGA V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15475:                break;
                   15476:        case 0x0071:
                   15477:                REG16(CX) = 0x0073;
                   15478:                sprintf((char *)(mem + WORK_TOP), "Extended CGA TEXT Mode (80x25)$");
                   15479:                break;
                   15480:        default:
                   15481:                REG16(CX) = 0x0000;
                   15482:                break;
                   15483:        }
                   15484:        if(REG16(CX) != 0) {
                   15485:                SREG(DS) = (WORK_TOP >> 4);
                   15486:        } else {
                   15487:                SREG(DS) = 0x0000;
                   15488:        }
                   15489:        i386_load_segment_descriptor(DS);
                   15490:        REG16(DX) = 0x0000;
                   15491: }
                   15492: 
1.1.1.24  root     15493: inline void msdos_int_33h_002ah()
                   15494: {
1.1.1.34  root     15495:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     15496:        REG16(BX) = mouse.hot_spot[0];
                   15497:        REG16(CX) = mouse.hot_spot[1];
                   15498:        REG16(DX) = 4; // PS/2
                   15499: }
                   15500: 
                   15501: inline void msdos_int_33h_0031h()
                   15502: {
                   15503:        REG16(AX) = mouse.min_position.x;
                   15504:        REG16(BX) = mouse.min_position.y;
                   15505:        REG16(CX) = mouse.max_position.x;
                   15506:        REG16(DX) = mouse.max_position.y;
                   15507: }
                   15508: 
                   15509: inline void msdos_int_33h_0032h()
                   15510: {
                   15511:        REG16(AX) = 0;
1.1.1.49  root     15512:        REG16(AX) |= 0x8000; // 0025h
1.1.1.24  root     15513:        REG16(AX) |= 0x4000; // 0026h
1.1.1.49  root     15514:        REG16(AX) |= 0x2000; // 0027h
1.1.1.24  root     15515: //     REG16(AX) |= 0x1000; // 0028h
                   15516: //     REG16(AX) |= 0x0800; // 0029h
                   15517:        REG16(AX) |= 0x0400; // 002ah
                   15518: //     REG16(AX) |= 0x0200; // 002bh
                   15519: //     REG16(AX) |= 0x0100; // 002ch
                   15520: //     REG16(AX) |= 0x0080; // 002dh
                   15521: //     REG16(AX) |= 0x0040; // 002eh
                   15522:        REG16(AX) |= 0x0020; // 002fh
                   15523: //     REG16(AX) |= 0x0010; // 0030h
                   15524:        REG16(AX) |= 0x0008; // 0031h
                   15525:        REG16(AX) |= 0x0004; // 0032h
                   15526: //     REG16(AX) |= 0x0002; // 0033h
                   15527: //     REG16(AX) |= 0x0001; // 0034h
                   15528: }
                   15529: 
1.1.1.49  root     15530: inline void msdos_int_33h_004dh()
                   15531: {
                   15532:        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), "Copyright 2017 MS-DOS Player");
                   15533: }
                   15534: 
                   15535: inline void msdos_int_33h_006dh()
                   15536: {
                   15537:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x08; // V8.05
                   15538:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = 0x05;
                   15539: }
                   15540: 
1.1.1.19  root     15541: inline void msdos_int_67h_40h()
                   15542: {
                   15543:        if(!support_ems) {
                   15544:                REG8(AH) = 0x84;
                   15545:        } else {
                   15546:                REG8(AH) = 0x00;
                   15547:        }
                   15548: }
                   15549: 
                   15550: inline void msdos_int_67h_41h()
                   15551: {
                   15552:        if(!support_ems) {
                   15553:                REG8(AH) = 0x84;
                   15554:        } else {
                   15555:                REG8(AH) = 0x00;
                   15556:                REG16(BX) = EMS_TOP >> 4;
                   15557:        }
                   15558: }
                   15559: 
                   15560: inline void msdos_int_67h_42h()
                   15561: {
                   15562:        if(!support_ems) {
                   15563:                REG8(AH) = 0x84;
                   15564:        } else {
                   15565:                REG8(AH) = 0x00;
                   15566:                REG16(BX) = free_ems_pages;
                   15567:                REG16(DX) = MAX_EMS_PAGES;
                   15568:        }
                   15569: }
                   15570: 
                   15571: inline void msdos_int_67h_43h()
                   15572: {
                   15573:        if(!support_ems) {
                   15574:                REG8(AH) = 0x84;
                   15575:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15576:                REG8(AH) = 0x87;
                   15577:        } else if(REG16(BX) > free_ems_pages) {
                   15578:                REG8(AH) = 0x88;
                   15579:        } else if(REG16(BX) == 0) {
                   15580:                REG8(AH) = 0x89;
                   15581:        } else {
1.1.1.31  root     15582:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15583:                        if(!ems_handles[i].allocated) {
                   15584:                                ems_allocate_pages(i, REG16(BX));
                   15585:                                REG8(AH) = 0x00;
                   15586:                                REG16(DX) = i;
                   15587:                                return;
                   15588:                        }
                   15589:                }
                   15590:                REG8(AH) = 0x85;
                   15591:        }
                   15592: }
                   15593: 
                   15594: inline void msdos_int_67h_44h()
                   15595: {
                   15596:        if(!support_ems) {
                   15597:                REG8(AH) = 0x84;
1.1.1.31  root     15598:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15599:                REG8(AH) = 0x83;
                   15600:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   15601:                REG8(AH) = 0x8a;
                   15602: //     } else if(!(REG8(AL) < 4)) {
                   15603: //             REG8(AH) = 0x8b;
                   15604:        } else if(REG16(BX) == 0xffff) {
                   15605:                ems_unmap_page(REG8(AL) & 3);
                   15606:                REG8(AH) = 0x00;
                   15607:        } else {
                   15608:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   15609:                REG8(AH) = 0x00;
                   15610:        }
                   15611: }
                   15612: 
                   15613: inline void msdos_int_67h_45h()
                   15614: {
                   15615:        if(!support_ems) {
                   15616:                REG8(AH) = 0x84;
1.1.1.31  root     15617:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15618:                REG8(AH) = 0x83;
                   15619:        } else {
                   15620:                ems_release_pages(REG16(DX));
                   15621:                REG8(AH) = 0x00;
                   15622:        }
                   15623: }
                   15624: 
                   15625: inline void msdos_int_67h_46h()
                   15626: {
                   15627:        if(!support_ems) {
                   15628:                REG8(AH) = 0x84;
                   15629:        } else {
1.1.1.29  root     15630: //             REG16(AX) = 0x0032; // EMS 3.2
                   15631:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     15632:        }
                   15633: }
                   15634: 
                   15635: inline void msdos_int_67h_47h()
                   15636: {
                   15637:        // NOTE: the map data should be stored in the specified ems page, not process data
                   15638:        process_t *process = msdos_process_info_get(current_psp);
                   15639:        
                   15640:        if(!support_ems) {
                   15641:                REG8(AH) = 0x84;
1.1.1.31  root     15642: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15643: //             REG8(AH) = 0x83;
                   15644:        } else if(process->ems_pages_stored) {
                   15645:                REG8(AH) = 0x8d;
                   15646:        } else {
                   15647:                for(int i = 0; i < 4; i++) {
                   15648:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   15649:                        process->ems_pages[i].page   = ems_pages[i].page;
                   15650:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   15651:                }
                   15652:                process->ems_pages_stored = true;
                   15653:                REG8(AH) = 0x00;
                   15654:        }
                   15655: }
                   15656: 
                   15657: inline void msdos_int_67h_48h()
                   15658: {
                   15659:        // NOTE: the map data should be restored from the specified ems page, not process data
                   15660:        process_t *process = msdos_process_info_get(current_psp);
                   15661:        
                   15662:        if(!support_ems) {
                   15663:                REG8(AH) = 0x84;
1.1.1.31  root     15664: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15665: //             REG8(AH) = 0x83;
                   15666:        } else if(!process->ems_pages_stored) {
                   15667:                REG8(AH) = 0x8e;
                   15668:        } else {
                   15669:                for(int i = 0; i < 4; i++) {
                   15670:                        if(process->ems_pages[i].mapped) {
                   15671:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   15672:                        } else {
                   15673:                                ems_unmap_page(i);
                   15674:                        }
                   15675:                }
                   15676:                process->ems_pages_stored = false;
                   15677:                REG8(AH) = 0x00;
                   15678:        }
                   15679: }
                   15680: 
                   15681: inline void msdos_int_67h_4bh()
                   15682: {
                   15683:        if(!support_ems) {
                   15684:                REG8(AH) = 0x84;
                   15685:        } else {
                   15686:                REG8(AH) = 0x00;
                   15687:                REG16(BX) = 0;
1.1.1.31  root     15688:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15689:                        if(ems_handles[i].allocated) {
                   15690:                                REG16(BX)++;
                   15691:                        }
                   15692:                }
                   15693:        }
                   15694: }
                   15695: 
                   15696: inline void msdos_int_67h_4ch()
                   15697: {
                   15698:        if(!support_ems) {
                   15699:                REG8(AH) = 0x84;
1.1.1.31  root     15700:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15701:                REG8(AH) = 0x83;
                   15702:        } else {
                   15703:                REG8(AH) = 0x00;
                   15704:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   15705:        }
                   15706: }
                   15707: 
                   15708: inline void msdos_int_67h_4dh()
                   15709: {
                   15710:        if(!support_ems) {
                   15711:                REG8(AH) = 0x84;
                   15712:        } else {
                   15713:                REG8(AH) = 0x00;
                   15714:                REG16(BX) = 0;
1.1.1.31  root     15715:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15716:                        if(ems_handles[i].allocated) {
                   15717:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   15718:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   15719:                                REG16(BX)++;
                   15720:                        }
                   15721:                }
                   15722:        }
                   15723: }
                   15724: 
1.1.1.20  root     15725: inline void msdos_int_67h_4eh()
                   15726: {
                   15727:        if(!support_ems) {
                   15728:                REG8(AH) = 0x84;
                   15729:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15730:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   15731:                        // save page map
                   15732:                        for(int i = 0; i < 4; i++) {
                   15733:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15734:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15735:                        }
                   15736:                }
                   15737:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15738:                        // restore page map
                   15739:                        for(int i = 0; i < 4; i++) {
                   15740:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15741:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15742:                                
1.1.1.31  root     15743:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     15744:                                        ems_map_page(i, handle, page);
                   15745:                                } else {
                   15746:                                        ems_unmap_page(i);
                   15747:                                }
                   15748:                        }
                   15749:                }
                   15750:                REG8(AH) = 0x00;
                   15751:        } else if(REG8(AL) == 0x03) {
                   15752:                REG8(AH) = 0x00;
1.1.1.21  root     15753:                REG8(AL) = 4 * 4;
                   15754:        } else {
1.1.1.22  root     15755:                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     15756:                REG8(AH) = 0x8f;
                   15757:        }
                   15758: }
                   15759: 
                   15760: inline void msdos_int_67h_4fh()
                   15761: {
                   15762:        if(!support_ems) {
                   15763:                REG8(AH) = 0x84;
                   15764:        } else if(REG8(AL) == 0x00) {
                   15765:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15766:                
                   15767:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   15768:                for(int i = 0; i < count; i++) {
                   15769:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   15770:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15771:                        
                   15772: //                     if(!(physical < 4)) {
                   15773: //                             REG8(AH) = 0x8b;
                   15774: //                             return;
                   15775: //                     }
                   15776:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     15777:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   15778:                        *(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     15779:                }
                   15780:                REG8(AH) = 0x00;
                   15781:        } else if(REG8(AL) == 0x01) {
                   15782:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15783:                
                   15784:                for(int i = 0; i < count; i++) {
                   15785:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   15786:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15787:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   15788:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   15789:                        
                   15790: //                     if(!(physical < 4)) {
                   15791: //                             REG8(AH) = 0x8b;
                   15792: //                             return;
                   15793: //                     } else
1.1.1.41  root     15794:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     15795:                                ems_map_page(physical & 3, handle, logical);
                   15796:                        } else {
1.1.1.41  root     15797:                                ems_unmap_page(physical & 3);
1.1.1.21  root     15798:                        }
                   15799:                }
                   15800:                REG8(AH) = 0x00;
                   15801:        } else if(REG8(AL) == 0x02) {
                   15802:                REG8(AH) = 0x00;
                   15803:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     15804:        } else {
1.1.1.22  root     15805:                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     15806:                REG8(AH) = 0x8f;
                   15807:        }
                   15808: }
                   15809: 
                   15810: inline void msdos_int_67h_50h()
                   15811: {
                   15812:        if(!support_ems) {
                   15813:                REG8(AH) = 0x84;
1.1.1.31  root     15814:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     15815:                REG8(AH) = 0x83;
                   15816:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15817:                for(int i = 0; i < REG16(CX); i++) {
                   15818:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15819:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15820:                        
                   15821:                        if(REG8(AL) == 0x01) {
                   15822:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15823:                        }
                   15824: //                     if(!(physical < 4)) {
                   15825: //                             REG8(AH) = 0x8b;
                   15826: //                             return;
                   15827: //                     } else
                   15828:                        if(logical == 0xffff) {
                   15829:                                ems_unmap_page(physical & 3);
                   15830:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15831:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15832:                        } else {
                   15833:                                REG8(AH) = 0x8a;
                   15834:                                return;
                   15835:                        }
                   15836:                }
                   15837:                REG8(AH) = 0x00;
                   15838:        } else {
1.1.1.22  root     15839:                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     15840:                REG8(AH) = 0x8f;
                   15841:        }
                   15842: }
                   15843: 
1.1.1.19  root     15844: inline void msdos_int_67h_51h()
                   15845: {
                   15846:        if(!support_ems) {
                   15847:                REG8(AH) = 0x84;
1.1.1.31  root     15848:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15849:                REG8(AH) = 0x83;
                   15850:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15851:                REG8(AH) = 0x87;
                   15852:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   15853:                REG8(AH) = 0x88;
                   15854:        } else {
                   15855:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   15856:                REG8(AH) = 0x00;
                   15857:        }
                   15858: }
                   15859: 
1.1.1.20  root     15860: inline void msdos_int_67h_52h()
                   15861: {
                   15862:        if(!support_ems) {
                   15863:                REG8(AH) = 0x84;
1.1.1.31  root     15864: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15865: //             REG8(AH) = 0x83;
1.1.1.20  root     15866:        } else if(REG8(AL) == 0x00) {
                   15867:                REG8(AL) = 0x00; // handle is volatile
                   15868:                REG8(AH) = 0x00;
                   15869:        } else if(REG8(AL) == 0x01) {
                   15870:                if(REG8(BL) == 0x00) {
                   15871:                        REG8(AH) = 0x00;
                   15872:                } else {
                   15873:                        REG8(AH) = 0x90; // undefined attribute type
                   15874:                }
                   15875:        } else if(REG8(AL) == 0x02) {
                   15876:                REG8(AL) = 0x00; // only volatile handles supported
                   15877:                REG8(AH) = 0x00;
                   15878:        } else {
1.1.1.22  root     15879:                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     15880:                REG8(AH) = 0x8f;
                   15881:        }
                   15882: }
                   15883: 
1.1.1.19  root     15884: inline void msdos_int_67h_53h()
                   15885: {
                   15886:        if(!support_ems) {
                   15887:                REG8(AH) = 0x84;
1.1.1.31  root     15888:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15889:                REG8(AH) = 0x83;
                   15890:        } else if(REG8(AL) == 0x00) {
                   15891:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   15892:                REG8(AH) = 0x00;
                   15893:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     15894:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15895:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15896:                                REG8(AH) = 0xa1;
                   15897:                                return;
                   15898:                        }
                   15899:                }
                   15900:                REG8(AH) = 0x00;
                   15901:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   15902:        } else {
1.1.1.22  root     15903:                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     15904:                REG8(AH) = 0x8f;
1.1.1.19  root     15905:        }
                   15906: }
                   15907: 
                   15908: inline void msdos_int_67h_54h()
                   15909: {
                   15910:        if(!support_ems) {
                   15911:                REG8(AH) = 0x84;
                   15912:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     15913:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15914:                        if(ems_handles[i].allocated) {
                   15915:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   15916:                        } else {
                   15917:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   15918:                        }
                   15919:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   15920:                }
                   15921:                REG8(AH) = 0x00;
                   15922:                REG8(AL) = MAX_EMS_HANDLES;
                   15923:        } else if(REG8(AL) == 0x01) {
                   15924:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     15925:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15926:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15927:                                REG8(AH) = 0x00;
                   15928:                                REG16(DX) = i;
                   15929:                                break;
                   15930:                        }
                   15931:                }
                   15932:        } else if(REG8(AL) == 0x02) {
                   15933:                REG8(AH) = 0x00;
                   15934:                REG16(BX) = MAX_EMS_HANDLES;
                   15935:        } else {
1.1.1.22  root     15936:                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     15937:                REG8(AH) = 0x8f;
                   15938:        }
                   15939: }
                   15940: 
1.1.1.49  root     15941: inline void msdos_int_67h_55h()
                   15942: {
                   15943:        if(!support_ems) {
                   15944:                REG8(AH) = 0x84;
                   15945:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15946:                REG8(AH) = 0x83;
                   15947:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15948:                UINT16 jump_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
                   15949:                UINT16 jump_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
                   15950:                UINT8  entries  = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
                   15951:                UINT16 map_ofs  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   15952:                UINT16 map_seg  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
                   15953:                
                   15954:                for(int i = 0; i < (int)entries; i++) {
                   15955:                        int logical  = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 0);
                   15956:                        int physical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 2);
                   15957:                        
                   15958:                        if(REG8(AL) == 0x01) {
                   15959:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15960:                        }
                   15961: //                     if(!(physical < 4)) {
                   15962: //                             REG8(AH) = 0x8b;
                   15963: //                             return;
                   15964: //                     } else
                   15965:                        if(logical == 0xffff) {
                   15966:                                ems_unmap_page(physical & 3);
                   15967:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15968:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15969:                        } else {
                   15970:                                REG8(AH) = 0x8a;
                   15971:                                return;
                   15972:                        }
                   15973:                }
                   15974:                i386_jmp_far(jump_seg, jump_ofs);
                   15975:                REG8(AH) = 0x00;
                   15976:        } else {
                   15977:                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));
                   15978:                REG8(AH) = 0x8f;
                   15979:        }
                   15980: }
                   15981: 
                   15982: inline void msdos_int_67h_56h()
                   15983: {
                   15984:        if(!support_ems) {
                   15985:                REG8(AH) = 0x84;
                   15986:        } else if(REG8(AL) == 0x02) {
                   15987:                REG16(BX) = (2 + 2) * 4;
                   15988:                REG8(AH) = 0x00;
                   15989:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15990:                REG8(AH) = 0x83;
                   15991:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15992:                UINT16 call_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  0);
                   15993:                UINT16 call_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  2);
                   15994:                UINT8  new_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  4);
                   15995:                UINT16 new_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  5);
                   15996:                UINT16 new_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  7);
                   15997: #if 0
                   15998:                UINT8  old_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  9);
                   15999:                UINT16 old_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 10);
                   16000:                UINT16 old_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 12);
                   16001: #endif
                   16002:                UINT16 handles[4], pages[4];
                   16003:                
                   16004:                // alter page map and call routine is at fffc:001f
                   16005:                if(!(call_seg == 0 && call_ofs == 0)) {
                   16006:                        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   16007:                        mem[DUMMY_TOP + 0x20] = (call_ofs >> 0) & 0xff;
                   16008:                        mem[DUMMY_TOP + 0x21] = (call_ofs >> 8) & 0xff;
                   16009:                        mem[DUMMY_TOP + 0x22] = (call_seg >> 0) & 0xff;
                   16010:                        mem[DUMMY_TOP + 0x23] = (call_seg >> 8) & 0xff;
                   16011:                } else {
                   16012:                        // invalid call addr :-(
                   16013:                        mem[DUMMY_TOP + 0x1f] = 0x90;   // nop
                   16014:                        mem[DUMMY_TOP + 0x20] = 0x90;   // nop
                   16015:                        mem[DUMMY_TOP + 0x21] = 0x90;   // nop
                   16016:                        mem[DUMMY_TOP + 0x22] = 0x90;   // nop
                   16017:                        mem[DUMMY_TOP + 0x23] = 0x90;   // nop
                   16018:                }
                   16019:                // do call far (push cs/ip) in old mapping
                   16020:                i386_call_far(DUMMY_TOP >> 4, 0x001f);
                   16021:                
                   16022:                // get old mapping data
                   16023: #if 0
                   16024:                for(int i = 0; i < (int)old_entries; i++) {
                   16025:                        int logical  = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 0);
                   16026:                        int physical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 2);
                   16027:                        
                   16028:                        if(REG8(AL) == 0x01) {
                   16029:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   16030:                        }
                   16031: //                     if(!(physical < 4)) {
                   16032: //                             REG8(AH) = 0x8b;
                   16033: //                             return;
                   16034: //                     } else
                   16035:                        if(logical == 0xffff) {
                   16036:                                ems_unmap_page(physical & 3);
                   16037:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   16038:                                ems_map_page(physical & 3, REG16(DX), logical);
                   16039:                        } else {
                   16040:                                REG8(AH) = 0x8a;
                   16041:                                return;
                   16042:                        }
                   16043:                }
                   16044: #endif
                   16045:                for(int i = 0; i < 4; i++) {
                   16046:                        handles[i] = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   16047:                        pages  [i] = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   16048:                }
                   16049:                
                   16050:                // set new mapping
                   16051:                for(int i = 0; i < (int)new_entries; i++) {
                   16052:                        int logical  = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 0);
                   16053:                        int physical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 2);
                   16054:                        
                   16055:                        if(REG8(AL) == 0x01) {
                   16056:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   16057:                        }
                   16058: //                     if(!(physical < 4)) {
                   16059: //                             REG8(AH) = 0x8b;
                   16060: //                             return;
                   16061: //                     } else
                   16062:                        if(logical == 0xffff) {
                   16063:                                ems_unmap_page(physical & 3);
                   16064:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   16065:                                ems_map_page(physical & 3, REG16(DX), logical);
                   16066:                        } else {
                   16067:                                REG8(AH) = 0x8a;
                   16068:                                return;
                   16069:                        }
                   16070:                }
                   16071:                
                   16072:                // push old mapping data in new mapping
                   16073:                for(int i = 0; i < 4; i++) {
                   16074:                        i386_push16(handles[i]);
                   16075:                        i386_push16(pages  [i]);
                   16076:                }
                   16077:                REG8(AH) = 0x00;
                   16078:        } else {
                   16079:                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));
                   16080:                REG8(AH) = 0x8f;
                   16081:        }
                   16082: }
                   16083: 
1.1.1.20  root     16084: inline void msdos_int_67h_57h_tmp()
                   16085: {
                   16086:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   16087:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   16088:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   16089:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   16090:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   16091:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   16092:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   16093:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   16094:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   16095:        
1.1.1.32  root     16096:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     16097:        UINT32 src_addr, dest_addr;
                   16098:        UINT32 src_addr_max, dest_addr_max;
                   16099:        
                   16100:        if(src_type == 0) {
                   16101:                src_buffer = mem;
                   16102:                src_addr = (src_seg << 4) + src_ofs;
                   16103:                src_addr_max = MAX_MEM;
                   16104:        } else {
1.1.1.31  root     16105:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     16106:                        REG8(AH) = 0x83;
                   16107:                        return;
                   16108:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   16109:                        REG8(AH) = 0x8a;
                   16110:                        return;
                   16111:                }
1.1.1.32  root     16112:                if(ems_handles[src_handle].buffer != NULL) {
                   16113:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   16114:                }
1.1.1.20  root     16115:                src_addr = src_ofs;
1.1.1.32  root     16116:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     16117:        }
                   16118:        if(dest_type == 0) {
                   16119:                dest_buffer = mem;
                   16120:                dest_addr = (dest_seg << 4) + dest_ofs;
                   16121:                dest_addr_max = MAX_MEM;
                   16122:        } else {
1.1.1.31  root     16123:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     16124:                        REG8(AH) = 0x83;
                   16125:                        return;
                   16126:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   16127:                        REG8(AH) = 0x8a;
                   16128:                        return;
                   16129:                }
1.1.1.32  root     16130:                if(ems_handles[dest_handle].buffer != NULL) {
                   16131:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   16132:                }
1.1.1.20  root     16133:                dest_addr = dest_ofs;
1.1.1.32  root     16134:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     16135:        }
1.1.1.32  root     16136:        if(src_buffer != NULL && dest_buffer != NULL) {
                   16137:                for(int i = 0; i < copy_length; i++) {
                   16138:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   16139:                                if(REG8(AL) == 0x00) {
                   16140:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   16141:                                } else if(REG8(AL) == 0x01) {
                   16142:                                        UINT8 tmp = dest_buffer[dest_addr];
                   16143:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   16144:                                        src_buffer[src_addr++] = tmp;
                   16145:                                }
                   16146:                        } else {
                   16147:                                REG8(AH) = 0x93;
                   16148:                                return;
1.1.1.20  root     16149:                        }
                   16150:                }
1.1.1.32  root     16151:                REG8(AH) = 0x00;
                   16152:        } else {
                   16153:                REG8(AH) = 0x80;
1.1.1.20  root     16154:        }
                   16155: }
                   16156: 
                   16157: inline void msdos_int_67h_57h()
                   16158: {
                   16159:        if(!support_ems) {
                   16160:                REG8(AH) = 0x84;
                   16161:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   16162:                struct {
                   16163:                        UINT16 handle;
                   16164:                        UINT16 page;
                   16165:                        bool mapped;
                   16166:                } tmp_pages[4];
                   16167:                
                   16168:                // unmap pages to copy memory data to ems buffer
                   16169:                for(int i = 0; i < 4; i++) {
                   16170:                        tmp_pages[i].handle = ems_pages[i].handle;
                   16171:                        tmp_pages[i].page   = ems_pages[i].page;
                   16172:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   16173:                        ems_unmap_page(i);
                   16174:                }
                   16175:                
                   16176:                // run move/exchange operation
                   16177:                msdos_int_67h_57h_tmp();
                   16178:                
                   16179:                // restore unmapped pages
                   16180:                for(int i = 0; i < 4; i++) {
                   16181:                        if(tmp_pages[i].mapped) {
                   16182:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   16183:                        }
                   16184:                }
                   16185:        } else {
1.1.1.22  root     16186:                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     16187:                REG8(AH) = 0x8f;
                   16188:        }
                   16189: }
                   16190: 
                   16191: inline void msdos_int_67h_58h()
                   16192: {
                   16193:        if(!support_ems) {
                   16194:                REG8(AH) = 0x84;
                   16195:        } else if(REG8(AL) == 0x00) {
                   16196:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     16197:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   16198:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     16199:                }
                   16200:                REG8(AH) = 0x00;
                   16201:                REG16(CX) = 4;
                   16202:        } else if(REG8(AL) == 0x01) {
                   16203:                REG8(AH) = 0x00;
                   16204:                REG16(CX) = 4;
                   16205:        } else {
1.1.1.22  root     16206:                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     16207:                REG8(AH) = 0x8f;
                   16208:        }
                   16209: }
                   16210: 
1.1.1.42  root     16211: inline void msdos_int_67h_59h()
                   16212: {
                   16213:        if(!support_ems) {
                   16214:                REG8(AH) = 0x84;
                   16215:        } else if(REG8(AL) == 0x00) {
1.1.1.49  root     16216:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1024;
                   16217:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 0;
                   16218:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 4 * 4;
                   16219:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = 0;
                   16220:                REG8(AH) = 0x00;
                   16221: //             REG8(AH) = 0xa4; // access denied by operating system
1.1.1.42  root     16222:        } else if(REG8(AL) == 0x01) {
                   16223:                REG8(AH) = 0x00;
                   16224:                REG16(BX) = free_ems_pages;
                   16225:                REG16(DX) = MAX_EMS_PAGES;
                   16226:        } else {
                   16227:                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));
                   16228:                REG8(AH) = 0x8f;
                   16229:        }
                   16230: }
                   16231: 
1.1.1.20  root     16232: inline void msdos_int_67h_5ah()
                   16233: {
                   16234:        if(!support_ems) {
1.1.1.19  root     16235:                REG8(AH) = 0x84;
1.1.1.20  root     16236:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   16237:                REG8(AH) = 0x87;
                   16238:        } else if(REG16(BX) > free_ems_pages) {
                   16239:                REG8(AH) = 0x88;
                   16240: //     } else if(REG16(BX) == 0) {
                   16241: //             REG8(AH) = 0x89;
                   16242:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     16243:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     16244:                        if(!ems_handles[i].allocated) {
                   16245:                                ems_allocate_pages(i, REG16(BX));
                   16246:                                REG8(AH) = 0x00;
                   16247:                                REG16(DX) = i;
                   16248:                                return;
                   16249:                        }
                   16250:                }
                   16251:                REG8(AH) = 0x85;
                   16252:        } else {
1.1.1.22  root     16253:                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     16254:                REG8(AH) = 0x8f;
1.1.1.19  root     16255:        }
                   16256: }
                   16257: 
1.1.1.49  root     16258: inline void msdos_int_67h_5bh()
                   16259: {
                   16260:        static UINT8  stored_bl = 0x00;
                   16261:        static UINT16 stored_es = 0x0000;
                   16262:        static UINT16 stored_di = 0x0000;
                   16263:        
                   16264:        if(!support_ems) {
                   16265:                REG8(AH) = 0x84;
                   16266:        } else if(REG8(AL) == 0x00) {
                   16267:                if(stored_bl == 0x00) {
                   16268:                        if(!(stored_es == 0 && stored_di == 0)) {
                   16269:                                for(int i = 0; i < 4; i++) {
                   16270:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   16271:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   16272:                                }
                   16273:                        }
                   16274:                        SREG(ES) = stored_es;
                   16275:                        i386_load_segment_descriptor(ES);
                   16276:                        REG16(DI) = stored_di;
                   16277:                } else {
                   16278:                        REG8(BL) = stored_bl;
                   16279:                }
                   16280:                REG8(AH) = 0x00;
                   16281:        } else if(REG8(AL) == 0x01) {
                   16282:                if(REG8(BL) == 0x00) {
                   16283:                        if(!(SREG(ES) == 0 && REG16(DI) == 0)) {
                   16284:                                for(int i = 0; i < 4; i++) {
                   16285:                                        UINT16 handle = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0);
                   16286:                                        UINT16 page   = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2);
                   16287:                                        
                   16288:                                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   16289:                                                ems_map_page(i, handle, page);
                   16290:                                        } else {
                   16291:                                                ems_unmap_page(i);
                   16292:                                        }
                   16293:                                }
                   16294:                        }
                   16295:                }
                   16296:                stored_bl = REG8(BL);
                   16297:                stored_es = SREG(ES);
                   16298:                stored_di = REG16(DI);
                   16299:                REG8(AH) = 0x00;
                   16300:        } else if(REG8(AL) == 0x02) {
                   16301:                REG16(DX) = 4 * 4;
                   16302:                REG8(AH) = 0x00;
                   16303:        } else if(REG8(AL) == 0x03) {
                   16304:                REG8(BL) = 0x00; // not supported
                   16305:                REG8(AH) = 0x00;
                   16306:        } else if(REG8(AL) == 0x04) {
                   16307:                REG8(AH) = 0x00;
                   16308:        } else if(REG8(AL) == 0x05) {
                   16309:                REG8(BL) = 0x00; // not supported
                   16310:                REG8(AH) = 0x00;
                   16311:        } else {
                   16312:                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));
                   16313:                REG8(AH) = 0x8f;
                   16314:        }
                   16315: }
                   16316: 
1.1.1.43  root     16317: inline void msdos_int_67h_5dh()
                   16318: {
                   16319:        if(!support_ems) {
                   16320:                REG8(AH) = 0x84;
                   16321:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   16322:                REG8(AH) = 0xa4; // operating system denied access
                   16323:        } else {
                   16324:                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));
                   16325:                REG8(AH) = 0x8f;
                   16326:        }
                   16327: }
                   16328: 
1.1.1.49  root     16329: inline void msdos_int_67h_70h()
                   16330: {
                   16331:        if(!support_ems) {
                   16332:                REG8(AH) = 0x84;
                   16333:        } else if(REG8(AL) == 0x00) {
                   16334:                REG8(AL) = 0x00;
                   16335:                REG8(AH) = 0x00;
                   16336:        } else if(REG8(AL) == 0x01) {
                   16337:                REG8(AL) = 0x00;
                   16338: //             REG8(AH) = (REG8(BL) == 0x00) ? 0x00 : 0x80;
                   16339:                REG8(AH) = 0x00;
                   16340:        } else {
                   16341:                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));
                   16342:                REG8(AH) = 0x8f;
                   16343:        }
                   16344: }
                   16345: 
1.1.1.30  root     16346: inline void msdos_int_67h_deh()
                   16347: {
1.1.1.63  root     16348: #if defined(SUPPORT_VCPI)
                   16349:        if(!support_ems) {
                   16350:                REG8(AH) = 0x84;
                   16351:        } else if(REG8(AL) == 0x00) {
                   16352:                REG8(AH) = 0x00;
                   16353:                REG16(BX) = 0x0100;
                   16354:        } else if(REG8(AL) == 0x01) {
                   16355:                REG8(AH) = 0x00;
                   16356:                // from DOSBox
                   16357:                for(int ct = 0; ct < 0xff; ct++) {
                   16358:                        *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x00) = 0x67;            // access bits
                   16359:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x01) = ct * 0x10;       // mapping
                   16360:                        *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x03) = 0x00;
                   16361:                }
                   16362:                for(int ct = 0xff; ct < 0x100; ct++) {
                   16363:                        *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x00) = 0x67;            // access bits
                   16364:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x01) = (ct - 0xff) * 0x10 + 0x1100;     // mapping
                   16365:                        *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x03) = 0x00;
                   16366:                }
                   16367:                REG16(DI) += 0x400;             // advance pointer by 0x100*4
                   16368:                
                   16369:                // Set up three descriptor table entries
                   16370:                UINT32 cbseg_low  = (DUMMY_TOP & 0x00ffff) << 16;
                   16371:                UINT32 cbseg_high = (DUMMY_TOP & 0x1f0000) >> 16;
                   16372:                // Descriptor 1 (code segment, callback segment)
                   16373:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x0000ffff | cbseg_low ;
                   16374:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = 0x00009a00 | cbseg_high;
                   16375:                // Descriptor 2 (data segment, full access)
                   16376:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x08) = 0x0000ffff;
                   16377:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c) = 0x00009200;
                   16378:                // Descriptor 3 (full access)
                   16379:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10) = 0x0000ffff;
                   16380:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x14) = 0x00009200;
                   16381:                // Offset in code segment of protected mode entry point
                   16382:                REG32(EBX) = 0x2a; // fffc:002a
                   16383:        } else if(REG8(AL) == 0x02) {
                   16384:                REG8(AH) = 0x00;
                   16385:                REG32(EDX) = (MAX_MEM - 1) & 0xfffff000;
                   16386:        } else if(REG8(AL) == 0x03) {
                   16387:                REG8(AH) = 0x00;
                   16388:                REG32(EDX) = 0;
                   16389:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16390:                        if(emb_handle->handle == 0) {
                   16391:                                REG32(EDX) += emb_handle->size_kb;
                   16392:                        }
                   16393:                }
                   16394:                REG32(EDX) /= 4;
                   16395:        } else if(REG8(AL) == 0x04) {
                   16396:                emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(4);
                   16397:                if(emb_handle != NULL) {
                   16398:                        REG8(AH) = 0x00;
                   16399:                        REG32(EDX) = emb_handle->address;
                   16400:                }
                   16401:        } else if(REG8(AL) == 0x05) {
                   16402:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16403:                        if(emb_handle->handle != 0 && emb_handle->address == REG32(EDX)) {
                   16404:                                REG8(AH) = 0x00;
                   16405:                                msdos_xms_free_emb_handle(emb_handle);
                   16406:                                break;
                   16407:                        }
                   16408:                }
                   16409:        } else if(REG8(AL) == 0x06) {
                   16410:                REG8(AH) = 0x00;
                   16411:                REG32(EDX) = REG16(CX) << 12;
                   16412:        } else if(REG8(AL) == 0x07) {
                   16413:                REG8(AH) = 0x00;
                   16414:                REG32(EBX) = m_cr[0];
                   16415:        } else if(REG8(AL) == 0x08) {
                   16416:                REG8(AH) = 0x00;
                   16417:                for(int i = 0; i < 8; i++) {
                   16418:                        *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i) = m_dr[i];
                   16419:                }
                   16420:        } else if(REG8(AL) == 0x09) {
                   16421:                REG8(AH) = 0x00;
                   16422:                for(int i = 0; i < 8; i++) {
                   16423:                        m_dr[i] = *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i);
                   16424:                }
                   16425:        } else if(REG8(AL) == 0x0a) {
                   16426:                REG8(AH) = 0x00;
                   16427:                REG16(BX) = pic[0].icw2;
                   16428:                REG16(CX) = pic[1].icw2;
                   16429:        } else if(REG8(AL) == 0x0b) {
                   16430:                REG8(AH) = 0x00;
                   16431:                pic[0].icw2 = REG8(BL);
                   16432:                pic[1].icw2 = REG8(CL);
                   16433:        } else if(REG8(AL) == 0x0c) {
                   16434:                // from DOSBox
                   16435:                m_IF = 0;
                   16436:                m_CPL = 0;
                   16437:                
                   16438:                // Read data from ESI (linear address)
                   16439:                UINT32 new_cr3      = *(UINT32 *)(mem + REG32(ESI) + 0x00);
                   16440:                UINT32 new_gdt_addr = *(UINT32 *)(mem + REG32(ESI) + 0x04);
                   16441:                UINT32 new_idt_addr = *(UINT32 *)(mem + REG32(ESI) + 0x08);
                   16442:                UINT16 new_ldt      = *(UINT16 *)(mem + REG32(ESI) + 0x0c);
                   16443:                UINT16 new_tr       = *(UINT16 *)(mem + REG32(ESI) + 0x0e);
                   16444:                UINT32 new_eip      = *(UINT32 *)(mem + REG32(ESI) + 0x10);
                   16445:                UINT16 new_cs       = *(UINT16 *)(mem + REG32(ESI) + 0x14);
                   16446:                
                   16447:                // Get GDT and IDT entries
                   16448:                UINT16 new_gdt_limit = *(UINT16 *)(mem + new_gdt_addr + 0);
                   16449:                UINT32 new_gdt_base  = *(UINT32 *)(mem + new_gdt_addr + 2);
                   16450:                UINT16 new_idt_limit = *(UINT16 *)(mem + new_idt_addr + 0);
                   16451:                UINT32 new_idt_base  = *(UINT32 *)(mem + new_idt_addr + 2);
                   16452:                
                   16453:                // Switch to protected mode, paging enabled if necessary
                   16454:                if(new_cr3 != 0) {
                   16455:                        m_cr[0] |= 0x80000000;
                   16456:                }
                   16457:                m_cr[3] = new_cr3;
                   16458:                
                   16459:                *(UINT8 *)(mem + new_gdt_base + (new_tr & 0xfff8) + 5) &= 0xfd;
                   16460:                
                   16461:                // Load tables and initialize segment registers
                   16462:                m_gdtr.limit = new_gdt_limit;
                   16463:                m_gdtr.base = new_gdt_base;
                   16464:                m_idtr.limit = new_idt_limit;
                   16465:                m_idtr.base = new_idt_base;
                   16466:                
1.1.1.64  root     16467:                i386_sreg_load(0x00, DS, NULL);
                   16468:                i386_sreg_load(0x00, ES, NULL);
                   16469:                i386_sreg_load(0x00, FS, NULL);
                   16470:                i386_sreg_load(0x00, GS, NULL);
1.1.1.63  root     16471:                
                   16472: //             i386_set_a20_line(1);
                   16473:                
                   16474:                /* Switch to protected mode */
                   16475:                m_VM = m_NT = 0;
                   16476:                m_IOP1 = m_IOP2 = 1;
                   16477:                
                   16478:                i386_jmp_far(new_cs, new_eip);
                   16479:        } else {
                   16480:                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));
                   16481:                REG8(AH) = 0x8f;
                   16482:        }
                   16483: #else
1.1.1.30  root     16484:        REG8(AH) = 0x84;
1.1.1.63  root     16485: #endif
1.1.1.30  root     16486: }
                   16487: 
1.1.1.19  root     16488: #ifdef SUPPORT_XMS
                   16489: 
1.1.1.32  root     16490: void msdos_xms_init()
1.1.1.26  root     16491: {
1.1.1.30  root     16492:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   16493:        emb_handle_top->address = EMB_TOP;
                   16494:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     16495:        xms_a20_local_enb_count = 0;
                   16496: }
                   16497: 
1.1.1.32  root     16498: void msdos_xms_finish()
                   16499: {
                   16500:        msdos_xms_release();
                   16501: }
                   16502: 
                   16503: void msdos_xms_release()
1.1.1.30  root     16504: {
                   16505:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   16506:                emb_handle_t *next_handle = emb_handle->next;
                   16507:                free(emb_handle);
                   16508:                emb_handle = next_handle;
                   16509:        }
                   16510: }
                   16511: 
                   16512: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   16513: {
                   16514:        if(handle != 0) {
                   16515:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16516:                        if(emb_handle->handle == handle) {
                   16517:                                return(emb_handle);
                   16518:                        }
                   16519:                }
                   16520:        }
                   16521:        return(NULL);
                   16522: }
                   16523: 
                   16524: int msdos_xms_get_unused_emb_handle_id()
                   16525: {
                   16526:        for(int handle = 1;; handle++) {
                   16527:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   16528:                        return(handle);
                   16529:                }
                   16530:        }
                   16531:        return(0);
                   16532: }
                   16533: 
                   16534: int msdos_xms_get_unused_emb_handle_count()
                   16535: {
                   16536:        int count = 64; //255;
                   16537:        
                   16538:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16539:                if(emb_handle->handle != 0) {
                   16540:                        if(--count == 1) {
                   16541:                                break;
                   16542:                        }
                   16543:                }
                   16544:        }
                   16545:        return(count);
                   16546: }
                   16547: 
                   16548: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   16549: {
                   16550:        if(emb_handle->size_kb > size_kb) {
                   16551:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   16552:                
                   16553:                new_handle->address = emb_handle->address + size_kb * 1024;
                   16554:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   16555:                emb_handle->size_kb = size_kb;
                   16556:                
                   16557:                new_handle->prev = emb_handle;
                   16558:                new_handle->next = emb_handle->next;
                   16559:                if(emb_handle->next != NULL) {
                   16560:                        emb_handle->next->prev = new_handle;
                   16561:                }
                   16562:                emb_handle->next = new_handle;
                   16563:        }
                   16564: }
                   16565: 
                   16566: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   16567: {
                   16568:        emb_handle_t *next_handle = emb_handle->next;
                   16569:        
                   16570:        if(next_handle != NULL) {
                   16571:                emb_handle->size_kb += next_handle->size_kb;
                   16572:                
                   16573:                if(next_handle->next != NULL) {
                   16574:                        next_handle->next->prev = emb_handle;
                   16575:                }
                   16576:                emb_handle->next = next_handle->next;
                   16577:                free(next_handle);
                   16578:        }
                   16579: }
                   16580: 
                   16581: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   16582: {
                   16583:        emb_handle_t *target_handle = NULL;
                   16584:        
                   16585:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16586:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   16587:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   16588:                                target_handle = emb_handle;
                   16589:                        }
                   16590:                }
                   16591:        }
                   16592:        if(target_handle != NULL) {
                   16593:                if(target_handle->size_kb > size_kb) {
                   16594:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   16595:                }
                   16596: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16597:                return(target_handle);
                   16598:        }
                   16599:        return(NULL);
                   16600: }
                   16601: 
                   16602: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   16603: {
                   16604:        emb_handle_t *prev_handle = emb_handle->prev;
                   16605:        emb_handle_t *next_handle = emb_handle->next;
                   16606:        
                   16607:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   16608:                msdos_xms_combine_emb_handles(prev_handle);
                   16609:                emb_handle = prev_handle;
                   16610:        }
                   16611:        if(next_handle != NULL && next_handle->handle == 0) {
                   16612:                msdos_xms_combine_emb_handles(emb_handle);
                   16613:        }
                   16614:        emb_handle->handle = 0;
                   16615: }
                   16616: 
1.1.1.19  root     16617: inline void msdos_call_xms_00h()
                   16618: {
1.1.1.29  root     16619: #if defined(HAS_I386)
                   16620:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.63  root     16621:        REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
                   16622: //     REG16(BX) = 0x035f; // V3.95 (Driver Revision)
1.1.1.29  root     16623: #else
                   16624:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   16625:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   16626: #endif
                   16627: //     REG16(DX) = 0x0000; // HMA does not exist
                   16628:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     16629: }
                   16630: 
                   16631: inline void msdos_call_xms_01h()
                   16632: {
1.1.1.29  root     16633:        if(REG8(AL) == 0x40) {
                   16634:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   16635:                // DX=KB free extended memory returned by last call of function 08h
                   16636:                REG16(AX) = 0x0000;
                   16637:                REG8(BL) = 0x91;
                   16638:                REG16(DX) = xms_dx_after_call_08h;
                   16639:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16640:                REG16(AX) = 0x0000;
                   16641:                REG8(BL) = 0x81; // Vdisk was detected
                   16642: #ifdef SUPPORT_HMA
                   16643:        } else if(is_hma_used_by_int_2fh) {
                   16644:                REG16(AX) = 0x0000;
                   16645:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16646:        } else if(is_hma_used_by_xms) {
                   16647:                REG16(AX) = 0x0000;
                   16648:                REG8(BL) = 0x91; // HMA is already in use
                   16649:        } else {
                   16650:                REG16(AX) = 0x0001;
                   16651:                is_hma_used_by_xms = true;
                   16652: #else
                   16653:        } else {
                   16654:                REG16(AX) = 0x0000;
                   16655:                REG8(BL) = 0x91; // HMA is already in use
                   16656: #endif
                   16657:        }
1.1.1.19  root     16658: }
                   16659: 
                   16660: inline void msdos_call_xms_02h()
                   16661: {
1.1.1.29  root     16662:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16663:                REG16(AX) = 0x0000;
                   16664:                REG8(BL) = 0x81; // Vdisk was detected
                   16665: #ifdef SUPPORT_HMA
                   16666:        } else if(is_hma_used_by_int_2fh) {
                   16667:                REG16(AX) = 0x0000;
                   16668:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16669:        } else if(!is_hma_used_by_xms) {
                   16670:                REG16(AX) = 0x0000;
                   16671:                REG8(BL) = 0x93; // HMA is not allocated
                   16672:        } else {
                   16673:                REG16(AX) = 0x0001;
                   16674:                is_hma_used_by_xms = false;
                   16675:                // restore first free mcb in high memory area
                   16676:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   16677: #else
                   16678:        } else {
                   16679:                REG16(AX) = 0x0000;
                   16680:                REG8(BL) = 0x91; // HMA is already in use
                   16681: #endif
                   16682:        }
1.1.1.19  root     16683: }
                   16684: 
                   16685: inline void msdos_call_xms_03h()
                   16686: {
                   16687:        i386_set_a20_line(1);
                   16688:        REG16(AX) = 0x0001;
                   16689:        REG8(BL) = 0x00;
                   16690: }
                   16691: 
                   16692: inline void msdos_call_xms_04h()
                   16693: {
1.1.1.21  root     16694:        i386_set_a20_line(0);
                   16695:        REG16(AX) = 0x0001;
                   16696:        REG8(BL) = 0x00;
1.1.1.19  root     16697: }
                   16698: 
                   16699: inline void msdos_call_xms_05h()
                   16700: {
                   16701:        i386_set_a20_line(1);
                   16702:        REG16(AX) = 0x0001;
                   16703:        REG8(BL) = 0x00;
1.1.1.21  root     16704:        xms_a20_local_enb_count++;
1.1.1.19  root     16705: }
                   16706: 
                   16707: void msdos_call_xms_06h()
                   16708: {
1.1.1.21  root     16709:        if(xms_a20_local_enb_count > 0) {
1.1.1.45  root     16710:                if(--xms_a20_local_enb_count == 0) {
                   16711:                        i386_set_a20_line(0);
                   16712:                        REG16(AX) = 0x0001;
                   16713:                        REG8(BL) = 0x00;
                   16714:                } else {
                   16715:                        REG16(AX) = 0x0000;
                   16716:                        REG8(BL) = 0x94;
                   16717:                }
1.1.1.21  root     16718:        } else {
1.1.1.45  root     16719:                i386_set_a20_line(0);
1.1.1.21  root     16720:                REG16(AX) = 0x0001;
                   16721:                REG8(BL) = 0x00;
1.1.1.19  root     16722:        }
                   16723: }
                   16724: 
                   16725: inline void msdos_call_xms_07h()
                   16726: {
                   16727:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   16728:        REG8(BL) = 0x00;
                   16729: }
                   16730: 
                   16731: inline void msdos_call_xms_08h()
                   16732: {
1.1.1.45  root     16733:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     16734:        
1.1.1.30  root     16735:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16736:                if(emb_handle->handle == 0) {
1.1.1.45  root     16737:                        if(eax < emb_handle->size_kb) {
                   16738:                                eax = emb_handle->size_kb;
1.1.1.19  root     16739:                        }
1.1.1.45  root     16740:                        edx += emb_handle->size_kb;
1.1.1.19  root     16741:                }
                   16742:        }
1.1.1.45  root     16743:        if(eax > 65535) {
                   16744:                eax = 65535;
                   16745:        }
                   16746:        if(edx > 65535) {
                   16747:                edx = 65535;
                   16748:        }
                   16749:        if(eax == 0 && edx == 0) {
1.1.1.19  root     16750:                REG8(BL) = 0xa0;
                   16751:        } else {
                   16752:                REG8(BL) = 0x00;
                   16753:        }
1.1.1.45  root     16754: #if defined(HAS_I386)
                   16755:        REG32(EAX) = eax;
                   16756:        REG32(EDX) = edx;
                   16757: #else
                   16758:        REG16(AX) = (UINT16)eax;
                   16759:        REG16(DX) = (UINT16)edx;
                   16760: #endif
1.1.1.29  root     16761:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     16762: }
                   16763: 
1.1.1.30  root     16764: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     16765: {
1.1.1.30  root     16766:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   16767:        
                   16768:        if(emb_handle != NULL) {
                   16769:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16770:                
                   16771:                REG16(AX) = 0x0001;
                   16772:                REG16(DX) = emb_handle->handle;
                   16773:                REG8(BL) = 0x00;
                   16774:        } else {
                   16775:                REG16(AX) = REG16(DX) = 0x0000;
                   16776:                REG8(BL) = 0xa0;
1.1.1.19  root     16777:        }
1.1.1.30  root     16778: }
                   16779: 
                   16780: inline void msdos_call_xms_09h()
                   16781: {
                   16782:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     16783: }
                   16784: 
                   16785: inline void msdos_call_xms_0ah()
                   16786: {
1.1.1.30  root     16787:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16788:        
                   16789:        if(emb_handle == NULL) {
1.1.1.19  root     16790:                REG16(AX) = 0x0000;
                   16791:                REG8(BL) = 0xa2;
1.1.1.45  root     16792: //     } else if(emb_handle->lock > 0) {
                   16793: //             REG16(AX) = 0x0000;
                   16794: //             REG8(BL) = 0xab;
1.1.1.19  root     16795:        } else {
1.1.1.30  root     16796:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     16797:                
                   16798:                REG16(AX) = 0x0001;
                   16799:                REG8(BL) = 0x00;
                   16800:        }
                   16801: }
                   16802: 
                   16803: inline void msdos_call_xms_0bh()
                   16804: {
                   16805:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   16806:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   16807:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   16808:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   16809:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   16810:        
                   16811:        UINT8 *src_buffer, *dest_buffer;
                   16812:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     16813:        emb_handle_t *emb_handle;
1.1.1.19  root     16814:        
                   16815:        if(src_handle == 0) {
                   16816:                src_buffer = mem;
                   16817:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   16818:                src_addr_max = MAX_MEM;
                   16819:        } else {
1.1.1.30  root     16820:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     16821:                        REG16(AX) = 0x0000;
                   16822:                        REG8(BL) = 0xa3;
                   16823:                        return;
                   16824:                }
1.1.1.30  root     16825:                src_buffer = mem + emb_handle->address;
                   16826:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16827:        }
                   16828:        if(dest_handle == 0) {
                   16829:                dest_buffer = mem;
                   16830:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   16831:                dest_addr_max = MAX_MEM;
                   16832:        } else {
1.1.1.30  root     16833:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     16834:                        REG16(AX) = 0x0000;
                   16835:                        REG8(BL) = 0xa5;
                   16836:                        return;
                   16837:                }
1.1.1.30  root     16838:                dest_buffer = mem + emb_handle->address;
                   16839:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16840:        }
                   16841:        for(int i = 0; i < copy_length; i++) {
                   16842:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   16843:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   16844:                } else {
                   16845:                        break;
                   16846:                }
                   16847:        }
                   16848:        REG16(AX) = 0x0001;
                   16849:        REG8(BL) = 0x00;
                   16850: }
                   16851: 
                   16852: inline void msdos_call_xms_0ch()
                   16853: {
1.1.1.30  root     16854:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16855:        
                   16856:        if(emb_handle == NULL) {
1.1.1.19  root     16857:                REG16(AX) = 0x0000;
                   16858:                REG8(BL) = 0xa2;
                   16859:        } else {
1.1.1.45  root     16860:                if(emb_handle->lock < 255) {
                   16861:                        emb_handle->lock++;
                   16862:                }
1.1.1.19  root     16863:                REG16(AX) = 0x0001;
1.1.1.30  root     16864:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   16865:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     16866:        }
                   16867: }
                   16868: 
                   16869: inline void msdos_call_xms_0dh()
                   16870: {
1.1.1.30  root     16871:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16872:        
                   16873:        if(emb_handle == NULL) {
1.1.1.19  root     16874:                REG16(AX) = 0x0000;
                   16875:                REG8(BL) = 0xa2;
1.1.1.30  root     16876:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     16877:                REG16(AX) = 0x0000;
                   16878:                REG8(BL) = 0xaa;
                   16879:        } else {
1.1.1.30  root     16880:                emb_handle->lock--;
1.1.1.19  root     16881:                REG16(AX) = 0x0001;
                   16882:                REG8(BL) = 0x00;
                   16883:        }
                   16884: }
                   16885: 
                   16886: inline void msdos_call_xms_0eh()
                   16887: {
1.1.1.30  root     16888:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16889:        
                   16890:        if(emb_handle == NULL) {
1.1.1.19  root     16891:                REG16(AX) = 0x0000;
                   16892:                REG8(BL) = 0xa2;
                   16893:        } else {
                   16894:                REG16(AX) = 0x0001;
1.1.1.30  root     16895:                REG8(BH) = emb_handle->lock;
                   16896:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   16897:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     16898:        }
                   16899: }
                   16900: 
1.1.1.30  root     16901: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     16902: {
1.1.1.30  root     16903:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16904:        
                   16905:        if(emb_handle == NULL) {
1.1.1.19  root     16906:                REG16(AX) = 0x0000;
                   16907:                REG8(BL) = 0xa2;
1.1.1.30  root     16908:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     16909:                REG16(AX) = 0x0000;
                   16910:                REG8(BL) = 0xab;
                   16911:        } else {
1.1.1.30  root     16912:                if(emb_handle->size_kb < size_kb) {
                   16913:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   16914:                                msdos_xms_combine_emb_handles(emb_handle);
                   16915:                                if(emb_handle->size_kb > size_kb) {
                   16916:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16917:                                }
                   16918:                        } else {
                   16919:                                int old_handle = emb_handle->handle;
                   16920:                                int old_size_kb = emb_handle->size_kb;
                   16921:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   16922:                                
                   16923:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   16924:                                msdos_xms_free_emb_handle(emb_handle);
                   16925:                                
                   16926:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   16927:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   16928:                                }
                   16929:                                emb_handle->handle = old_handle;
                   16930:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   16931:                                free(buffer);
                   16932:                        }
                   16933:                } else if(emb_handle->size_kb > size_kb) {
                   16934:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16935:                }
                   16936:                if(emb_handle->size_kb != size_kb) {
                   16937:                        REG16(AX) = 0x0000;
                   16938:                        REG8(BL) = 0xa0;
                   16939:                } else {
                   16940:                        REG16(AX) = 0x0001;
                   16941:                        REG8(BL) = 0x00;
                   16942:                }
1.1.1.19  root     16943:        }
                   16944: }
                   16945: 
1.1.1.30  root     16946: inline void msdos_call_xms_0fh()
                   16947: {
                   16948:        msdos_call_xms_0fh(REG16(BX));
                   16949: }
                   16950: 
1.1.1.19  root     16951: inline void msdos_call_xms_10h()
                   16952: {
                   16953:        int seg;
                   16954:        
                   16955:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   16956:                REG16(AX) = 0x0001;
                   16957:                REG16(BX) = seg;
                   16958:        } else {
                   16959:                REG16(AX) = 0x0000;
                   16960:                REG8(BL) = 0xb0;
                   16961:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   16962:        }
                   16963: }
                   16964: 
                   16965: inline void msdos_call_xms_11h()
                   16966: {
                   16967:        int mcb_seg = REG16(DX) - 1;
                   16968:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16969:        
                   16970:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16971:                msdos_mem_free(REG16(DX));
                   16972:                REG16(AX) = 0x0001;
                   16973:                REG8(BL) = 0x00;
                   16974:        } else {
                   16975:                REG16(AX) = 0x0000;
                   16976:                REG8(BL) = 0xb2;
                   16977:        }
                   16978: }
                   16979: 
                   16980: inline void msdos_call_xms_12h()
                   16981: {
                   16982:        int mcb_seg = REG16(DX) - 1;
                   16983:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16984:        int max_paragraphs;
                   16985:        
                   16986:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16987:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   16988:                        REG16(AX) = 0x0001;
                   16989:                        REG8(BL) = 0x00;
                   16990:                } else {
                   16991:                        REG16(AX) = 0x0000;
                   16992:                        REG8(BL) = 0xb0;
                   16993:                        REG16(DX) = max_paragraphs;
                   16994:                }
                   16995:        } else {
                   16996:                REG16(AX) = 0x0000;
                   16997:                REG8(BL) = 0xb2;
                   16998:        }
                   16999: }
                   17000: 
1.1.1.29  root     17001: #if defined(HAS_I386)
                   17002: 
                   17003: inline void msdos_call_xms_88h()
                   17004: {
                   17005:        REG32(EAX) = REG32(EDX) = 0x0000;
                   17006:        
1.1.1.30  root     17007:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   17008:                if(emb_handle->handle == 0) {
                   17009:                        if(REG32(EAX) < emb_handle->size_kb) {
                   17010:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     17011:                        }
1.1.1.30  root     17012:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     17013:                }
                   17014:        }
                   17015:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   17016:                REG8(BL) = 0xa0;
                   17017:        } else {
                   17018:                REG8(BL) = 0x00;
                   17019:        }
                   17020:        REG32(ECX) = EMB_END - 1;
                   17021: }
                   17022: 
                   17023: inline void msdos_call_xms_89h()
                   17024: {
1.1.1.30  root     17025:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     17026: }
                   17027: 
                   17028: inline void msdos_call_xms_8eh()
                   17029: {
1.1.1.30  root     17030:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   17031:        
                   17032:        if(emb_handle == NULL) {
1.1.1.29  root     17033:                REG16(AX) = 0x0000;
                   17034:                REG8(BL) = 0xa2;
                   17035:        } else {
                   17036:                REG16(AX) = 0x0001;
1.1.1.30  root     17037:                REG8(BH) = emb_handle->lock;
                   17038:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   17039:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     17040:        }
                   17041: }
                   17042: 
                   17043: inline void msdos_call_xms_8fh()
                   17044: {
1.1.1.30  root     17045:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     17046: }
                   17047: 
                   17048: #endif
1.1.1.19  root     17049: #endif
                   17050: 
1.1.1.26  root     17051: UINT16 msdos_get_equipment()
                   17052: {
                   17053:        static UINT16 equip = 0;
                   17054:        
                   17055:        if(equip == 0) {
                   17056: #ifdef SUPPORT_FPU
                   17057:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   17058: #endif
                   17059:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   17060:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   17061: //             equip |= (1 << 8);      // 0 if DMA installed
                   17062:                equip |= (2 << 9);      // number of serial ports
                   17063:                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     17064:                
                   17065:                // check only A: and B: if it is floppy drive
                   17066:                int n = 0;
                   17067:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     17068:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   17069:                                n++;
1.1.1.28  root     17070:                        }
                   17071:                }
                   17072:                if(n != 0) {
                   17073:                        equip |= (1 << 0);      // floppy disk(s) installed
                   17074:                        n--;
                   17075:                        equip |= (n << 6);      // number of floppies installed less 1
                   17076:                }
                   17077: //             if(joyGetNumDevs() != 0) {
                   17078: //                     equip |= (1 << 12);     // game port installed
                   17079: //             }
1.1.1.26  root     17080:        }
                   17081:        return(equip);
                   17082: }
                   17083: 
1.1       root     17084: void msdos_syscall(unsigned num)
                   17085: {
1.1.1.22  root     17086: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     17087:        if(num == 0x08 || num == 0x1c) {
                   17088:                // don't log the timer interrupts
1.1.1.45  root     17089: //             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     17090:        } else if(num == 0x30) {
                   17091:                // dummy interrupt for call 0005h (call near)
                   17092:                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     17093:        } else if(num == 0x65) {
1.1.1.22  root     17094:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     17095:                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     17096:        } else if(num == 0x66) {
1.1.1.22  root     17097:                // dummy interrupt for XMS (call far)
1.1.1.33  root     17098:                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     17099:        } else if(num >= 0x68 && num <= 0x6f) {
1.1.1.45  root     17100:                // dummy interrupt
1.1.1.22  root     17101:        } else {
1.1.1.33  root     17102:                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     17103:        }
                   17104: #endif
1.1.1.36  root     17105:        // update cursor position
                   17106:        if(cursor_moved) {
                   17107:                pcbios_update_cursor_position();
                   17108:                cursor_moved = false;
                   17109:        }
1.1.1.50  root     17110: #ifdef USE_SERVICE_THREAD
                   17111:        // this is called from dummy loop to wait until a serive that waits input is done
                   17112:        if(!in_service)
                   17113: #endif
1.1.1.33  root     17114:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     17115:        
1.1       root     17116:        switch(num) {
                   17117:        case 0x00:
1.1.1.28  root     17118:                try {
                   17119:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17120:                        error("division by zero\n");
                   17121:                } catch(...) {
                   17122:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   17123:                }
1.1       root     17124:                break;
                   17125:        case 0x04:
1.1.1.28  root     17126:                try {
                   17127:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17128:                        error("overflow\n");
                   17129:                } catch(...) {
                   17130:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   17131:                }
1.1       root     17132:                break;
                   17133:        case 0x06:
                   17134:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     17135:                if(!ignore_illegal_insn) {
1.1.1.28  root     17136:                        try {
                   17137:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17138:                                error("illegal instruction\n");
                   17139:                        } catch(...) {
                   17140:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   17141:                        }
1.1.1.14  root     17142:                } else {
                   17143: #if defined(HAS_I386)
1.1.1.39  root     17144:                        m_eip = m_int6h_skip_eip;
                   17145: #elif defined(HAS_I286)
                   17146:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     17147: #else
1.1.1.39  root     17148:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     17149: #endif
                   17150:                }
1.1       root     17151:                break;
1.1.1.33  root     17152:        case 0x09:
                   17153:                // ctrl-break is pressed
                   17154:                if(raise_int_1bh) {
                   17155: #if defined(HAS_I386)
                   17156:                        m_ext = 0; // not an external interrupt
                   17157:                        i386_trap(0x1b, 1, 0);
                   17158:                        m_ext = 1;
                   17159: #else
                   17160:                        PREFIX86(_interrupt)(0x1b);
                   17161: #endif
                   17162:                        raise_int_1bh = false;
                   17163:                }
1.1.1.8   root     17164:        case 0x08:
1.1.1.14  root     17165: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     17166:        case 0x0b:
                   17167:        case 0x0c:
                   17168:        case 0x0d:
                   17169:        case 0x0e:
                   17170:        case 0x0f:
                   17171:                // EOI
                   17172:                pic[0].isr &= ~(1 << (num - 0x08));
                   17173:                pic_update();
                   17174:                break;
1.1       root     17175:        case 0x10:
                   17176:                // PC BIOS - Video
1.1.1.14  root     17177:                if(!restore_console_on_exit) {
1.1.1.15  root     17178:                        change_console_size(scr_width, scr_height);
1.1       root     17179:                }
1.1.1.3   root     17180:                m_CF = 0;
1.1       root     17181:                switch(REG8(AH)) {
1.1.1.16  root     17182:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     17183:                case 0x01: pcbios_int_10h_01h(); break;
                   17184:                case 0x02: pcbios_int_10h_02h(); break;
                   17185:                case 0x03: pcbios_int_10h_03h(); break;
                   17186:                case 0x05: pcbios_int_10h_05h(); break;
                   17187:                case 0x06: pcbios_int_10h_06h(); break;
                   17188:                case 0x07: pcbios_int_10h_07h(); break;
                   17189:                case 0x08: pcbios_int_10h_08h(); break;
                   17190:                case 0x09: pcbios_int_10h_09h(); break;
                   17191:                case 0x0a: pcbios_int_10h_0ah(); break;
                   17192:                case 0x0b: break;
1.1.1.40  root     17193:                case 0x0c: pcbios_int_10h_0ch(); break;
                   17194:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     17195:                case 0x0e: pcbios_int_10h_0eh(); break;
                   17196:                case 0x0f: pcbios_int_10h_0fh(); break;
                   17197:                case 0x10: break;
1.1.1.14  root     17198:                case 0x11: pcbios_int_10h_11h(); break;
                   17199:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     17200:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     17201:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     17202:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     17203:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   17204:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     17205:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     17206:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   17207:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     17208:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     17209:                case 0x6f: break;
1.1.1.22  root     17210:                case 0x80: m_CF = 1; break; // unknown
                   17211:                case 0x81: m_CF = 1; break; // unknown
1.1       root     17212:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     17213:                case 0x83: pcbios_int_10h_83h(); break;
                   17214:                case 0x8b: break;
                   17215:                case 0x8c: m_CF = 1; break; // unknown
                   17216:                case 0x8d: m_CF = 1; break; // unknown
                   17217:                case 0x8e: m_CF = 1; break; // unknown
                   17218:                case 0x90: pcbios_int_10h_90h(); break;
                   17219:                case 0x91: pcbios_int_10h_91h(); break;
                   17220:                case 0x92: break;
                   17221:                case 0x93: break;
                   17222:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     17223:                case 0xfa: break; // ega register interface library is not installed
1.1       root     17224:                case 0xfe: pcbios_int_10h_feh(); break;
                   17225:                case 0xff: pcbios_int_10h_ffh(); break;
                   17226:                default:
1.1.1.22  root     17227:                        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));
                   17228:                        m_CF = 1;
1.1       root     17229:                        break;
                   17230:                }
                   17231:                break;
                   17232:        case 0x11:
                   17233:                // PC BIOS - Get Equipment List
1.1.1.26  root     17234:                REG16(AX) = msdos_get_equipment();
1.1       root     17235:                break;
                   17236:        case 0x12:
                   17237:                // PC BIOS - Get Memory Size
1.1.1.33  root     17238:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     17239:                break;
                   17240:        case 0x13:
1.1.1.42  root     17241:                // PC BIOS - Disk I/O
                   17242:                {
                   17243:                        static UINT8 last = 0x00;
                   17244:                        switch(REG8(AH)) {
                   17245:                        case 0x00: pcbios_int_13h_00h(); break;
                   17246:                        case 0x01: // get last status
                   17247:                                REG8(AH) = last;
                   17248:                                break;
                   17249:                        case 0x02: pcbios_int_13h_02h(); break;
                   17250:                        case 0x03: pcbios_int_13h_03h(); break;
                   17251:                        case 0x04: pcbios_int_13h_04h(); break;
                   17252:                        case 0x08: pcbios_int_13h_08h(); break;
                   17253:                        case 0x0a: pcbios_int_13h_02h(); break;
                   17254:                        case 0x0b: pcbios_int_13h_03h(); break;
                   17255:                        case 0x0d: pcbios_int_13h_00h(); break;
                   17256:                        case 0x10: pcbios_int_13h_10h(); break;
                   17257:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     17258:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     17259:                        case 0x05: // format
                   17260:                        case 0x06:
                   17261:                        case 0x07:
                   17262:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   17263:                                m_CF = 1;
                   17264:                                break;
                   17265:                        case 0x09:
                   17266:                        case 0x0c: // seek
                   17267:                        case 0x11: // recalib
                   17268:                        case 0x14:
                   17269:                        case 0x17:
                   17270:                                REG8(AH) = 0x00; // successful completion
                   17271:                                break;
1.1.1.43  root     17272:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   17273:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   17274:                                REG8(AH) = 0x01; // invalid function
                   17275:                                m_CF = 1;
                   17276:                                break;
1.1.1.42  root     17277:                        default:
                   17278:                                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));
                   17279:                                REG8(AH) = 0x01; // invalid function
                   17280:                                m_CF = 1;
                   17281:                                break;
                   17282:                        }
                   17283:                        last = REG8(AH);
                   17284:                }
1.1       root     17285:                break;
                   17286:        case 0x14:
                   17287:                // PC BIOS - Serial I/O
1.1.1.25  root     17288:                switch(REG8(AH)) {
                   17289:                case 0x00: pcbios_int_14h_00h(); break;
                   17290:                case 0x01: pcbios_int_14h_01h(); break;
                   17291:                case 0x02: pcbios_int_14h_02h(); break;
                   17292:                case 0x03: pcbios_int_14h_03h(); break;
                   17293:                case 0x04: pcbios_int_14h_04h(); break;
                   17294:                case 0x05: pcbios_int_14h_05h(); break;
                   17295:                default:
                   17296:                        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));
                   17297:                        break;
                   17298:                }
1.1       root     17299:                break;
                   17300:        case 0x15:
                   17301:                // PC BIOS
1.1.1.3   root     17302:                m_CF = 0;
1.1       root     17303:                switch(REG8(AH)) {
                   17304:                case 0x23: pcbios_int_15h_23h(); break;
                   17305:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     17306:                case 0x41: break;
1.1       root     17307:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.64  root     17308:                case 0x4f: m_CF = 1; break; // from DOSBox
1.1.1.22  root     17309:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     17310:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     17311:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     17312:                case 0x86: pcbios_int_15h_86h(); break;
                   17313:                case 0x87: pcbios_int_15h_87h(); break;
                   17314:                case 0x88: pcbios_int_15h_88h(); break;
                   17315:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     17316:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.64  root     17317:                case 0x90: REG8(AH) = 0x00; break; // from DOSBox
                   17318:                case 0x91: REG8(AH) = 0x00; break; // from DOSBox
1.1.1.22  root     17319:                case 0xc0: // PS/2 ???
1.1.1.54  root     17320: #ifndef EXT_BIOS_TOP
1.1.1.22  root     17321:                case 0xc1:
1.1.1.54  root     17322: #endif
1.1.1.30  root     17323:                case 0xc3: // PS50+ ???
                   17324:                case 0xc4:
1.1.1.22  root     17325:                        REG8(AH) = 0x86;
                   17326:                        m_CF = 1;
                   17327:                        break;
1.1.1.54  root     17328: #ifdef EXT_BIOS_TOP
                   17329:                case 0xc1: pcbios_int_15h_c1h(); break;
                   17330: #endif
                   17331:                case 0xc2: pcbios_int_15h_c2h(); break;
1.1.1.3   root     17332: #if defined(HAS_I386)
1.1       root     17333:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     17334: #endif
1.1       root     17335:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     17336:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     17337:                default:
1.1.1.22  root     17338:                        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));
                   17339:                        REG8(AH) = 0x86;
1.1.1.3   root     17340:                        m_CF = 1;
1.1       root     17341:                        break;
                   17342:                }
                   17343:                break;
                   17344:        case 0x16:
                   17345:                // PC BIOS - Keyboard
1.1.1.3   root     17346:                m_CF = 0;
1.1       root     17347:                switch(REG8(AH)) {
                   17348:                case 0x00: pcbios_int_16h_00h(); break;
                   17349:                case 0x01: pcbios_int_16h_01h(); break;
                   17350:                case 0x02: pcbios_int_16h_02h(); break;
                   17351:                case 0x03: pcbios_int_16h_03h(); break;
                   17352:                case 0x05: pcbios_int_16h_05h(); break;
1.1.1.60  root     17353:                case 0x09: pcbios_int_16h_09h(); break;
                   17354:                case 0x0a: pcbios_int_16h_0ah(); break;
1.1       root     17355:                case 0x10: pcbios_int_16h_00h(); break;
1.1.1.60  root     17356:                case 0x11: pcbios_int_16h_11h(); break;
1.1       root     17357:                case 0x12: pcbios_int_16h_12h(); break;
                   17358:                case 0x13: pcbios_int_16h_13h(); break;
                   17359:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     17360:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     17361:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     17362:                case 0xda: break; // unknown
1.1.1.43  root     17363:                case 0xdb: break; // unknown
1.1.1.22  root     17364:                case 0xff: break; // unknown
1.1       root     17365:                default:
1.1.1.22  root     17366:                        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     17367:                        break;
                   17368:                }
                   17369:                break;
                   17370:        case 0x17:
                   17371:                // PC BIOS - Printer
1.1.1.37  root     17372:                m_CF = 0;
                   17373:                switch(REG8(AH)) {
                   17374:                case 0x00: pcbios_int_17h_00h(); break;
                   17375:                case 0x01: pcbios_int_17h_01h(); break;
                   17376:                case 0x02: pcbios_int_17h_02h(); break;
                   17377:                case 0x03: pcbios_int_17h_03h(); break;
                   17378:                case 0x50: pcbios_int_17h_50h(); break;
                   17379:                case 0x51: pcbios_int_17h_51h(); break;
                   17380:                case 0x52: pcbios_int_17h_52h(); break;
                   17381:                case 0x84: pcbios_int_17h_84h(); break;
                   17382:                case 0x85: pcbios_int_17h_85h(); break;
                   17383:                default:
                   17384:                        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));
                   17385:                        break;
                   17386:                }
1.1       root     17387:                break;
                   17388:        case 0x1a:
                   17389:                // PC BIOS - Timer
1.1.1.3   root     17390:                m_CF = 0;
1.1       root     17391:                switch(REG8(AH)) {
                   17392:                case 0x00: pcbios_int_1ah_00h(); break;
                   17393:                case 0x01: break;
                   17394:                case 0x02: pcbios_int_1ah_02h(); break;
                   17395:                case 0x03: break;
                   17396:                case 0x04: pcbios_int_1ah_04h(); break;
                   17397:                case 0x05: break;
                   17398:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   17399:                case 0x0b: break;
1.1.1.14  root     17400:                case 0x35: break; // Word Perfect Third Party Interface?
                   17401:                case 0x36: break; // Word Perfect Third Party Interface
                   17402:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     17403:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     17404:                case 0xb1: break; // PCI BIOS v2.0c+
                   17405:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     17406:                default:
1.1.1.22  root     17407:                        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     17408:                        break;
                   17409:                }
                   17410:                break;
1.1.1.33  root     17411:        case 0x1b:
                   17412:                mem[0x471] = 0x00;
                   17413:                break;
1.1       root     17414:        case 0x20:
1.1.1.28  root     17415:                try {
                   17416:                        msdos_process_terminate(SREG(CS), retval, 1);
                   17417:                } catch(...) {
                   17418:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   17419:                }
1.1       root     17420:                break;
1.1.1.49  root     17421:        case 0x30:
1.1.1.46  root     17422:                // dummy interrupt for case map routine pointed in the country info
                   17423: //             if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
                   17424: //                     REG8(AL) = 0x00;
                   17425: //                     break;
                   17426: //             }
1.1       root     17427:        case 0x21:
                   17428:                // MS-DOS System Call
1.1.1.3   root     17429:                m_CF = 0;
1.1.1.28  root     17430:                try {
1.1.1.46  root     17431:                        switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28  root     17432:                        case 0x00: msdos_int_21h_00h(); break;
                   17433:                        case 0x01: msdos_int_21h_01h(); break;
                   17434:                        case 0x02: msdos_int_21h_02h(); break;
                   17435:                        case 0x03: msdos_int_21h_03h(); break;
                   17436:                        case 0x04: msdos_int_21h_04h(); break;
                   17437:                        case 0x05: msdos_int_21h_05h(); break;
                   17438:                        case 0x06: msdos_int_21h_06h(); break;
                   17439:                        case 0x07: msdos_int_21h_07h(); break;
                   17440:                        case 0x08: msdos_int_21h_08h(); break;
                   17441:                        case 0x09: msdos_int_21h_09h(); break;
                   17442:                        case 0x0a: msdos_int_21h_0ah(); break;
                   17443:                        case 0x0b: msdos_int_21h_0bh(); break;
                   17444:                        case 0x0c: msdos_int_21h_0ch(); break;
                   17445:                        case 0x0d: msdos_int_21h_0dh(); break;
                   17446:                        case 0x0e: msdos_int_21h_0eh(); break;
                   17447:                        case 0x0f: msdos_int_21h_0fh(); break;
                   17448:                        case 0x10: msdos_int_21h_10h(); break;
                   17449:                        case 0x11: msdos_int_21h_11h(); break;
                   17450:                        case 0x12: msdos_int_21h_12h(); break;
                   17451:                        case 0x13: msdos_int_21h_13h(); break;
                   17452:                        case 0x14: msdos_int_21h_14h(); break;
                   17453:                        case 0x15: msdos_int_21h_15h(); break;
                   17454:                        case 0x16: msdos_int_21h_16h(); break;
                   17455:                        case 0x17: msdos_int_21h_17h(); break;
                   17456:                        case 0x18: msdos_int_21h_18h(); break;
                   17457:                        case 0x19: msdos_int_21h_19h(); break;
                   17458:                        case 0x1a: msdos_int_21h_1ah(); break;
                   17459:                        case 0x1b: msdos_int_21h_1bh(); break;
                   17460:                        case 0x1c: msdos_int_21h_1ch(); break;
                   17461:                        case 0x1d: msdos_int_21h_1dh(); break;
                   17462:                        case 0x1e: msdos_int_21h_1eh(); break;
                   17463:                        case 0x1f: msdos_int_21h_1fh(); break;
                   17464:                        case 0x20: msdos_int_21h_20h(); break;
                   17465:                        case 0x21: msdos_int_21h_21h(); break;
                   17466:                        case 0x22: msdos_int_21h_22h(); break;
                   17467:                        case 0x23: msdos_int_21h_23h(); break;
                   17468:                        case 0x24: msdos_int_21h_24h(); break;
                   17469:                        case 0x25: msdos_int_21h_25h(); break;
                   17470:                        case 0x26: msdos_int_21h_26h(); break;
                   17471:                        case 0x27: msdos_int_21h_27h(); break;
                   17472:                        case 0x28: msdos_int_21h_28h(); break;
                   17473:                        case 0x29: msdos_int_21h_29h(); break;
                   17474:                        case 0x2a: msdos_int_21h_2ah(); break;
                   17475:                        case 0x2b: msdos_int_21h_2bh(); break;
                   17476:                        case 0x2c: msdos_int_21h_2ch(); break;
                   17477:                        case 0x2d: msdos_int_21h_2dh(); break;
                   17478:                        case 0x2e: msdos_int_21h_2eh(); break;
                   17479:                        case 0x2f: msdos_int_21h_2fh(); break;
                   17480:                        case 0x30: msdos_int_21h_30h(); break;
                   17481:                        case 0x31: msdos_int_21h_31h(); break;
                   17482:                        case 0x32: msdos_int_21h_32h(); break;
                   17483:                        case 0x33: msdos_int_21h_33h(); break;
                   17484:                        case 0x34: msdos_int_21h_34h(); break;
                   17485:                        case 0x35: msdos_int_21h_35h(); break;
                   17486:                        case 0x36: msdos_int_21h_36h(); break;
                   17487:                        case 0x37: msdos_int_21h_37h(); break;
                   17488:                        case 0x38: msdos_int_21h_38h(); break;
                   17489:                        case 0x39: msdos_int_21h_39h(0); break;
                   17490:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   17491:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   17492:                        case 0x3c: msdos_int_21h_3ch(); break;
                   17493:                        case 0x3d: msdos_int_21h_3dh(); break;
                   17494:                        case 0x3e: msdos_int_21h_3eh(); break;
                   17495:                        case 0x3f: msdos_int_21h_3fh(); break;
                   17496:                        case 0x40: msdos_int_21h_40h(); break;
                   17497:                        case 0x41: msdos_int_21h_41h(0); break;
                   17498:                        case 0x42: msdos_int_21h_42h(); break;
                   17499:                        case 0x43: msdos_int_21h_43h(0); break;
                   17500:                        case 0x44: msdos_int_21h_44h(); break;
                   17501:                        case 0x45: msdos_int_21h_45h(); break;
                   17502:                        case 0x46: msdos_int_21h_46h(); break;
                   17503:                        case 0x47: msdos_int_21h_47h(0); break;
                   17504:                        case 0x48: msdos_int_21h_48h(); break;
                   17505:                        case 0x49: msdos_int_21h_49h(); break;
                   17506:                        case 0x4a: msdos_int_21h_4ah(); break;
                   17507:                        case 0x4b: msdos_int_21h_4bh(); break;
                   17508:                        case 0x4c: msdos_int_21h_4ch(); break;
                   17509:                        case 0x4d: msdos_int_21h_4dh(); break;
                   17510:                        case 0x4e: msdos_int_21h_4eh(); break;
                   17511:                        case 0x4f: msdos_int_21h_4fh(); break;
                   17512:                        case 0x50: msdos_int_21h_50h(); break;
                   17513:                        case 0x51: msdos_int_21h_51h(); break;
                   17514:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     17515:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     17516:                        case 0x54: msdos_int_21h_54h(); break;
                   17517:                        case 0x55: msdos_int_21h_55h(); break;
                   17518:                        case 0x56: msdos_int_21h_56h(0); break;
                   17519:                        case 0x57: msdos_int_21h_57h(); break;
                   17520:                        case 0x58: msdos_int_21h_58h(); break;
                   17521:                        case 0x59: msdos_int_21h_59h(); break;
                   17522:                        case 0x5a: msdos_int_21h_5ah(); break;
                   17523:                        case 0x5b: msdos_int_21h_5bh(); break;
                   17524:                        case 0x5c: msdos_int_21h_5ch(); break;
                   17525:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     17526:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     17527:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     17528:                        case 0x60: msdos_int_21h_60h(0); break;
                   17529:                        case 0x61: msdos_int_21h_61h(); break;
                   17530:                        case 0x62: msdos_int_21h_62h(); break;
                   17531:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     17532:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     17533:                        case 0x65: msdos_int_21h_65h(); break;
                   17534:                        case 0x66: msdos_int_21h_66h(); break;
                   17535:                        case 0x67: msdos_int_21h_67h(); break;
                   17536:                        case 0x68: msdos_int_21h_68h(); break;
                   17537:                        case 0x69: msdos_int_21h_69h(); break;
                   17538:                        case 0x6a: msdos_int_21h_6ah(); break;
                   17539:                        case 0x6b: msdos_int_21h_6bh(); break;
                   17540:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.48  root     17541:                        case 0x6d: // Find First ROM Program
                   17542:                        case 0x6e: // Find Next ROM Program
                   17543:                        case 0x6f: // Get/Set ROM Scan Start Address
                   17544:                                REG8(AL) = 0x00; // if not supported (DOS <5, MS-DOS 5+ non-ROM versions)
                   17545:                                break;
1.1.1.43  root     17546:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.48  root     17547:                        case 0x71: // Windows95 - Long Filename Functions
1.1.1.28  root     17548:                                switch(REG8(AL)) {
                   17549:                                case 0x0d: msdos_int_21h_710dh(); break;
                   17550:                                case 0x39: msdos_int_21h_39h(1); break;
                   17551:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   17552:                                case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.48  root     17553:                                case 0x41: msdos_int_21h_7141h(); break;
1.1.1.28  root     17554:                                case 0x43: msdos_int_21h_43h(1); break;
                   17555:                                case 0x47: msdos_int_21h_47h(1); break;
                   17556:                                case 0x4e: msdos_int_21h_714eh(); break;
                   17557:                                case 0x4f: msdos_int_21h_714fh(); break;
                   17558:                                case 0x56: msdos_int_21h_56h(1); break;
                   17559:                                case 0x60: msdos_int_21h_60h(1); break;
                   17560:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   17561:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   17562:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   17563:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   17564:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   17565:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45  root     17566:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     17567:                                case 0xaa: msdos_int_21h_71aah(); break;
                   17568:                                default:
                   17569:                                        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));
                   17570:                                        REG16(AX) = 0x7100;
                   17571:                                        m_CF = 1;
                   17572:                                        break;
                   17573:                                }
                   17574:                                break;
1.1.1.48  root     17575:                        case 0x72: // Windows95 beta - LFN FindClose
                   17576: //                             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));
                   17577:                                REG16(AX) = 0x7200;
                   17578:                                m_CF = 1;
                   17579:                                break;
                   17580:                        case 0x73: // Windows95 - FAT32 Functions
1.1.1.28  root     17581:                                switch(REG8(AL)) {
                   17582:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     17583:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     17584:                                case 0x02: msdos_int_21h_7302h(); break;
                   17585:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     17586:                                // 0x04: Set DPB to Use for Formatting
                   17587:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     17588:                                default:
                   17589:                                        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));
                   17590:                                        REG16(AX) = 0x7300;
                   17591:                                        m_CF = 1;
                   17592:                                        break;
                   17593:                                }
1.1       root     17594:                                break;
1.1.1.30  root     17595:                        case 0xdb: msdos_int_21h_dbh(); break;
                   17596:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     17597:                        default:
1.1.1.22  root     17598:                                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     17599:                                REG16(AX) = 0x01;
1.1.1.3   root     17600:                                m_CF = 1;
1.1       root     17601:                                break;
                   17602:                        }
1.1.1.28  root     17603:                } catch(int error) {
                   17604:                        REG16(AX) = error;
                   17605:                        m_CF = 1;
                   17606:                } catch(...) {
                   17607:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     17608:                        m_CF = 1;
1.1       root     17609:                }
1.1.1.3   root     17610:                if(m_CF) {
1.1.1.23  root     17611:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     17612:                        sda->int21h_5d0ah_called = 0;
1.1.1.23  root     17613:                        sda->extended_error_code = REG16(AX);
                   17614:                        switch(sda->extended_error_code) {
                   17615:                        case  4: // Too many open files
                   17616:                        case  8: // Insufficient memory
                   17617:                                sda->error_class = 1; // Out of resource
                   17618:                                break;
                   17619:                        case  5: // Access denied
                   17620:                                sda->error_class = 3; // Authorization
                   17621:                                break;
                   17622:                        case  7: // Memory control block destroyed
                   17623:                                sda->error_class = 4; // Internal
                   17624:                                break;
                   17625:                        case  2: // File not found
                   17626:                        case  3: // Path not found
                   17627:                        case 15: // Invaid drive specified
                   17628:                        case 18: // No more files
                   17629:                                sda->error_class = 8; // Not found
                   17630:                                break;
                   17631:                        case 32: // Sharing violation
                   17632:                        case 33: // Lock violation
                   17633:                                sda->error_class = 10; // Locked
                   17634:                                break;
                   17635: //                     case 16: // Removal of current directory attempted
                   17636:                        case 19: // Attempted write on protected disk
                   17637:                        case 21: // Drive not ready
                   17638: //                     case 29: // Write failure
                   17639: //                     case 30: // Read failure
                   17640: //                     case 82: // Cannot create subdirectory
                   17641:                                sda->error_class = 11; // Media
                   17642:                                break;
                   17643:                        case 80: // File already exists
                   17644:                                sda->error_class = 12; // Already exist
                   17645:                                break;
                   17646:                        default:
                   17647:                                sda->error_class = 13; // Unknown
                   17648:                                break;
                   17649:                        }
                   17650:                        sda->suggested_action = 1; // Retry
                   17651:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     17652:                }
1.1.1.33  root     17653:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     17654:                        // raise int 23h
                   17655: #if defined(HAS_I386)
                   17656:                        m_ext = 0; // not an external interrupt
                   17657:                        i386_trap(0x23, 1, 0);
                   17658:                        m_ext = 1;
                   17659: #else
                   17660:                        PREFIX86(_interrupt)(0x23);
                   17661: #endif
                   17662:                }
1.1       root     17663:                break;
                   17664:        case 0x22:
                   17665:                fatalerror("int 22h (terminate address)\n");
                   17666:        case 0x23:
1.1.1.28  root     17667:                try {
                   17668:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   17669:                } catch(...) {
                   17670:                        fatalerror("failed to terminate the current process by int 23h\n");
                   17671:                }
1.1       root     17672:                break;
                   17673:        case 0x24:
1.1.1.32  root     17674: /*
1.1.1.28  root     17675:                try {
                   17676:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17677:                } catch(...) {
                   17678:                        fatalerror("failed to terminate the current process by int 24h\n");
                   17679:                }
1.1.1.32  root     17680: */
                   17681:                msdos_int_24h();
1.1       root     17682:                break;
                   17683:        case 0x25:
                   17684:                msdos_int_25h();
                   17685:                break;
                   17686:        case 0x26:
                   17687:                msdos_int_26h();
                   17688:                break;
                   17689:        case 0x27:
1.1.1.28  root     17690:                try {
                   17691:                        msdos_int_27h();
                   17692:                } catch(...) {
                   17693:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   17694:                }
1.1       root     17695:                break;
                   17696:        case 0x28:
                   17697:                Sleep(10);
1.1.1.35  root     17698:                REQUEST_HARDWRE_UPDATE();
1.1       root     17699:                break;
                   17700:        case 0x29:
                   17701:                msdos_int_29h();
                   17702:                break;
                   17703:        case 0x2e:
                   17704:                msdos_int_2eh();
                   17705:                break;
                   17706:        case 0x2f:
                   17707:                // multiplex interrupt
                   17708:                switch(REG8(AH)) {
1.1.1.22  root     17709:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     17710:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     17711:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     17712:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     17713:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     17714:                case 0x14: msdos_int_2fh_14h(); break;
                   17715:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     17716:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     17717:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     17718:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     17719:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     17720:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     17721:                case 0x46: msdos_int_2fh_46h(); break;
                   17722:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     17723:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     17724:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     17725:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     17726:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     17727:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     17728:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     17729:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     17730:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     17731:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     17732:                default:
1.1.1.30  root     17733:                        switch(REG8(AL)) {
                   17734:                        case 0x00:
                   17735:                                // This is not installed
                   17736: //                             REG8(AL) = 0x00;
                   17737:                                break;
1.1.1.33  root     17738:                        case 0x01:
1.1.1.42  root     17739:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   17740:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   17741:                                        break;
                   17742:                                }
1.1.1.33  root     17743:                                // Banyan VINES v4+ is not installed
                   17744:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   17745:                                        break;
                   17746:                                }
1.1.1.42  root     17747:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   17748:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   17749:                                        break;
                   17750:                                }
1.1.1.30  root     17751:                        default:
1.1.1.42  root     17752:                                // NORTON UTILITIES 5.0+
                   17753:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   17754:                                        break;
                   17755:                                }
1.1.1.30  root     17756:                                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     17757:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     17758:                                m_CF = 1;
                   17759:                                break;
                   17760:                        }
                   17761:                        break;
1.1       root     17762:                }
                   17763:                break;
1.1.1.24  root     17764:        case 0x33:
                   17765:                switch(REG8(AH)) {
                   17766:                case 0x00:
                   17767:                        // Mouse
1.1.1.49  root     17768:                        switch(REG16(AX)) {
                   17769:                        case 0x0000: msdos_int_33h_0000h(); break;
                   17770:                        case 0x0001: msdos_int_33h_0001h(); break;
                   17771:                        case 0x0002: msdos_int_33h_0002h(); break;
                   17772:                        case 0x0003: msdos_int_33h_0003h(); break;
                   17773:                        case 0x0004: msdos_int_33h_0004h(); break;
                   17774:                        case 0x0005: msdos_int_33h_0005h(); break;
                   17775:                        case 0x0006: msdos_int_33h_0006h(); break;
                   17776:                        case 0x0007: msdos_int_33h_0007h(); break;
                   17777:                        case 0x0008: msdos_int_33h_0008h(); break;
                   17778:                        case 0x0009: msdos_int_33h_0009h(); break;
                   17779:                        case 0x000a: msdos_int_33h_000ah(); break;
                   17780:                        case 0x000b: msdos_int_33h_000bh(); break;
                   17781:                        case 0x000c: msdos_int_33h_000ch(); break;
                   17782:                        case 0x000d: break; // MS MOUSE v1.0+ - Light Pen Emulation On
                   17783:                        case 0x000e: break; // MS MOUSE v1.0+ - Light Pen Emulation Off
                   17784:                        case 0x000f: msdos_int_33h_000fh(); break;
                   17785:                        case 0x0010: break; // MS MOUSE v1.0+ - Define Screen Region for Updating
                   17786:                        case 0x0011: msdos_int_33h_0011h(); break;
                   17787:                        case 0x0012: REG16(AX) = 0xffff; break; // MS MOUSE - Set Large Graphics Cursor Block
                   17788:                        case 0x0013: break; // MS MOUSE v5.0+ - Define Double-Speed Threshold
                   17789:                        case 0x0014: msdos_int_33h_0014h(); break;
                   17790:                        case 0x0015: msdos_int_33h_0015h(); break;
                   17791:                        case 0x0016: msdos_int_33h_0016h(); break;
                   17792:                        case 0x0017: msdos_int_33h_0017h(); break;
                   17793:                        case 0x0018: msdos_int_33h_0018h(); break;
                   17794:                        case 0x0019: msdos_int_33h_0019h(); break;
                   17795:                        case 0x001a: msdos_int_33h_001ah(); break;
                   17796:                        case 0x001b: msdos_int_33h_001bh(); break;
                   17797:                        case 0x001c: break; // MS MOUSE v6.0+ - Set Interrupt Rate
                   17798:                        case 0x001d: msdos_int_33h_001dh(); break;
                   17799:                        case 0x001e: msdos_int_33h_001eh(); break;
                   17800:                        case 0x001f: msdos_int_33h_001fh(); break;
                   17801:                        case 0x0020: msdos_int_33h_0020h(); break;
                   17802:                        case 0x0021: msdos_int_33h_0021h(); break;
                   17803:                        case 0x0022: msdos_int_33h_0022h(); break;
                   17804:                        case 0x0023: msdos_int_33h_0023h(); break;
                   17805:                        case 0x0024: msdos_int_33h_0024h(); break;
                   17806:                        case 0x0025: msdos_int_33h_0025h(); break;
                   17807:                        case 0x0026: msdos_int_33h_0026h(); break;
                   17808:                        case 0x0027: msdos_int_33h_0027h(); break;
                   17809:                        case 0x0028: msdos_int_33h_0028h(); break;
                   17810:                        case 0x0029: msdos_int_33h_0029h(); break;
                   17811:                        case 0x002a: msdos_int_33h_002ah(); break;
                   17812:                        // 0x002b: MS MOUSE v7.0+ - Load Acceleration Profiles
                   17813:                        // 0x002c: MS MOUSE v7.0+ - Get Acceleration Profiles
                   17814:                        // 0x002d: MS MOUSE v7.0+ - Select Acceleration Profile
                   17815:                        // 0x002e: MS MOUSE v8.10+ - Set Acceleration Profile Names
                   17816:                        case 0x002f: break; // Mouse Hardware Reset
                   17817:                        // 0x0030: MS MOUSE v7.04+ - Get/Set BallPoint Information
                   17818:                        case 0x0031: msdos_int_33h_0031h(); break;
                   17819:                        case 0x0032: msdos_int_33h_0032h(); break;
                   17820:                        // 0x0033: MS MOUSE v7.05+ - Get Switch Settings And Acceleration Profile Data
                   17821:                        // 0x0034: MS MOUSE v8.0+ - Get Initialization File
                   17822:                        // 0x0035: MS MOUSE v8.10+ - LCD Screen Large Pointer Support
                   17823:                        case 0x004d: msdos_int_33h_004dh(); break;
                   17824:                        case 0x006d: msdos_int_33h_006dh(); break;
1.1.1.24  root     17825:                        default:
                   17826:                                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));
                   17827:                                break;
                   17828:                        }
                   17829:                        break;
                   17830:                default:
                   17831:                        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));
                   17832:                        break;
                   17833:                }
                   17834:                break;
1.1.1.59  root     17835:        case 0x65:
1.1.1.19  root     17836:                // dummy interrupt for EMS (int 67h)
                   17837:                switch(REG8(AH)) {
                   17838:                case 0x40: msdos_int_67h_40h(); break;
                   17839:                case 0x41: msdos_int_67h_41h(); break;
                   17840:                case 0x42: msdos_int_67h_42h(); break;
                   17841:                case 0x43: msdos_int_67h_43h(); break;
                   17842:                case 0x44: msdos_int_67h_44h(); break;
                   17843:                case 0x45: msdos_int_67h_45h(); break;
                   17844:                case 0x46: msdos_int_67h_46h(); break;
                   17845:                case 0x47: msdos_int_67h_47h(); break;
                   17846:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     17847:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   17848:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     17849:                case 0x4b: msdos_int_67h_4bh(); break;
                   17850:                case 0x4c: msdos_int_67h_4ch(); break;
                   17851:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     17852:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     17853:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     17854:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     17855:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     17856:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     17857:                case 0x53: msdos_int_67h_53h(); break;
                   17858:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.49  root     17859:                case 0x55: msdos_int_67h_55h(); break;
                   17860:                case 0x56: msdos_int_67h_56h(); break;
1.1.1.20  root     17861:                case 0x57: msdos_int_67h_57h(); break;
                   17862:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     17863:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     17864:                case 0x5a: msdos_int_67h_5ah(); break;
1.1.1.49  root     17865:                case 0x5b: msdos_int_67h_5bh(); break;
1.1.1.43  root     17866:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   17867:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.49  root     17868:                case 0x70: msdos_int_67h_70h(); break;
1.1.1.31  root     17869:                // 0xde: VCPI
1.1.1.30  root     17870:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     17871:                default:
1.1.1.22  root     17872:                        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     17873:                        REG8(AH) = 0x84;
                   17874:                        break;
                   17875:                }
                   17876:                break;
                   17877: #ifdef SUPPORT_XMS
1.1.1.59  root     17878:        case 0x66:
1.1.1.19  root     17879:                // dummy interrupt for XMS (call far)
1.1.1.28  root     17880:                try {
                   17881:                        switch(REG8(AH)) {
                   17882:                        case 0x00: msdos_call_xms_00h(); break;
                   17883:                        case 0x01: msdos_call_xms_01h(); break;
                   17884:                        case 0x02: msdos_call_xms_02h(); break;
                   17885:                        case 0x03: msdos_call_xms_03h(); break;
                   17886:                        case 0x04: msdos_call_xms_04h(); break;
                   17887:                        case 0x05: msdos_call_xms_05h(); break;
                   17888:                        case 0x06: msdos_call_xms_06h(); break;
                   17889:                        case 0x07: msdos_call_xms_07h(); break;
                   17890:                        case 0x08: msdos_call_xms_08h(); break;
                   17891:                        case 0x09: msdos_call_xms_09h(); break;
                   17892:                        case 0x0a: msdos_call_xms_0ah(); break;
                   17893:                        case 0x0b: msdos_call_xms_0bh(); break;
                   17894:                        case 0x0c: msdos_call_xms_0ch(); break;
                   17895:                        case 0x0d: msdos_call_xms_0dh(); break;
                   17896:                        case 0x0e: msdos_call_xms_0eh(); break;
                   17897:                        case 0x0f: msdos_call_xms_0fh(); break;
                   17898:                        case 0x10: msdos_call_xms_10h(); break;
                   17899:                        case 0x11: msdos_call_xms_11h(); break;
                   17900:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     17901: #if defined(HAS_I386)
                   17902:                        case 0x88: msdos_call_xms_88h(); break;
                   17903:                        case 0x89: msdos_call_xms_89h(); break;
                   17904:                        case 0x8e: msdos_call_xms_8eh(); break;
                   17905:                        case 0x8f: msdos_call_xms_8fh(); break;
                   17906: #endif
1.1.1.28  root     17907:                        default:
                   17908:                                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));
                   17909:                                REG16(AX) = 0x0000;
                   17910:                                REG8(BL) = 0x80; // function not implemented
                   17911:                                break;
                   17912:                        }
                   17913:                } catch(...) {
1.1.1.19  root     17914:                        REG16(AX) = 0x0000;
1.1.1.28  root     17915:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     17916:                }
                   17917:                break;
                   17918: #endif
1.1.1.59  root     17919: /*
                   17920:        case 0x67:
                   17921:                // int 67h handler is in EMS device driver (EMMXXXX0) and it calls int 65h
                   17922:                // NOTE: some softwares get address of int 67h handler and recognize the address is in EMS device driver
                   17923:                break;
                   17924: */
                   17925:        case 0x69:
1.1.1.24  root     17926:                // irq12 (mouse)
                   17927:                mouse_push_ax = REG16(AX);
                   17928:                mouse_push_bx = REG16(BX);
                   17929:                mouse_push_cx = REG16(CX);
                   17930:                mouse_push_dx = REG16(DX);
                   17931:                mouse_push_si = REG16(SI);
                   17932:                mouse_push_di = REG16(DI);
                   17933:                
1.1.1.43  root     17934:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     17935:                        REG16(AX) = mouse.status_irq;
                   17936:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     17937:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17938:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     17939:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17940:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17941:                        
1.1.1.49  root     17942:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17943:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr.w.l & 0xff;
                   17944:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr.w.l >> 8;
                   17945:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr.w.h & 0xff;
                   17946:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.59  root     17947:                        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17948:                        mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.43  root     17949:                        break;
1.1.1.24  root     17950:                }
1.1.1.43  root     17951:                for(int i = 0; i < 8; i++) {
                   17952:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17953:                                REG16(AX) = mouse.status_irq_alt;
                   17954:                                REG16(BX) = mouse.get_buttons();
                   17955:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17956:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   17957:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17958:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17959:                                
1.1.1.49  root     17960:                                mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17961:                                mem[DUMMY_TOP + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   17962:                                mem[DUMMY_TOP + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   17963:                                mem[DUMMY_TOP + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   17964:                                mem[DUMMY_TOP + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
1.1.1.59  root     17965:                                mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17966:                                mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.43  root     17967:                                break;
                   17968:                        }
                   17969:                }
1.1.1.59  root     17970:                if(mouse.status_irq_ps2 && mouse.call_addr_ps2.dw && mouse.enabled_ps2) {
1.1.1.54  root     17971:                        UINT16 data_1st, data_2nd, data_3rd;
                   17972:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   17973:                        i386_push16(data_1st);
                   17974:                        i386_push16(data_2nd);
                   17975:                        i386_push16(data_3rd);
                   17976:                        i386_push16(0x0000);
                   17977:                        
                   17978:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17979:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr_ps2.w.l & 0xff;
                   17980:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr_ps2.w.l >> 8;
                   17981:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr_ps2.w.h & 0xff;
                   17982:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr_ps2.w.h >> 8;
1.1.1.59  root     17983:                        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6ah (dummy)
                   17984:                        mem[DUMMY_TOP + 0x08] = 0x6a;
1.1.1.54  root     17985:                        break;
                   17986:                }
1.1.1.43  root     17987:                // invalid call addr :-(
1.1.1.49  root     17988:                mem[DUMMY_TOP + 0x02] = 0x90;   // nop
                   17989:                mem[DUMMY_TOP + 0x03] = 0x90;   // nop
                   17990:                mem[DUMMY_TOP + 0x04] = 0x90;   // nop
                   17991:                mem[DUMMY_TOP + 0x05] = 0x90;   // nop
                   17992:                mem[DUMMY_TOP + 0x06] = 0x90;   // nop
1.1.1.59  root     17993:                mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17994:                mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.24  root     17995:                break;
1.1.1.59  root     17996:        case 0x6a:
                   17997:                // end of ps/2 mouse bios
                   17998:                i386_pop16();
                   17999:                i386_pop16();
                   18000:                i386_pop16();
                   18001:                i386_pop16();
1.1.1.24  root     18002:        case 0x6b:
                   18003:                // end of irq12 (mouse)
                   18004:                REG16(AX) = mouse_push_ax;
                   18005:                REG16(BX) = mouse_push_bx;
                   18006:                REG16(CX) = mouse_push_cx;
                   18007:                REG16(DX) = mouse_push_dx;
                   18008:                REG16(SI) = mouse_push_si;
                   18009:                REG16(DI) = mouse_push_di;
                   18010:                
                   18011:                // EOI
                   18012:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   18013:                        pic[0].isr &= ~(1 << 2); // master
                   18014:                }
                   18015:                pic_update();
                   18016:                break;
                   18017:        case 0x6c:
1.1.1.19  root     18018:                // dummy interrupt for case map routine pointed in the country info
                   18019:                if(REG8(AL) >= 0x80) {
                   18020:                        char tmp[2] = {0};
                   18021:                        tmp[0] = REG8(AL);
                   18022:                        my_strupr(tmp);
                   18023:                        REG8(AL) = tmp[0];
                   18024:                }
                   18025:                break;
1.1.1.27  root     18026:        case 0x6d:
                   18027:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   18028:                REG8(AL) = 0x86; // not supported
                   18029:                m_CF = 1;
                   18030:                break;
1.1.1.32  root     18031:        case 0x6e:
                   18032:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   18033:                {
                   18034:                        UINT16 code = REG16(AX);
                   18035:                        if(code & 0xf0) {
                   18036:                                code = (code & 7) | ((code & 0x10) >> 1);
                   18037:                        }
                   18038:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   18039:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   18040:                                        const char *message = NULL;
                   18041:                                        if(active_code_page == 932) {
                   18042:                                                message = param_error_table[i].message_japanese;
                   18043:                                        }
                   18044:                                        if(message == NULL) {
                   18045:                                                message = param_error_table[i].message_english;
                   18046:                                        }
                   18047:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   18048:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   18049:                                        
                   18050:                                        SREG(ES) = WORK_TOP >> 4;
                   18051:                                        i386_load_segment_descriptor(ES);
                   18052:                                        REG16(DI) = 0x0000;
                   18053:                                        break;
                   18054:                                }
                   18055:                        }
                   18056:                }
                   18057:                break;
1.1.1.49  root     18058:        case 0x6f:
                   18059:                // dummy interrupt for end of alter page map and call
                   18060:                {
                   18061:                        UINT16 handles[4], pages[4];
                   18062:                        
                   18063:                        // pop old mapping data in new mapping
                   18064:                        for(int i = 0; i < 4; i++) {
                   18065:                                pages  [3 - i] = i386_pop16();
                   18066:                                handles[3 - i] = i386_pop16();
                   18067:                        }
                   18068:                        
                   18069:                        // restore old mapping
                   18070:                        for(int i = 0; i < 4; i++) {
                   18071:                                UINT16 handle = handles[i];
                   18072:                                UINT16 page   = pages  [i];
                   18073:                                
                   18074:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   18075:                                        ems_map_page(i, handle, page);
                   18076:                                } else {
                   18077:                                        ems_unmap_page(i);
                   18078:                                }
                   18079:                        }
                   18080:                        // do ret_far (pop cs/ip) in old mapping
                   18081:                }
                   18082:                break;
1.1.1.8   root     18083:        case 0x70:
                   18084:        case 0x71:
                   18085:        case 0x72:
                   18086:        case 0x73:
                   18087:        case 0x74:
                   18088:        case 0x75:
                   18089:        case 0x76:
                   18090:        case 0x77:
                   18091:                // EOI
                   18092:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   18093:                        pic[0].isr &= ~(1 << 2); // master
                   18094:                }
                   18095:                pic_update();
                   18096:                break;
1.1       root     18097:        default:
1.1.1.22  root     18098: //             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     18099:                break;
                   18100:        }
                   18101:        
                   18102:        // update cursor position
                   18103:        if(cursor_moved) {
1.1.1.36  root     18104:                pcbios_update_cursor_position();
1.1       root     18105:                cursor_moved = false;
                   18106:        }
                   18107: }
                   18108: 
                   18109: // init
                   18110: 
                   18111: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   18112: {
                   18113:        // init file handler
                   18114:        memset(file_handler, 0, sizeof(file_handler));
                   18115:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   18116:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   18117:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     18118: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     18119:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     18120: #else
                   18121:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   18122: #endif
                   18123:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     18124:        }
1.1.1.21  root     18125:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45  root     18126: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   18127:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     18128:        }
1.1       root     18129:        _dup2(0, DUP_STDIN);
                   18130:        _dup2(1, DUP_STDOUT);
                   18131:        _dup2(2, DUP_STDERR);
1.1.1.21  root     18132:        _dup2(3, DUP_STDAUX);
                   18133:        _dup2(4, DUP_STDPRN);
1.1       root     18134:        
1.1.1.24  root     18135:        // init mouse
                   18136:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     18137:        mouse.enabled = true;   // from DOSBox
                   18138:        mouse.hidden = 1;       // hidden in default ???
                   18139:        mouse.old_hidden = 1;   // from DOSBox
                   18140:        mouse.max_position.x = 8 * (scr_width  - 1);
                   18141:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     18142:        mouse.mickey.x = 8;
                   18143:        mouse.mickey.y = 16;
                   18144:        
1.1.1.26  root     18145: #ifdef SUPPORT_XMS
                   18146:        // init xms
                   18147:        msdos_xms_init();
                   18148: #endif
                   18149:        
1.1       root     18150:        // init process
                   18151:        memset(process, 0, sizeof(process));
                   18152:        
1.1.1.13  root     18153:        // init dtainfo
                   18154:        msdos_dta_info_init();
                   18155:        
1.1       root     18156:        // init memory
                   18157:        memset(mem, 0, sizeof(mem));
                   18158:        
                   18159:        // bios data area
1.1.1.23  root     18160:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     18161:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   18162:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.58  root     18163: //     CONSOLE_FONT_INFO cfi;
1.1.1.57  root     18164: //     GetCurrentConsoleFont(hStdout, FALSE, &cfi);
1.1.1.14  root     18165:        
                   18166:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   18167:        text_vram_top_address = TEXT_VRAM_TOP;
                   18168:        text_vram_end_address = text_vram_top_address + regen;
                   18169:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   18170:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     18171:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1.1.14  root     18172:        
                   18173:        if(regen > 0x4000) {
                   18174:                regen = 0x8000;
                   18175:                vram_pages = 1;
                   18176:        } else if(regen > 0x2000) {
                   18177:                regen = 0x4000;
                   18178:                vram_pages = 2;
                   18179:        } else if(regen > 0x1000) {
                   18180:                regen = 0x2000;
                   18181:                vram_pages = 4;
                   18182:        } else {
                   18183:                regen = 0x1000;
                   18184:                vram_pages = 8;
                   18185:        }
1.1       root     18186:        
1.1.1.25  root     18187:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   18188:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     18189:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   18190:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     18191:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     18192:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   18193:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     18194: #ifdef EXT_BIOS_TOP
1.1.1.25  root     18195:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     18196: #endif
1.1.1.26  root     18197:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     18198:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     18199:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   18200:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     18201:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     18202:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   18203:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     18204:        *(UINT16 *)(mem + 0x44e) = 0;
                   18205:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     18206:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     18207:        *(UINT8  *)(mem + 0x460) = 7;
                   18208:        *(UINT8  *)(mem + 0x461) = 7;
                   18209:        *(UINT8  *)(mem + 0x462) = 0;
                   18210:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     18211:        *(UINT8  *)(mem + 0x465) = 0x09;
                   18212:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     18213:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     18214:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   18215:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     18216:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
1.1.1.58  root     18217:        *(UINT16 *)(mem + 0x485) = font_height;
1.1.1.14  root     18218:        *(UINT8  *)(mem + 0x487) = 0x60;
                   18219:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     18220: #ifdef EXT_BIOS_TOP
1.1.1.25  root     18221:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     18222: #endif
1.1.1.14  root     18223:        
                   18224:        // initial screen
                   18225:        SMALL_RECT rect;
                   18226:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     18227:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     18228:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   18229:                for(int x = 0; x < scr_width; x++) {
                   18230:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   18231:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   18232:                }
                   18233:        }
1.1       root     18234:        
1.1.1.19  root     18235:        // init mcb
1.1       root     18236:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     18237:        
                   18238:        // iret table
                   18239:        // note: int 2eh vector should address the routine in command.com,
                   18240:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   18241:        // so move iret table into allocated memory block
                   18242:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.58  root     18243:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, (IRET_SIZE + 5 * 128) >> 4);
1.1.1.19  root     18244:        IRET_TOP = seg << 4;
1.1.1.58  root     18245:        seg += (IRET_SIZE + 5 * 128) >> 4;
1.1.1.25  root     18246:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     18247:        
1.1.1.58  root     18248:        // note: SO1 checks int 21h vector and if it aims iret (cfh)
                   18249:        // it is recognized SO1 is not running on MS-DOS environment
                   18250:        for(int i = 0; i < 128; i++) {
                   18251:                // jmp far (IRET_TOP >> 4):(interrupt number)
                   18252:                *(UINT8  *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 0) = 0xea;
                   18253:                *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 1) = i;
                   18254:                *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 3) = IRET_TOP >> 4;
                   18255:        }
                   18256:        
1.1.1.19  root     18257:        // dummy xms/ems device
1.1.1.33  root     18258:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     18259:        XMS_TOP = seg << 4;
                   18260:        seg += XMS_SIZE >> 4;
                   18261:        
                   18262:        // environment
1.1.1.33  root     18263:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     18264:        int env_seg = seg;
                   18265:        int ofs = 0;
1.1.1.32  root     18266:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   18267:        char comspec_added = 0;
1.1.1.33  root     18268:        char lastdrive_added = 0;
1.1.1.32  root     18269:        char env_msdos_path[ENV_SIZE] = {0};
                   18270:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     18271:        char prompt_added = 0;
1.1.1.32  root     18272:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     18273:        char tz_added = 0;
1.1.1.45  root     18274:        const char *path, *short_path;
1.1.1.32  root     18275:        
                   18276:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   18277:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18278:                        strcpy(env_append, short_path);
                   18279:                }
                   18280:        }
                   18281:        if((path = getenv("APPEND")) != NULL) {
                   18282:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18283:                        if(env_append[0] != '\0') {
                   18284:                                strcat(env_append, ";");
                   18285:                        }
                   18286:                        strcat(env_append, short_path);
                   18287:                }
                   18288:        }
                   18289:        
                   18290:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   18291:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18292:                        strcpy(comspec_path, short_path);
                   18293:                }
                   18294:        }
                   18295:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   18296:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18297:                        strcpy(comspec_path, short_path);
                   18298:                }
                   18299:        }
1.1       root     18300:        
1.1.1.28  root     18301:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     18302:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18303:                        strcpy(env_msdos_path, short_path);
                   18304:                        strcpy(env_path, short_path);
1.1.1.14  root     18305:                }
                   18306:        }
1.1.1.28  root     18307:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     18308:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18309:                        if(env_path[0] != '\0') {
                   18310:                                strcat(env_path, ";");
                   18311:                        }
                   18312:                        strcat(env_path, short_path);
1.1.1.9   root     18313:                }
                   18314:        }
1.1.1.32  root     18315:        
1.1.1.60  root     18316:        if(GetTempPathA(ENV_SIZE, env_temp) != 0) {
1.1.1.32  root     18317:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     18318:        }
1.1.1.32  root     18319:        for(int i = 0; i < 4; i++) {
                   18320:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   18321:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   18322:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18323:                                strcpy(env_temp, short_path);
                   18324:                                break;
                   18325:                        }
                   18326:                }
1.1.1.24  root     18327:        }
1.1.1.32  root     18328:        
1.1.1.9   root     18329:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     18330:                // lower to upper
1.1.1.28  root     18331:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     18332:                strcpy(tmp, *p);
                   18333:                for(int i = 0;; i++) {
                   18334:                        if(tmp[i] == '=') {
                   18335:                                tmp[i] = '\0';
                   18336:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     18337:                                my_strupr(name);
1.1       root     18338:                                tmp[i] = '=';
                   18339:                                break;
                   18340:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     18341:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     18342:                        }
                   18343:                }
1.1.1.33  root     18344:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   18345:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   18346:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     18347:                        // ignore non standard environments
                   18348:                } else {
1.1.1.33  root     18349:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     18350:                                if(env_append[0] != '\0') {
                   18351:                                        sprintf(tmp, "APPEND=%s", env_append);
                   18352:                                } else {
                   18353:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   18354:                                }
                   18355:                                append_added = 1;
                   18356:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     18357:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     18358:                                comspec_added = 1;
1.1.1.33  root     18359:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   18360:                                char *env = getenv("MSDOS_LASTDRIVE");
                   18361:                                if(env != NULL) {
                   18362:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   18363:                                }
                   18364:                                lastdrive_added = 1;
                   18365:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     18366:                                if(env_msdos_path[0] != '\0') {
                   18367:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   18368:                                } else {
                   18369:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   18370:                                }
1.1.1.33  root     18371:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     18372:                                if(env_path[0] != '\0') {
                   18373:                                        sprintf(tmp, "PATH=%s", env_path);
                   18374:                                } else {
                   18375:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   18376:                                }
1.1.1.32  root     18377:                                path_added = 1;
1.1.1.33  root     18378:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   18379:                                prompt_added = 1;
1.1.1.28  root     18380:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   18381:                                if(env_temp[0] != '\0') {
                   18382:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   18383:                                } else {
                   18384:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   18385:                                }
1.1.1.32  root     18386:                                temp_added = 1;
1.1.1.33  root     18387:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     18388:                                if(env_temp[0] != '\0') {
                   18389:                                        sprintf(tmp, "TMP=%s", env_temp);
                   18390:                                } else {
                   18391:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     18392:                                }
1.1.1.32  root     18393:                                tmp_added = 1;
1.1.1.33  root     18394:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   18395:                                char *env = getenv("MSDOS_TZ");
                   18396:                                if(env != NULL) {
                   18397:                                        sprintf(tmp, "TZ=%s", env);
                   18398:                                }
                   18399:                                tz_added = 1;
1.1       root     18400:                        }
                   18401:                        int len = strlen(tmp);
1.1.1.14  root     18402:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     18403:                                fatalerror("too many environments\n");
                   18404:                        }
                   18405:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   18406:                        ofs += len + 1;
                   18407:                }
                   18408:        }
1.1.1.32  root     18409:        if(!append_added && env_append[0] != '\0') {
                   18410:                #define SET_ENV(name, value) { \
                   18411:                        char tmp[ENV_SIZE]; \
                   18412:                        sprintf(tmp, "%s=%s", name, value); \
                   18413:                        int len = strlen(tmp); \
                   18414:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   18415:                                fatalerror("too many environments\n"); \
                   18416:                        } \
                   18417:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   18418:                        ofs += len + 1; \
                   18419:                }
                   18420:                SET_ENV("APPEND", env_append);
                   18421:        }
                   18422:        if(!comspec_added) {
                   18423:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   18424:        }
1.1.1.33  root     18425:        if(!lastdrive_added) {
                   18426:                SET_ENV("LASTDRIVE", "Z");
                   18427:        }
1.1.1.32  root     18428:        if(!path_added) {
                   18429:                SET_ENV("PATH", env_path);
                   18430:        }
1.1.1.33  root     18431:        if(!prompt_added) {
                   18432:                SET_ENV("PROMPT", "$P$G");
                   18433:        }
1.1.1.32  root     18434:        if(!temp_added) {
                   18435:                SET_ENV("TEMP", env_temp);
                   18436:        }
                   18437:        if(!tmp_added) {
                   18438:                SET_ENV("TMP", env_temp);
                   18439:        }
1.1.1.33  root     18440:        if(!tz_added) {
                   18441:                TIME_ZONE_INFORMATION tzi;
                   18442:                HKEY hKey, hSubKey;
                   18443:                char tzi_std_name[64];
                   18444:                char tz_std[8] = "GMT";
                   18445:                char tz_dlt[8] = "GST";
                   18446:                char tz_value[32];
                   18447:                
                   18448:                // timezone name from GetTimeZoneInformation may not be english
                   18449:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   18450:                setlocale(LC_CTYPE, "");
                   18451:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   18452:                
                   18453:                // get english timezone name from registry
1.1.1.60  root     18454:                if(RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
1.1.1.33  root     18455:                        for(DWORD i = 0; !tz_added; i++) {
                   18456:                                char reg_name[256], sub_key[1024], std_name[256];
                   18457:                                DWORD size;
                   18458:                                FILETIME ftTime;
1.1.1.60  root     18459:                                LONG result = RegEnumKeyExA(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
1.1.1.33  root     18460:                                
                   18461:                                if(result == ERROR_SUCCESS) {
                   18462:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
1.1.1.60  root     18463:                                        if(RegOpenKeyExA(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   18464:                                                if(RegQueryValueExA(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
1.1.1.33  root     18465:                                                        // search english timezone name from table
1.1.1.37  root     18466:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     18467:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   18468:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   18469:                                                                                if(tz_table[j].std != NULL) {
                   18470:                                                                                        strcpy(tz_std, tz_table[j].std);
                   18471:                                                                                }
                   18472:                                                                                if(tz_table[j].dlt != NULL) {
                   18473:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   18474:                                                                                }
                   18475:                                                                                tz_added = 1;
                   18476:                                                                                break;
                   18477:                                                                        }
                   18478:                                                                }
                   18479:                                                        }
                   18480:                                                }
                   18481:                                                RegCloseKey(hSubKey);
                   18482:                                        }
                   18483:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   18484:                                        break;
                   18485:                                }
                   18486:                        }
                   18487:                        RegCloseKey(hKey);
                   18488:                }
                   18489:                if((tzi.Bias % 60) != 0) {
                   18490:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   18491:                } else {
                   18492:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   18493:                }
                   18494:                if(daylight) {
                   18495:                        strcat(tz_value, tz_dlt);
                   18496:                }
                   18497:                SET_ENV("TZ", tz_value);
                   18498:        }
1.1       root     18499:        seg += (ENV_SIZE >> 4);
                   18500:        
                   18501:        // psp
1.1.1.33  root     18502:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     18503:        current_psp = seg;
1.1.1.35  root     18504:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   18505:        psp->parent_psp = current_psp;
1.1       root     18506:        seg += (PSP_SIZE >> 4);
                   18507:        
1.1.1.19  root     18508:        // first free mcb in conventional memory
1.1.1.33  root     18509:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     18510:        first_mcb = seg;
                   18511:        
1.1.1.19  root     18512:        // dummy mcb to link to umb
1.1.1.33  root     18513: #if 0
1.1.1.39  root     18514:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     18515: #else
1.1.1.39  root     18516:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     18517: #endif
1.1.1.19  root     18518:        
                   18519:        // first free mcb in upper memory block
1.1.1.8   root     18520:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     18521:        
1.1.1.29  root     18522: #ifdef SUPPORT_HMA
                   18523:        // first free mcb in high memory area
                   18524:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   18525: #endif
                   18526:        
1.1.1.26  root     18527:        // interrupt vector
1.1.1.58  root     18528:        for(int i = 0; i < 256; i++) {
                   18529:                // 00-07: CPU exception handler
                   18530:                // 08-0F: IRQ 0-7
                   18531:                // 10-1F: PC BIOS
                   18532:                // 20-3F: MS-DOS system call
                   18533:                // 70-77: IRQ 8-15
                   18534:                *(UINT16 *)(mem + 4 * i + 0) = (i <= 0x3f || (i >= 0x70 && i <= 0x77)) ? (IRET_SIZE + 5 * i) : i;
1.1.1.26  root     18535:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   18536:        }
1.1.1.49  root     18537:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffc:0018 irq0 (system timer)
                   18538:        *(UINT16 *)(mem + 4 * 0x08 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     18539:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   18540:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   18541:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   18542:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
1.1.1.49  root     18543:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffc:0000 irq12 (mouse)
                   18544:        *(UINT16 *)(mem + 4 * 0x74 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     18545:        
1.1.1.29  root     18546:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     18547:        static const struct {
                   18548:                UINT16 attributes;
                   18549:                char *dev_name;
                   18550:        } dummy_devices[] = {
                   18551:                {0x8013, "CON     "},
                   18552:                {0x8000, "AUX     "},
                   18553:                {0xa0c0, "PRN     "},
                   18554:                {0x8008, "CLOCK$  "},
                   18555:                {0x8000, "COM1    "},
                   18556:                {0xa0c0, "LPT1    "},
                   18557:                {0xa0c0, "LPT2    "},
                   18558:                {0xa0c0, "LPT3    "},
                   18559:                {0x8000, "COM2    "},
                   18560:                {0x8000, "COM3    "},
                   18561:                {0x8000, "COM4    "},
1.1.1.30  root     18562: //             {0xc000, "CONFIG$ "},
                   18563:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     18564:        };
                   18565:        static const UINT8 dummy_device_routine[] = {
                   18566:                // from NUL device of Windows 98 SE
                   18567:                // or word ptr ES:[BX+03],0100
                   18568:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   18569:                // retf
                   18570:                0xcb,
                   18571:        };
1.1.1.29  root     18572:        device_t *last = NULL;
1.1.1.32  root     18573:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     18574:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     18575:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   18576:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     18577:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     18578:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   18579:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     18580:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     18581:                last = device;
                   18582:        }
                   18583:        if(last != NULL) {
                   18584:                last->next_driver.w.l = 0;
                   18585:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     18586:        }
1.1.1.29  root     18587:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     18588:        
1.1.1.25  root     18589:        // dos info
                   18590:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   18591:        dos_info->magic_word = 1;
                   18592:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   18593:        dos_info->first_dpb.w.l = 0;
                   18594:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   18595:        dos_info->first_sft.w.l = 0;
                   18596:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     18597:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     18598:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     18599:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     18600:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   18601:        dos_info->max_sector_len = 512;
                   18602:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   18603:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   18604:        dos_info->cds.w.l = 0;
                   18605:        dos_info->cds.w.h = CDS_TOP >> 4;
                   18606:        dos_info->fcb_table.w.l = 0;
                   18607:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   18608:        dos_info->last_drive = 'Z' - 'A' + 1;
                   18609:        dos_info->buffers_x = 20;
                   18610:        dos_info->buffers_y = 0;
                   18611:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     18612:        dos_info->nul_device.next_driver.w.l = 22;
                   18613:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     18614:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     18615:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   18616:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18617:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   18618:        dos_info->disk_buf_heads.w.l = 0;
                   18619:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     18620:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     18621:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   18622:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     18623:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     18624:        
                   18625:        char *env;
                   18626:        if((env = getenv("LASTDRIVE")) != NULL) {
                   18627:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18628:                        dos_info->last_drive = env[0] - 'A' + 1;
                   18629:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18630:                        dos_info->last_drive = env[0] - 'a' + 1;
                   18631:                }
                   18632:        }
                   18633:        if((env = getenv("windir")) != NULL) {
                   18634:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18635:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   18636:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18637:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   18638:                }
                   18639:        }
                   18640: #if defined(HAS_I386)
                   18641:        dos_info->i386_or_later = 1;
                   18642: #else
                   18643:        dos_info->i386_or_later = 0;
                   18644: #endif
                   18645:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   18646:        
1.1.1.27  root     18647:        // ems (int 67h) and xms
1.1.1.25  root     18648:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   18649:        xms_device->next_driver.w.l = 0xffff;
                   18650:        xms_device->next_driver.w.h = 0xffff;
                   18651:        xms_device->attributes = 0xc000;
1.1.1.29  root     18652:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   18653:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18654:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   18655:        
1.1.1.59  root     18656:        mem[XMS_TOP + 0x12] = 0xcd;     // int 65h (dummy)
                   18657:        mem[XMS_TOP + 0x13] = 0x65;
1.1.1.26  root     18658:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     18659: #ifdef SUPPORT_XMS
                   18660:        if(support_xms) {
1.1.1.59  root     18661:                mem[XMS_TOP + 0x15] = 0xcd;     // int 66h (dummy)
                   18662:                mem[XMS_TOP + 0x16] = 0x66;
1.1.1.26  root     18663:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     18664:        } else
                   18665: #endif
1.1.1.26  root     18666:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     18667:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     18668:        
1.1.1.26  root     18669:        // irq12 routine (mouse)
1.1.1.59  root     18670:        mem[DUMMY_TOP + 0x00] = 0xcd;   // int 69h (dummy)
                   18671:        mem[DUMMY_TOP + 0x01] = 0x69;
1.1.1.49  root     18672:        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far mouse
                   18673:        mem[DUMMY_TOP + 0x03] = 0xff;
                   18674:        mem[DUMMY_TOP + 0x04] = 0xff;
                   18675:        mem[DUMMY_TOP + 0x05] = 0xff;
                   18676:        mem[DUMMY_TOP + 0x06] = 0xff;
1.1.1.59  root     18677: //     mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6ah (dummy)
                   18678: //     mem[DUMMY_TOP + 0x08] = 0x6a;
1.1.1.49  root     18679:        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   18680:        mem[DUMMY_TOP + 0x08] = 0x6b;
                   18681:        mem[DUMMY_TOP + 0x09] = 0xcf;   // iret
1.1.1.24  root     18682:        
1.1.1.27  root     18683:        // case map routine
1.1.1.49  root     18684:        mem[DUMMY_TOP + 0x0a] = 0xcd;   // int 6ch (dummy)
                   18685:        mem[DUMMY_TOP + 0x0b] = 0x6c;
                   18686:        mem[DUMMY_TOP + 0x0c] = 0xcb;   // retf
1.1.1.27  root     18687:        
                   18688:        // font read routine
1.1.1.49  root     18689:        mem[DUMMY_TOP + 0x0d] = 0xcd;   // int 6dh (dummy)
                   18690:        mem[DUMMY_TOP + 0x0e] = 0x6d;
                   18691:        mem[DUMMY_TOP + 0x0f] = 0xcb;   // retf
1.1.1.19  root     18692:        
1.1.1.32  root     18693:        // error message read routine
1.1.1.49  root     18694:        mem[DUMMY_TOP + 0x10] = 0xcd;   // int 6eh (dummy)
                   18695:        mem[DUMMY_TOP + 0x11] = 0x6e;
                   18696:        mem[DUMMY_TOP + 0x12] = 0xcb;   // retf
1.1.1.32  root     18697:        
1.1.1.35  root     18698:        // dummy loop to wait BIOS/DOS service is done
1.1.1.49  root     18699:        mem[DUMMY_TOP + 0x13] = 0xe6;   // out f7h, al
                   18700:        mem[DUMMY_TOP + 0x14] = 0xf7;
                   18701:        mem[DUMMY_TOP + 0x15] = 0x78;   // js/jns -4
                   18702:        mem[DUMMY_TOP + 0x16] = 0xfc;
                   18703:        mem[DUMMY_TOP + 0x17] = 0xcb;   // retf
1.1.1.35  root     18704:        
1.1.1.50  root     18705:        // irq0 routine (system timer)
1.1.1.49  root     18706:        mem[DUMMY_TOP + 0x18] = 0xcd;   // int 1ch
                   18707:        mem[DUMMY_TOP + 0x19] = 0x1c;
                   18708:        mem[DUMMY_TOP + 0x1a] = 0xea;   // jmp far (IRET_TOP >> 4):0008
                   18709:        mem[DUMMY_TOP + 0x1b] = 0x08;
                   18710:        mem[DUMMY_TOP + 0x1c] = 0x00;
                   18711:        mem[DUMMY_TOP + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   18712:        mem[DUMMY_TOP + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
                   18713:        
                   18714:        // alter page map and call routine
                   18715:        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   18716:        mem[DUMMY_TOP + 0x20] = 0xff;
                   18717:        mem[DUMMY_TOP + 0x21] = 0xff;
                   18718:        mem[DUMMY_TOP + 0x22] = 0xff;
                   18719:        mem[DUMMY_TOP + 0x23] = 0xff;
                   18720:        mem[DUMMY_TOP + 0x24] = 0xcd;   // int 6fh (dummy)
                   18721:        mem[DUMMY_TOP + 0x25] = 0x6f;
                   18722:        mem[DUMMY_TOP + 0x26] = 0xcb;   // retf
1.1.1.14  root     18723:        
1.1.1.50  root     18724:        // call int 29h routine
                   18725:        mem[DUMMY_TOP + 0x27] = 0xcd;   // int 29h
                   18726:        mem[DUMMY_TOP + 0x28] = 0x29;
                   18727:        mem[DUMMY_TOP + 0x29] = 0xcb;   // retf
                   18728:        
1.1.1.63  root     18729:        // VCPI entry point
                   18730:        mem[DUMMY_TOP + 0x2a] = 0xcd;   // int 65h
                   18731:        mem[DUMMY_TOP + 0x2b] = 0x65;
                   18732:        mem[DUMMY_TOP + 0x2c] = 0xcb;   // retf
                   18733:        
1.1.1.26  root     18734:        // boot routine
1.1.1.59  root     18735:        mem[0xffff0 + 0x00] = 0xf4;     // halt to exit MS-DOS Player
                   18736: #if 1
                   18737:        mem[0xffff0 + 0x05] = '0';      // rom date (same as DOSBox)
                   18738:        mem[0xffff0 + 0x06] = '1';
                   18739:        mem[0xffff0 + 0x07] = '/';
                   18740:        mem[0xffff0 + 0x08] = '0';
                   18741:        mem[0xffff0 + 0x09] = '1';
                   18742:        mem[0xffff0 + 0x0a] = '/';
                   18743:        mem[0xffff0 + 0x0b] = '9';
                   18744:        mem[0xffff0 + 0x0c] = '2';
                   18745:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id (pc/at)
                   18746:        mem[0xffff0 + 0x0f] = 0x55;     // signature
                   18747: #else
                   18748:        mem[0xffff0 + 0x05] = '0';      // rom date (same as Windows 98 SE)
1.1.1.49  root     18749:        mem[0xffff0 + 0x06] = '2';
                   18750:        mem[0xffff0 + 0x07] = '/';
                   18751:        mem[0xffff0 + 0x08] = '2';
                   18752:        mem[0xffff0 + 0x09] = '2';
                   18753:        mem[0xffff0 + 0x0a] = '/';
                   18754:        mem[0xffff0 + 0x0b] = '0';
                   18755:        mem[0xffff0 + 0x0c] = '6';
1.1.1.59  root     18756:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id (pc/at)
1.1.1.49  root     18757:        mem[0xffff0 + 0x0f] = 0x00;
1.1.1.59  root     18758: #endif
1.1.1.24  root     18759:        
1.1       root     18760:        // param block
                   18761:        // + 0: param block (22bytes)
                   18762:        // +24: fcb1/2 (20bytes)
                   18763:        // +44: command tail (128bytes)
                   18764:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   18765:        param->env_seg = 0;
                   18766:        param->cmd_line.w.l = 44;
                   18767:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   18768:        param->fcb1.w.l = 24;
                   18769:        param->fcb1.w.h = (WORK_TOP >> 4);
                   18770:        param->fcb2.w.l = 24;
                   18771:        param->fcb2.w.h = (WORK_TOP >> 4);
                   18772:        
                   18773:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   18774:        
                   18775:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   18776:        if(argc > 1) {
                   18777:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   18778:                for(int i = 2; i < argc; i++) {
                   18779:                        char tmp[128];
                   18780:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   18781:                        strcpy(cmd_line->cmd, tmp);
                   18782:                }
                   18783:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   18784:        } else {
                   18785:                cmd_line->len = 0;
                   18786:        }
                   18787:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   18788:        
                   18789:        // system file table
1.1.1.21  root     18790:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   18791:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     18792:        
1.1.1.19  root     18793:        // disk buffer header (from DOSBox)
                   18794:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   18795:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   18796:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   18797:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   18798:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   18799:        
1.1       root     18800:        // fcb table
                   18801:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     18802:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     18803:        
1.1.1.41  root     18804:        // drive parameter block
1.1.1.42  root     18805:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     18806:                // may be a floppy drive
1.1.1.44  root     18807:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   18808:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   18809:                cds->drive_attrib = 0x4000;     // physical drive
                   18810:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   18811:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   18812:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   18813:                cds->bs_offset = 2;
                   18814:                
1.1.1.41  root     18815:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   18816:                dpb->drive_num = i;
                   18817:                dpb->unit_num = i;
1.1.1.43  root     18818:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   18819:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     18820:        }
                   18821:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     18822:                msdos_cds_update(i);
1.1.1.42  root     18823:                UINT16 seg, ofs;
                   18824:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     18825:        }
                   18826:        
1.1.1.17  root     18827:        // nls stuff
                   18828:        msdos_nls_tables_init();
1.1       root     18829:        
                   18830:        // execute command
1.1.1.28  root     18831:        try {
1.1.1.52  root     18832:                if(msdos_process_exec(argv[0], param, 0, true)) {
1.1.1.28  root     18833:                        fatalerror("'%s' not found\n", argv[0]);
                   18834:                }
                   18835:        } catch(...) {
                   18836:                // we should not reach here :-(
                   18837:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     18838:        }
                   18839:        retval = 0;
                   18840:        return(0);
                   18841: }
                   18842: 
                   18843: #define remove_std_file(path) { \
                   18844:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   18845:        if(fd != -1) { \
                   18846:                _lseek(fd, 0, SEEK_END); \
                   18847:                int size = _tell(fd); \
                   18848:                _close(fd); \
                   18849:                if(size == 0) { \
                   18850:                        remove(path); \
                   18851:                } \
                   18852:        } \
                   18853: }
                   18854: 
                   18855: void msdos_finish()
                   18856: {
                   18857:        for(int i = 0; i < MAX_FILES; i++) {
                   18858:                if(file_handler[i].valid) {
                   18859:                        _close(i);
                   18860:                }
                   18861:        }
1.1.1.21  root     18862: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     18863:        remove_std_file("stdaux.txt");
1.1.1.21  root     18864: #endif
1.1.1.30  root     18865: #ifdef SUPPORT_XMS
                   18866:        msdos_xms_finish();
                   18867: #endif
1.1       root     18868:        msdos_dbcs_table_finish();
                   18869: }
                   18870: 
                   18871: /* ----------------------------------------------------------------------------
                   18872:        PC/AT hardware emulation
                   18873: ---------------------------------------------------------------------------- */
                   18874: 
                   18875: void hardware_init()
                   18876: {
1.1.1.3   root     18877:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     18878:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     18879:        m_IF = 1;
1.1.1.3   root     18880: #if defined(HAS_I386)
1.1       root     18881:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   18882:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     18883: #endif
                   18884:        i386_set_a20_line(0);
1.1.1.14  root     18885:        
1.1.1.19  root     18886:        ems_init();
1.1.1.25  root     18887:        dma_init();
1.1       root     18888:        pic_init();
1.1.1.25  root     18889:        pio_init();
1.1.1.8   root     18890: #ifdef PIT_ALWAYS_RUNNING
                   18891:        pit_init();
                   18892: #else
1.1       root     18893:        pit_active = 0;
                   18894: #endif
1.1.1.25  root     18895:        sio_init();
1.1.1.8   root     18896:        cmos_init();
                   18897:        kbd_init();
1.1       root     18898: }
                   18899: 
1.1.1.10  root     18900: void hardware_finish()
                   18901: {
                   18902: #if defined(HAS_I386)
                   18903:        vtlb_free(m_vtlb);
                   18904: #endif
1.1.1.19  root     18905:        ems_finish();
1.1.1.37  root     18906:        pio_finish();
1.1.1.25  root     18907:        sio_finish();
1.1.1.10  root     18908: }
                   18909: 
1.1.1.28  root     18910: void hardware_release()
                   18911: {
                   18912:        // release hardware resources when this program will be terminated abnormally
                   18913: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18914:        if(fp_debug_log != NULL) {
                   18915:                fclose(fp_debug_log);
                   18916:                fp_debug_log = NULL;
1.1.1.28  root     18917:        }
                   18918: #endif
                   18919: #if defined(HAS_I386)
                   18920:        vtlb_free(m_vtlb);
                   18921: #endif
                   18922:        ems_release();
1.1.1.37  root     18923:        pio_release();
1.1.1.28  root     18924:        sio_release();
                   18925: }
                   18926: 
1.1       root     18927: void hardware_run()
                   18928: {
1.1.1.22  root     18929: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     18930:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     18931:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     18932: #endif
1.1.1.51  root     18933: #ifdef USE_DEBUGGER
                   18934:        m_int_num = -1;
                   18935: #endif
1.1.1.54  root     18936:        while(!m_exit) {
1.1.1.50  root     18937:                hardware_run_cpu();
1.1       root     18938:        }
1.1.1.22  root     18939: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18940:        if(fp_debug_log != NULL) {
                   18941:                fclose(fp_debug_log);
                   18942:                fp_debug_log = NULL;
1.1.1.28  root     18943:        }
1.1.1.22  root     18944: #endif
1.1       root     18945: }
                   18946: 
1.1.1.50  root     18947: inline void hardware_run_cpu()
                   18948: {
                   18949: #if defined(HAS_I386)
                   18950:        CPU_EXECUTE_CALL(i386);
                   18951:        if(m_eip != m_prev_eip) {
                   18952:                idle_ops++;
                   18953:        }
                   18954: #else
                   18955:        CPU_EXECUTE_CALL(CPU_MODEL);
                   18956:        if(m_pc != m_prevpc) {
                   18957:                idle_ops++;
                   18958:        }
                   18959: #endif
                   18960: #ifdef USE_DEBUGGER
                   18961:        // Disallow reentering CPU_EXECUTE() in msdos_syscall()
                   18962:        if(m_int_num >= 0) {
                   18963:                unsigned num = (unsigned)m_int_num;
                   18964:                m_int_num = -1;
                   18965:                msdos_syscall(num);
                   18966:        }
                   18967: #endif
                   18968:        if(++update_ops == UPDATE_OPS) {
                   18969:                update_ops = 0;
                   18970:                hardware_update();
                   18971:        }
                   18972: }
                   18973: 
1.1       root     18974: void hardware_update()
                   18975: {
1.1.1.8   root     18976:        static UINT32 prev_time = 0;
                   18977:        UINT32 cur_time = timeGetTime();
                   18978:        
                   18979:        if(prev_time != cur_time) {
                   18980:                // update pit and raise irq0
                   18981: #ifndef PIT_ALWAYS_RUNNING
                   18982:                if(pit_active)
                   18983: #endif
                   18984:                {
                   18985:                        if(pit_run(0, cur_time)) {
                   18986:                                pic_req(0, 0, 1);
                   18987:                        }
                   18988:                        pit_run(1, cur_time);
                   18989:                        pit_run(2, cur_time);
                   18990:                }
1.1.1.24  root     18991:                
1.1.1.25  root     18992:                // update sio and raise irq4/3
1.1.1.29  root     18993:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     18994:                        sio_update(c);
                   18995:                }
                   18996:                
1.1.1.24  root     18997:                // update keyboard and mouse
1.1.1.14  root     18998:                static UINT32 prev_tick = 0;
                   18999:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     19000:                
1.1.1.14  root     19001:                if(prev_tick != cur_tick) {
                   19002:                        // update keyboard flags
                   19003:                        UINT8 state;
1.1.1.24  root     19004:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   19005:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   19006:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   19007:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   19008:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   19009:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   19010:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   19011:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     19012:                        mem[0x417] = state;
                   19013:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   19014:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   19015:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   19016:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     19017: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   19018: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     19019:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   19020:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   19021:                        mem[0x418] = state;
                   19022:                        
1.1.1.24  root     19023:                        // update console input if needed
1.1.1.34  root     19024:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     19025:                                update_console_input();
                   19026:                        }
1.1.1.57  root     19027:                        if(!(kbd_status & 1)) {
                   19028:                                if(key_buf_data != NULL) {
                   19029: #ifdef USE_SERVICE_THREAD
                   19030:                                        EnterCriticalSection(&key_buf_crit_sect);
                   19031: #endif
                   19032:                                        if(!key_buf_data->empty()) {
                   19033:                                                kbd_data = key_buf_data->read();
                   19034:                                                kbd_status |= 1;
                   19035:                                                key_changed = true;
                   19036:                                        }
                   19037: #ifdef USE_SERVICE_THREAD
                   19038:                                        LeaveCriticalSection(&key_buf_crit_sect);
                   19039: #endif
                   19040:                                }
                   19041:                        }
1.1.1.24  root     19042:                        
1.1.1.57  root     19043:                        // raise irq1 if key is pressed/released or key buffer is not empty
1.1.1.56  root     19044:                        if(!key_changed) {
1.1.1.55  root     19045: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     19046:                                EnterCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     19047: #endif
1.1.1.57  root     19048:                                if(!pcbios_is_key_buffer_empty()) {
                   19049: /*
                   19050:                                        if(!(kbd_status & 1)) {
                   19051:                                                UINT16 head = *(UINT16 *)(mem + 0x41a);
                   19052:                                                UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   19053:                                                if(head != tail) {
                   19054:                                                        int key_char = mem[0x400 + (head++)];
                   19055:                                                        int key_scan = mem[0x400 + (head++)];
                   19056:                                                        kbd_data = key_char ? key_char : key_scan;
                   19057:                                                        kbd_status |= 1;
                   19058:                                                }
                   19059:                                        }
                   19060: */
                   19061:                                        key_changed = true;
                   19062:                                }
1.1.1.55  root     19063: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     19064:                                LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     19065: #endif
1.1.1.56  root     19066:                        }
                   19067:                        if(key_changed) {
1.1.1.8   root     19068:                                pic_req(0, 1, 1);
1.1.1.56  root     19069:                                key_changed = false;
1.1.1.24  root     19070:                        }
                   19071:                        
                   19072:                        // raise irq12 if mouse status is changed
1.1.1.59  root     19073:                        if((mouse.status & 0x1f) && mouse.call_addr_ps2.dw && mouse.enabled_ps2) {
1.1.1.54  root     19074:                                mouse.status_irq = 0; // ???
                   19075:                                mouse.status_irq_alt = 0; // ???
                   19076:                                mouse.status_irq_ps2 = mouse.status & 0x1f;
                   19077:                                mouse.status = 0;
                   19078:                                pic_req(1, 4, 1);
1.1.1.59  root     19079:                        } else if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
1.1.1.43  root     19080:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   19081:                                mouse.status_irq_alt = 0; // ???
1.1.1.54  root     19082:                                mouse.status_irq_ps2 = 0; // ???
1.1.1.24  root     19083:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     19084:                                pic_req(1, 4, 1);
                   19085:                        } else {
                   19086:                                for(int i = 0; i < 8; i++) {
                   19087:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   19088:                                                mouse.status_irq = 0; // ???
                   19089:                                                mouse.status_irq_alt = 0;
1.1.1.54  root     19090:                                                mouse.status_irq_ps2 = 0; // ???
1.1.1.43  root     19091:                                                for(int j = 0; j < 8; j++) {
                   19092:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   19093:                                                                mouse.status_irq_alt |=  (1 << j);
                   19094:                                                                mouse.status_alt     &= ~(1 << j);
                   19095:                                                        }
                   19096:                                                }
                   19097:                                                pic_req(1, 4, 1);
                   19098:                                                break;
                   19099:                                        }
                   19100:                                }
1.1.1.8   root     19101:                        }
1.1.1.24  root     19102:                        
1.1.1.60  root     19103:                        prev_tick = cur_tick;
                   19104:                }
                   19105:                
                   19106:                // update cursor size/position by crtc
                   19107:                if(crtc_changed[10] != 0 || crtc_changed[11] != 0) {
                   19108:                        int size = (int)(crtc_regs[11] & 7) - (int)(crtc_regs[10] & 7) + 1;
                   19109:                        if(!((crtc_regs[10] & 0x20) != 0 || size < 0)) {
                   19110:                                ci_new.bVisible = TRUE;
                   19111:                                ci_new.dwSize = (size + 2) * 100 / (8 + 2);
1.1.1.59  root     19112:                        } else {
1.1.1.60  root     19113:                                ci_new.bVisible = FALSE;
                   19114:                        }
                   19115:                        crtc_changed[10] = crtc_changed[11] = 0;
                   19116:                }
                   19117:                if(crtc_changed[14] != 0 || crtc_changed[15] != 0) {
                   19118:                        if(cursor_moved) {
                   19119:                                pcbios_update_cursor_position();
                   19120:                                cursor_moved = false;
1.1.1.59  root     19121:                        }
1.1.1.60  root     19122:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   19123:                        int position = crtc_regs[14] * 256 + crtc_regs[15];
                   19124:                        int width = *(UINT16 *)(mem + 0x44a);
                   19125:                        COORD co;
                   19126:                        co.X = position % width;
                   19127:                        co.Y = position / width + scr_top;
                   19128:                        SetConsoleCursorPosition(hStdout, co);
1.1.1.59  root     19129:                        
1.1.1.60  root     19130:                        crtc_changed[14] = crtc_changed[15] = 0;
                   19131:                        cursor_moved_by_crtc = true;
                   19132:                }
                   19133:                
                   19134:                // update cursor info
                   19135:                if(!is_cursor_blink_off()) {
                   19136:                        ci_new.bVisible = TRUE;
                   19137:                }
                   19138:                if(!(ci_old.dwSize == ci_new.dwSize && ci_old.bVisible == ci_new.bVisible)) {
                   19139:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   19140:                        SetConsoleCursorInfo(hStdout, &ci_new);
1.1.1.8   root     19141:                }
1.1.1.60  root     19142:                ci_old = ci_new;
1.1.1.24  root     19143:                
1.1.1.19  root     19144:                // update daily timer counter
                   19145:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     19146:                
1.1.1.8   root     19147:                prev_time = cur_time;
1.1       root     19148:        }
                   19149: }
                   19150: 
1.1.1.19  root     19151: // ems
                   19152: 
                   19153: void ems_init()
                   19154: {
                   19155:        memset(ems_handles, 0, sizeof(ems_handles));
                   19156:        memset(ems_pages, 0, sizeof(ems_pages));
                   19157:        free_ems_pages = MAX_EMS_PAGES;
                   19158: }
                   19159: 
                   19160: void ems_finish()
                   19161: {
1.1.1.28  root     19162:        ems_release();
                   19163: }
                   19164: 
                   19165: void ems_release()
                   19166: {
1.1.1.31  root     19167:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     19168:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     19169:                        free(ems_handles[i].buffer);
                   19170:                        ems_handles[i].buffer = NULL;
                   19171:                }
                   19172:        }
                   19173: }
                   19174: 
                   19175: void ems_allocate_pages(int handle, int pages)
                   19176: {
                   19177:        if(pages > 0) {
                   19178:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   19179:        } else {
                   19180:                ems_handles[handle].buffer = NULL;
                   19181:        }
                   19182:        ems_handles[handle].pages = pages;
                   19183:        ems_handles[handle].allocated = true;
                   19184:        free_ems_pages -= pages;
                   19185: }
                   19186: 
                   19187: void ems_reallocate_pages(int handle, int pages)
                   19188: {
                   19189:        if(ems_handles[handle].allocated) {
                   19190:                if(ems_handles[handle].pages != pages) {
                   19191:                        UINT8 *new_buffer = NULL;
                   19192:                        
                   19193:                        if(pages > 0) {
                   19194:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   19195:                        }
1.1.1.32  root     19196:                        if(ems_handles[handle].buffer != NULL) {
                   19197:                                if(new_buffer != NULL) {
1.1.1.19  root     19198:                                        if(pages > ems_handles[handle].pages) {
                   19199:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   19200:                                        } else {
                   19201:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   19202:                                        }
                   19203:                                }
                   19204:                                free(ems_handles[handle].buffer);
                   19205:                                ems_handles[handle].buffer = NULL;
                   19206:                        }
                   19207:                        free_ems_pages += ems_handles[handle].pages;
                   19208:                        
                   19209:                        ems_handles[handle].buffer = new_buffer;
                   19210:                        ems_handles[handle].pages = pages;
                   19211:                        free_ems_pages -= pages;
                   19212:                }
                   19213:        } else {
                   19214:                ems_allocate_pages(handle, pages);
                   19215:        }
                   19216: }
                   19217: 
                   19218: void ems_release_pages(int handle)
                   19219: {
                   19220:        if(ems_handles[handle].allocated) {
1.1.1.32  root     19221:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     19222:                        free(ems_handles[handle].buffer);
                   19223:                        ems_handles[handle].buffer = NULL;
                   19224:                }
                   19225:                free_ems_pages += ems_handles[handle].pages;
                   19226:                ems_handles[handle].allocated = false;
                   19227:        }
                   19228: }
                   19229: 
                   19230: void ems_map_page(int physical, int handle, int logical)
                   19231: {
                   19232:        if(ems_pages[physical].mapped) {
                   19233:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   19234:                        return;
                   19235:                }
                   19236:                ems_unmap_page(physical);
                   19237:        }
1.1.1.32  root     19238:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     19239:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   19240:        }
                   19241:        ems_pages[physical].handle = handle;
                   19242:        ems_pages[physical].page = logical;
                   19243:        ems_pages[physical].mapped = true;
                   19244: }
                   19245: 
                   19246: void ems_unmap_page(int physical)
                   19247: {
                   19248:        if(ems_pages[physical].mapped) {
                   19249:                int handle = ems_pages[physical].handle;
                   19250:                int logical = ems_pages[physical].page;
                   19251:                
1.1.1.32  root     19252:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     19253:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   19254:                }
                   19255:                ems_pages[physical].mapped = false;
                   19256:        }
                   19257: }
                   19258: 
1.1.1.25  root     19259: // dma
1.1       root     19260: 
1.1.1.25  root     19261: void dma_init()
1.1       root     19262: {
1.1.1.26  root     19263:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     19264:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     19265: //             for(int ch = 0; ch < 4; ch++) {
                   19266: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   19267: //             }
1.1.1.25  root     19268:                dma_reset(c);
                   19269:        }
1.1       root     19270: }
                   19271: 
1.1.1.25  root     19272: void dma_reset(int c)
1.1       root     19273: {
1.1.1.25  root     19274:        dma[c].low_high = false;
                   19275:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   19276:        dma[c].mask = 0xff;
                   19277: }
                   19278: 
                   19279: void dma_write(int c, UINT32 addr, UINT8 data)
                   19280: {
                   19281:        int ch = (addr >> 1) & 3;
                   19282:        UINT8 bit = 1 << (data & 3);
                   19283:        
                   19284:        switch(addr & 0x0f) {
                   19285:        case 0x00: case 0x02: case 0x04: case 0x06:
                   19286:                if(dma[c].low_high) {
                   19287:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     19288:                } else {
1.1.1.25  root     19289:                        dma[c].ch[ch].bareg.b.l = data;
                   19290:                }
                   19291:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   19292:                dma[c].low_high = !dma[c].low_high;
                   19293:                break;
                   19294:        case 0x01: case 0x03: case 0x05: case 0x07:
                   19295:                if(dma[c].low_high) {
                   19296:                        dma[c].ch[ch].bcreg.b.h = data;
                   19297:                } else {
                   19298:                        dma[c].ch[ch].bcreg.b.l = data;
                   19299:                }
                   19300:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   19301:                dma[c].low_high = !dma[c].low_high;
                   19302:                break;
                   19303:        case 0x08:
                   19304:                // command register
                   19305:                dma[c].cmd = data;
                   19306:                break;
                   19307:        case 0x09:
                   19308:                // dma[c].request register
                   19309:                if(data & 4) {
                   19310:                        if(!(dma[c].req & bit)) {
                   19311:                                dma[c].req |= bit;
                   19312: //                             dma_run(c, ch);
                   19313:                        }
                   19314:                } else {
                   19315:                        dma[c].req &= ~bit;
                   19316:                }
                   19317:                break;
                   19318:        case 0x0a:
                   19319:                // single mask register
                   19320:                if(data & 4) {
                   19321:                        dma[c].mask |= bit;
                   19322:                } else {
                   19323:                        dma[c].mask &= ~bit;
                   19324:                }
                   19325:                break;
                   19326:        case 0x0b:
                   19327:                // mode register
                   19328:                dma[c].ch[data & 3].mode = data;
                   19329:                break;
                   19330:        case 0x0c:
                   19331:                dma[c].low_high = false;
                   19332:                break;
                   19333:        case 0x0d:
                   19334:                // clear master
                   19335:                dma_reset(c);
                   19336:                break;
                   19337:        case 0x0e:
                   19338:                // clear mask register
                   19339:                dma[c].mask = 0;
                   19340:                break;
                   19341:        case 0x0f:
                   19342:                // all mask register
                   19343:                dma[c].mask = data & 0x0f;
                   19344:                break;
                   19345:        }
                   19346: }
                   19347: 
                   19348: UINT8 dma_read(int c, UINT32 addr)
                   19349: {
                   19350:        int ch = (addr >> 1) & 3;
                   19351:        UINT8 val = 0xff;
                   19352:        
                   19353:        switch(addr & 0x0f) {
                   19354:        case 0x00: case 0x02: case 0x04: case 0x06:
                   19355:                if(dma[c].low_high) {
                   19356:                        val = dma[c].ch[ch].areg.b.h;
                   19357:                } else {
                   19358:                        val = dma[c].ch[ch].areg.b.l;
                   19359:                }
                   19360:                dma[c].low_high = !dma[c].low_high;
                   19361:                return(val);
                   19362:        case 0x01: case 0x03: case 0x05: case 0x07:
                   19363:                if(dma[c].low_high) {
                   19364:                        val = dma[c].ch[ch].creg.b.h;
                   19365:                } else {
                   19366:                        val = dma[c].ch[ch].creg.b.l;
                   19367:                }
                   19368:                dma[c].low_high = !dma[c].low_high;
                   19369:                return(val);
                   19370:        case 0x08:
                   19371:                // status register
                   19372:                val = (dma[c].req << 4) | dma[c].tc;
                   19373:                dma[c].tc = 0;
                   19374:                return(val);
                   19375:        case 0x0d:
1.1.1.26  root     19376:                // temporary register (intel 82374 does not support)
1.1.1.25  root     19377:                return(dma[c].tmp & 0xff);
1.1.1.26  root     19378:        case 0x0f:
                   19379:                // mask register (intel 82374 does support)
                   19380:                return(dma[c].mask);
1.1.1.25  root     19381:        }
                   19382:        return(0xff);
                   19383: }
                   19384: 
                   19385: void dma_page_write(int c, int ch, UINT8 data)
                   19386: {
                   19387:        dma[c].ch[ch].pagereg = data;
                   19388: }
                   19389: 
                   19390: UINT8 dma_page_read(int c, int ch)
                   19391: {
                   19392:        return(dma[c].ch[ch].pagereg);
                   19393: }
                   19394: 
                   19395: void dma_run(int c, int ch)
                   19396: {
                   19397:        UINT8 bit = 1 << ch;
                   19398:        
                   19399:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   19400:                // execute dma
                   19401:                while(dma[c].req & bit) {
                   19402:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   19403:                                // memory -> memory
                   19404:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   19405:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   19406:                                
                   19407:                                if(c == 0) {
                   19408:                                        dma[c].tmp = read_byte(saddr);
                   19409:                                        write_byte(daddr, dma[c].tmp);
                   19410:                                } else {
                   19411:                                        dma[c].tmp = read_word(saddr << 1);
                   19412:                                        write_word(daddr << 1, dma[c].tmp);
                   19413:                                }
                   19414:                                if(!(dma[c].cmd & 0x02)) {
                   19415:                                        if(dma[c].ch[0].mode & 0x20) {
                   19416:                                                dma[c].ch[0].areg.w--;
                   19417:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   19418:                                                        dma[c].ch[0].pagereg--;
                   19419:                                                }
                   19420:                                        } else {
                   19421:                                                dma[c].ch[0].areg.w++;
                   19422:                                                if(dma[c].ch[0].areg.w == 0) {
                   19423:                                                        dma[c].ch[0].pagereg++;
                   19424:                                                }
                   19425:                                        }
                   19426:                                }
                   19427:                                if(dma[c].ch[1].mode & 0x20) {
                   19428:                                        dma[c].ch[1].areg.w--;
                   19429:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   19430:                                                dma[c].ch[1].pagereg--;
                   19431:                                        }
                   19432:                                } else {
                   19433:                                        dma[c].ch[1].areg.w++;
                   19434:                                        if(dma[c].ch[1].areg.w == 0) {
                   19435:                                                dma[c].ch[1].pagereg++;
                   19436:                                        }
                   19437:                                }
                   19438:                                
                   19439:                                // check dma condition
                   19440:                                if(dma[c].ch[0].creg.w-- == 0) {
                   19441:                                        if(dma[c].ch[0].mode & 0x10) {
                   19442:                                                // self initialize
                   19443:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   19444:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   19445:                                        } else {
                   19446: //                                             dma[c].mask |= bit;
                   19447:                                        }
                   19448:                                }
                   19449:                                if(dma[c].ch[1].creg.w-- == 0) {
                   19450:                                        // terminal count
                   19451:                                        if(dma[c].ch[1].mode & 0x10) {
                   19452:                                                // self initialize
                   19453:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   19454:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   19455:                                        } else {
                   19456:                                                dma[c].mask |= bit;
                   19457:                                        }
                   19458:                                        dma[c].req &= ~bit;
                   19459:                                        dma[c].tc |= bit;
                   19460:                                }
                   19461:                        } else {
                   19462:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   19463:                                
                   19464:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   19465:                                        // verify
                   19466:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   19467:                                        // io -> memory
                   19468:                                        if(c == 0) {
                   19469:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   19470:                                                write_byte(addr, dma[c].tmp);
                   19471:                                        } else {
                   19472:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   19473:                                                write_word(addr << 1, dma[c].tmp);
                   19474:                                        }
                   19475:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   19476:                                        // memory -> io
                   19477:                                        if(c == 0) {
                   19478:                                                dma[c].tmp = read_byte(addr);
                   19479:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   19480:                                        } else {
                   19481:                                                dma[c].tmp = read_word(addr << 1);
                   19482:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   19483:                                        }
                   19484:                                }
                   19485:                                if(dma[c].ch[ch].mode & 0x20) {
                   19486:                                        dma[c].ch[ch].areg.w--;
                   19487:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   19488:                                                dma[c].ch[ch].pagereg--;
                   19489:                                        }
                   19490:                                } else {
                   19491:                                        dma[c].ch[ch].areg.w++;
                   19492:                                        if(dma[c].ch[ch].areg.w == 0) {
                   19493:                                                dma[c].ch[ch].pagereg++;
                   19494:                                        }
                   19495:                                }
                   19496:                                
                   19497:                                // check dma condition
                   19498:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   19499:                                        // terminal count
                   19500:                                        if(dma[c].ch[ch].mode & 0x10) {
                   19501:                                                // self initialize
                   19502:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   19503:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   19504:                                        } else {
                   19505:                                                dma[c].mask |= bit;
                   19506:                                        }
                   19507:                                        dma[c].req &= ~bit;
                   19508:                                        dma[c].tc |= bit;
                   19509:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   19510:                                        // single mode
                   19511:                                        break;
                   19512:                                }
                   19513:                        }
                   19514:                }
                   19515:        }
                   19516: }
                   19517: 
                   19518: // pic
                   19519: 
                   19520: void pic_init()
                   19521: {
                   19522:        memset(pic, 0, sizeof(pic));
                   19523:        pic[0].imr = pic[1].imr = 0xff;
                   19524:        
                   19525:        // from bochs bios
                   19526:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   19527:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   19528:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   19529:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   19530:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   19531:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   19532:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   19533:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   19534:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   19535: }
                   19536: 
                   19537: void pic_write(int c, UINT32 addr, UINT8 data)
                   19538: {
                   19539:        if(addr & 1) {
                   19540:                if(pic[c].icw2_r) {
                   19541:                        // icw2
                   19542:                        pic[c].icw2 = data;
                   19543:                        pic[c].icw2_r = 0;
                   19544:                } else if(pic[c].icw3_r) {
                   19545:                        // icw3
                   19546:                        pic[c].icw3 = data;
                   19547:                        pic[c].icw3_r = 0;
                   19548:                } else if(pic[c].icw4_r) {
                   19549:                        // icw4
                   19550:                        pic[c].icw4 = data;
                   19551:                        pic[c].icw4_r = 0;
                   19552:                } else {
                   19553:                        // ocw1
1.1       root     19554:                        pic[c].imr = data;
                   19555:                }
                   19556:        } else {
                   19557:                if(data & 0x10) {
                   19558:                        // icw1
                   19559:                        pic[c].icw1 = data;
                   19560:                        pic[c].icw2_r = 1;
                   19561:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   19562:                        pic[c].icw4_r = data & 1;
                   19563:                        pic[c].irr = 0;
                   19564:                        pic[c].isr = 0;
                   19565:                        pic[c].imr = 0;
                   19566:                        pic[c].prio = 0;
                   19567:                        if(!(pic[c].icw1 & 1)) {
                   19568:                                pic[c].icw4 = 0;
                   19569:                        }
                   19570:                        pic[c].ocw3 = 0;
                   19571:                } else if(data & 8) {
                   19572:                        // ocw3
                   19573:                        if(!(data & 2)) {
                   19574:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   19575:                        }
                   19576:                        if(!(data & 0x40)) {
                   19577:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   19578:                        }
                   19579:                        pic[c].ocw3 = data;
                   19580:                } else {
                   19581:                        // ocw2
                   19582:                        int level = 0;
                   19583:                        if(data & 0x40) {
                   19584:                                level = data & 7;
                   19585:                        } else {
                   19586:                                if(!pic[c].isr) {
                   19587:                                        return;
                   19588:                                }
                   19589:                                level = pic[c].prio;
                   19590:                                while(!(pic[c].isr & (1 << level))) {
                   19591:                                        level = (level + 1) & 7;
                   19592:                                }
                   19593:                        }
                   19594:                        if(data & 0x80) {
                   19595:                                pic[c].prio = (level + 1) & 7;
                   19596:                        }
                   19597:                        if(data & 0x20) {
                   19598:                                pic[c].isr &= ~(1 << level);
                   19599:                        }
                   19600:                }
                   19601:        }
                   19602:        pic_update();
                   19603: }
                   19604: 
                   19605: UINT8 pic_read(int c, UINT32 addr)
                   19606: {
                   19607:        if(addr & 1) {
                   19608:                return(pic[c].imr);
                   19609:        } else {
                   19610:                // polling mode is not supported...
                   19611:                //if(pic[c].ocw3 & 4) {
                   19612:                //      return ???;
                   19613:                //}
                   19614:                if(pic[c].ocw3 & 1) {
                   19615:                        return(pic[c].isr);
                   19616:                } else {
                   19617:                        return(pic[c].irr);
                   19618:                }
                   19619:        }
                   19620: }
                   19621: 
                   19622: void pic_req(int c, int level, int signal)
                   19623: {
                   19624:        if(signal) {
                   19625:                pic[c].irr |= (1 << level);
                   19626:        } else {
                   19627:                pic[c].irr &= ~(1 << level);
                   19628:        }
                   19629:        pic_update();
                   19630: }
                   19631: 
                   19632: int pic_ack()
                   19633: {
                   19634:        // ack (INTA=L)
                   19635:        pic[pic_req_chip].isr |= pic_req_bit;
                   19636:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   19637:        if(pic_req_chip > 0) {
                   19638:                // update isr and irr of master
                   19639:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   19640:                pic[pic_req_chip - 1].isr |= slave;
                   19641:                pic[pic_req_chip - 1].irr &= ~slave;
                   19642:        }
                   19643:        //if(pic[pic_req_chip].icw4 & 1) {
                   19644:                // 8086 mode
                   19645:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   19646:        //} else {
                   19647:        //      // 8080 mode
                   19648:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   19649:        //      if(pic[pic_req_chip].icw1 & 4) {
                   19650:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   19651:        //      } else {
                   19652:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   19653:        //      }
                   19654:        //      vector = 0xcd | (addr << 8);
                   19655:        //}
                   19656:        if(pic[pic_req_chip].icw4 & 2) {
                   19657:                // auto eoi
                   19658:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   19659:        }
                   19660:        return(vector);
                   19661: }
                   19662: 
                   19663: void pic_update()
                   19664: {
                   19665:        for(int c = 0; c < 2; c++) {
                   19666:                UINT8 irr = pic[c].irr;
                   19667:                if(c + 1 < 2) {
                   19668:                        // this is master
                   19669:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   19670:                                // request from slave
                   19671:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   19672:                        }
                   19673:                }
                   19674:                irr &= (~pic[c].imr);
                   19675:                if(!irr) {
                   19676:                        break;
                   19677:                }
                   19678:                if(!(pic[c].ocw3 & 0x20)) {
                   19679:                        irr |= pic[c].isr;
                   19680:                }
                   19681:                int level = pic[c].prio;
                   19682:                UINT8 bit = 1 << level;
                   19683:                while(!(irr & bit)) {
                   19684:                        level = (level + 1) & 7;
                   19685:                        bit = 1 << level;
                   19686:                }
                   19687:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   19688:                        // check slave
                   19689:                        continue;
                   19690:                }
                   19691:                if(pic[c].isr & bit) {
                   19692:                        break;
                   19693:                }
                   19694:                // interrupt request
                   19695:                pic_req_chip = c;
                   19696:                pic_req_level = level;
                   19697:                pic_req_bit = bit;
1.1.1.3   root     19698:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     19699:                return;
                   19700:        }
1.1.1.3   root     19701:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     19702: }
1.1       root     19703: 
1.1.1.25  root     19704: // pio
                   19705: 
                   19706: void pio_init()
                   19707: {
1.1.1.38  root     19708: //     bool conv_mode = (GetConsoleCP() == 932);
                   19709:        
1.1.1.26  root     19710:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     19711:        
1.1.1.25  root     19712:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     19713:                pio[c].stat = 0xdf;
1.1.1.25  root     19714:                pio[c].ctrl = 0x0c;
1.1.1.38  root     19715: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     19716:        }
                   19717: }
                   19718: 
1.1.1.37  root     19719: void pio_finish()
                   19720: {
                   19721:        pio_release();
                   19722: }
                   19723: 
                   19724: void pio_release()
                   19725: {
                   19726:        for(int c = 0; c < 2; c++) {
                   19727:                if(pio[c].fp != NULL) {
1.1.1.38  root     19728:                        if(pio[c].jis_mode) {
                   19729:                                fputc(0x1c, pio[c].fp);
                   19730:                                fputc(0x2e, pio[c].fp);
                   19731:                        }
1.1.1.37  root     19732:                        fclose(pio[c].fp);
                   19733:                        pio[c].fp = NULL;
                   19734:                }
                   19735:        }
                   19736: }
                   19737: 
1.1.1.25  root     19738: void pio_write(int c, UINT32 addr, UINT8 data)
                   19739: {
                   19740:        switch(addr & 3) {
                   19741:        case 0:
                   19742:                pio[c].data = data;
                   19743:                break;
                   19744:        case 2:
1.1.1.37  root     19745:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   19746:                        // strobe H -> L
                   19747:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   19748:                                // auto feed
                   19749:                                printer_out(c, 0x0d);
                   19750:                                printer_out(c, 0x0a);
                   19751:                        } else {
                   19752:                                printer_out(c, pio[c].data);
                   19753:                        }
                   19754:                        pio[c].stat &= ~0x40; // set ack
                   19755:                }
1.1.1.25  root     19756:                pio[c].ctrl = data;
                   19757:                break;
                   19758:        }
                   19759: }
                   19760: 
                   19761: UINT8 pio_read(int c, UINT32 addr)
                   19762: {
                   19763:        switch(addr & 3) {
                   19764:        case 0:
1.1.1.37  root     19765:                if(pio[c].ctrl & 0x20) {
                   19766:                        // input mode
                   19767:                        return(0xff);
                   19768:                }
1.1.1.25  root     19769:                return(pio[c].data);
                   19770:        case 1:
1.1.1.37  root     19771:                {
                   19772:                        UINT8 stat = pio[c].stat;
                   19773:                        pio[c].stat |= 0x40; // clear ack
                   19774:                        return(stat);
                   19775:                }
1.1.1.25  root     19776:        case 2:
                   19777:                return(pio[c].ctrl);
                   19778:        }
                   19779:        return(0xff);
                   19780: }
                   19781: 
1.1.1.37  root     19782: void printer_out(int c, UINT8 data)
                   19783: {
                   19784:        SYSTEMTIME time;
1.1.1.38  root     19785:        bool jis_mode = false;
1.1.1.37  root     19786:        
                   19787:        GetLocalTime(&time);
                   19788:        
                   19789:        if(pio[c].fp != NULL) {
                   19790:                // if at least 1000ms passed from last written, close the current file
                   19791:                FILETIME ftime1;
                   19792:                FILETIME ftime2;
                   19793:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   19794:                SystemTimeToFileTime(&time, &ftime2);
                   19795:                INT64 *time1 = (INT64 *)&ftime1;
                   19796:                INT64 *time2 = (INT64 *)&ftime2;
                   19797:                INT64 msec = (*time2 - *time1) / 10000;
                   19798:                
                   19799:                if(msec >= 1000) {
1.1.1.38  root     19800:                        if(pio[c].jis_mode) {
                   19801:                                fputc(0x1c, pio[c].fp);
                   19802:                                fputc(0x2e, pio[c].fp);
                   19803:                                jis_mode = true;
                   19804:                        }
1.1.1.37  root     19805:                        fclose(pio[c].fp);
                   19806:                        pio[c].fp = NULL;
                   19807:                }
                   19808:        }
                   19809:        if(pio[c].fp == NULL) {
                   19810:                // create a new file in the temp folder
                   19811:                char file_name[MAX_PATH];
                   19812:                
                   19813:                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     19814:                if(GetTempPathA(MAX_PATH, pio[c].path)) {
1.1.1.37  root     19815:                        strcat(pio[c].path, file_name);
                   19816:                } else {
                   19817:                        strcpy(pio[c].path, file_name);
                   19818:                }
1.1.1.38  root     19819:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     19820:        }
                   19821:        if(pio[c].fp != NULL) {
1.1.1.38  root     19822:                if(jis_mode) {
                   19823:                        fputc(0x1c, pio[c].fp);
                   19824:                        fputc(0x26, pio[c].fp);
                   19825:                }
1.1.1.37  root     19826:                fputc(data, pio[c].fp);
1.1.1.38  root     19827:                
                   19828:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   19829:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   19830:                        UINT8 buffer[4];
                   19831:                        fseek(pio[c].fp, 0, SEEK_SET);
                   19832:                        fread(buffer, 4, 1, pio[c].fp);
                   19833:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   19834:                                fclose(pio[c].fp);
                   19835:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   19836:                        }
                   19837:                }
1.1.1.37  root     19838:                pio[c].time = time;
                   19839:        }
                   19840: }
                   19841: 
1.1       root     19842: // pit
                   19843: 
1.1.1.22  root     19844: #define PIT_FREQ 1193182ULL
1.1       root     19845: #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)
                   19846: 
                   19847: void pit_init()
                   19848: {
1.1.1.8   root     19849:        memset(pit, 0, sizeof(pit));
1.1       root     19850:        for(int ch = 0; ch < 3; ch++) {
                   19851:                pit[ch].count = 0x10000;
                   19852:                pit[ch].ctrl_reg = 0x34;
                   19853:                pit[ch].mode = 3;
                   19854:        }
                   19855:        
                   19856:        // from bochs bios
                   19857:        pit_write(3, 0x34);
                   19858:        pit_write(0, 0x00);
                   19859:        pit_write(0, 0x00);
                   19860: }
                   19861: 
                   19862: void pit_write(int ch, UINT8 val)
                   19863: {
1.1.1.8   root     19864: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19865:        if(!pit_active) {
                   19866:                pit_active = 1;
                   19867:                pit_init();
                   19868:        }
1.1.1.8   root     19869: #endif
1.1       root     19870:        switch(ch) {
                   19871:        case 0:
                   19872:        case 1:
                   19873:        case 2:
                   19874:                // write count register
                   19875:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19876:                        if(pit[ch].ctrl_reg & 0x10) {
                   19877:                                pit[ch].low_write = 1;
                   19878:                        }
                   19879:                        if(pit[ch].ctrl_reg & 0x20) {
                   19880:                                pit[ch].high_write = 1;
                   19881:                        }
                   19882:                }
                   19883:                if(pit[ch].low_write) {
                   19884:                        pit[ch].count_reg = val;
                   19885:                        pit[ch].low_write = 0;
                   19886:                } else if(pit[ch].high_write) {
                   19887:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19888:                                pit[ch].count_reg = val << 8;
                   19889:                        } else {
                   19890:                                pit[ch].count_reg |= val << 8;
                   19891:                        }
                   19892:                        pit[ch].high_write = 0;
                   19893:                }
                   19894:                // start count
1.1.1.8   root     19895:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19896:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   19897:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   19898:                                pit[ch].prev_time = timeGetTime();
                   19899:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19900:                        }
                   19901:                }
                   19902:                break;
                   19903:        case 3: // ctrl reg
                   19904:                if((val & 0xc0) == 0xc0) {
                   19905:                        // i8254 read-back command
                   19906:                        for(ch = 0; ch < 3; ch++) {
                   19907:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   19908:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   19909:                                        pit[ch].status_latched = 1;
                   19910:                                }
                   19911:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   19912:                                        pit_latch_count(ch);
                   19913:                                }
                   19914:                        }
                   19915:                        break;
                   19916:                }
                   19917:                ch = (val >> 6) & 3;
                   19918:                if(val & 0x30) {
1.1.1.35  root     19919:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     19920:                        pit[ch].mode = modes[(val >> 1) & 7];
                   19921:                        pit[ch].count_latched = 0;
                   19922:                        pit[ch].low_read = pit[ch].high_read = 0;
                   19923:                        pit[ch].low_write = pit[ch].high_write = 0;
                   19924:                        pit[ch].ctrl_reg = val;
                   19925:                        // stop count
1.1.1.8   root     19926:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     19927:                        pit[ch].count_reg = 0;
                   19928:                } else if(!pit[ch].count_latched) {
                   19929:                        pit_latch_count(ch);
                   19930:                }
                   19931:                break;
                   19932:        }
                   19933: }
                   19934: 
                   19935: UINT8 pit_read(int ch)
                   19936: {
1.1.1.8   root     19937: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19938:        if(!pit_active) {
                   19939:                pit_active = 1;
                   19940:                pit_init();
                   19941:        }
1.1.1.8   root     19942: #endif
1.1       root     19943:        switch(ch) {
                   19944:        case 0:
                   19945:        case 1:
                   19946:        case 2:
                   19947:                if(pit[ch].status_latched) {
                   19948:                        pit[ch].status_latched = 0;
                   19949:                        return(pit[ch].status);
                   19950:                }
                   19951:                // if not latched, through current count
                   19952:                if(!pit[ch].count_latched) {
                   19953:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   19954:                                pit_latch_count(ch);
                   19955:                        }
                   19956:                }
                   19957:                // return latched count
                   19958:                if(pit[ch].low_read) {
                   19959:                        pit[ch].low_read = 0;
                   19960:                        if(!pit[ch].high_read) {
                   19961:                                pit[ch].count_latched = 0;
                   19962:                        }
                   19963:                        return(pit[ch].latch & 0xff);
                   19964:                } else if(pit[ch].high_read) {
                   19965:                        pit[ch].high_read = 0;
                   19966:                        pit[ch].count_latched = 0;
                   19967:                        return((pit[ch].latch >> 8) & 0xff);
                   19968:                }
                   19969:        }
                   19970:        return(0xff);
                   19971: }
                   19972: 
1.1.1.8   root     19973: int pit_run(int ch, UINT32 cur_time)
1.1       root     19974: {
1.1.1.8   root     19975:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     19976:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     19977:                pit[ch].prev_time = pit[ch].expired_time;
                   19978:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   19979:                if(cur_time >= pit[ch].expired_time) {
                   19980:                        pit[ch].prev_time = cur_time;
                   19981:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19982:                }
1.1.1.8   root     19983:                return(1);
1.1       root     19984:        }
1.1.1.8   root     19985:        return(0);
1.1       root     19986: }
                   19987: 
                   19988: void pit_latch_count(int ch)
                   19989: {
1.1.1.8   root     19990:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     19991:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     19992:                pit_run(ch, cur_time);
                   19993:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     19994:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19995:                
                   19996:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   19997:                        // decrement counter in 1msec period
                   19998:                        if(pit[ch].next_latch == 0) {
                   19999:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   20000:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   20001:                        }
                   20002:                        if(pit[ch].latch > pit[ch].next_latch) {
                   20003:                                pit[ch].latch--;
                   20004:                        }
                   20005:                } else {
                   20006:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   20007:                        pit[ch].next_latch = 0;
                   20008:                }
1.1.1.8   root     20009:        } else {
                   20010:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     20011:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     20012:        }
                   20013:        pit[ch].count_latched = 1;
                   20014:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   20015:                // lower byte
                   20016:                pit[ch].low_read = 1;
                   20017:                pit[ch].high_read = 0;
                   20018:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   20019:                // upper byte
                   20020:                pit[ch].low_read = 0;
                   20021:                pit[ch].high_read = 1;
                   20022:        } else {
                   20023:                // lower -> upper
1.1.1.14  root     20024:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     20025:        }
                   20026: }
                   20027: 
1.1.1.8   root     20028: int pit_get_expired_time(int ch)
1.1       root     20029: {
1.1.1.22  root     20030:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   20031:        UINT64 val = pit[ch].accum >> 10;
                   20032:        pit[ch].accum -= val << 10;
                   20033:        return((val != 0) ? val : 1);
1.1.1.8   root     20034: }
                   20035: 
1.1.1.25  root     20036: // sio
                   20037: 
                   20038: void sio_init()
                   20039: {
1.1.1.26  root     20040:        memset(sio, 0, sizeof(sio));
                   20041:        memset(sio_mt, 0, sizeof(sio_mt));
                   20042:        
1.1.1.29  root     20043:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     20044:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   20045:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   20046:                
                   20047:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   20048:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     20049:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   20050:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     20051:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   20052:                sio[c].irq_identify = 0x01;     // no pending irq
                   20053:                
                   20054:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   20055:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   20056:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   20057:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   20058:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   20059:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   20060:                
1.1.1.26  root     20061:                if(sio_port_number[c] != 0) {
1.1.1.25  root     20062:                        sio[c].channel = c;
                   20063:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   20064:                }
                   20065:        }
                   20066: }
                   20067: 
                   20068: void sio_finish()
                   20069: {
1.1.1.29  root     20070:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     20071:                if(sio_mt[c].hThread != NULL) {
                   20072:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   20073:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     20074:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     20075:                }
                   20076:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   20077:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   20078:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   20079:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   20080:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   20081:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     20082:        }
                   20083:        sio_release();
                   20084: }
                   20085: 
                   20086: void sio_release()
                   20087: {
1.1.1.29  root     20088:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     20089:                // sio_thread() may access the resources :-(
1.1.1.32  root     20090:                bool running = (sio_mt[c].hThread != NULL);
                   20091:                
                   20092:                if(running) {
                   20093:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   20094:                }
                   20095:                if(sio[c].send_buffer != NULL) {
                   20096:                        sio[c].send_buffer->release();
                   20097:                        delete sio[c].send_buffer;
                   20098:                        sio[c].send_buffer = NULL;
                   20099:                }
                   20100:                if(running) {
                   20101:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   20102:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   20103:                }
                   20104:                if(sio[c].recv_buffer != NULL) {
                   20105:                        sio[c].recv_buffer->release();
                   20106:                        delete sio[c].recv_buffer;
                   20107:                        sio[c].recv_buffer = NULL;
                   20108:                }
                   20109:                if(running) {
                   20110:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     20111:                }
1.1.1.25  root     20112:        }
                   20113: }
                   20114: 
                   20115: void sio_write(int c, UINT32 addr, UINT8 data)
                   20116: {
                   20117:        switch(addr & 7) {
                   20118:        case 0:
                   20119:                if(sio[c].selector & 0x80) {
                   20120:                        if(sio[c].divisor.b.l != data) {
                   20121:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   20122:                                sio[c].divisor.b.l = data;
                   20123:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   20124:                        }
                   20125:                } else {
                   20126:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     20127:                        if(sio[c].send_buffer != NULL) {
                   20128:                                sio[c].send_buffer->write(data);
                   20129:                        }
1.1.1.25  root     20130:                        // transmitter holding/shift registers are not empty
                   20131:                        sio[c].line_stat_buf &= ~0x60;
                   20132:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   20133:                        
                   20134:                        if(sio[c].irq_enable & 0x02) {
                   20135:                                sio_update_irq(c);
                   20136:                        }
                   20137:                }
                   20138:                break;
                   20139:        case 1:
                   20140:                if(sio[c].selector & 0x80) {
                   20141:                        if(sio[c].divisor.b.h != data) {
                   20142:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   20143:                                sio[c].divisor.b.h = data;
                   20144:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   20145:                        }
                   20146:                } else {
                   20147:                        if(sio[c].irq_enable != data) {
                   20148:                                sio[c].irq_enable = data;
                   20149:                                sio_update_irq(c);
                   20150:                        }
                   20151:                }
                   20152:                break;
                   20153:        case 3:
                   20154:                {
                   20155:                        UINT8 line_ctrl = data & 0x3f;
                   20156:                        bool set_brk = ((data & 0x40) != 0);
                   20157:                        
                   20158:                        if(sio[c].line_ctrl != line_ctrl) {
                   20159:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   20160:                                sio[c].line_ctrl = line_ctrl;
                   20161:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   20162:                        }
                   20163:                        if(sio[c].set_brk != set_brk) {
                   20164:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   20165:                                sio[c].set_brk = set_brk;
                   20166:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   20167:                        }
                   20168:                }
                   20169:                sio[c].selector = data;
                   20170:                break;
                   20171:        case 4:
                   20172:                {
                   20173:                        bool set_dtr = ((data & 0x01) != 0);
                   20174:                        bool set_rts = ((data & 0x02) != 0);
                   20175:                        
                   20176:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     20177: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     20178:                                sio[c].set_dtr = set_dtr;
                   20179:                                sio[c].set_rts = set_rts;
1.1.1.26  root     20180: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   20181:                                
                   20182:                                bool state_changed = false;
                   20183:                                
                   20184:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   20185:                                if(set_dtr) {
                   20186:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   20187:                                } else {
                   20188:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   20189:                                }
                   20190:                                if(set_rts) {
                   20191:                                        sio[c].modem_stat |= 0x10;      // cts on
                   20192:                                } else {
                   20193:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   20194:                                }
                   20195:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   20196:                                        if(!(sio[c].modem_stat & 0x02)) {
                   20197:                                                if(sio[c].irq_enable & 0x08) {
                   20198:                                                        state_changed = true;
                   20199:                                                }
                   20200:                                                sio[c].modem_stat |= 0x02;
                   20201:                                        }
                   20202:                                }
                   20203:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   20204:                                        if(!(sio[c].modem_stat & 0x01)) {
                   20205:                                                if(sio[c].irq_enable & 0x08) {
                   20206:                                                        state_changed = true;
                   20207:                                                }
                   20208:                                                sio[c].modem_stat |= 0x01;
                   20209:                                        }
                   20210:                                }
                   20211:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   20212:                                
                   20213:                                if(state_changed) {
                   20214:                                        sio_update_irq(c);
                   20215:                                }
1.1.1.25  root     20216:                        }
                   20217:                }
                   20218:                sio[c].modem_ctrl = data;
                   20219:                break;
                   20220:        case 7:
                   20221:                sio[c].scratch = data;
                   20222:                break;
                   20223:        }
                   20224: }
                   20225: 
                   20226: UINT8 sio_read(int c, UINT32 addr)
                   20227: {
                   20228:        switch(addr & 7) {
                   20229:        case 0:
                   20230:                if(sio[c].selector & 0x80) {
                   20231:                        return(sio[c].divisor.b.l);
                   20232:                } else {
                   20233:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     20234:                        UINT8 data = 0;
                   20235:                        if(sio[c].recv_buffer != NULL) {
                   20236:                                data = sio[c].recv_buffer->read();
                   20237:                        }
1.1.1.25  root     20238:                        // data is not ready
                   20239:                        sio[c].line_stat_buf &= ~0x01;
                   20240:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   20241:                        
                   20242:                        if(sio[c].irq_enable & 0x01) {
                   20243:                                sio_update_irq(c);
                   20244:                        }
                   20245:                        return(data);
                   20246:                }
                   20247:        case 1:
                   20248:                if(sio[c].selector & 0x80) {
                   20249:                        return(sio[c].divisor.b.h);
                   20250:                } else {
                   20251:                        return(sio[c].irq_enable);
                   20252:                }
                   20253:        case 2:
                   20254:                return(sio[c].irq_identify);
                   20255:        case 3:
                   20256:                return(sio[c].selector);
                   20257:        case 4:
                   20258:                return(sio[c].modem_ctrl);
                   20259:        case 5:
                   20260:                {
                   20261:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   20262:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   20263:                        sio[c].line_stat_err = 0x00;
                   20264:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   20265:                        
                   20266:                        bool state_changed = false;
                   20267:                        
                   20268:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   20269:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     20270:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     20271:                                        // transmitter holding register will be empty first
                   20272:                                        if(sio[c].irq_enable & 0x02) {
                   20273:                                                state_changed = true;
                   20274:                                        }
                   20275:                                        sio[c].line_stat_buf |= 0x20;
                   20276:                                }
                   20277:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   20278:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   20279:                                // transmitter shift register will be empty later
                   20280:                                sio[c].line_stat_buf |= 0x40;
                   20281:                        }
                   20282:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   20283:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     20284:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     20285:                                        // data is ready
                   20286:                                        if(sio[c].irq_enable & 0x01) {
                   20287:                                                state_changed = true;
                   20288:                                        }
                   20289:                                        sio[c].line_stat_buf |= 0x01;
                   20290:                                }
                   20291:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   20292:                        }
                   20293:                        if(state_changed) {
                   20294:                                sio_update_irq(c);
                   20295:                        }
                   20296:                        return(val);
                   20297:                }
                   20298:        case 6:
                   20299:                {
                   20300:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   20301:                        UINT8 val = sio[c].modem_stat;
                   20302:                        sio[c].modem_stat &= 0xf0;
                   20303:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   20304:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   20305:                        
                   20306:                        if(sio[c].modem_ctrl & 0x10) {
                   20307:                                // loop-back
                   20308:                                val &= 0x0f;
                   20309:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   20310:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   20311:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   20312:                        }
                   20313:                        return(val);
                   20314:                }
                   20315:        case 7:
                   20316:                return(sio[c].scratch);
                   20317:        }
                   20318:        return(0xff);
                   20319: }
                   20320: 
                   20321: void sio_update(int c)
                   20322: {
                   20323:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   20324:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     20325:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     20326:                        // transmitter holding/shift registers will be empty
                   20327:                        sio[c].line_stat_buf |= 0x60;
                   20328:                }
                   20329:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   20330:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   20331:                // transmitter shift register will be empty
                   20332:                sio[c].line_stat_buf |= 0x40;
                   20333:        }
                   20334:        if(!(sio[c].line_stat_buf & 0x01)) {
                   20335:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     20336:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     20337:                        // data is ready
                   20338:                        sio[c].line_stat_buf |= 0x01;
                   20339:                }
                   20340:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   20341:        }
                   20342:        sio_update_irq(c);
                   20343: }
                   20344: 
                   20345: void sio_update_irq(int c)
                   20346: {
                   20347:        int level = -1;
                   20348:        
                   20349:        if(sio[c].irq_enable & 0x08) {
                   20350:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   20351:                if((sio[c].modem_stat & 0x0f) != 0) {
                   20352:                        level = 0;
                   20353:                }
                   20354:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   20355:        }
                   20356:        if(sio[c].irq_enable & 0x02) {
                   20357:                if(sio[c].line_stat_buf & 0x20) {
                   20358:                        level = 1;
                   20359:                }
                   20360:        }
                   20361:        if(sio[c].irq_enable & 0x01) {
                   20362:                if(sio[c].line_stat_buf & 0x01) {
                   20363:                        level = 2;
                   20364:                }
                   20365:        }
                   20366:        if(sio[c].irq_enable & 0x04) {
                   20367:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   20368:                if(sio[c].line_stat_err != 0) {
                   20369:                        level = 3;
                   20370:                }
                   20371:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   20372:        }
1.1.1.29  root     20373:        
                   20374:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     20375:        if(level != -1) {
                   20376:                sio[c].irq_identify = level << 1;
1.1.1.29  root     20377:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     20378:        } else {
                   20379:                sio[c].irq_identify = 1;
1.1.1.29  root     20380:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     20381:        }
                   20382: }
                   20383: 
                   20384: DWORD WINAPI sio_thread(void *lpx)
                   20385: {
                   20386:        volatile sio_t *p = (sio_t *)lpx;
                   20387:        sio_mt_t *q = &sio_mt[p->channel];
                   20388:        
                   20389:        char name[] = "COM1";
1.1.1.26  root     20390:        name[3] = '0' + sio_port_number[p->channel];
                   20391:        HANDLE hComm = NULL;
                   20392:        COMMPROP commProp;
                   20393:        DCB dcb;
                   20394:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   20395:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   20396:        
1.1.1.60  root     20397:        if((hComm = CreateFileA(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
1.1.1.26  root     20398:                if(GetCommProperties(hComm, &commProp)) {
                   20399:                        dwSettableBaud = commProp.dwSettableBaud;
                   20400:                }
1.1.1.25  root     20401:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     20402: //             EscapeCommFunction(hComm, SETRTS);
                   20403: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     20404:                
1.1.1.54  root     20405:                while(!m_exit) {
1.1.1.25  root     20406:                        // setup comm port
                   20407:                        bool comm_state_changed = false;
                   20408:                        
                   20409:                        EnterCriticalSection(&q->csLineCtrl);
                   20410:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   20411:                                p->prev_divisor = p->divisor.w;
                   20412:                                p->prev_line_ctrl = p->line_ctrl;
                   20413:                                comm_state_changed = true;
                   20414:                        }
                   20415:                        LeaveCriticalSection(&q->csLineCtrl);
                   20416:                        
                   20417:                        if(comm_state_changed) {
1.1.1.26  root     20418:                                if(GetCommState(hComm, &dcb)) {
                   20419: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   20420:                                        DWORD baud = 115200 / p->prev_divisor;
                   20421:                                        dcb.BaudRate = 9600; // default
                   20422:                                        
                   20423:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   20424:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   20425:                                        // 134.5bps is not supported ???
                   20426: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   20427:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   20428:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   20429:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   20430:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   20431:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   20432:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   20433:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   20434:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   20435:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   20436: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   20437: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   20438: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   20439:                                        
                   20440:                                        switch(p->prev_line_ctrl & 0x03) {
                   20441:                                        case 0x00: dcb.ByteSize = 5; break;
                   20442:                                        case 0x01: dcb.ByteSize = 6; break;
                   20443:                                        case 0x02: dcb.ByteSize = 7; break;
                   20444:                                        case 0x03: dcb.ByteSize = 8; break;
                   20445:                                        }
                   20446:                                        switch(p->prev_line_ctrl & 0x04) {
                   20447:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   20448:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   20449:                                        }
                   20450:                                        switch(p->prev_line_ctrl & 0x38) {
                   20451:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   20452:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   20453:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   20454:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   20455:                                        default:   dcb.Parity = NOPARITY;    break;
                   20456:                                        }
                   20457:                                        dcb.fBinary = TRUE;
                   20458:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   20459:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   20460:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   20461:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   20462:                                        dcb.fTXContinueOnXoff = TRUE;
                   20463:                                        dcb.fOutX = dcb.fInX = FALSE;
                   20464:                                        dcb.fErrorChar = FALSE;
                   20465:                                        dcb.fNull = FALSE;
                   20466:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   20467:                                        dcb.fAbortOnError = FALSE;
                   20468:                                        
                   20469:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     20470:                                }
                   20471:                                
                   20472:                                // check again to apply all comm state changes
                   20473:                                Sleep(10);
                   20474:                                continue;
                   20475:                        }
                   20476:                        
                   20477:                        // set comm pins
                   20478:                        bool change_brk = false;
1.1.1.26  root     20479: //                     bool change_rts = false;
                   20480: //                     bool change_dtr = false;
1.1.1.25  root     20481:                        
                   20482:                        EnterCriticalSection(&q->csModemCtrl);
                   20483:                        if(p->prev_set_brk != p->set_brk) {
                   20484:                                p->prev_set_brk = p->set_brk;
                   20485:                                change_brk = true;
                   20486:                        }
1.1.1.26  root     20487: //                     if(p->prev_set_rts != p->set_rts) {
                   20488: //                             p->prev_set_rts = p->set_rts;
                   20489: //                             change_rts = true;
                   20490: //                     }
                   20491: //                     if(p->prev_set_dtr != p->set_dtr) {
                   20492: //                             p->prev_set_dtr = p->set_dtr;
                   20493: //                             change_dtr = true;
                   20494: //                     }
1.1.1.25  root     20495:                        LeaveCriticalSection(&q->csModemCtrl);
                   20496:                        
                   20497:                        if(change_brk) {
1.1.1.26  root     20498:                                static UINT32 clear_time = 0;
                   20499:                                if(p->prev_set_brk) {
                   20500:                                        EscapeCommFunction(hComm, SETBREAK);
                   20501:                                        clear_time = timeGetTime() + 200;
                   20502:                                } else {
                   20503:                                        // keep break for at least 200msec
                   20504:                                        UINT32 cur_time = timeGetTime();
                   20505:                                        if(clear_time > cur_time) {
                   20506:                                                Sleep(clear_time - cur_time);
                   20507:                                        }
                   20508:                                        EscapeCommFunction(hComm, CLRBREAK);
                   20509:                                }
1.1.1.25  root     20510:                        }
1.1.1.26  root     20511: //                     if(change_rts) {
                   20512: //                             if(p->prev_set_rts) {
                   20513: //                                     EscapeCommFunction(hComm, SETRTS);
                   20514: //                             } else {
                   20515: //                                     EscapeCommFunction(hComm, CLRRTS);
                   20516: //                             }
                   20517: //                     }
                   20518: //                     if(change_dtr) {
                   20519: //                             if(p->prev_set_dtr) {
                   20520: //                                     EscapeCommFunction(hComm, SETDTR);
                   20521: //                             } else {
                   20522: //                                     EscapeCommFunction(hComm, CLRDTR);
                   20523: //                             }
                   20524: //                     }
1.1.1.25  root     20525:                        
                   20526:                        // get comm pins
                   20527:                        DWORD dwModemStat = 0;
                   20528:                        
                   20529:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   20530:                                EnterCriticalSection(&q->csModemStat);
                   20531:                                if(dwModemStat & MS_RLSD_ON) {
                   20532:                                        p->modem_stat |= 0x80;
                   20533:                                } else {
                   20534:                                        p->modem_stat &= ~0x80;
                   20535:                                }
                   20536:                                if(dwModemStat & MS_RING_ON) {
                   20537:                                        p->modem_stat |= 0x40;
                   20538:                                } else {
                   20539:                                        p->modem_stat &= ~0x40;
                   20540:                                }
1.1.1.26  root     20541: //                             if(dwModemStat & MS_DSR_ON) {
                   20542: //                                     p->modem_stat |= 0x20;
                   20543: //                             } else {
                   20544: //                                     p->modem_stat &= ~0x20;
                   20545: //                             }
                   20546: //                             if(dwModemStat & MS_CTS_ON) {
                   20547: //                                     p->modem_stat |= 0x10;
                   20548: //                             } else {
                   20549: //                                     p->modem_stat &= ~0x10;
                   20550: //                             }
1.1.1.25  root     20551:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   20552:                                        p->modem_stat |= 0x08;
                   20553:                                }
                   20554:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   20555:                                        p->modem_stat |= 0x04;
                   20556:                                }
1.1.1.26  root     20557: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   20558: //                                     p->modem_stat |= 0x02;
                   20559: //                             }
                   20560: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   20561: //                                     p->modem_stat |= 0x01;
                   20562: //                             }
1.1.1.25  root     20563:                                LeaveCriticalSection(&q->csModemStat);
                   20564:                        }
                   20565:                        
                   20566:                        // send data
                   20567:                        DWORD dwSend = 0;
                   20568:                        
                   20569:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     20570:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     20571:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   20572:                        }
                   20573:                        LeaveCriticalSection(&q->csSendData);
                   20574:                        
                   20575:                        if(dwSend != 0) {
                   20576:                                DWORD dwWritten = 0;
                   20577:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   20578:                        }
                   20579:                        
                   20580:                        // get line status and recv data
                   20581:                        DWORD dwLineStat = 0;
                   20582:                        COMSTAT comStat;
                   20583:                        
                   20584:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   20585:                                EnterCriticalSection(&q->csLineStat);
                   20586:                                if(dwLineStat & CE_BREAK) {
                   20587:                                        p->line_stat_err |= 0x10;
                   20588:                                }
                   20589:                                if(dwLineStat & CE_FRAME) {
                   20590:                                        p->line_stat_err |= 0x08;
                   20591:                                }
                   20592:                                if(dwLineStat & CE_RXPARITY) {
                   20593:                                        p->line_stat_err |= 0x04;
                   20594:                                }
                   20595:                                if(dwLineStat & CE_OVERRUN) {
                   20596:                                        p->line_stat_err |= 0x02;
                   20597:                                }
                   20598:                                LeaveCriticalSection(&q->csLineStat);
                   20599:                                
                   20600:                                if(comStat.cbInQue != 0) {
                   20601:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     20602:                                        DWORD dwRecv = 0;
                   20603:                                        if(p->recv_buffer != NULL) {
                   20604:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   20605:                                        }
1.1.1.25  root     20606:                                        LeaveCriticalSection(&q->csRecvData);
                   20607:                                        
                   20608:                                        if(dwRecv != 0) {
                   20609:                                                DWORD dwRead = 0;
                   20610:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   20611:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     20612:                                                        if(p->recv_buffer != NULL) {
                   20613:                                                                for(int i = 0; i < dwRead; i++) {
                   20614:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   20615:                                                                }
1.1.1.25  root     20616:                                                        }
                   20617:                                                        LeaveCriticalSection(&q->csRecvData);
                   20618:                                                }
                   20619:                                        }
                   20620:                                }
                   20621:                        }
                   20622:                        Sleep(10);
                   20623:                }
                   20624:                CloseHandle(hComm);
                   20625:        }
                   20626:        return 0;
                   20627: }
                   20628: 
1.1.1.8   root     20629: // cmos
                   20630: 
                   20631: void cmos_init()
                   20632: {
                   20633:        memset(cmos, 0, sizeof(cmos));
                   20634:        cmos_addr = 0;
1.1       root     20635:        
1.1.1.8   root     20636:        // from DOSBox
                   20637:        cmos_write(0x0a, 0x26);
                   20638:        cmos_write(0x0b, 0x02);
                   20639:        cmos_write(0x0d, 0x80);
1.1       root     20640: }
                   20641: 
1.1.1.8   root     20642: void cmos_write(int addr, UINT8 val)
1.1       root     20643: {
1.1.1.8   root     20644:        cmos[addr & 0x7f] = val;
                   20645: }
                   20646: 
                   20647: #define CMOS_GET_TIME() { \
                   20648:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   20649:        if(prev_sec != cur_sec) { \
                   20650:                GetLocalTime(&time); \
                   20651:                prev_sec = cur_sec; \
                   20652:        } \
1.1       root     20653: }
1.1.1.8   root     20654: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     20655: 
1.1.1.8   root     20656: UINT8 cmos_read(int addr)
1.1       root     20657: {
1.1.1.8   root     20658:        static SYSTEMTIME time;
                   20659:        static UINT32 prev_sec = 0;
1.1       root     20660:        
1.1.1.8   root     20661:        switch(addr & 0x7f) {
                   20662:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   20663:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   20664:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   20665:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   20666:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   20667:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   20668:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   20669: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   20670:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   20671:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   20672:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   20673:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20674:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20675:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20676:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20677:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     20678:        }
1.1.1.8   root     20679:        return(cmos[addr & 0x7f]);
1.1       root     20680: }
                   20681: 
1.1.1.7   root     20682: // kbd (a20)
                   20683: 
                   20684: void kbd_init()
                   20685: {
1.1.1.8   root     20686:        kbd_data = kbd_command = 0;
1.1.1.7   root     20687:        kbd_status = 0x18;
                   20688: }
                   20689: 
                   20690: UINT8 kbd_read_data()
                   20691: {
1.1.1.57  root     20692:        UINT8 data = kbd_data;
                   20693:        kbd_data = 0;
1.1.1.8   root     20694:        kbd_status &= ~1;
1.1.1.57  root     20695:        return(data);
1.1.1.7   root     20696: }
                   20697: 
                   20698: void kbd_write_data(UINT8 val)
                   20699: {
                   20700:        switch(kbd_command) {
                   20701:        case 0xd1:
                   20702:                i386_set_a20_line((val >> 1) & 1);
                   20703:                break;
                   20704:        }
                   20705:        kbd_command = 0;
1.1.1.8   root     20706:        kbd_status &= ~8;
1.1.1.7   root     20707: }
                   20708: 
                   20709: UINT8 kbd_read_status()
                   20710: {
                   20711:        return(kbd_status);
                   20712: }
                   20713: 
                   20714: void kbd_write_command(UINT8 val)
                   20715: {
                   20716:        switch(val) {
                   20717:        case 0xd0:
                   20718:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     20719:                kbd_status |= 1;
1.1.1.7   root     20720:                break;
                   20721:        case 0xdd:
                   20722:                i386_set_a20_line(0);
                   20723:                break;
                   20724:        case 0xdf:
                   20725:                i386_set_a20_line(1);
                   20726:                break;
1.1.1.26  root     20727:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   20728:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     20729:                if(!(val & 1)) {
1.1.1.8   root     20730:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     20731:                                // reset pic
                   20732:                                pic_init();
                   20733:                                pic[0].irr = pic[1].irr = 0x00;
                   20734:                                pic[0].imr = pic[1].imr = 0xff;
                   20735:                        }
                   20736:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     20737:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   20738:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   20739:                        i386_jmp_far(selector, address);
1.1.1.7   root     20740:                }
                   20741:                i386_set_a20_line((val >> 1) & 1);
                   20742:                break;
                   20743:        }
                   20744:        kbd_command = val;
1.1.1.8   root     20745:        kbd_status |= 8;
1.1.1.7   root     20746: }
                   20747: 
1.1.1.9   root     20748: // vga
                   20749: 
                   20750: UINT8 vga_read_status()
                   20751: {
                   20752:        // 60hz
                   20753:        static const int period[3] = {16, 17, 17};
                   20754:        static int index = 0;
                   20755:        UINT32 time = timeGetTime() % period[index];
                   20756:        
                   20757:        index = (index + 1) % 3;
1.1.1.14  root     20758:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     20759: }
                   20760: 
1.1       root     20761: // i/o bus
                   20762: 
1.1.1.29  root     20763: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   20764: //#define SW1US_PATCH
                   20765: 
1.1.1.25  root     20766: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     20767: #ifdef USE_DEBUGGER
1.1.1.25  root     20768: {
1.1.1.33  root     20769:        if(now_debugging) {
                   20770:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20771:                        if(in_break_point.table[i].status == 1) {
                   20772:                                if(addr == in_break_point.table[i].addr) {
                   20773:                                        in_break_point.hit = i + 1;
                   20774:                                        now_suspended = true;
                   20775:                                        break;
                   20776:                                }
                   20777:                        }
                   20778:                }
1.1.1.25  root     20779:        }
1.1.1.33  root     20780:        return(debugger_read_io_byte(addr));
1.1.1.25  root     20781: }
1.1.1.33  root     20782: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     20783: #endif
1.1       root     20784: {
1.1.1.33  root     20785:        UINT8 val = 0xff;
                   20786:        
1.1       root     20787:        switch(addr) {
1.1.1.29  root     20788: #ifdef SW1US_PATCH
                   20789:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20790:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     20791:                val = sio_read(0, addr - 1);
                   20792:                break;
1.1.1.29  root     20793: #else
1.1.1.25  root     20794:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20795:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     20796:                val = dma_read(0, addr);
                   20797:                break;
1.1.1.29  root     20798: #endif
1.1.1.25  root     20799:        case 0x20: case 0x21:
1.1.1.33  root     20800:                val = pic_read(0, addr);
                   20801:                break;
1.1.1.25  root     20802:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     20803:                val = pit_read(addr & 0x03);
                   20804:                break;
1.1.1.7   root     20805:        case 0x60:
1.1.1.33  root     20806:                val = kbd_read_data();
                   20807:                break;
1.1.1.9   root     20808:        case 0x61:
1.1.1.33  root     20809:                val = system_port;
                   20810:                break;
1.1.1.7   root     20811:        case 0x64:
1.1.1.33  root     20812:                val = kbd_read_status();
                   20813:                break;
1.1       root     20814:        case 0x71:
1.1.1.33  root     20815:                val = cmos_read(cmos_addr);
                   20816:                break;
1.1.1.25  root     20817:        case 0x81:
1.1.1.33  root     20818:                val = dma_page_read(0, 2);
                   20819:                break;
1.1.1.25  root     20820:        case 0x82:
1.1.1.33  root     20821:                val = dma_page_read(0, 3);
                   20822:                break;
1.1.1.25  root     20823:        case 0x83:
1.1.1.33  root     20824:                val = dma_page_read(0, 1);
                   20825:                break;
1.1.1.25  root     20826:        case 0x87:
1.1.1.33  root     20827:                val = dma_page_read(0, 0);
                   20828:                break;
1.1.1.25  root     20829:        case 0x89:
1.1.1.33  root     20830:                val = dma_page_read(1, 2);
                   20831:                break;
1.1.1.25  root     20832:        case 0x8a:
1.1.1.33  root     20833:                val = dma_page_read(1, 3);
                   20834:                break;
1.1.1.25  root     20835:        case 0x8b:
1.1.1.33  root     20836:                val = dma_page_read(1, 1);
                   20837:                break;
1.1.1.25  root     20838:        case 0x8f:
1.1.1.33  root     20839:                val = dma_page_read(1, 0);
                   20840:                break;
1.1       root     20841:        case 0x92:
1.1.1.33  root     20842:                val = (m_a20_mask >> 19) & 2;
                   20843:                break;
1.1.1.25  root     20844:        case 0xa0: case 0xa1:
1.1.1.33  root     20845:                val = pic_read(1, addr);
                   20846:                break;
1.1.1.25  root     20847:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20848:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     20849:                val = dma_read(1, (addr - 0xc0) >> 1);
                   20850:                break;
1.1.1.37  root     20851:        case 0x278: case 0x279: case 0x27a:
                   20852:                val = pio_read(1, addr);
                   20853:                break;
1.1.1.29  root     20854:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     20855:                val = sio_read(3, addr);
                   20856:                break;
1.1.1.25  root     20857:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     20858:                val = sio_read(1, addr);
                   20859:                break;
1.1.1.25  root     20860:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     20861:                val = pio_read(0, addr);
                   20862:                break;
1.1.1.25  root     20863:        case 0x3ba: case 0x3da:
1.1.1.33  root     20864:                val = vga_read_status();
                   20865:                break;
1.1.1.37  root     20866:        case 0x3bc: case 0x3bd: case 0x3be:
                   20867:                val = pio_read(2, addr);
                   20868:                break;
1.1.1.58  root     20869:        case 0x3d5:
                   20870:                if(crtc_addr < 16) {
                   20871:                        val = crtc_regs[crtc_addr];
                   20872:                }
                   20873:                break;
1.1.1.29  root     20874:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     20875:                val = sio_read(2, addr);
                   20876:                break;
1.1.1.25  root     20877:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     20878:                val = sio_read(0, addr);
                   20879:                break;
1.1       root     20880:        default:
1.1.1.33  root     20881: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     20882:                break;
                   20883:        }
1.1.1.33  root     20884: #ifdef ENABLE_DEBUG_IOPORT
                   20885:        if(fp_debug_log != NULL) {
                   20886:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   20887:        }
                   20888: #endif
                   20889:        return(val);
1.1       root     20890: }
                   20891: 
                   20892: UINT16 read_io_word(offs_t addr)
                   20893: {
                   20894:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   20895: }
                   20896: 
1.1.1.33  root     20897: #ifdef USE_DEBUGGER
                   20898: UINT16 debugger_read_io_word(offs_t addr)
                   20899: {
                   20900:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   20901: }
                   20902: #endif
                   20903: 
1.1       root     20904: UINT32 read_io_dword(offs_t addr)
                   20905: {
                   20906:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   20907: }
                   20908: 
1.1.1.33  root     20909: #ifdef USE_DEBUGGER
                   20910: UINT32 debugger_read_io_dword(offs_t addr)
                   20911: {
                   20912:        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));
                   20913: }
                   20914: #endif
                   20915: 
1.1       root     20916: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     20917: #ifdef USE_DEBUGGER
                   20918: {
                   20919:        if(now_debugging) {
                   20920:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20921:                        if(out_break_point.table[i].status == 1) {
                   20922:                                if(addr == out_break_point.table[i].addr) {
                   20923:                                        out_break_point.hit = i + 1;
                   20924:                                        now_suspended = true;
                   20925:                                        break;
                   20926:                                }
                   20927:                        }
                   20928:                }
                   20929:        }
                   20930:        debugger_write_io_byte(addr, val);
                   20931: }
                   20932: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   20933: #endif
1.1       root     20934: {
1.1.1.25  root     20935: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     20936:        if(fp_debug_log != NULL) {
1.1.1.43  root     20937: #ifdef USE_SERVICE_THREAD
                   20938:                if(addr != 0xf7)
                   20939: #endif
1.1.1.33  root     20940:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     20941:        }
                   20942: #endif
1.1       root     20943:        switch(addr) {
1.1.1.29  root     20944: #ifdef SW1US_PATCH
                   20945:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20946:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   20947:                sio_write(0, addr - 1, val);
                   20948:                break;
                   20949: #else
1.1.1.25  root     20950:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20951:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   20952:                dma_write(0, addr, val);
                   20953:                break;
1.1.1.29  root     20954: #endif
1.1.1.25  root     20955:        case 0x20: case 0x21:
1.1       root     20956:                pic_write(0, addr, val);
                   20957:                break;
1.1.1.25  root     20958:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     20959:                pit_write(addr & 0x03, val);
                   20960:                break;
1.1.1.7   root     20961:        case 0x60:
                   20962:                kbd_write_data(val);
                   20963:                break;
1.1.1.9   root     20964:        case 0x61:
                   20965:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   20966:                        // beep on
                   20967: //                     MessageBeep(-1);
                   20968:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   20969:                        // beep off
                   20970:                }
                   20971:                system_port = val;
                   20972:                break;
1.1       root     20973:        case 0x64:
1.1.1.7   root     20974:                kbd_write_command(val);
1.1       root     20975:                break;
                   20976:        case 0x70:
                   20977:                cmos_addr = val;
                   20978:                break;
                   20979:        case 0x71:
1.1.1.8   root     20980:                cmos_write(cmos_addr, val);
1.1       root     20981:                break;
1.1.1.25  root     20982:        case 0x81:
                   20983:                dma_page_write(0, 2, val);
                   20984:        case 0x82:
                   20985:                dma_page_write(0, 3, val);
                   20986:        case 0x83:
                   20987:                dma_page_write(0, 1, val);
                   20988:        case 0x87:
                   20989:                dma_page_write(0, 0, val);
                   20990:        case 0x89:
                   20991:                dma_page_write(1, 2, val);
                   20992:        case 0x8a:
                   20993:                dma_page_write(1, 3, val);
                   20994:        case 0x8b:
                   20995:                dma_page_write(1, 1, val);
                   20996:        case 0x8f:
                   20997:                dma_page_write(1, 0, val);
1.1       root     20998:        case 0x92:
1.1.1.7   root     20999:                i386_set_a20_line((val >> 1) & 1);
1.1       root     21000:                break;
1.1.1.25  root     21001:        case 0xa0: case 0xa1:
1.1       root     21002:                pic_write(1, addr, val);
                   21003:                break;
1.1.1.25  root     21004:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   21005:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     21006:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     21007:                break;
1.1.1.35  root     21008: #ifdef USE_SERVICE_THREAD
                   21009:        case 0xf7:
                   21010:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     21011:                if(in_service && cursor_moved) {
                   21012:                        // update cursor position before service is done
                   21013:                        pcbios_update_cursor_position();
                   21014:                        cursor_moved = false;
                   21015:                }
1.1.1.35  root     21016:                finish_service_loop();
                   21017:                break;
                   21018: #endif
1.1.1.37  root     21019:        case 0x278: case 0x279: case 0x27a:
                   21020:                pio_write(1, addr, val);
                   21021:                break;
1.1.1.29  root     21022:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   21023:                sio_write(3, addr, val);
                   21024:                break;
1.1.1.25  root     21025:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   21026:                sio_write(1, addr, val);
                   21027:                break;
                   21028:        case 0x378: case 0x379: case 0x37a:
                   21029:                pio_write(0, addr, val);
                   21030:                break;
1.1.1.37  root     21031:        case 0x3bc: case 0x3bd: case 0x3be:
                   21032:                pio_write(2, addr, val);
                   21033:                break;
1.1.1.58  root     21034:        case 0x3d4:
                   21035:                crtc_addr = val;
                   21036:                break;
                   21037:        case 0x3d5:
                   21038:                if(crtc_addr < 16) {
                   21039:                        if(crtc_regs[crtc_addr] != val) {
                   21040:                                crtc_regs[crtc_addr] = val;
                   21041:                                crtc_changed[crtc_addr] = 1;
                   21042:                        }
                   21043:                }
                   21044:                break;
1.1.1.29  root     21045:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   21046:                sio_write(2, addr, val);
                   21047:                break;
1.1.1.25  root     21048:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   21049:                sio_write(0, addr, val);
                   21050:                break;
1.1       root     21051:        default:
1.1.1.33  root     21052: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     21053:                break;
                   21054:        }
                   21055: }
                   21056: 
                   21057: void write_io_word(offs_t addr, UINT16 val)
                   21058: {
                   21059:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   21060:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   21061: }
                   21062: 
1.1.1.33  root     21063: #ifdef USE_DEBUGGER
                   21064: void debugger_write_io_word(offs_t addr, UINT16 val)
                   21065: {
                   21066:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   21067:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   21068: }
                   21069: #endif
                   21070: 
1.1       root     21071: void write_io_dword(offs_t addr, UINT32 val)
                   21072: {
                   21073:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   21074:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   21075:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   21076:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   21077: }
1.1.1.33  root     21078: 
                   21079: #ifdef USE_DEBUGGER
                   21080: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   21081: {
                   21082:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   21083:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   21084:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   21085:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   21086: }
                   21087: #endif

unix.superglobalmegacorp.com

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