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

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.32  root       84: #define array_length(array) (sizeof(array) / sizeof(array[0]))
1.1.1.22  root       85: #define my_strchr(str, chr) (char *)_mbschr((unsigned char *)(str), (unsigned int)(chr))
                     86: #define my_strtok(tok, del) (char *)_mbstok((unsigned char *)(tok), (const unsigned char *)(del))
                     87: #define my_strupr(str) (char *)_mbsupr((unsigned char *)(str))
1.1       root       88: 
1.1.1.12  root       89: #if defined(__MINGW32__)
                     90: extern "C" int _CRT_glob = 0;
                     91: #endif
                     92: 
                     93: /*
                     94:        kludge for "more-standardized" C++
                     95: */
                     96: #if !defined(_MSC_VER)
                     97: inline int kludge_min(int a, int b) { return (a<b ? a:b); }
                     98: inline int kludge_max(int a, int b) { return (a>b ? a:b); }
                     99: #define min(a,b) kludge_min(a,b)
                    100: #define max(a,b) kludge_max(a,b)
1.1.1.14  root      101: #elif _MSC_VER >= 1400
                    102: void ignore_invalid_parameters(const wchar_t *, const wchar_t *, const wchar_t *, unsigned int, uintptr_t)
                    103: {
                    104: }
                    105: #endif
                    106: 
1.1.1.35  root      107: #define USE_VRAM_THREAD
1.1.1.14  root      108: 
1.1.1.35  root      109: #ifdef USE_VRAM_THREAD
1.1.1.14  root      110: static CRITICAL_SECTION vram_crit_sect;
                    111: #else
                    112: #define vram_flush()
1.1.1.12  root      113: #endif
                    114: 
1.1.1.14  root      115: #define VIDEO_REGEN *(UINT16 *)(mem + 0x44c)
                    116: #define SCR_BUF(y,x) scr_buf[(y) * scr_buf_size.X + (x)]
                    117: 
                    118: void change_console_size(int width, int height);
                    119: void clear_scr_buffer(WORD attr);
                    120: 
                    121: static UINT32 vram_length_char = 0, vram_length_attr = 0;
                    122: static UINT32 vram_last_length_char = 0, vram_last_length_attr = 0;
                    123: static COORD vram_coord_char, vram_coord_attr;
                    124: 
1.1.1.28  root      125: char temp_file_path[MAX_PATH];
                    126: bool temp_file_created = false;
                    127: 
1.1.1.14  root      128: bool ignore_illegal_insn = false;
                    129: bool limit_max_memory = false;
                    130: bool no_windows = false;
                    131: bool stay_busy = false;
1.1.1.19  root      132: bool support_ems = false;
                    133: #ifdef SUPPORT_XMS
                    134: bool support_xms = false;
                    135: #endif
1.1.1.29  root      136: int sio_port_number[4] = {0, 0, 0, 0};
1.1.1.14  root      137: 
1.1.1.54  root      138: BOOL is_xp_64_or_later;
1.1.1.14  root      139: BOOL is_vista_or_later;
                    140: 
1.1.1.35  root      141: #define UPDATE_OPS 16384
                    142: #define REQUEST_HARDWRE_UPDATE() { \
                    143:        update_ops = UPDATE_OPS - 1; \
                    144: }
                    145: UINT32 update_ops = 0;
                    146: UINT32 idle_ops = 0;
                    147: 
1.1.1.54  root      148: inline BOOL is_sse2_ready()
                    149: {
                    150:        static int result = -1;
                    151:        int cpu_info[4];
                    152:        
                    153:        if(result == -1) {
                    154:                result = 0;
                    155:                __cpuid(cpu_info, 0);
                    156:                if(cpu_info[0] >= 1){
                    157:                        __cpuid(cpu_info, 1);
                    158:                        if(cpu_info[3] & (1 << 26)) {
                    159:                                result = 1;
                    160:                        }
                    161:                }
                    162:        }
                    163:        return(result == 1);
                    164: }
                    165: 
1.1.1.14  root      166: inline void maybe_idle()
                    167: {
                    168:        // if it appears to be in a tight loop, assume waiting for input
                    169:        // allow for one updated video character, for a spinning cursor
1.1.1.35  root      170:        if(!stay_busy && idle_ops < 1024 && vram_length_char <= 1 && vram_length_attr <= 1) {
1.1.1.54  root      171:                if(is_sse2_ready()) {
                    172:                        _mm_pause();    // SSE2 pause
                    173:                } else if(is_xp_64_or_later) {
                    174:                        Sleep(0);       // switch to other thread that is ready to run, without checking priority
                    175:                } else {
                    176:                        Sleep(1);
                    177:                        REQUEST_HARDWRE_UPDATE();
                    178:                }
1.1.1.14  root      179:        }
1.1.1.35  root      180:        idle_ops = 0;
1.1.1.14  root      181: }
1.1.1.12  root      182: 
1.1       root      183: /* ----------------------------------------------------------------------------
1.1.1.3   root      184:        MAME i86/i386
1.1       root      185: ---------------------------------------------------------------------------- */
                    186: 
1.1.1.10  root      187: #ifndef __BIG_ENDIAN__
1.1       root      188: #define LSB_FIRST
1.1.1.10  root      189: #endif
1.1       root      190: 
                    191: #ifndef INLINE
                    192: #define INLINE inline
                    193: #endif
                    194: #define U64(v) UINT64(v)
                    195: 
                    196: //#define logerror(...) fprintf(stderr, __VA_ARGS__)
                    197: #define logerror(...)
                    198: //#define popmessage(...) fprintf(stderr, __VA_ARGS__)
                    199: #define popmessage(...)
                    200: 
                    201: /*****************************************************************************/
1.1.1.10  root      202: /* src/emu/devcpu.h */
                    203: 
                    204: // CPU interface functions
                    205: #define CPU_INIT_NAME(name)                    cpu_init_##name
                    206: #define CPU_INIT(name)                         void CPU_INIT_NAME(name)()
                    207: #define CPU_INIT_CALL(name)                    CPU_INIT_NAME(name)()
                    208: 
                    209: #define CPU_RESET_NAME(name)                   cpu_reset_##name
                    210: #define CPU_RESET(name)                                void CPU_RESET_NAME(name)()
                    211: #define CPU_RESET_CALL(name)                   CPU_RESET_NAME(name)()
                    212: 
                    213: #define CPU_EXECUTE_NAME(name)                 cpu_execute_##name
                    214: #define CPU_EXECUTE(name)                      void CPU_EXECUTE_NAME(name)()
                    215: #define CPU_EXECUTE_CALL(name)                 CPU_EXECUTE_NAME(name)()
                    216: 
                    217: #define CPU_TRANSLATE_NAME(name)               cpu_translate_##name
                    218: #define CPU_TRANSLATE(name)                    int CPU_TRANSLATE_NAME(name)(address_spacenum space, int intention, offs_t *address)
                    219: #define CPU_TRANSLATE_CALL(name)               CPU_TRANSLATE_NAME(name)(space, intention, address)
                    220: 
                    221: #define CPU_DISASSEMBLE_NAME(name)             cpu_disassemble_##name
                    222: #define CPU_DISASSEMBLE(name)                  int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
                    223: #define CPU_DISASSEMBLE_CALL(name)             CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
                    224: 
1.1.1.14  root      225: #define CPU_MODEL_STR(name)                    #name
                    226: #define CPU_MODEL_NAME(name)                   CPU_MODEL_STR(name)
                    227: 
1.1.1.10  root      228: /*****************************************************************************/
                    229: /* src/emu/didisasm.h */
                    230: 
                    231: // Disassembler constants
                    232: const UINT32 DASMFLAG_SUPPORTED     = 0x80000000;   // are disassembly flags supported?
                    233: const UINT32 DASMFLAG_STEP_OUT      = 0x40000000;   // this instruction should be the end of a step out sequence
                    234: const UINT32 DASMFLAG_STEP_OVER     = 0x20000000;   // this instruction should be stepped over by setting a breakpoint afterwards
                    235: const UINT32 DASMFLAG_OVERINSTMASK  = 0x18000000;   // number of extra instructions to skip when stepping over
                    236: const UINT32 DASMFLAG_OVERINSTSHIFT = 27;           // bits to shift after masking to get the value
                    237: const UINT32 DASMFLAG_LENGTHMASK    = 0x0000ffff;   // the low 16-bits contain the actual length
                    238: 
                    239: /*****************************************************************************/
1.1       root      240: /* src/emu/diexec.h */
                    241: 
                    242: // I/O line states
                    243: enum line_state
                    244: {
                    245:        CLEAR_LINE = 0,                         // clear (a fired or held) line
                    246:        ASSERT_LINE,                            // assert an interrupt immediately
                    247:        HOLD_LINE,                              // hold interrupt line until acknowledged
                    248:        PULSE_LINE                              // pulse interrupt line instantaneously (only for NMI, RESET)
                    249: };
                    250: 
                    251: // I/O line definitions
                    252: enum
                    253: {
                    254:        INPUT_LINE_IRQ = 0,
                    255:        INPUT_LINE_NMI
                    256: };
                    257: 
                    258: /*****************************************************************************/
1.1.1.10  root      259: /* src/emu/dimemory.h */
1.1       root      260: 
1.1.1.10  root      261: // Translation intentions
                    262: const int TRANSLATE_TYPE_MASK       = 0x03;     // read write or fetch
                    263: const int TRANSLATE_USER_MASK       = 0x04;     // user mode or fully privileged
                    264: const int TRANSLATE_DEBUG_MASK      = 0x08;     // debug mode (no side effects)
                    265: 
                    266: const int TRANSLATE_READ            = 0;        // translate for read
                    267: const int TRANSLATE_WRITE           = 1;        // translate for write
                    268: const int TRANSLATE_FETCH           = 2;        // translate for instruction fetch
                    269: const int TRANSLATE_READ_USER       = (TRANSLATE_READ | TRANSLATE_USER_MASK);
                    270: const int TRANSLATE_WRITE_USER      = (TRANSLATE_WRITE | TRANSLATE_USER_MASK);
                    271: const int TRANSLATE_FETCH_USER      = (TRANSLATE_FETCH | TRANSLATE_USER_MASK);
                    272: const int TRANSLATE_READ_DEBUG      = (TRANSLATE_READ | TRANSLATE_DEBUG_MASK);
                    273: const int TRANSLATE_WRITE_DEBUG     = (TRANSLATE_WRITE | TRANSLATE_DEBUG_MASK);
                    274: const int TRANSLATE_FETCH_DEBUG     = (TRANSLATE_FETCH | TRANSLATE_DEBUG_MASK);
1.1       root      275: 
1.1.1.10  root      276: /*****************************************************************************/
                    277: /* src/emu/emucore.h */
1.1       root      278: 
1.1.1.10  root      279: // constants for expression endianness
                    280: enum endianness_t
                    281: {
                    282:        ENDIANNESS_LITTLE,
                    283:        ENDIANNESS_BIG
                    284: };
1.1       root      285: 
1.1.1.10  root      286: // declare native endianness to be one or the other
                    287: #ifdef LSB_FIRST
                    288: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
                    289: #else
                    290: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
                    291: #endif
                    292: 
                    293: // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
                    294: #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
                    295: 
                    296: // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
                    297: #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
                    298: 
                    299: // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
                    300: #define ENDIAN_VALUE_NE_NNE(endian,leval,beval)        (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
1.1       root      301: 
                    302: /*****************************************************************************/
                    303: /* src/emu/memory.h */
                    304: 
1.1.1.10  root      305: // address spaces
                    306: enum address_spacenum
                    307: {
                    308:        AS_0,                           // first address space
                    309:        AS_1,                           // second address space
                    310:        AS_2,                           // third address space
                    311:        AS_3,                           // fourth address space
                    312:        ADDRESS_SPACES,                 // maximum number of address spaces
                    313: 
                    314:        // alternate address space names for common use
                    315:        AS_PROGRAM = AS_0,              // program address space
                    316:        AS_DATA = AS_1,                 // data address space
                    317:        AS_IO = AS_2                    // I/O address space
                    318: };
                    319: 
1.1       root      320: // offsets and addresses are 32-bit (for now...)
1.1.1.30  root      321: //typedef UINT32       offs_t;
1.1       root      322: 
                    323: // read accessors
                    324: UINT8 read_byte(offs_t byteaddress)
1.1.1.33  root      325: #ifdef USE_DEBUGGER
                    326: {
                    327:        if(now_debugging) {
                    328:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    329:                        if(rd_break_point.table[i].status == 1) {
                    330:                                if(byteaddress == rd_break_point.table[i].addr) {
                    331:                                        rd_break_point.hit = i + 1;
                    332:                                        now_suspended = true;
                    333:                                        break;
                    334:                                }
                    335:                        }
                    336:                }
                    337:        }
                    338:        return(debugger_read_byte(byteaddress));
                    339: }
                    340: UINT8 debugger_read_byte(offs_t byteaddress)
                    341: #endif
1.1       root      342: {
1.1.1.4   root      343: #if defined(HAS_I386)
1.1       root      344:        if(byteaddress < MAX_MEM) {
                    345:                return mem[byteaddress];
1.1.1.3   root      346: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    347: //             return read_byte(byteaddress & 0xfffff);
1.1       root      348:        }
                    349:        return 0;
1.1.1.4   root      350: #else
                    351:        return mem[byteaddress];
                    352: #endif
1.1       root      353: }
                    354: 
                    355: UINT16 read_word(offs_t byteaddress)
1.1.1.33  root      356: #ifdef USE_DEBUGGER
                    357: {
                    358:        if(now_debugging) {
                    359:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    360:                        if(rd_break_point.table[i].status == 1) {
                    361:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 2) {
                    362:                                        rd_break_point.hit = i + 1;
                    363:                                        now_suspended = true;
                    364:                                        break;
                    365:                                }
                    366:                        }
                    367:                }
                    368:        }
                    369:        return(debugger_read_word(byteaddress));
                    370: }
                    371: UINT16 debugger_read_word(offs_t byteaddress)
                    372: #endif
1.1       root      373: {
1.1.1.14  root      374:        if(byteaddress == 0x41c) {
                    375:                // pointer to first free slot in keyboard buffer
1.1.1.35  root      376:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                    377: #ifdef USE_SERVICE_THREAD
                    378:                        EnterCriticalSection(&key_buf_crit_sect);
                    379: #endif
1.1.1.55  root      380:                        bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root      381: #ifdef USE_SERVICE_THREAD
                    382:                        LeaveCriticalSection(&key_buf_crit_sect);
                    383: #endif
1.1.1.51  root      384:                        if(empty) maybe_idle();
1.1.1.14  root      385:                }
                    386:        }
1.1.1.4   root      387: #if defined(HAS_I386)
1.1       root      388:        if(byteaddress < MAX_MEM - 1) {
                    389:                return *(UINT16 *)(mem + byteaddress);
1.1.1.3   root      390: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    391: //             return read_word(byteaddress & 0xfffff);
1.1       root      392:        }
                    393:        return 0;
1.1.1.4   root      394: #else
                    395:        return *(UINT16 *)(mem + byteaddress);
                    396: #endif
1.1       root      397: }
                    398: 
                    399: UINT32 read_dword(offs_t byteaddress)
1.1.1.33  root      400: #ifdef USE_DEBUGGER
                    401: {
                    402:        if(now_debugging) {
                    403:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    404:                        if(rd_break_point.table[i].status == 1) {
                    405:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 4) {
                    406:                                        rd_break_point.hit = i + 1;
                    407:                                        now_suspended = true;
                    408:                                        break;
                    409:                                }
                    410:                        }
                    411:                }
                    412:        }
                    413:        return(debugger_read_dword(byteaddress));
                    414: }
                    415: UINT32 debugger_read_dword(offs_t byteaddress)
                    416: #endif
1.1       root      417: {
1.1.1.4   root      418: #if defined(HAS_I386)
1.1       root      419:        if(byteaddress < MAX_MEM - 3) {
                    420:                return *(UINT32 *)(mem + byteaddress);
1.1.1.3   root      421: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    422: //             return read_dword(byteaddress & 0xfffff);
1.1       root      423:        }
                    424:        return 0;
1.1.1.4   root      425: #else
                    426:        return *(UINT32 *)(mem + byteaddress);
                    427: #endif
1.1       root      428: }
                    429: 
                    430: // write accessors
1.1.1.35  root      431: #ifdef USE_VRAM_THREAD
1.1.1.14  root      432: void vram_flush_char()
                    433: {
                    434:        if(vram_length_char != 0) {
                    435:                DWORD num;
1.1.1.23  root      436:                WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, vram_length_char, vram_coord_char, &num);
1.1.1.14  root      437:                vram_length_char = vram_last_length_char = 0;
                    438:        }
                    439: }
                    440: 
                    441: void vram_flush_attr()
                    442: {
                    443:        if(vram_length_attr != 0) {
                    444:                DWORD num;
1.1.1.23  root      445:                WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, vram_length_attr, vram_coord_attr, &num);
1.1.1.14  root      446:                vram_length_attr = vram_last_length_attr = 0;
                    447:        }
                    448: }
                    449: 
                    450: void vram_flush()
                    451: {
                    452:        if(vram_length_char != 0 || vram_length_attr != 0) {
                    453:                EnterCriticalSection(&vram_crit_sect);
                    454:                vram_flush_char();
                    455:                vram_flush_attr();
                    456:                LeaveCriticalSection(&vram_crit_sect);
                    457:        }
                    458: }
                    459: #endif
                    460: 
                    461: void write_text_vram_char(offs_t offset, UINT8 data)
1.1.1.8   root      462: {
1.1.1.35  root      463: #ifdef USE_VRAM_THREAD
1.1.1.14  root      464:        static offs_t first_offset_char, last_offset_char;
                    465:        
                    466:        if(vram_length_char != 0) {
                    467:                if(offset <= last_offset_char && offset >= first_offset_char) {
                    468:                        scr_char[(offset - first_offset_char) >> 1] = data;
                    469:                        return;
                    470:                }
                    471:                if(offset != last_offset_char + 2) {
                    472:                        vram_flush_char();
                    473:                }
                    474:        }
                    475:        if(vram_length_char == 0) {
                    476:                first_offset_char = offset;
                    477:                vram_coord_char.X = (offset >> 1) % scr_width;
                    478:                vram_coord_char.Y = (offset >> 1) / scr_width + scr_top;
                    479:        }
                    480:        scr_char[vram_length_char++] = data;
                    481:        last_offset_char = offset;
                    482: #else
1.1.1.8   root      483:        COORD co;
                    484:        DWORD num;
                    485:        
1.1.1.14  root      486:        co.X = (offset >> 1) % scr_width;
                    487:        co.Y = (offset >> 1) / scr_width;
                    488:        scr_char[0] = data;
1.1.1.23  root      489:        WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, 1, co, &num);
1.1.1.14  root      490: #endif
                    491: }
                    492: 
                    493: void write_text_vram_attr(offs_t offset, UINT8 data)
                    494: {
1.1.1.35  root      495: #ifdef USE_VRAM_THREAD
1.1.1.14  root      496:        static offs_t first_offset_attr, last_offset_attr;
                    497:        
                    498:        if(vram_length_attr != 0) {
                    499:                if(offset <= last_offset_attr && offset >= first_offset_attr) {
                    500:                        scr_attr[(offset - first_offset_attr) >> 1] = data;
                    501:                        return;
                    502:                }
                    503:                if(offset != last_offset_attr + 2) {
                    504:                        vram_flush_attr();
                    505:                }
                    506:        }
                    507:        if(vram_length_attr == 0) {
                    508:                first_offset_attr = offset;
                    509:                vram_coord_attr.X = (offset >> 1) % scr_width;
                    510:                vram_coord_attr.Y = (offset >> 1) / scr_width + scr_top;
                    511:        }
                    512:        scr_attr[vram_length_attr++] = data;
                    513:        last_offset_attr = offset;
                    514: #else
                    515:        COORD co;
                    516:        DWORD num;
1.1.1.8   root      517:        
1.1.1.14  root      518:        co.X = (offset >> 1) % scr_width;
                    519:        co.Y = (offset >> 1) / scr_width;
                    520:        scr_attr[0] = data;
1.1.1.23  root      521:        WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, 1, co, &num);
1.1.1.14  root      522: #endif
                    523: }
                    524: 
                    525: void write_text_vram_byte(offs_t offset, UINT8 data)
                    526: {
1.1.1.35  root      527: #ifdef USE_VRAM_THREAD
1.1.1.14  root      528:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      529: #endif
1.1.1.8   root      530:        if(offset & 1) {
1.1.1.14  root      531:                write_text_vram_attr(offset, data);
1.1.1.8   root      532:        } else {
1.1.1.14  root      533:                write_text_vram_char(offset, data);
1.1.1.8   root      534:        }
1.1.1.35  root      535: #ifdef USE_VRAM_THREAD
1.1.1.14  root      536:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      537: #endif
1.1.1.8   root      538: }
                    539: 
                    540: void write_text_vram_word(offs_t offset, UINT16 data)
                    541: {
1.1.1.35  root      542: #ifdef USE_VRAM_THREAD
1.1.1.14  root      543:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      544: #endif
1.1.1.8   root      545:        if(offset & 1) {
1.1.1.14  root      546:                write_text_vram_attr(offset    , (data     ) & 0xff);
                    547:                write_text_vram_char(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      548:        } else {
1.1.1.14  root      549:                write_text_vram_char(offset    , (data     ) & 0xff);
                    550:                write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      551:        }
1.1.1.35  root      552: #ifdef USE_VRAM_THREAD
1.1.1.14  root      553:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      554: #endif
1.1.1.8   root      555: }
                    556: 
                    557: void write_text_vram_dword(offs_t offset, UINT32 data)
                    558: {
1.1.1.35  root      559: #ifdef USE_VRAM_THREAD
1.1.1.14  root      560:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      561: #endif
1.1.1.8   root      562:        if(offset & 1) {
1.1.1.14  root      563:                write_text_vram_attr(offset    , (data      ) & 0xff);
                    564:                write_text_vram_char(offset + 1, (data >>  8) & 0xff);
                    565:                write_text_vram_attr(offset + 2, (data >> 16) & 0xff);
                    566:                write_text_vram_char(offset + 3, (data >> 24) & 0xff);
                    567:        } else {
                    568:                write_text_vram_char(offset    , (data      ) & 0xff);
                    569:                write_text_vram_attr(offset + 1, (data >>  8) & 0xff);
                    570:                write_text_vram_char(offset + 2, (data >> 16) & 0xff);
                    571:                write_text_vram_attr(offset + 3, (data >> 24) & 0xff);
1.1.1.8   root      572:        }
1.1.1.35  root      573: #ifdef USE_VRAM_THREAD
1.1.1.14  root      574:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      575: #endif
1.1.1.8   root      576: }
                    577: 
1.1       root      578: void write_byte(offs_t byteaddress, UINT8 data)
1.1.1.33  root      579: #ifdef USE_DEBUGGER
                    580: {
                    581:        if(now_debugging) {
                    582:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    583:                        if(wr_break_point.table[i].status == 1) {
                    584:                                if(byteaddress == wr_break_point.table[i].addr) {
                    585:                                        wr_break_point.hit = i + 1;
                    586:                                        now_suspended = true;
                    587:                                        break;
                    588:                                }
                    589:                        }
                    590:                }
                    591:        }
                    592:        debugger_write_byte(byteaddress, data);
                    593: }
                    594: void debugger_write_byte(offs_t byteaddress, UINT8 data)
                    595: #endif
1.1       root      596: {
1.1.1.8   root      597:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      598:                mem[byteaddress] = data;
1.1.1.8   root      599:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      600:                if(!restore_console_on_exit) {
                    601:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      602:                }
1.1.1.8   root      603:                write_text_vram_byte(byteaddress - text_vram_top_address, data);
                    604:                mem[byteaddress] = data;
                    605:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    606:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    607:                        write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1       root      608:                }
                    609:                mem[byteaddress] = data;
1.1.1.4   root      610: #if defined(HAS_I386)
1.1.1.3   root      611:        } else if(byteaddress < MAX_MEM) {
1.1.1.4   root      612: #else
                    613:        } else {
                    614: #endif
1.1.1.3   root      615:                mem[byteaddress] = data;
1.1       root      616:        }
                    617: }
                    618: 
                    619: void write_word(offs_t byteaddress, UINT16 data)
1.1.1.33  root      620: #ifdef USE_DEBUGGER
                    621: {
                    622:        if(now_debugging) {
                    623:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    624:                        if(wr_break_point.table[i].status == 1) {
                    625:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 2) {
                    626:                                        wr_break_point.hit = i + 1;
                    627:                                        now_suspended = true;
                    628:                                        break;
                    629:                                }
                    630:                        }
                    631:                }
                    632:        }
                    633:        debugger_write_word(byteaddress, data);
                    634: }
                    635: void debugger_write_word(offs_t byteaddress, UINT16 data)
                    636: #endif
1.1       root      637: {
1.1.1.8   root      638:        if(byteaddress < MEMORY_END) {
1.1.1.51  root      639:                if(byteaddress == cursor_position_address) {
                    640:                        if(*(UINT16 *)(mem + byteaddress) != data) {
                    641:                                COORD co;
                    642:                                co.X = data & 0xff;
                    643:                                co.Y = (data >> 8) + scr_top;
                    644:                                SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), co);
                    645:                        }
1.1.1.14  root      646:                }
1.1.1.3   root      647:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8   root      648:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      649:                if(!restore_console_on_exit) {
                    650:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      651:                }
1.1.1.8   root      652:                write_text_vram_word(byteaddress - text_vram_top_address, data);
                    653:                *(UINT16 *)(mem + byteaddress) = data;
                    654:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    655:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    656:                        write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1       root      657:                }
                    658:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4   root      659: #if defined(HAS_I386)
1.1.1.3   root      660:        } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4   root      661: #else
                    662:        } else {
                    663: #endif
1.1.1.3   root      664:                *(UINT16 *)(mem + byteaddress) = data;
1.1       root      665:        }
                    666: }
                    667: 
                    668: void write_dword(offs_t byteaddress, UINT32 data)
1.1.1.33  root      669: #ifdef USE_DEBUGGER
                    670: {
                    671:        if(now_debugging) {
                    672:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    673:                        if(wr_break_point.table[i].status == 1) {
                    674:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 4) {
                    675:                                        wr_break_point.hit = i + 1;
                    676:                                        now_suspended = true;
                    677:                                        break;
                    678:                                }
                    679:                        }
                    680:                }
                    681:        }
                    682:        debugger_write_dword(byteaddress, data);
                    683: }
                    684: void debugger_write_dword(offs_t byteaddress, UINT32 data)
                    685: #endif
1.1       root      686: {
1.1.1.8   root      687:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      688:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8   root      689:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      690:                if(!restore_console_on_exit) {
                    691:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      692:                }
1.1.1.8   root      693:                write_text_vram_dword(byteaddress - text_vram_top_address, data);
                    694:                *(UINT32 *)(mem + byteaddress) = data;
                    695:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    696:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    697:                        write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1       root      698:                }
                    699:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4   root      700: #if defined(HAS_I386)
1.1.1.3   root      701:        } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4   root      702: #else
                    703:        } else {
                    704: #endif
1.1.1.3   root      705:                *(UINT32 *)(mem + byteaddress) = data;
1.1       root      706:        }
                    707: }
                    708: 
                    709: #define read_decrypted_byte read_byte
                    710: #define read_decrypted_word read_word
                    711: #define read_decrypted_dword read_dword
                    712: 
1.1.1.3   root      713: #define read_raw_byte read_byte
                    714: #define write_raw_byte write_byte
                    715: 
                    716: #define read_word_unaligned read_word
                    717: #define write_word_unaligned write_word
                    718: 
                    719: #define read_io_word_unaligned read_io_word
                    720: #define write_io_word_unaligned write_io_word
                    721: 
1.1       root      722: UINT8 read_io_byte(offs_t byteaddress);
                    723: UINT16 read_io_word(offs_t byteaddress);
                    724: UINT32 read_io_dword(offs_t byteaddress);
                    725: 
                    726: void write_io_byte(offs_t byteaddress, UINT8 data);
                    727: void write_io_word(offs_t byteaddress, UINT16 data);
                    728: void write_io_dword(offs_t byteaddress, UINT32 data);
                    729: 
                    730: /*****************************************************************************/
                    731: /* src/osd/osdcomm.h */
                    732: 
                    733: /* Highly useful macro for compile-time knowledge of an array size */
                    734: #define ARRAY_LENGTH(x)     (sizeof(x) / sizeof(x[0]))
                    735: 
1.1.1.54  root      736: // flag to exit MS-DOS Player
                    737: // this is set when the first process is terminated and jump to FFFF:0000 HALT
                    738: int m_exit = 0;
                    739: 
1.1.1.3   root      740: #if defined(HAS_I386)
1.1.1.10  root      741:        static CPU_TRANSLATE(i386);
                    742:        #include "mame/lib/softfloat/softfloat.c"
                    743:        #include "mame/emu/cpu/i386/i386.c"
1.1.1.12  root      744:        #include "mame/emu/cpu/vtlb.c"
1.1.1.3   root      745: #elif defined(HAS_I286)
1.1.1.10  root      746:        #include "mame/emu/cpu/i86/i286.c"
1.1.1.3   root      747: #else
1.1.1.10  root      748:        #include "mame/emu/cpu/i86/i86.c"
1.1.1.3   root      749: #endif
1.1.1.33  root      750: #ifdef USE_DEBUGGER
1.1.1.10  root      751:        #include "mame/emu/cpu/i386/i386dasm.c"
1.1       root      752: #endif
                    753: 
1.1.1.3   root      754: #if defined(HAS_I386)
                    755:        #define SREG(x)                         m_sreg[x].selector
                    756:        #define SREG_BASE(x)                    m_sreg[x].base
                    757:        int cpu_type, cpu_step;
                    758: #else
                    759:        #define REG8(x)                         m_regs.b[x]
                    760:        #define REG16(x)                        m_regs.w[x]
                    761:        #define SREG(x)                         m_sregs[x]
                    762:        #define SREG_BASE(x)                    m_base[x]
1.1.1.33  root      763:        #define m_eip                           (m_pc - m_base[CS])
1.1.1.3   root      764:        #define m_CF                            m_CarryVal
                    765:        #define m_a20_mask                      AMASK
                    766:        #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
                    767:        #if defined(HAS_I286)
                    768:                #define i386_set_a20_line(x)    i80286_set_a20_line(x)
                    769:        #else
                    770:                #define i386_set_a20_line(x)
                    771:        #endif
                    772:        #define i386_set_irq_line(x, y)         set_irq_line(x, y)
                    773: #endif
1.1       root      774: 
                    775: void i386_jmp_far(UINT16 selector, UINT32 address)
                    776: {
1.1.1.3   root      777: #if defined(HAS_I386)
1.1       root      778:        if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3   root      779:                i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1       root      780:        } else {
1.1.1.3   root      781:                SREG(CS) = selector;
                    782:                m_performed_intersegment_jump = 1;
                    783:                i386_load_segment_descriptor(CS);
                    784:                m_eip = address;
                    785:                CHANGE_PC(m_eip);
1.1       root      786:        }
1.1.1.3   root      787: #elif defined(HAS_I286)
                    788:        i80286_code_descriptor(selector, address, 1);
                    789: #else
                    790:        SREG(CS) = selector;
                    791:        i386_load_segment_descriptor(CS);
                    792:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    793: #endif
1.1       root      794: }
                    795: 
1.1.1.24  root      796: void i386_call_far(UINT16 selector, UINT32 address)
                    797: {
                    798: #if defined(HAS_I386)
                    799:        if(PROTECTED_MODE && !V8086_MODE) {
                    800:                i386_protected_mode_call(selector, address, 1, m_operand_size);
                    801:        } else {
                    802:                PUSH16(SREG(CS));
                    803:                PUSH16(m_eip);
                    804:                SREG(CS) = selector;
                    805:                m_performed_intersegment_jump = 1;
                    806:                i386_load_segment_descriptor(CS);
                    807:                m_eip = address;
                    808:                CHANGE_PC(m_eip);
                    809:        }
                    810: #else
                    811:        UINT16 ip = m_pc - SREG_BASE(CS);
                    812:        UINT16 cs = SREG(CS);
                    813: #if defined(HAS_I286)
                    814:        i80286_code_descriptor(selector, address, 2);
                    815: #else
                    816:        SREG(CS) = selector;
                    817:        i386_load_segment_descriptor(CS);
                    818:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    819: #endif
                    820:        PUSH(cs);
                    821:        PUSH(ip);
                    822:        CHANGE_PC(m_pc);
                    823: #endif
                    824: }
1.1.1.49  root      825: 
                    826: void i386_push16(UINT16 value)
                    827: {
                    828: #if defined(HAS_I386)
                    829:        PUSH16(value);
                    830: #else
                    831:        PUSH(value);
1.1.1.35  root      832: #endif
1.1.1.49  root      833: }
                    834: 
                    835: UINT16 i386_pop16()
                    836: {
                    837: #if defined(HAS_I386)
                    838:        return POP16();
                    839: #else
                    840:        UINT16 value;
                    841:        POP(value);
                    842:        return value;
                    843: #endif
                    844: }
1.1.1.24  root      845: 
1.1.1.29  root      846: UINT16 i386_read_stack()
                    847: {
                    848: #if defined(HAS_I386)
                    849:        UINT32 ea, new_esp;
                    850:        if( STACK_32BIT ) {
                    851:                new_esp = REG32(ESP) + 2;
                    852:                ea = i386_translate(SS, new_esp - 2, 0);
                    853:        } else {
                    854:                new_esp = REG16(SP) + 2;
                    855:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    856:        }
                    857:        return READ16(ea);
                    858: #else
                    859:        UINT16 sp = m_regs.w[SP] + 2;
                    860:        return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
                    861: #endif
                    862: }
                    863: 
1.1.1.53  root      864: void i386_write_stack(UINT16 value)
                    865: {
                    866: #if defined(HAS_I386)
                    867:        UINT32 ea, new_esp;
                    868:        if( STACK_32BIT ) {
                    869:                new_esp = REG32(ESP) + 2;
                    870:                ea = i386_translate(SS, new_esp - 2, 0);
                    871:        } else {
                    872:                new_esp = REG16(SP) + 2;
                    873:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    874:        }
                    875:        WRITE16(ea, value);
                    876: #else
                    877:        UINT16 sp = m_regs.w[SP] + 2;
                    878:        WriteWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK), value);
                    879: #endif
                    880: }
                    881: 
1.1       root      882: /* ----------------------------------------------------------------------------
1.1.1.33  root      883:        debugger
                    884: ---------------------------------------------------------------------------- */
                    885: 
                    886: #ifdef USE_DEBUGGER
                    887: #define TELNET_BLUE      0x0004 // text color contains blue.
                    888: #define TELNET_GREEN     0x0002 // text color contains green.
                    889: #define TELNET_RED       0x0001 // text color contains red.
                    890: #define TELNET_INTENSITY 0x0008 // text color is intensified.
                    891: 
                    892: int svr_socket = 0;
                    893: int cli_socket = 0;
                    894: 
1.1.1.55  root      895: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error);
1.1.1.33  root      896: 
                    897: void debugger_init()
                    898: {
                    899:        now_debugging = false;
                    900:        now_going = false;
                    901:        now_suspended = false;
                    902:        force_suspend = false;
                    903:        
                    904:        memset(&break_point, 0, sizeof(break_point_t));
                    905:        memset(&rd_break_point, 0, sizeof(break_point_t));
                    906:        memset(&wr_break_point, 0, sizeof(break_point_t));
                    907:        memset(&in_break_point, 0, sizeof(break_point_t));
                    908:        memset(&out_break_point, 0, sizeof(break_point_t));
                    909:        memset(&int_break_point, 0, sizeof(int_break_point_t));
                    910: }
                    911: 
1.1.1.45  root      912: void telnet_send(const char *string)
1.1.1.33  root      913: {
                    914:        char buffer[8192], *ptr;
                    915:        strcpy(buffer, string);
                    916:        while((ptr = strstr(buffer, "\n")) != NULL) {
                    917:                char tmp[8192];
                    918:                *ptr = '\0';
                    919:                sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
                    920:                strcpy(buffer, tmp);
                    921:        }
                    922:        
                    923:        int len = strlen(buffer), res;
                    924:        ptr = buffer;
                    925:        while(len > 0) {
                    926:                if((res = send(cli_socket, ptr, len, 0)) > 0) {
                    927:                        len -= res;
                    928:                        ptr += res;
                    929:                }
                    930:        }
                    931: }
                    932: 
                    933: void telnet_command(const char *format, ...)
                    934: {
                    935:        char buffer[1024];
                    936:        va_list ap;
                    937:        va_start(ap, format);
                    938:        vsprintf(buffer, format, ap);
                    939:        va_end(ap);
                    940:        
                    941:        telnet_send(buffer);
                    942: }
                    943: 
                    944: void telnet_printf(const char *format, ...)
                    945: {
                    946:        char buffer[1024];
                    947:        va_list ap;
                    948:        va_start(ap, format);
                    949:        vsprintf(buffer, format, ap);
                    950:        va_end(ap);
                    951:        
                    952:        if(fp_debugger != NULL) {
                    953:                fprintf(fp_debugger, "%s", buffer);
                    954:        }
                    955:        telnet_send(buffer);
                    956: }
                    957: 
                    958: bool telnet_gets(char *str, int n)
                    959: {
                    960:        char buffer[1024];
                    961:        int ptr = 0;
                    962:        
                    963:        telnet_command("\033[12l"); // local echo on
                    964:        telnet_command("\033[2l");  // key unlock
                    965:        
1.1.1.54  root      966:        while(!m_exit) {
1.1.1.33  root      967:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    968:                
                    969:                if(len > 0 && buffer[0] != 0xff) {
                    970:                        for(int i = 0; i < len; i++) {
                    971:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                    972:                                        str[ptr] = 0;
                    973:                                        telnet_command("\033[2h");  // key lock
                    974:                                        telnet_command("\033[12h"); // local echo off
1.1.1.54  root      975:                                        return(!m_exit);
1.1.1.33  root      976:                                } else if(buffer[i] == 0x08) {
                    977:                                        if(ptr > 0) {
                    978:                                                telnet_command("\033[0K"); // erase from cursor position
                    979:                                                ptr--;
                    980:                                        } else {
                    981:                                                telnet_command("\033[1C"); // move cursor forward
                    982:                                        }
                    983:                                } else if(ptr < n - 1) {
1.1.1.37  root      984:                                        if(buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
1.1.1.33  root      985:                                                str[ptr++] = buffer[i];
                    986:                                        }
                    987:                                } else {
                    988:                                        telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
                    989:                                }
                    990:                        }
                    991:                } else if(len == -1) {
                    992:                        if(WSAGetLastError() != WSAEWOULDBLOCK) {
                    993:                                return(false);
                    994:                        }
                    995:                } else if(len == 0) {
                    996:                        return(false);
                    997:                }
                    998:                Sleep(10);
                    999:        }
1.1.1.54  root     1000:        return(!m_exit);
1.1.1.33  root     1001: }
                   1002: 
                   1003: bool telnet_kbhit()
                   1004: {
                   1005:        char buffer[1024];
                   1006:        
1.1.1.54  root     1007:        if(!m_exit) {
1.1.1.33  root     1008:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1009:                
                   1010:                if(len > 0) {
                   1011:                        for(int i = 0; i < len; i++) {
                   1012:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                   1013:                                        return(true);
                   1014:                                }
                   1015:                        }
                   1016:                } else if(len == 0) {
                   1017:                        return(true); // disconnected
                   1018:                }
                   1019:        }
                   1020:        return(false);
                   1021: }
                   1022: 
                   1023: bool telnet_disconnected()
                   1024: {
                   1025:        char buffer[1024];
                   1026:        int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1027:        
                   1028:        if(len == 0) {
                   1029:                return(true);
                   1030:        } else if(len == -1) {
                   1031:                if(WSAGetLastError() != WSAEWOULDBLOCK) {
                   1032:                        return(true);
                   1033:                }
                   1034:        }
                   1035:        return(false);
                   1036: }
                   1037: 
                   1038: void telnet_set_color(int color)
                   1039: {
                   1040:        telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
                   1041: }
                   1042: 
                   1043: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
                   1044: {
                   1045: //     UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
                   1046:        UINT8 ops[16];
                   1047:        for(int i = 0; i < 16; i++) {
                   1048:                ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
                   1049:        }
                   1050:        UINT8 *oprom = ops;
                   1051:        
                   1052: #if defined(HAS_I386)
                   1053:        if(m_operand_size) {
                   1054:                return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
                   1055:        } else
                   1056: #endif
                   1057:        return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
                   1058: }
                   1059: 
                   1060: void debugger_regs_info(char *buffer)
                   1061: {
                   1062: #if defined(HAS_I386)
                   1063:        UINT32 flags = get_flags();
                   1064: #else
                   1065:        UINT32 flags = CompressFlags();
                   1066: #endif
                   1067: #if defined(HAS_I386)
                   1068:        if(m_operand_size) {
                   1069:                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",
                   1070:                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),
                   1071:                PROTECTED_MODE ? "PE" : "--",
                   1072:                (flags & 0x40000) ? 'A' : '-',
                   1073:                (flags & 0x20000) ? 'V' : '-',
                   1074:                (flags & 0x10000) ? 'R' : '-',
                   1075:                (flags & 0x04000) ? 'N' : '-',
                   1076:                (flags & 0x02000) ? '1' : '0',
                   1077:                (flags & 0x01000) ? '1' : '0',
                   1078:                (flags & 0x00800) ? 'O' : '-',
                   1079:                (flags & 0x00400) ? 'D' : '-',
                   1080:                (flags & 0x00200) ? 'I' : '-',
                   1081:                (flags & 0x00100) ? 'T' : '-',
                   1082:                (flags & 0x00080) ? 'S' : '-',
                   1083:                (flags & 0x00040) ? 'Z' : '-',
                   1084:                (flags & 0x00010) ? 'A' : '-',
                   1085:                (flags & 0x00004) ? 'P' : '-',
                   1086:                (flags & 0x00001) ? 'C' : '-');
                   1087:        } else {
                   1088: #endif
                   1089:                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",
                   1090:                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),
                   1091: #if defined(HAS_I386)
                   1092:                PROTECTED_MODE ? "PE" : "--",
                   1093: #else
                   1094:                "--",
                   1095: #endif
                   1096:                (flags & 0x40000) ? 'A' : '-',
                   1097:                (flags & 0x20000) ? 'V' : '-',
                   1098:                (flags & 0x10000) ? 'R' : '-',
                   1099:                (flags & 0x04000) ? 'N' : '-',
                   1100:                (flags & 0x02000) ? '1' : '0',
                   1101:                (flags & 0x01000) ? '1' : '0',
                   1102:                (flags & 0x00800) ? 'O' : '-',
                   1103:                (flags & 0x00400) ? 'D' : '-',
                   1104:                (flags & 0x00200) ? 'I' : '-',
                   1105:                (flags & 0x00100) ? 'T' : '-',
                   1106:                (flags & 0x00080) ? 'S' : '-',
                   1107:                (flags & 0x00040) ? 'Z' : '-',
                   1108:                (flags & 0x00010) ? 'A' : '-',
                   1109:                (flags & 0x00004) ? 'P' : '-',
                   1110:                (flags & 0x00001) ? 'C' : '-');
                   1111: #if defined(HAS_I386)
                   1112:        }
                   1113: #endif
                   1114: }
                   1115: 
                   1116: void debugger_process_info(char *buffer)
                   1117: {
                   1118:        UINT16 psp_seg = current_psp;
                   1119:        process_t *process;
                   1120:        bool check[0x10000] = {0};
                   1121:        
                   1122:        buffer[0] = '\0';
                   1123:        
                   1124:        while(!check[psp_seg] && (process  = msdos_process_info_get(psp_seg, false)) != NULL) {
                   1125:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1126:                char *file = process->module_path, *s;
                   1127:                char tmp[8192];
                   1128:                
                   1129:                while((s = strstr(file, "\\")) != NULL) {
                   1130:                        file = s + 1;
                   1131:                }
                   1132:                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));
                   1133:                strcat(tmp, buffer);
                   1134:                strcpy(buffer, tmp);
                   1135:                
                   1136:                check[psp_seg] = true;
                   1137:                psp_seg = psp->parent_psp;
                   1138:        }
                   1139: }
                   1140: 
                   1141: UINT32 debugger_get_val(const char *str)
                   1142: {
                   1143:        char tmp[1024];
                   1144:        
                   1145:        if(str == NULL || strlen(str) == 0) {
                   1146:                return(0);
                   1147:        }
                   1148:        strcpy(tmp, str);
                   1149:        
                   1150:        if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
                   1151:                // ank
                   1152:                return(tmp[1] & 0xff);
                   1153:        } else if(tmp[0] == '%') {
                   1154:                // decimal
                   1155:                return(strtoul(tmp + 1, NULL, 10));
                   1156:        }
                   1157:        return(strtoul(tmp, NULL, 16));
                   1158: }
                   1159: 
                   1160: UINT32 debugger_get_seg(const char *str, UINT32 val)
                   1161: {
                   1162:        char tmp[1024], *s;
                   1163:        
                   1164:        if(str == NULL || strlen(str) == 0) {
                   1165:                return(val);
                   1166:        }
                   1167:        strcpy(tmp, str);
                   1168:        
                   1169:        if((s = strstr(tmp, ":")) != NULL) {
                   1170:                // 0000:0000
                   1171:                *s = '\0';
                   1172:                return(debugger_get_val(tmp));
                   1173:        }
                   1174:        return(val);
                   1175: }
                   1176: 
                   1177: UINT32 debugger_get_ofs(const char *str)
                   1178: {
                   1179:        char tmp[1024], *s;
                   1180:        
                   1181:        if(str == NULL || strlen(str) == 0) {
                   1182:                return(0);
                   1183:        }
                   1184:        strcpy(tmp, str);
                   1185:        
                   1186:        if((s = strstr(tmp, ":")) != NULL) {
                   1187:                // 0000:0000
                   1188:                return(debugger_get_val(s + 1));
                   1189:        }
                   1190:        return(debugger_get_val(tmp));
                   1191: }
                   1192: 
                   1193: void debugger_main()
                   1194: {
                   1195:        telnet_command("\033[20h"); // cr-lf
                   1196:        
                   1197:        force_suspend = true;
                   1198:        now_going = false;
                   1199:        now_debugging = true;
                   1200:        Sleep(100);
                   1201:        
1.1.1.54  root     1202:        if(!m_exit && !now_suspended) {
1.1.1.33  root     1203:                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1204:                telnet_printf("waiting until cpu is suspended...\n");
                   1205:        }
1.1.1.54  root     1206:        while(!m_exit && !now_suspended) {
1.1.1.33  root     1207:                if(telnet_disconnected()) {
                   1208:                        break;
                   1209:                }
                   1210:                Sleep(10);
                   1211:        }
                   1212:        
                   1213:        char buffer[8192];
                   1214:        
                   1215:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1216:        debugger_process_info(buffer);
                   1217:        telnet_printf("%s", buffer);
                   1218:        debugger_regs_info(buffer);
                   1219:        telnet_printf("%s", buffer);
                   1220:        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1221:        telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
                   1222:        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1223:        debugger_dasm(buffer, SREG(CS), m_eip);
                   1224:        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1225:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1226:        
                   1227:        #define MAX_COMMAND_LEN 64
                   1228:        
                   1229:        char command[MAX_COMMAND_LEN + 1];
                   1230:        char prev_command[MAX_COMMAND_LEN + 1] = {0};
                   1231:        
                   1232:        UINT32 data_seg = SREG(DS);
                   1233:        UINT32 data_ofs = 0;
                   1234:        UINT32 dasm_seg = SREG(CS);
                   1235:        UINT32 dasm_ofs = m_eip;
                   1236:        
1.1.1.54  root     1237:        while(!m_exit) {
1.1.1.33  root     1238:                telnet_printf("- ");
                   1239:                command[0] = '\0';
                   1240:                
                   1241:                if(fi_debugger != NULL) {
                   1242:                        while(command[0] == '\0') {
                   1243:                                if(fgets(command, sizeof(command), fi_debugger) == NULL) {
                   1244:                                        break;
                   1245:                                }
                   1246:                                while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
                   1247:                                        command[strlen(command) - 1] = '\0';
                   1248:                                }
                   1249:                        }
                   1250:                        if(command[0] != '\0') {
                   1251:                                telnet_command("%s\n", command);
                   1252:                        }
                   1253:                }
                   1254:                if(command[0] == '\0') {
                   1255:                        if(!telnet_gets(command, sizeof(command))) {
                   1256:                                break;
                   1257:                        }
                   1258:                }
                   1259:                if(command[0] == '\0') {
                   1260:                        strcpy(command, prev_command);
                   1261:                } else {
                   1262:                        strcpy(prev_command, command);
                   1263:                }
                   1264:                if(fp_debugger != NULL) {
                   1265:                        fprintf(fp_debugger, "%s\n", command);
                   1266:                }
                   1267:                
1.1.1.54  root     1268:                if(!m_exit && command[0] != 0) {
1.1.1.33  root     1269:                        char *params[32], *token = NULL;
                   1270:                        int num = 0;
                   1271:                        
                   1272:                        if((token = strtok(command, " ")) != NULL) {
                   1273:                                params[num++] = token;
                   1274:                                while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
                   1275:                                        params[num++] = token;
                   1276:                                }
                   1277:                        }
                   1278:                        if(stricmp(params[0], "D") == 0) {
                   1279:                                if(num <= 3) {
                   1280:                                        if(num >= 2) {
                   1281:                                                data_seg = debugger_get_seg(params[1], data_seg);
                   1282:                                                data_ofs = debugger_get_ofs(params[1]);
                   1283:                                        }
                   1284:                                        UINT32 end_seg = data_seg;
                   1285:                                        UINT32 end_ofs = data_ofs + 8 * 16 - 1;
                   1286:                                        if(num == 3) {
                   1287:                                                end_seg = debugger_get_seg(params[2], data_seg);
                   1288:                                                end_ofs = debugger_get_ofs(params[2]);
                   1289:                                        }
                   1290:                                        UINT64 start_addr = (data_seg << 4) + data_ofs;
                   1291:                                        UINT64 end_addr = (end_seg << 4) + end_ofs;
1.1.1.37  root     1292: //                                     bool is_sjis = false;
1.1.1.33  root     1293:                                        
                   1294:                                        for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
                   1295:                                                if((addr & 0x0f) == 0) {
                   1296:                                                        if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
                   1297:                                                                data_seg += 0x1000;
                   1298:                                                                data_ofs -= 0x10000;
                   1299:                                                        }
                   1300:                                                        telnet_printf("%06X:%04X ", data_seg, data_ofs);
                   1301:                                                        memset(buffer, 0, sizeof(buffer));
                   1302:                                                }
                   1303:                                                if(addr < start_addr || addr > end_addr) {
                   1304:                                                        telnet_printf("   ");
                   1305:                                                        buffer[addr & 0x0f] = ' ';
                   1306:                                                } else {
                   1307:                                                        UINT8 data = debugger_read_byte(addr & ADDR_MASK);
                   1308:                                                        telnet_printf(" %02X", data);
1.1.1.37  root     1309: //                                                     if(is_sjis) {
1.1.1.33  root     1310: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1311: //                                                             is_sjis = false;
1.1.1.33  root     1312: //                                                     } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
                   1313: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1314: //                                                             is_sjis = true;
1.1.1.33  root     1315: //                                                     } else
                   1316:                                                        if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
                   1317:                                                                buffer[addr & 0x0f] = data;
                   1318:                                                        } else {
                   1319:                                                                buffer[addr & 0x0f] = '.';
                   1320:                                                        }
                   1321:                                                }
                   1322:                                                if((addr & 0x0f) == 0x0f) {
                   1323:                                                        telnet_printf("  %s\n", buffer);
                   1324:                                                }
                   1325:                                        }
                   1326:                                        if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
                   1327:                                                data_seg += 0x1000;
                   1328:                                                data_ofs -= 0x10000;
                   1329:                                        }
                   1330:                                        prev_command[1] = '\0'; // remove parameters to dump continuously
                   1331:                                } else {
                   1332:                                        telnet_printf("invalid parameter number\n");
                   1333:                                }
                   1334:                        } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 0) {
                   1335:                                if(num >= 3) {
                   1336:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1337:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1338:                                        for(int i = 2, j = 0; i < num; i++, j++) {
                   1339:                                                debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
                   1340:                                        }
                   1341:                                } else {
                   1342:                                        telnet_printf("invalid parameter number\n");
                   1343:                                }
                   1344:                        } else if(stricmp(params[0], "EW") == 0) {
                   1345:                                if(num >= 3) {
                   1346:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1347:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1348:                                        for(int i = 2, j = 0; i < num; i++, j += 2) {
                   1349:                                                debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
                   1350:                                        }
                   1351:                                } else {
                   1352:                                        telnet_printf("invalid parameter number\n");
                   1353:                                }
                   1354:                        } else if(stricmp(params[0], "ED") == 0) {
                   1355:                                if(num >= 3) {
                   1356:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1357:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1358:                                        for(int i = 2, j = 0; i < num; i++, j += 4) {
                   1359:                                                debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
                   1360:                                        }
                   1361:                                } else {
                   1362:                                        telnet_printf("invalid parameter number\n");
                   1363:                                }
                   1364:                        } else if(stricmp(params[0], "EA") == 0) {
                   1365:                                if(num >= 3) {
                   1366:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1367:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1368:                                        strcpy(buffer, prev_command);
                   1369:                                        if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
                   1370:                                                int len = strlen(token);
                   1371:                                                for(int i = 0; i < len; i++) {
                   1372:                                                        debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
                   1373:                                                }
                   1374:                                        } else {
                   1375:                                                telnet_printf("invalid parameter\n");
                   1376:                                        }
                   1377:                                } else {
                   1378:                                        telnet_printf("invalid parameter number\n");
                   1379:                                }
                   1380:                        } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
                   1381:                                if(num == 2) {
                   1382:                                        telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
                   1383:                                } else {
                   1384:                                        telnet_printf("invalid parameter number\n");
                   1385:                                }
                   1386:                        } else if(stricmp(params[0], "IW") == 0) {
                   1387:                                if(num == 2) {
                   1388:                                        telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
                   1389:                                } else {
                   1390:                                        telnet_printf("invalid parameter number\n");
                   1391:                                }
                   1392:                        } else if(stricmp(params[0], "ID") == 0) {
                   1393:                                if(num == 2) {
                   1394:                                        telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
                   1395:                                } else {
                   1396:                                        telnet_printf("invalid parameter number\n");
                   1397:                                }
                   1398:                        } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
                   1399:                                if(num == 3) {
                   1400:                                        debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
                   1401:                                } else {
                   1402:                                        telnet_printf("invalid parameter number\n");
                   1403:                                }
                   1404:                        } else if(stricmp(params[0], "OW") == 0) {
                   1405:                                if(num == 3) {
                   1406:                                        debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
                   1407:                                } else {
                   1408:                                        telnet_printf("invalid parameter number\n");
                   1409:                                }
                   1410:                        } else if(stricmp(params[0], "OD") == 0) {
                   1411:                                if(num == 3) {
                   1412:                                        debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
                   1413:                                } else {
                   1414:                                        telnet_printf("invalid parameter number\n");
                   1415:                                }
                   1416:                        } else if(stricmp(params[0], "R") == 0) {
                   1417:                                if(num == 1) {
                   1418:                                        debugger_regs_info(buffer);
                   1419:                                        telnet_printf("%s", buffer);
                   1420:                                } else if(num == 3) {
                   1421: #if defined(HAS_I386)
                   1422:                                        if(stricmp(params[1], "EAX") == 0) {
                   1423:                                                REG32(EAX) = debugger_get_val(params[2]);
                   1424:                                        } else if(stricmp(params[1], "EBX") == 0) {
                   1425:                                                REG32(EBX) = debugger_get_val(params[2]);
                   1426:                                        } else if(stricmp(params[1], "ECX") == 0) {
                   1427:                                                REG32(ECX) = debugger_get_val(params[2]);
                   1428:                                        } else if(stricmp(params[1], "EDX") == 0) {
                   1429:                                                REG32(EDX) = debugger_get_val(params[2]);
                   1430:                                        } else if(stricmp(params[1], "ESP") == 0) {
                   1431:                                                REG32(ESP) = debugger_get_val(params[2]);
                   1432:                                        } else if(stricmp(params[1], "EBP") == 0) {
                   1433:                                                REG32(EBP) = debugger_get_val(params[2]);
                   1434:                                        } else if(stricmp(params[1], "ESI") == 0) {
                   1435:                                                REG32(ESI) = debugger_get_val(params[2]);
                   1436:                                        } else if(stricmp(params[1], "EDI") == 0) {
                   1437:                                                REG32(EDI) = debugger_get_val(params[2]);
                   1438:                                        } else
                   1439: #endif
                   1440:                                        if(stricmp(params[1], "AX") == 0) {
                   1441:                                                REG16(AX) = debugger_get_val(params[2]);
                   1442:                                        } else if(stricmp(params[1], "BX") == 0) {
                   1443:                                                REG16(BX) = debugger_get_val(params[2]);
                   1444:                                        } else if(stricmp(params[1], "CX") == 0) {
                   1445:                                                REG16(CX) = debugger_get_val(params[2]);
                   1446:                                        } else if(stricmp(params[1], "DX") == 0) {
                   1447:                                                REG16(DX) = debugger_get_val(params[2]);
                   1448:                                        } else if(stricmp(params[1], "SP") == 0) {
                   1449:                                                REG16(SP) = debugger_get_val(params[2]);
                   1450:                                        } else if(stricmp(params[1], "BP") == 0) {
                   1451:                                                REG16(BP) = debugger_get_val(params[2]);
                   1452:                                        } else if(stricmp(params[1], "SI") == 0) {
                   1453:                                                REG16(SI) = debugger_get_val(params[2]);
                   1454:                                        } else if(stricmp(params[1], "DI") == 0) {
                   1455:                                                REG16(DI) = debugger_get_val(params[2]);
                   1456:                                        } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
                   1457: #if defined(HAS_I386)
                   1458:                                                if(m_operand_size) {
                   1459:                                                        m_eip = debugger_get_val(params[2]);
                   1460:                                                } else {
                   1461:                                                        m_eip = debugger_get_val(params[2]) & 0xffff;
                   1462:                                                }
                   1463:                                                CHANGE_PC(m_eip);
                   1464: #else
                   1465:                                                m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
                   1466:                                                CHANGE_PC(m_pc);
                   1467: #endif
                   1468:                                        } else if(stricmp(params[1], "AL") == 0) {
                   1469:                                                REG8(AL) = debugger_get_val(params[2]);
                   1470:                                        } else if(stricmp(params[1], "AH") == 0) {
                   1471:                                                REG8(AH) = debugger_get_val(params[2]);
                   1472:                                        } else if(stricmp(params[1], "BL") == 0) {
                   1473:                                                REG8(BL) = debugger_get_val(params[2]);
                   1474:                                        } else if(stricmp(params[1], "BH") == 0) {
                   1475:                                                REG8(BH) = debugger_get_val(params[2]);
                   1476:                                        } else if(stricmp(params[1], "CL") == 0) {
                   1477:                                                REG8(CL) = debugger_get_val(params[2]);
                   1478:                                        } else if(stricmp(params[1], "CH") == 0) {
                   1479:                                                REG8(CH) = debugger_get_val(params[2]);
                   1480:                                        } else if(stricmp(params[1], "DL") == 0) {
                   1481:                                                REG8(DL) = debugger_get_val(params[2]);
                   1482:                                        } else if(stricmp(params[1], "DH") == 0) {
                   1483:                                                REG8(DH) = debugger_get_val(params[2]);
                   1484:                                        } else {
                   1485:                                                telnet_printf("unknown register %s\n", params[1]);
                   1486:                                        }
                   1487:                                } else {
                   1488:                                        telnet_printf("invalid parameter number\n");
                   1489:                                }
                   1490:                        } else if(_tcsicmp(params[0], "S") == 0) {
                   1491:                                if(num >= 4) {
                   1492:                                        UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
                   1493:                                        UINT32 cur_ofs = debugger_get_ofs(params[1]);
                   1494:                                        UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
                   1495:                                        UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1496:                                        UINT8 list[32];
                   1497:                                        
                   1498:                                        for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1499:                                                list[j] = debugger_get_val(params[i]);
                   1500:                                        }
                   1501:                                        while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
                   1502:                                                bool found = true;
                   1503:                                                for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1504:                                                        if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
                   1505:                                                                found = false;
                   1506:                                                                break;
                   1507:                                                        }
                   1508:                                                }
                   1509:                                                if(found) {
                   1510:                                                        telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
                   1511:                                                }
                   1512:                                                if((cur_ofs += 1) > 0xffff) {
                   1513:                                                        cur_seg += 0x1000;
                   1514:                                                        cur_ofs -= 0x10000;
                   1515:                                                }
                   1516:                                        }
                   1517:                                } else {
                   1518:                                        telnet_printf("invalid parameter number\n");
                   1519:                                }
                   1520:                        } else if(stricmp(params[0], "U") == 0) {
                   1521:                                if(num <= 3) {
                   1522:                                        if(num >= 2) {
                   1523:                                                dasm_seg = debugger_get_seg(params[1], dasm_seg);
                   1524:                                                dasm_ofs = debugger_get_ofs(params[1]);
                   1525:                                        }
                   1526:                                        if(num == 3) {
                   1527:                                                UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
                   1528:                                                UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1529:                                                
                   1530:                                                while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
                   1531:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1532:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1533:                                                        for(int i = 0; i < len; i++) {
                   1534:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1535:                                                        }
                   1536:                                                        for(int i = len; i < 8; i++) {
                   1537:                                                                telnet_printf("  ");
                   1538:                                                        }
                   1539:                                                        telnet_printf("  %s\n", buffer);
                   1540:                                                        if((dasm_ofs += len) > 0xffff) {
                   1541:                                                                dasm_seg += 0x1000;
                   1542:                                                                dasm_ofs -= 0x10000;
                   1543:                                                        }
                   1544:                                                }
                   1545:                                        } else {
                   1546:                                                for(int i = 0; i < 16; i++) {
                   1547:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1548:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1549:                                                        for(int i = 0; i < len; i++) {
                   1550:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1551:                                                        }
                   1552:                                                        for(int i = len; i < 8; i++) {
                   1553:                                                                telnet_printf("  ");
                   1554:                                                        }
                   1555:                                                        telnet_printf("  %s\n", buffer);
                   1556:                                                        if((dasm_ofs += len) > 0xffff) {
                   1557:                                                                dasm_seg += 0x1000;
                   1558:                                                                dasm_ofs -= 0x10000;
                   1559:                                                        }
                   1560:                                                }
                   1561:                                        }
                   1562:                                        prev_command[1] = '\0'; // remove parameters to disassemble continuously
                   1563:                                } else {
                   1564:                                        telnet_printf("invalid parameter number\n");
                   1565:                                }
                   1566:                        } else if(stricmp(params[0], "H") == 0) {
                   1567:                                if(num == 3) {
                   1568:                                        UINT32 l = debugger_get_val(params[1]);
                   1569:                                        UINT32 r = debugger_get_val(params[2]);
                   1570:                                        telnet_printf("%08X  %08X\n", l + r, l - r);
                   1571:                                } else {
                   1572:                                        telnet_printf("invalid parameter number\n");
                   1573:                                }
                   1574:                        } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
                   1575:                                break_point_t *break_point_ptr;
                   1576:                                #define GET_BREAK_POINT_PTR() { \
                   1577:                                        if(params[0][0] == 'R') { \
                   1578:                                                break_point_ptr = &rd_break_point; \
                   1579:                                        } else if(params[0][0] == 'W') { \
                   1580:                                                break_point_ptr = &wr_break_point; \
                   1581:                                        } else if(params[0][0] == 'I') { \
                   1582:                                                break_point_ptr = &in_break_point; \
                   1583:                                        } else if(params[0][0] == 'O') { \
                   1584:                                                break_point_ptr = &out_break_point; \
                   1585:                                        } else { \
                   1586:                                                break_point_ptr = &break_point; \
                   1587:                                        } \
                   1588:                                }
                   1589:                                GET_BREAK_POINT_PTR();
                   1590:                                if(num == 2) {
                   1591:                                        UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1592:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1593:                                        bool found = false;
                   1594:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1595:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
                   1596:                                                        break_point_ptr->table[i].addr = (seg << 4) + ofs;
                   1597:                                                        break_point_ptr->table[i].seg = seg;
                   1598:                                                        break_point_ptr->table[i].ofs = ofs;
                   1599:                                                        break_point_ptr->table[i].status = 1;
                   1600:                                                        found = true;
                   1601:                                                }
                   1602:                                        }
                   1603:                                        if(!found) {
                   1604:                                                telnet_printf("too many break points\n");
                   1605:                                        }
                   1606:                                } else {
                   1607:                                        telnet_printf("invalid parameter number\n");
                   1608:                                }
                   1609:                        } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
                   1610:                                break_point_t *break_point_ptr;
                   1611:                                GET_BREAK_POINT_PTR();
                   1612:                                if(num == 2) {
                   1613:                                        UINT32 addr = debugger_get_val(params[1]);
                   1614:                                        bool found = false;
                   1615:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1616:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
                   1617:                                                        break_point_ptr->table[i].addr = addr;
                   1618:                                                        break_point_ptr->table[i].status = 1;
                   1619:                                                        found = true;
                   1620:                                                }
                   1621:                                        }
                   1622:                                        if(!found) {
                   1623:                                                telnet_printf("too many break points\n");
                   1624:                                        }
                   1625:                                } else {
                   1626:                                        telnet_printf("invalid parameter number\n");
                   1627:                                }
                   1628:                        } 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) {
                   1629:                                break_point_t *break_point_ptr;
                   1630:                                GET_BREAK_POINT_PTR();
                   1631:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1632:                                        memset(break_point_ptr, 0, sizeof(break_point_t));
                   1633:                                } else if(num >= 2) {
                   1634:                                        for(int i = 1; i < num; i++) {
                   1635:                                                int index = debugger_get_val(params[i]);
                   1636:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1637:                                                        telnet_printf("invalid index %x\n", index);
                   1638:                                                } else {
                   1639:                                                        break_point_ptr->table[index - 1].addr = 0;
                   1640:                                                        break_point_ptr->table[index - 1].seg = 0;
                   1641:                                                        break_point_ptr->table[index - 1].ofs = 0;
                   1642:                                                        break_point_ptr->table[index - 1].status = 0;
                   1643:                                                }
                   1644:                                        }
                   1645:                                } else {
                   1646:                                        telnet_printf("invalid parameter number\n");
                   1647:                                }
                   1648:                        } 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 ||
                   1649:                                  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) {
                   1650:                                break_point_t *break_point_ptr;
                   1651:                                GET_BREAK_POINT_PTR();
                   1652:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1653:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1654:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1655:                                                if(break_point_ptr->table[i].status != 0) {
                   1656:                                                        break_point_ptr->table[i].status = enabled ? 1 : -1;
                   1657:                                                }
                   1658:                                        }
                   1659:                                } else if(num >= 2) {
                   1660:                                        for(int i = 1; i < num; i++) {
                   1661:                                                int index = debugger_get_val(params[i]);
                   1662:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1663:                                                        telnet_printf("invalid index %x\n", index);
                   1664:                                                } else if(break_point_ptr->table[index - 1].status == 0) {
                   1665:                                                        telnet_printf("break point %x is null\n", index);
                   1666:                                                } else {
                   1667:                                                        break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
                   1668:                                                }
                   1669:                                        }
                   1670:                                } else {
                   1671:                                        telnet_printf("invalid parameter number\n");
                   1672:                                }
                   1673:                        } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
                   1674:                                break_point_t *break_point_ptr;
                   1675:                                GET_BREAK_POINT_PTR();
                   1676:                                if(num == 1) {
                   1677:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1678:                                                if(break_point_ptr->table[i].status) {
                   1679:                                                        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);
                   1680:                                                }
                   1681:                                        }
                   1682:                                } else {
                   1683:                                        telnet_printf("invalid parameter number\n");
                   1684:                                }
                   1685:                        } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
                   1686:                                break_point_t *break_point_ptr;
                   1687:                                GET_BREAK_POINT_PTR();
                   1688:                                if(num == 1) {
                   1689:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1690:                                                if(break_point_ptr->table[i].status) {
                   1691:                                                        telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
                   1692:                                                }
                   1693:                                        }
                   1694:                                } else {
                   1695:                                        telnet_printf("invalid parameter number\n");
                   1696:                                }
                   1697:                        } else if(stricmp(params[0], "INTBP") == 0) {
                   1698:                                if(num >= 2 && num <= 4) {
                   1699:                                        int int_num = debugger_get_val(params[1]);
                   1700:                                        UINT8 ah = 0, ah_registered = 0;
                   1701:                                        UINT8 al = 0, al_registered = 0;
                   1702:                                        if(num >= 3) {
                   1703:                                                ah = debugger_get_val(params[2]);
                   1704:                                                ah_registered = 1;
                   1705:                                        }
                   1706:                                        if(num == 4) {
                   1707:                                                al = debugger_get_val(params[3]);
                   1708:                                                al_registered = 1;
                   1709:                                        }
                   1710:                                        bool found = false;
                   1711:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1712:                                                if(int_break_point.table[i].status == 0 || (
                   1713:                                                   int_break_point.table[i].int_num == int_num &&
                   1714:                                                   int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
                   1715:                                                   int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
                   1716:                                                        int_break_point.table[i].int_num = int_num;
                   1717:                                                        int_break_point.table[i].ah = ah;
                   1718:                                                        int_break_point.table[i].ah_registered = ah_registered;
                   1719:                                                        int_break_point.table[i].al = al;
                   1720:                                                        int_break_point.table[i].al_registered = al_registered;
                   1721:                                                        int_break_point.table[i].status = 1;
                   1722:                                                        found = true;
                   1723:                                                }
                   1724:                                        }
                   1725:                                        if(!found) {
                   1726:                                                telnet_printf("too many break points\n");
                   1727:                                        }
                   1728:                                } else {
                   1729:                                        telnet_printf("invalid parameter number\n");
                   1730:                                }
                   1731:                        } else if(stricmp(params[0], "INTBC") == 0) {
                   1732:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1733:                                        memset(&int_break_point, 0, sizeof(int_break_point_t));
                   1734:                                } else if(num >= 2) {
                   1735:                                        for(int i = 1; i < num; i++) {
                   1736:                                                int index = debugger_get_val(params[i]);
                   1737:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1738:                                                        telnet_printf("invalid index %x\n", index);
                   1739:                                                } else {
                   1740:                                                        int_break_point.table[index - 1].int_num = 0;
                   1741:                                                        int_break_point.table[index - 1].ah = 0;
                   1742:                                                        int_break_point.table[index - 1].ah_registered = 0;
                   1743:                                                        int_break_point.table[index - 1].al = 0;
                   1744:                                                        int_break_point.table[index - 1].al_registered = 0;
                   1745:                                                        int_break_point.table[index - 1].status = 0;
                   1746:                                                }
                   1747:                                        }
                   1748:                                } else {
                   1749:                                        telnet_printf("invalid parameter number\n");
                   1750:                                }
                   1751:                        } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
                   1752:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1753:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1754:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1755:                                                if(int_break_point.table[i].status != 0) {
                   1756:                                                        int_break_point.table[i].status = enabled ? 1 : -1;
                   1757:                                                }
                   1758:                                        }
                   1759:                                } else if(num >= 2) {
                   1760:                                        for(int i = 1; i < num; i++) {
                   1761:                                                int index = debugger_get_val(params[i]);
                   1762:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1763:                                                        telnet_printf("invalid index %x\n", index);
                   1764:                                                } else if(int_break_point.table[index - 1].status == 0) {
                   1765:                                                        telnet_printf("break point %x is null\n", index);
                   1766:                                                } else {
                   1767:                                                        int_break_point.table[index - 1].status = enabled ? 1 : -1;
                   1768:                                                }
                   1769:                                        }
                   1770:                                } else {
                   1771:                                        telnet_printf("invalid parameter number\n");
                   1772:                                }
                   1773:                        } else if(stricmp(params[0], "INTBL") == 0) {
                   1774:                                if(num == 1) {
                   1775:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1776:                                                if(int_break_point.table[i].status) {
                   1777:                                                        telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
                   1778:                                                        if(int_break_point.table[i].ah_registered) {
                   1779:                                                                telnet_printf(" %02X", int_break_point.table[i].ah);
                   1780:                                                        }
                   1781:                                                        if(int_break_point.table[i].al_registered) {
                   1782:                                                                telnet_printf(" %02X", int_break_point.table[i].al);
                   1783:                                                        }
                   1784:                                                        telnet_printf("\n");
                   1785:                                                }
                   1786:                                        }
                   1787:                                } else {
                   1788:                                        telnet_printf("invalid parameter number\n");
                   1789:                                }
                   1790:                        } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
                   1791:                                if(num == 1 || num == 2) {
                   1792:                                        break_point_t break_point_stored;
                   1793:                                        bool break_points_stored = false;
                   1794:                                        
                   1795:                                        if(stricmp(params[0], "P") == 0) {
                   1796:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1797:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1798:                                                break_points_stored = true;
                   1799:                                                
                   1800:                                                break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
                   1801:                                                break_point.table[0].status = 1;
                   1802:                                        } else if(num >= 2) {
                   1803:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1804:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1805:                                                break_points_stored = true;
                   1806:                                                
                   1807:                                                UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1808:                                                UINT32 ofs = debugger_get_ofs(params[1]);
                   1809:                                                break_point.table[0].addr = (seg << 4) + ofs;
                   1810:                                                break_point.table[0].seg = seg;
                   1811:                                                break_point.table[0].ofs = ofs;
                   1812:                                                break_point.table[0].status = 1;
                   1813:                                        }
                   1814:                                        break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1815:                                        now_going = true;
                   1816:                                        now_suspended = false;
                   1817:                                        
                   1818:                                        telnet_command("\033[2l"); // key unlock
1.1.1.54  root     1819:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1820:                                                if(telnet_kbhit()) {
                   1821:                                                        break;
                   1822:                                                }
                   1823:                                                Sleep(10);
                   1824:                                        }
                   1825:                                        now_going = false;
                   1826:                                        telnet_command("\033[2h"); // key lock
                   1827:                                        
                   1828:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1829:                                                Sleep(100);
1.1.1.54  root     1830:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1831:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1832:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1833:                                                }
                   1834:                                        }
1.1.1.54  root     1835:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1836:                                                if(telnet_disconnected()) {
                   1837:                                                        break;
                   1838:                                                }
                   1839:                                                Sleep(10);
                   1840:                                        }
                   1841:                                        dasm_seg = SREG(CS);
                   1842:                                        dasm_ofs = m_eip;
                   1843:                                        
                   1844:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1845:                                        debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1846:                                        telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1847:                                        
                   1848:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1849:                                        debugger_regs_info(buffer);
                   1850:                                        telnet_printf("%s", buffer);
                   1851:                                        
                   1852:                                        if(break_point.hit) {
                   1853:                                                if(stricmp(params[0], "G") == 0 && num == 1) {
                   1854:                                                        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1855:                                                        telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1856:                                                }
                   1857:                                        } else if(rd_break_point.hit) {
                   1858:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1859:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1860:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1861:                                                m_prev_cs, m_prev_eip);
                   1862:                                        } else if(wr_break_point.hit) {
                   1863:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1864:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1865:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1866:                                                m_prev_cs, m_prev_eip);
                   1867:                                        } else if(in_break_point.hit) {
                   1868:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1869:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1870:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1871:                                                m_prev_cs, m_prev_eip);
                   1872:                                        } else if(out_break_point.hit) {
                   1873:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1874:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1875:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1876:                                                m_prev_cs, m_prev_eip);
                   1877:                                        } else if(int_break_point.hit) {
                   1878:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1879:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1880:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1881:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1882:                                                }
                   1883:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1884:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1885:                                                }
                   1886:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1887:                                        } else {
                   1888:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1889:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1890:                                        }
                   1891:                                        if(break_points_stored) {
                   1892:                                                memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
                   1893:                                        }
                   1894:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1895:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1896:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1897:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1898:                                } else {
                   1899:                                        telnet_printf("invalid parameter number\n");
                   1900:                                }
                   1901:                        } else if(stricmp(params[0], "T") == 0) {
                   1902:                                if(num == 1 || num == 2) {
                   1903:                                        int steps = 1;
                   1904:                                        if(num >= 2) {
                   1905:                                                steps = debugger_get_val(params[1]);
                   1906:                                        }
                   1907:                                        
                   1908:                                        telnet_command("\033[2l"); // key unlock
                   1909:                                        while(steps-- > 0) {
                   1910:                                                break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1911:                                                now_going = false;
                   1912:                                                now_suspended = false;
                   1913:                                                
1.1.1.54  root     1914:                                                while(!m_exit && !now_suspended) {
1.1.1.33  root     1915:                                                        if(telnet_disconnected()) {
                   1916:                                                                break;
                   1917:                                                        }
                   1918:                                                        Sleep(10);
                   1919:                                                }
                   1920:                                                dasm_seg = SREG(CS);
                   1921:                                                dasm_ofs = m_eip;
                   1922:                                                
                   1923:                                                telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1924:                                                debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1925:                                                telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1926:                                                
                   1927:                                                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1928:                                                debugger_regs_info(buffer);
                   1929:                                                telnet_printf("%s", buffer);
                   1930:                                                
                   1931:                                                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()) {
                   1932:                                                        break;
                   1933:                                                }
                   1934:                                        }
                   1935:                                        telnet_command("\033[2h"); // key lock
                   1936:                                        
                   1937:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1938:                                                Sleep(100);
1.1.1.54  root     1939:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1940:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1941:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1942:                                                }
                   1943:                                        }
1.1.1.54  root     1944:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1945:                                                if(telnet_disconnected()) {
                   1946:                                                        break;
                   1947:                                                }
                   1948:                                                Sleep(10);
                   1949:                                        }
                   1950:                                        if(break_point.hit) {
                   1951:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1952:                                                telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1953:                                        } else if(rd_break_point.hit) {
                   1954:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1955:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1956:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1957:                                                m_prev_cs, m_prev_eip);
                   1958:                                        } else if(wr_break_point.hit) {
                   1959:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1960:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1961:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1962:                                                m_prev_cs, m_prev_eip);
                   1963:                                        } else if(in_break_point.hit) {
                   1964:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1965:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1966:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1967:                                                m_prev_cs, m_prev_eip);
                   1968:                                        } else if(out_break_point.hit) {
                   1969:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1970:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1971:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1972:                                                m_prev_cs, m_prev_eip);
                   1973:                                        } else if(int_break_point.hit) {
                   1974:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1975:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1976:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1977:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1978:                                                }
                   1979:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1980:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1981:                                                }
                   1982:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1983:                                        } else if(steps > 0) {
                   1984:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1985:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1986:                                        }
                   1987:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1988:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1989:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1990:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1991:                                } else {
                   1992:                                        telnet_printf("invalid parameter number\n");
                   1993:                                }
                   1994:                        } else if(stricmp(params[0], "Q") == 0) {
                   1995:                                break;
                   1996:                        } else if(stricmp(params[0], "X") == 0) {
                   1997:                                debugger_process_info(buffer);
                   1998:                                telnet_printf("%s", buffer);
                   1999:                        } else if(stricmp(params[0], ">") == 0) {
                   2000:                                if(num == 2) {
                   2001:                                        if(fp_debugger != NULL) {
                   2002:                                                fclose(fp_debugger);
                   2003:                                                fp_debugger = NULL;
                   2004:                                        }
                   2005:                                        fp_debugger = fopen(params[1], "w");
                   2006:                                } else {
                   2007:                                        telnet_printf("invalid parameter number\n");
                   2008:                                }
                   2009:                        } else if(stricmp(params[0], "<") == 0) {
                   2010:                                if(num == 2) {
                   2011:                                        if(fi_debugger != NULL) {
                   2012:                                                fclose(fi_debugger);
                   2013:                                                fi_debugger = NULL;
                   2014:                                        }
                   2015:                                        fi_debugger = fopen(params[1], "r");
                   2016:                                } else {
                   2017:                                        telnet_printf("invalid parameter number\n");
                   2018:                                }
                   2019:                        } else if(stricmp(params[0], "?") == 0) {
                   2020:                                telnet_printf("D [<start> [<end>]] - dump memory\n");
                   2021:                                telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
                   2022:                                telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
                   2023:                                telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
                   2024:                                telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
                   2025:                                
                   2026:                                telnet_printf("R - show registers\n");
                   2027:                                telnet_printf("R <reg> <value> - edit register\n");
                   2028:                                telnet_printf("S <start> <end> <list> - search\n");
                   2029:                                telnet_printf("U [<start> [<end>]] - unassemble\n");
                   2030:                                
                   2031:                                telnet_printf("H <value> <value> - hexadd\n");
                   2032:                                
                   2033:                                telnet_printf("BP <address> - set breakpoint\n");
                   2034:                                telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
                   2035:                                telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
                   2036:                                telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
                   2037:                                telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
                   2038:                                telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
                   2039:                                
                   2040:                                telnet_printf("G - go (press enter key to break)\n");
                   2041:                                telnet_printf("G <address> - go and break at address\n");
                   2042:                                telnet_printf("P - trace one opcode (step over)\n");
                   2043:                                telnet_printf("T [<count>] - trace (step in)\n");
                   2044:                                telnet_printf("Q - quit\n");
                   2045:                                telnet_printf("X - show dos process info\n");
                   2046:                                
                   2047:                                telnet_printf("> <filename> - output logfile\n");
                   2048:                                telnet_printf("< <filename> - input commands from file\n");
                   2049:                                
                   2050:                                telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
                   2051:                                telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
                   2052:                        } else {
                   2053:                                telnet_printf("unknown command %s\n", params[0]);
                   2054:                        }
                   2055:                }
                   2056:        }
                   2057:        if(fp_debugger != NULL) {
                   2058:                fclose(fp_debugger);
                   2059:                fp_debugger = NULL;
                   2060:        }
                   2061:        if(fi_debugger != NULL) {
                   2062:                fclose(fi_debugger);
                   2063:                fi_debugger = NULL;
                   2064:        }
                   2065:        now_debugging = now_going = now_suspended = force_suspend = false;
                   2066:        closesocket(cli_socket);
                   2067: }
                   2068: 
                   2069: const char *debugger_get_ttermpro_path()
                   2070: {
                   2071:        static char path[MAX_PATH] = {0};
                   2072:        
                   2073:        if(getenv("ProgramFiles")) {
                   2074:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
                   2075:        }
                   2076:        return(path);
                   2077: }
                   2078: 
                   2079: const char *debugger_get_ttermpro_x86_path()
                   2080: {
                   2081:        static char path[MAX_PATH] = {0};
                   2082:        
                   2083:        if(getenv("ProgramFiles(x86)")) {
                   2084:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
                   2085:        }
                   2086:        return(path);
                   2087: }
                   2088: 
                   2089: const char *debugger_get_putty_path()
                   2090: {
                   2091:        static char path[MAX_PATH] = {0};
                   2092:        
                   2093:        if(getenv("ProgramFiles")) {
                   2094:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
                   2095:        }
                   2096:        return(path);
                   2097: }
                   2098: 
                   2099: const char *debugger_get_putty_x86_path()
                   2100: {
                   2101:        static char path[MAX_PATH] = {0};
                   2102:        
                   2103:        if(getenv("ProgramFiles(x86)")) {
                   2104:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
                   2105:        }
                   2106:        return(path);
                   2107: }
                   2108: 
                   2109: const char *debugger_get_telnet_path()
                   2110: {
                   2111:        // NOTE: When you run 32bit version of msdos.exe on Windows x64,
                   2112:        // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
                   2113:        // But 32bit version of telnet.exe will not be installed in SysWOW64
                   2114:        // and 64bit version of telnet.exe will be installed in System32.
                   2115:        static char path[MAX_PATH] = {0};
                   2116:        
                   2117:        if(getenv("windir") != NULL) {
                   2118:                sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
                   2119:        }
                   2120:        return(path);
                   2121: }
                   2122: 
                   2123: DWORD WINAPI debugger_thread(LPVOID)
                   2124: {
                   2125:        WSADATA was_data;
                   2126:        struct sockaddr_in svr_addr;
                   2127:        struct sockaddr_in cli_addr;
                   2128:        int cli_addr_len = sizeof(cli_addr);
                   2129:        int port = 23;
                   2130:        int bind_stat = SOCKET_ERROR;
                   2131:        struct timeval timeout;
                   2132:        
                   2133:        WSAStartup(MAKEWORD(2,0), &was_data);
                   2134:        
                   2135:        if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
                   2136:                memset(&svr_addr, 0, sizeof(svr_addr));
                   2137:                svr_addr.sin_family = AF_INET;
                   2138:                svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
                   2139:                
1.1.1.54  root     2140:                while(!m_exit && port < 10000) {
1.1.1.33  root     2141:                        svr_addr.sin_port = htons(port);
                   2142:                        if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
                   2143:                                break;
                   2144:                        } else {
                   2145:                                port = (port == 23) ? 9000 : (port + 1);
                   2146:                        }
                   2147:                }
                   2148:                if(bind_stat == 0) {
                   2149:                        timeout.tv_sec = 1;
                   2150:                        timeout.tv_usec = 0;
1.1.1.45  root     2151:                        setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
1.1.1.33  root     2152:                        
                   2153:                        listen(svr_socket, 1);
                   2154:                        
                   2155:                        char command[MAX_PATH] = {0};
                   2156:                        STARTUPINFO si;
                   2157:                        PROCESS_INFORMATION pi;
                   2158:                        
                   2159:                        if(_access(debugger_get_ttermpro_path(), 0) == 0) {
                   2160:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
                   2161:                        } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
                   2162:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
                   2163:                        } else if(_access(debugger_get_putty_path(), 0) == 0) {
                   2164:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
                   2165:                        } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
                   2166:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
                   2167:                        } else if(_access(debugger_get_telnet_path(), 0) == 0) {
                   2168:                                sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
                   2169:                        }
                   2170:                        if(command[0] != '\0') {
                   2171:                                memset(&si, 0, sizeof(STARTUPINFO));
                   2172:                                memset(&pi, 0, sizeof(PROCESS_INFORMATION));
                   2173:                                CreateProcess(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
                   2174:                        }
                   2175:                        
1.1.1.54  root     2176:                        while(!m_exit) {
1.1.1.33  root     2177:                                if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
                   2178:                                        u_long val = 1;
                   2179:                                        ioctlsocket(cli_socket, FIONBIO, &val);
                   2180:                                        debugger_main();
                   2181:                                }
                   2182:                        }
                   2183:                }
                   2184:        }
                   2185:        WSACleanup();
                   2186:        return(0);
                   2187: }
                   2188: #endif
                   2189: 
                   2190: /* ----------------------------------------------------------------------------
1.1       root     2191:        main
                   2192: ---------------------------------------------------------------------------- */
                   2193: 
1.1.1.28  root     2194: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                   2195: {
                   2196:        if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33  root     2197:                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     2198: #ifdef USE_SERVICE_THREAD
                   2199:                        EnterCriticalSection(&key_buf_crit_sect);
                   2200: #endif
1.1.1.51  root     2201:                        pcbios_clear_key_buffer();
1.1.1.35  root     2202: #ifdef USE_SERVICE_THREAD
                   2203:                        LeaveCriticalSection(&key_buf_crit_sect);
                   2204: #endif
1.1.1.33  root     2205:                }
                   2206: //             key_code = key_recv = 0;
1.1.1.28  root     2207:                return TRUE;
                   2208:        } else if(dwCtrlType == CTRL_C_EVENT) {
                   2209:                return TRUE;
                   2210:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
                   2211:                // this program will be terminated abnormally, do minimum end process
                   2212:                exit_handler();
                   2213:                exit(1);
                   2214:        }
                   2215:        return FALSE;
                   2216: }
                   2217: 
                   2218: void exit_handler()
                   2219: {
                   2220:        if(temp_file_created) {
                   2221:                DeleteFile(temp_file_path);
                   2222:                temp_file_created = false;
                   2223:        }
                   2224:        if(key_buf_char != NULL) {
                   2225:                key_buf_char->release();
                   2226:                delete key_buf_char;
                   2227:                key_buf_char = NULL;
                   2228:        }
                   2229:        if(key_buf_scan != NULL) {
                   2230:                key_buf_scan->release();
                   2231:                delete key_buf_scan;
                   2232:                key_buf_scan = NULL;
                   2233:        }
1.1.1.32  root     2234: #ifdef SUPPORT_XMS
                   2235:        msdos_xms_release();
                   2236: #endif
1.1.1.28  root     2237:        hardware_release();
                   2238: }
                   2239: 
1.1.1.35  root     2240: #ifdef USE_VRAM_THREAD
1.1.1.28  root     2241: DWORD WINAPI vram_thread(LPVOID)
                   2242: {
1.1.1.54  root     2243:        while(!m_exit) {
1.1.1.28  root     2244:                EnterCriticalSection(&vram_crit_sect);
                   2245:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                   2246:                        vram_flush_char();
                   2247:                }
                   2248:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                   2249:                        vram_flush_attr();
                   2250:                }
                   2251:                vram_last_length_char = vram_length_char;
                   2252:                vram_last_length_attr = vram_length_attr;
                   2253:                LeaveCriticalSection(&vram_crit_sect);
                   2254:                // this is about half the maximum keyboard repeat rate - any
                   2255:                // lower tends to be jerky, any higher misses updates
                   2256:                Sleep(15);
                   2257:        }
                   2258:        return 0;
                   2259: }
                   2260: #endif
                   2261: 
1.1.1.45  root     2262: long get_section_in_exec_file(FILE *fp, const char *name)
1.1.1.28  root     2263: {
                   2264:        UINT8 header[0x400];
                   2265:        
                   2266:        long position = ftell(fp);
                   2267:        fseek(fp, 0, SEEK_SET);
                   2268:        fread(header, sizeof(header), 1, fp);
                   2269:        fseek(fp, position, SEEK_SET);
                   2270:        
                   2271:        try {
                   2272:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2273:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2274:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2275:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2276:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2277:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2278:                
                   2279:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                   2280:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                   2281:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                   2282:                                return(sectionHeader->PointerToRawData);
                   2283:                        }
                   2284:                }
                   2285:        } catch(...) {
                   2286:        }
                   2287:        return(0);
                   2288: }
                   2289: 
1.1.1.10  root     2290: bool is_started_from_command_prompt()
                   2291: {
1.1.1.18  root     2292:        bool ret = false;
                   2293:        
                   2294:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                   2295:        if(hLibrary) {
                   2296:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                   2297:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                   2298:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
                   2299:                if(lpfnGetConsoleProcessList) {
                   2300:                        DWORD pl;
                   2301:                        ret = (lpfnGetConsoleProcessList(&pl, 1) > 1);
                   2302:                        FreeLibrary(hLibrary);
                   2303:                        return(ret);
                   2304:                }
                   2305:                FreeLibrary(hLibrary);
                   2306:        }
                   2307:        
                   2308:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   2309:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                   2310:                DWORD dwParentProcessID = 0;
                   2311:                PROCESSENTRY32 pe32;
                   2312:                pe32.dwSize = sizeof(PROCESSENTRY32);
                   2313:                if(Process32First(hSnapshot, &pe32)) {
                   2314:                        do {
                   2315:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                   2316:                                        dwParentProcessID = pe32.th32ParentProcessID;
                   2317:                                        break;
                   2318:                                }
                   2319:                        } while(Process32Next(hSnapshot, &pe32));
                   2320:                }
                   2321:                CloseHandle(hSnapshot);
                   2322:                if(dwParentProcessID != 0) {
                   2323:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                   2324:                        if(hProcess != NULL) {
                   2325:                                HMODULE hMod;
                   2326:                                DWORD cbNeeded;
                   2327:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                   2328:                                        char module_name[MAX_PATH];
                   2329:                                        if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
                   2330:                                                ret = (_strnicmp(module_name, "cmd.exe", 7) == 0);
                   2331:                                        }
                   2332:                                }
                   2333:                                CloseHandle(hProcess);
                   2334:                        }
                   2335:                }
                   2336:        }
                   2337:        return(ret);
1.1.1.14  root     2338: }
                   2339: 
                   2340: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                   2341: {
1.1.1.24  root     2342:        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
1.1.1.14  root     2343:        OSVERSIONINFOEX osvi;
                   2344:        DWORDLONG dwlConditionMask = 0;
                   2345:        int op = VER_GREATER_EQUAL;
                   2346:        
                   2347:        // Initialize the OSVERSIONINFOEX structure.
                   2348:        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
                   2349:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
                   2350:        osvi.dwMajorVersion = dwMajorVersion;
                   2351:        osvi.dwMinorVersion = dwMinorVersion;
                   2352:        osvi.wServicePackMajor = wServicePackMajor;
                   2353:        osvi.wServicePackMinor = wServicePackMinor;
                   2354:        
                   2355:         // Initialize the condition mask.
                   2356:        VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                   2357:        VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                   2358:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                   2359:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                   2360:        
                   2361:        // Perform the test.
                   2362:        return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                   2363: }
                   2364: 
1.1.1.56! root     2365: bool set_console_font_size(HANDLE hStdout, int width, int height)
        !          2366: {
        !          2367:        // http://d.hatena.ne.jp/aharisu/20090427/1240852598
        !          2368:        bool result = false;
        !          2369:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
        !          2370:        
        !          2371:        if(hLibrary) {
        !          2372:                typedef BOOL (WINAPI* GetConsoleFontInfoFunction)(HANDLE, BOOL, DWORD, PCONSOLE_FONT_INFO);
        !          2373:                typedef DWORD (WINAPI* GetNumberOfConsoleFontsFunction)(VOID);
        !          2374:                typedef BOOL (WINAPI* SetConsoleFontFunction)(HANDLE, DWORD);
        !          2375:                typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
        !          2376:                
        !          2377:                GetConsoleFontInfoFunction lpfnGetConsoleFontInfo = reinterpret_cast<GetConsoleFontInfoFunction>(::GetProcAddress(hLibrary, "GetConsoleFontInfo"));
        !          2378:                GetNumberOfConsoleFontsFunction lpfnGetNumberOfConsoleFonts = reinterpret_cast<GetNumberOfConsoleFontsFunction>(::GetProcAddress(hLibrary, "GetNumberOfConsoleFonts"));
        !          2379:                SetConsoleFontFunction lpfnSetConsoleFont = reinterpret_cast<SetConsoleFontFunction>(::GetProcAddress(hLibrary, "SetConsoleFont"));
        !          2380:                GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
        !          2381:                
        !          2382:                if(lpfnGetConsoleFontInfo && lpfnGetNumberOfConsoleFonts && lpfnSetConsoleFont) {
        !          2383:                        DWORD dwFontNum = lpfnGetNumberOfConsoleFonts();
        !          2384:                        CONSOLE_FONT_INFO* fonts = (CONSOLE_FONT_INFO*)malloc(sizeof(CONSOLE_FONT_INFO) * dwFontNum);
        !          2385:                        lpfnGetConsoleFontInfo(hStdout, FALSE, dwFontNum, fonts);
        !          2386:                        for(int i = 0; i < dwFontNum; i++) {
        !          2387:                                fonts[i].dwFontSize = GetConsoleFontSize(hStdout, fonts[i].nFont);
        !          2388:                                if(fonts[i].dwFontSize.X == width && fonts[i].dwFontSize.Y == height) {
        !          2389:                                        lpfnSetConsoleFont(hStdout, fonts[i].nFont);
        !          2390:                                        result = true;
        !          2391:                                        break;
        !          2392:                                }
        !          2393:                        }
        !          2394:                        free(fonts);
        !          2395:                }
        !          2396:                if(lpfnGetCurrentConsoleFont && result) {
        !          2397:                        CONSOLE_FONT_INFO fi;
        !          2398:                        if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi)) {
        !          2399:                                result = (fi.dwFontSize.X == width && fi.dwFontSize.Y == height);
        !          2400:                        }
        !          2401:                }
        !          2402:                FreeLibrary(hLibrary);
        !          2403:        }
        !          2404:        return(result);
        !          2405: }
        !          2406: 
1.1.1.27  root     2407: void get_sio_port_numbers()
                   2408: {
                   2409:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                   2410:        HDEVINFO hDevInfo = 0;
                   2411:        HKEY hKey = 0;
                   2412:        if((hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
                   2413:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                   2414:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                   2415:                                char chData[256];
                   2416:                                DWORD dwType = 0;
                   2417:                                DWORD dwSize = sizeof(chData);
                   2418:                                int port_number = 0;
                   2419:                                
                   2420:                                if(RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
                   2421:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                   2422:                                                port_number = atoi(chData + 3);
                   2423:                                        }
                   2424:                                }
                   2425:                                RegCloseKey(hKey);
                   2426:                                
1.1.1.29  root     2427:                                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     2428:                                        continue;
                   2429:                                }
                   2430:                                if(sio_port_number[0] == 0) {
                   2431:                                        sio_port_number[0] = port_number;
                   2432:                                } else if(sio_port_number[1] == 0) {
                   2433:                                        sio_port_number[1] = port_number;
1.1.1.29  root     2434:                                } else if(sio_port_number[2] == 0) {
                   2435:                                        sio_port_number[2] = port_number;
                   2436:                                } else if(sio_port_number[3] == 0) {
                   2437:                                        sio_port_number[3] = port_number;
1.1.1.27  root     2438:                                }
1.1.1.29  root     2439:                                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     2440:                                        break;
                   2441:                                }
                   2442:                        }
                   2443:                }
                   2444:        }
                   2445: }
                   2446: 
1.1.1.28  root     2447: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                   2448: 
1.1       root     2449: int main(int argc, char *argv[], char *envp[])
                   2450: {
1.1.1.9   root     2451:        int arg_offset = 0;
                   2452:        int standard_env = 0;
1.1.1.14  root     2453:        int buf_width = 0, buf_height = 0;
1.1.1.28  root     2454:        bool get_console_info_success = false;
1.1.1.56! root     2455:        bool get_console_font_success = false;
1.1.1.28  root     2456:        bool screen_size_changed = false;
                   2457:        
                   2458:        _TCHAR path[MAX_PATH], full[MAX_PATH], *name = NULL;
                   2459:        GetModuleFileName(NULL, path, MAX_PATH);
                   2460:        GetFullPathName(path, MAX_PATH, full, &name);
1.1       root     2461:        
1.1.1.27  root     2462:        char dummy_argv_0[] = "msdos.exe";
                   2463:        char dummy_argv_1[MAX_PATH];
                   2464:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                   2465:        char new_exec_file[MAX_PATH];
                   2466:        bool convert_cmd_file = false;
1.1.1.28  root     2467:        unsigned int code_page = 0;
1.1.1.27  root     2468:        
                   2469:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root     2470:                // check if command file is embedded to this execution file
                   2471:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root     2472:                FILE* fp = fopen(full, "rb");
1.1.1.28  root     2473:                long offset = get_section_in_exec_file(fp, ".msdos");
                   2474:                if(offset != 0) {
1.1.1.30  root     2475:                        UINT8 buffer[16];
1.1.1.28  root     2476:                        fseek(fp, offset, SEEK_SET);
                   2477:                        fread(buffer, sizeof(buffer), 1, fp);
                   2478:                        
                   2479:                        // restore flags
                   2480:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                   2481:                        no_windows          = ((buffer[0] & 0x02) != 0);
                   2482:                        standard_env        = ((buffer[0] & 0x04) != 0);
                   2483:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                   2484:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                   2485:                        if((buffer[0] & 0x20) != 0) {
                   2486:                                get_sio_port_numbers();
                   2487:                        }
                   2488:                        if((buffer[0] & 0x40) != 0) {
                   2489:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                   2490:                                support_ems = true;
1.1.1.30  root     2491:                        }
1.1.1.27  root     2492: #ifdef SUPPORT_XMS
1.1.1.30  root     2493:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root     2494:                                support_xms = true;
                   2495:                        }
1.1.1.30  root     2496: #endif
1.1.1.28  root     2497:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                   2498:                                buf_width  = buffer[1] | (buffer[2] << 8);
                   2499:                                buf_height = buffer[3] | (buffer[4] << 8);
                   2500:                        }
                   2501:                        if(buffer[5] != 0) {
1.1.1.30  root     2502:                                dos_major_version = buffer[5];
                   2503:                                dos_minor_version = buffer[6];
                   2504:                        }
                   2505:                        if(buffer[7] != 0) {
                   2506:                                win_major_version = buffer[7];
                   2507:                                win_minor_version = buffer[8];
1.1.1.28  root     2508:                        }
1.1.1.30  root     2509:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root     2510:                                SetConsoleCP(code_page);
                   2511:                                SetConsoleOutputCP(code_page);
                   2512:                        }
1.1.1.30  root     2513:                        int name_len = buffer[11];
                   2514:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root     2515:                        
                   2516:                        // restore command file name
                   2517:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                   2518:                        fread(dummy_argv_1, name_len, 1, fp);
                   2519:                        
                   2520:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                   2521:                                // if original command file exists, create a temporary file name
                   2522:                                if(GetTempFileName(_T("."), _T("DOS"), 0, dummy_argv_1) != 0) {
                   2523:                                        // create a temporary command file in the current director
                   2524:                                        DeleteFile(dummy_argv_1);
1.1.1.27  root     2525:                                } else {
1.1.1.28  root     2526:                                        // create a temporary command file in the temporary folder
                   2527:                                        GetTempPath(MAX_PATH, path);
                   2528:                                        if(GetTempFileName(path, _T("DOS"), 0, dummy_argv_1) != 0) {
                   2529:                                                DeleteFile(dummy_argv_1);
                   2530:                                        } else {
                   2531:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                   2532:                                        }
1.1.1.27  root     2533:                                }
1.1.1.28  root     2534:                                // check the command file type
                   2535:                                fread(buffer, 2, 1, fp);
                   2536:                                fseek(fp, -2, SEEK_CUR);
                   2537:                                if(memcmp(buffer, "MZ", 2) != 0) {
                   2538:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                   2539:                                } else {
                   2540:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root     2541:                                }
                   2542:                        }
1.1.1.28  root     2543:                        
                   2544:                        // restore command file
                   2545:                        FILE* fo = fopen(dummy_argv_1, "wb");
                   2546:                        for(int i = 0; i < file_len; i++) {
                   2547:                                fputc(fgetc(fp), fo);
                   2548:                        }
                   2549:                        fclose(fo);
                   2550:                        
                   2551:                        GetFullPathName(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
                   2552:                        temp_file_created = true;
                   2553:                        SetFileAttributes(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
                   2554:                        
                   2555:                        // adjust argc/argv
                   2556:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                   2557:                                dummy_argv[i + 1] = argv[i];
                   2558:                        }
                   2559:                        argc++;
                   2560:                        argv = dummy_argv;
1.1.1.27  root     2561:                }
                   2562:                fclose(fp);
                   2563:        }
1.1.1.9   root     2564:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     2565:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   2566:                        stay_busy = true;
                   2567:                        arg_offset++;
1.1.1.27  root     2568:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   2569:                        if(argv[i][2] != '\0') {
                   2570:                                strcpy(new_exec_file, &argv[i][2]);
                   2571:                        } else {
                   2572:                                strcpy(new_exec_file, "new_exec_file.exe");
                   2573:                        }
                   2574:                        convert_cmd_file = true;
                   2575:                        arg_offset++;
1.1.1.28  root     2576:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   2577:                        if(IS_NUMERIC(argv[i][2])) {
                   2578:                                code_page = atoi(&argv[i][2]);
                   2579:                        } else {
                   2580:                                code_page = GetConsoleCP();
                   2581:                        }
                   2582:                        arg_offset++;
1.1.1.25  root     2583:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   2584:                        no_windows = true;
                   2585:                        arg_offset++;
                   2586:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     2587:                        standard_env = 1;
                   2588:                        arg_offset++;
1.1.1.14  root     2589:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   2590:                        ignore_illegal_insn = true;
                   2591:                        arg_offset++;
                   2592:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   2593:                        limit_max_memory = true;
                   2594:                        arg_offset++;
                   2595:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.51  root     2596:                        int result = sscanf(argv[i] + 2, "%d,%d", &buf_height, &buf_width);
                   2597:                        if(result == 1) {
                   2598:                                buf_width = 0;
                   2599:                        } else if(result != 2) {
1.1.1.17  root     2600:                                buf_width = buf_height = 0;
                   2601:                        }
                   2602:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   2603:                                buf_width = 80;
                   2604:                        }
                   2605:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   2606:                                buf_height = 25;
                   2607:                        }
1.1.1.14  root     2608:                        arg_offset++;
1.1.1.25  root     2609:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     2610:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     2611:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   2612:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   2613:                                        sio_port_number[1] = atoi(p1 + 1);
                   2614:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   2615:                                                sio_port_number[2] = atoi(p2 + 1);
                   2616:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   2617:                                                        sio_port_number[3] = atoi(p3 + 1);
                   2618:                                                }
                   2619:                                        }
1.1.1.25  root     2620:                                }
1.1.1.29  root     2621:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     2622:                        }
1.1.1.29  root     2623:                        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     2624:                                get_sio_port_numbers();
1.1.1.25  root     2625:                        }
                   2626:                        arg_offset++;
1.1.1.9   root     2627:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     2628:                        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     2629:                                dos_major_version = argv[i][2] - '0';
                   2630:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   2631:                        }
                   2632:                        arg_offset++;
                   2633:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   2634:                        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]))) {
                   2635:                                win_major_version = argv[i][2] - '0';
                   2636:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     2637:                        }
                   2638:                        arg_offset++;
1.1.1.25  root     2639:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   2640:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   2641:                        support_ems = true;
                   2642: #ifdef SUPPORT_XMS
                   2643:                        support_xms = true;
                   2644: #endif
                   2645:                        arg_offset++;
1.1.1.9   root     2646:                } else {
                   2647:                        break;
                   2648:                }
                   2649:        }
                   2650:        if(argc < 2 + arg_offset) {
1.1       root     2651: #ifdef _WIN64
1.1.1.14  root     2652:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     2653: #else
1.1.1.14  root     2654:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     2655: #endif
1.1.1.25  root     2656:                fprintf(stderr,
1.1.1.28  root     2657:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     2658:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     2659:                        "\n"
                   2660:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     2661: #ifdef _WIN64
1.1.1.27  root     2662:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     2663: #else
1.1.1.27  root     2664:                        "\t-c\tconvert command file to 32bit execution file\n"
                   2665: #endif
1.1.1.28  root     2666:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     2667:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   2668:                        "\t-e\tuse a reduced environment block\n"
                   2669:                        "\t-i\tignore invalid instructions\n"
                   2670:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   2671:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   2672:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   2673:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     2674:                        "\t-w\tset the Windows version\n"
1.1.1.19  root     2675: #ifdef SUPPORT_XMS
1.1.1.28  root     2676:                        "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19  root     2677: #else
1.1.1.28  root     2678:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2679: #endif
                   2680:                );
1.1.1.10  root     2681:                
                   2682:                if(!is_started_from_command_prompt()) {
                   2683:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2684:                        while(!_kbhit()) {
                   2685:                                Sleep(10);
                   2686:                        }
                   2687:                }
1.1.1.20  root     2688: #ifdef _DEBUG
                   2689:                _CrtDumpMemoryLeaks();
                   2690: #endif
1.1       root     2691:                return(EXIT_FAILURE);
                   2692:        }
1.1.1.27  root     2693:        if(convert_cmd_file) {
                   2694:                retval = EXIT_FAILURE;
1.1.1.28  root     2695:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2696:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2697:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2698:                        
1.1.1.28  root     2699:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2700:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2701:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2702:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2703:                        } else {
1.1.1.28  root     2704:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2705:                                if(offset != 0) {
                   2706:                                        UINT8 buffer[14];
                   2707:                                        fseek(fp, offset, SEEK_SET);
                   2708:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2709:                                        memset(path, 0, sizeof(path));
                   2710:                                        fread(path, buffer[9], 1, fp);
                   2711:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2712:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2713:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2714:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2715:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2716:                                } else {
                   2717:                                        // read pe header of msdos.exe
                   2718:                                        UINT8 header[0x400];
                   2719:                                        fseek(fp, 0, SEEK_SET);
                   2720:                                        fread(header, sizeof(header), 1, fp);
                   2721:                                        
                   2722:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2723:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2724:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2725:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2726:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2727:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2728:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2729:                                        
                   2730:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2731:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2732:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2733:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2734:                                        if(dwExtraLastSectionBytes != 0) {
                   2735:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2736:                                                dwLastSectionSize += dwRemain;
                   2737:                                        }
                   2738:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2739:                                        
                   2740:                                        // store msdos.exe
                   2741:                                        fseek(fp, 0, SEEK_SET);
                   2742:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2743:                                                if((data = fgetc(fp)) != EOF) {
                   2744:                                                        fputc(data, fo);
                   2745:                                                } else {
                   2746:                                                        // we should not reach here :-(
                   2747:                                                        fputc(0, fo);
                   2748:                                                }
                   2749:                                        }
                   2750:                                        
                   2751:                                        // store options
                   2752:                                        UINT8 flags = 0;
                   2753:                                        if(stay_busy) {
                   2754:                                                flags |= 0x01;
                   2755:                                        }
                   2756:                                        if(no_windows) {
                   2757:                                                flags |= 0x02;
                   2758:                                        }
                   2759:                                        if(standard_env) {
                   2760:                                                flags |= 0x04;
                   2761:                                        }
                   2762:                                        if(ignore_illegal_insn) {
                   2763:                                                flags |= 0x08;
                   2764:                                        }
                   2765:                                        if(limit_max_memory) {
                   2766:                                                flags |= 0x10;
                   2767:                                        }
1.1.1.29  root     2768:                                        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     2769:                                                flags |= 0x20;
                   2770:                                        }
                   2771:                                        if(support_ems) {
                   2772:                                                flags |= 0x40;
                   2773:                                        }
1.1.1.30  root     2774: #ifdef SUPPORT_XMS
                   2775:                                        if(support_xms) {
                   2776:                                                flags |= 0x80;
                   2777:                                        }
                   2778: #endif
1.1.1.28  root     2779:                                        
                   2780:                                        fputc(flags, fo);
                   2781:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2782:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2783:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2784:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2785:                                        fputc(dos_major_version, fo);
                   2786:                                        fputc(dos_minor_version, fo);
                   2787:                                        fputc(win_major_version, fo);
                   2788:                                        fputc(win_minor_version, fo);
1.1.1.28  root     2789:                                        fputc((code_page >> 0) & 0xff, fo);
                   2790:                                        fputc((code_page >> 8) & 0xff, fo);
                   2791:                                        
                   2792:                                        // store command file info
                   2793:                                        GetFullPathName(argv[arg_offset + 1], MAX_PATH, full, &name);
                   2794:                                        int name_len = strlen(name);
                   2795:                                        fseek(fs, 0, SEEK_END);
                   2796:                                        long file_size = ftell(fs);
                   2797:                                        
                   2798:                                        fputc(name_len, fo);
                   2799:                                        fputc((file_size >>  0) & 0xff, fo);
                   2800:                                        fputc((file_size >>  8) & 0xff, fo);
                   2801:                                        fputc((file_size >> 16) & 0xff, fo);
                   2802:                                        fputc((file_size >> 24) & 0xff, fo);
                   2803:                                        fwrite(name, name_len, 1, fo);
                   2804:                                        
                   2805:                                        // store command file
                   2806:                                        fseek(fs, 0, SEEK_SET);
                   2807:                                        for(int i = 0; i < file_size; i++) {
                   2808:                                                if((data = fgetc(fs)) != EOF) {
                   2809:                                                        fputc(data, fo);
                   2810:                                                } else {
                   2811:                                                        // we should not reach here :-(
                   2812:                                                        fputc(0, fo);
                   2813:                                                }
                   2814:                                        }
                   2815:                                        
                   2816:                                        // store padding data and update pe header
1.1.1.29  root     2817:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   2818:                                        coffHeader->NumberOfSections++;
                   2819:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   2820:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   2821:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   2822:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   2823:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     2824:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   2825:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   2826:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     2827:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     2828:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   2829:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     2830:                                                        if(i < 2) {
                   2831:                                                                fputc(padding[i & 15], fo);
                   2832:                                                        } else {
                   2833:                                                                fputc(padding[(i - 2) & 15], fo);
                   2834:                                                        }
1.1.1.28  root     2835:                                                }
                   2836:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   2837:                                        }
                   2838:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   2839:                                        
                   2840:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   2841:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   2842:                                        if(dwExtraNewSectionBytes != 0) {
                   2843:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   2844:                                                dwNewSectionSize += dwRemain;
                   2845:                                        }
                   2846:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   2847:                                        
                   2848:                                        fseek(fo, 0, SEEK_SET);
                   2849:                                        fwrite(header, sizeof(header), 1, fo);
                   2850:                                        
                   2851:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   2852:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     2853:                                }
                   2854:                        }
                   2855:                        if(fp != NULL) {
                   2856:                                fclose(fp);
                   2857:                        }
                   2858:                        if(fs != NULL) {
                   2859:                                fclose(fs);
                   2860:                        }
                   2861:                        if(fo != NULL) {
                   2862:                                fclose(fo);
                   2863:                        }
                   2864:                }
                   2865: #ifdef _DEBUG
                   2866:                _CrtDumpMemoryLeaks();
                   2867: #endif
                   2868:                return(retval);
                   2869:        }
1.1       root     2870:        
1.1.1.54  root     2871:        is_xp_64_or_later = is_greater_windows_version(5, 2, 0, 0);
1.1.1.14  root     2872:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   2873:        
1.1.1.23  root     2874:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     2875:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     2876:        CONSOLE_CURSOR_INFO ci;
1.1.1.56! root     2877:        CONSOLE_FONT_INFO fi;
1.1.1.23  root     2878:        
1.1.1.28  root     2879:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     2880:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.24  root     2881:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1.1.56! root     2882: //     get_console_font_success = (GetCurrentConsoleFont(hStdout, FALSE, &fi) != 0);
        !          2883:        
        !          2884:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
        !          2885:        if(hLibrary) {
        !          2886:                typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
        !          2887:                GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
        !          2888:                if(lpfnGetCurrentConsoleFont) {
        !          2889:                        get_console_font_success = (lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi) != 0);
        !          2890:                }
        !          2891:                FreeLibrary(hLibrary);
        !          2892:        }
1.1       root     2893:        
1.1.1.14  root     2894:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   2895:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   2896:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   2897:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     2898:                }
                   2899:        }
1.1.1.28  root     2900:        if(get_console_info_success) {
1.1.1.12  root     2901:                scr_width = csbi.dwSize.X;
1.1.1.14  root     2902:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2903:                
1.1.1.28  root     2904:                // v-text shadow buffer size must be lesser than 0x7fd0
                   2905:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     2906:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   2907:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   2908:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     2909:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     2910:                                scr_width = 80;
                   2911:                                scr_height = 25;
                   2912:                        }
1.1.1.28  root     2913:                        screen_size_changed = true;
1.1.1.14  root     2914:                }
1.1.1.12  root     2915:        } else {
                   2916:                // for a proof (not a console)
                   2917:                scr_width = 80;
                   2918:                scr_height = 25;
                   2919:        }
1.1.1.14  root     2920:        scr_buf_size.X = scr_width;
                   2921:        scr_buf_size.Y = scr_height;
                   2922:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   2923:        scr_top = csbi.srWindow.Top;
1.1       root     2924:        cursor_moved = false;
                   2925:        
1.1.1.54  root     2926:        SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
                   2927:        
1.1.1.35  root     2928: #ifdef USE_SERVICE_THREAD
                   2929:        InitializeCriticalSection(&input_crit_sect);
                   2930:        InitializeCriticalSection(&key_buf_crit_sect);
                   2931:        InitializeCriticalSection(&putch_crit_sect);
1.1.1.50  root     2932:        main_thread_id = GetCurrentThreadId();
1.1.1.35  root     2933: #endif
1.1.1.50  root     2934:        
1.1.1.25  root     2935:        key_buf_char = new FIFO(256);
                   2936:        key_buf_scan = new FIFO(256);
1.1       root     2937:        
                   2938:        hardware_init();
                   2939:        
1.1.1.33  root     2940: #ifdef USE_DEBUGGER
                   2941:        debugger_init();
                   2942: #endif
                   2943:        
1.1.1.9   root     2944:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     2945:                retval = EXIT_FAILURE;
                   2946:        } else {
1.1.1.27  root     2947: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     2948:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   2949: #endif
                   2950:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   2951:                
1.1.1.28  root     2952:                if(screen_size_changed) {
1.1.1.24  root     2953:                        change_console_size(scr_width, scr_height);
                   2954:                }
1.1.1.8   root     2955:                TIMECAPS caps;
                   2956:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   2957:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.35  root     2958: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2959:                InitializeCriticalSection(&vram_crit_sect);
                   2960:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   2961: #endif
1.1.1.33  root     2962: #ifdef USE_DEBUGGER
                   2963:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   2964:                // wait until telnet client starts and connects to me
                   2965:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   2966:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   2967:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   2968:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   2969:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   2970:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   2971:                                Sleep(100);
                   2972:                        }
                   2973:                }
                   2974: #endif
1.1       root     2975:                hardware_run();
1.1.1.35  root     2976: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2977:                vram_flush();
                   2978:                DeleteCriticalSection(&vram_crit_sect);
                   2979: #endif
1.1.1.24  root     2980:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     2981:                
1.1.1.24  root     2982:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.56! root     2983:                if(get_console_font_success) {
        !          2984:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
        !          2985:                        set_console_font_size(hStdout, fi.dwFontSize.X, fi.dwFontSize.Y);
        !          2986:                }
1.1.1.28  root     2987:                if(get_console_info_success) {
1.1.1.23  root     2988:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2989:                        if(restore_console_on_exit) {
1.1.1.14  root     2990:                                // window can't be bigger than buffer,
                   2991:                                // buffer can't be smaller than window,
                   2992:                                // so make a tiny window,
                   2993:                                // set the required buffer,
                   2994:                                // then set the required window
                   2995:                                SMALL_RECT rect;
                   2996:                                SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
                   2997:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     2998:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     2999:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12  root     3000:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3001:                        }
1.1.1.14  root     3002:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   3003:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     3004:                }
1.1.1.24  root     3005:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   3006:                
1.1       root     3007:                msdos_finish();
1.1.1.14  root     3008:                
                   3009:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     3010:        }
1.1.1.35  root     3011:        if(temp_file_created) {
                   3012:                DeleteFile(temp_file_path);
                   3013:                temp_file_created = false;
                   3014:        }
1.1.1.10  root     3015:        hardware_finish();
                   3016:        
1.1.1.28  root     3017:        if(key_buf_char != NULL) {
                   3018:                key_buf_char->release();
                   3019:                delete key_buf_char;
                   3020:                key_buf_char = NULL;
                   3021:        }
                   3022:        if(key_buf_scan != NULL) {
                   3023:                key_buf_scan->release();
                   3024:                delete key_buf_scan;
                   3025:                key_buf_scan = NULL;
                   3026:        }
1.1.1.35  root     3027: #ifdef USE_SERVICE_THREAD
                   3028:        DeleteCriticalSection(&input_crit_sect);
                   3029:        DeleteCriticalSection(&key_buf_crit_sect);
                   3030:        DeleteCriticalSection(&putch_crit_sect);
                   3031: #endif
1.1.1.20  root     3032: #ifdef _DEBUG
                   3033:        _CrtDumpMemoryLeaks();
                   3034: #endif
1.1       root     3035:        return(retval);
                   3036: }
                   3037: 
1.1.1.20  root     3038: /* ----------------------------------------------------------------------------
                   3039:        console
                   3040: ---------------------------------------------------------------------------- */
                   3041: 
1.1.1.14  root     3042: void change_console_size(int width, int height)
1.1.1.12  root     3043: {
1.1.1.23  root     3044:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     3045:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   3046:        SMALL_RECT rect;
                   3047:        COORD co;
                   3048:        
                   3049:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     3050:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   3051:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
                   3052:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
                   3053:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   3054:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   3055:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   3056:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
                   3057:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   3058:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   3059:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     3060:                }
                   3061:        }
1.1.1.14  root     3062:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     3063:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     3064:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     3065:                SetConsoleCursorPosition(hStdout, co);
                   3066:                cursor_moved = true;
                   3067:        }
1.1.1.14  root     3068:        
                   3069:        // window can't be bigger than buffer,
                   3070:        // buffer can't be smaller than window,
                   3071:        // so make a tiny window,
                   3072:        // set the required buffer,
                   3073:        // then set the required window
                   3074:        SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12  root     3075:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     3076:        co.X = width;
                   3077:        co.Y = height;
1.1.1.12  root     3078:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     3079:        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   3080:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3081:        
                   3082:        scr_width = scr_buf_size.X = width;
                   3083:        scr_height = scr_buf_size.Y = height;
                   3084:        scr_top = 0;
                   3085:        
                   3086:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   3087:        
                   3088:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     3089:        text_vram_end_address = text_vram_top_address + regen;
                   3090:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   3091:        
1.1.1.14  root     3092:        if(regen > 0x4000) {
                   3093:                regen = 0x8000;
                   3094:                vram_pages = 1;
                   3095:        } else if(regen > 0x2000) {
                   3096:                regen = 0x4000;
                   3097:                vram_pages = 2;
                   3098:        } else if(regen > 0x1000) {
                   3099:                regen = 0x2000;
                   3100:                vram_pages = 4;
                   3101:        } else {
                   3102:                regen = 0x1000;
                   3103:                vram_pages = 8;
                   3104:        }
1.1.1.15  root     3105:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   3106:        *(UINT16 *)(mem + 0x44c) = regen;
                   3107:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   3108:        
1.1.1.24  root     3109:        mouse.min_position.x = 0;
                   3110:        mouse.min_position.y = 0;
1.1.1.34  root     3111:        mouse.max_position.x = 8 * (scr_width  - 1);
                   3112:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     3113:        
1.1.1.15  root     3114:        restore_console_on_exit = true;
1.1.1.14  root     3115: }
                   3116: 
                   3117: void clear_scr_buffer(WORD attr)
                   3118: {
                   3119:        for(int y = 0; y < scr_height; y++) {
                   3120:                for(int x = 0; x < scr_width; x++) {
                   3121:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3122:                        SCR_BUF(y,x).Attributes = attr;
                   3123:                }
                   3124:        }
1.1.1.12  root     3125: }
                   3126: 
1.1.1.24  root     3127: bool update_console_input()
1.1       root     3128: {
1.1.1.35  root     3129: #ifdef USE_SERVICE_THREAD
                   3130:        EnterCriticalSection(&input_crit_sect);
                   3131: #endif
1.1.1.23  root     3132:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     3133:        DWORD dwNumberOfEvents = 0;
1.1       root     3134:        DWORD dwRead;
                   3135:        INPUT_RECORD ir[16];
1.1.1.24  root     3136:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   3137:        bool result = false;
1.1       root     3138:        
1.1.1.8   root     3139:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   3140:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   3141:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     3142:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.34  root     3143:                                        if(mouse.hidden == 0) {
                   3144:                                                // NOTE: if restore_console_on_exit, console is not scrolled
                   3145:                                                if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   3146:                                                        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   3147:                                                }
                   3148:                                                // FIXME: character size is always 8x8 ???
                   3149:                                                int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   3150:                                                int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
                   3151:                                                
                   3152:                                                if(mouse.position.x != x || mouse.position.y != y) {
                   3153:                                                        mouse.position.x = x;
                   3154:                                                        mouse.position.y = y;
                   3155:                                                        mouse.status |= 1;
1.1.1.43  root     3156:                                                        mouse.status_alt |= 1;
1.1.1.34  root     3157:                                                }
                   3158:                                        }
                   3159:                                        if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   3160:                                                for(int i = 0; i < MAX_MOUSE_BUTTONS; i++) {
                   3161:                                                        static const DWORD bits[] = {
                   3162:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   3163:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
                   3164:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
                   3165:                                                        };
                   3166:                                                        bool prev_status = mouse.buttons[i].status;
                   3167:                                                        mouse.buttons[i].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[i]) != 0);
                   3168:                                                        
                   3169:                                                        if(!prev_status && mouse.buttons[i].status) {
                   3170:                                                                mouse.buttons[i].pressed_times++;
                   3171:                                                                mouse.buttons[i].pressed_position.x = mouse.position.x;
                   3172:                                                                mouse.buttons[i].pressed_position.y = mouse.position.x;
1.1.1.43  root     3173:                                                                if(i < 2) {
                   3174:                                                                        mouse.status_alt |= 2 << (i * 2);
                   3175:                                                                }
1.1.1.34  root     3176:                                                                mouse.status |= 2 << (i * 2);
                   3177:                                                        } else if(prev_status && !mouse.buttons[i].status) {
                   3178:                                                                mouse.buttons[i].released_times++;
                   3179:                                                                mouse.buttons[i].released_position.x = mouse.position.x;
                   3180:                                                                mouse.buttons[i].released_position.y = mouse.position.x;
1.1.1.43  root     3181:                                                                if(i < 2) {
                   3182:                                                                        mouse.status_alt |= 4 << (i * 2);
                   3183:                                                                }
1.1.1.34  root     3184:                                                                mouse.status |= 4 << (i * 2);
1.1.1.14  root     3185:                                                        }
                   3186:                                                }
                   3187:                                        }
1.1.1.24  root     3188:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3189:                                        // update keyboard flags in bios data area
1.1.1.35  root     3190:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
                   3191:                                                mem[0x417] |= 0x40;
1.1.1.33  root     3192:                                        } else {
1.1.1.35  root     3193:                                                mem[0x417] &= ~0x40;
1.1.1.33  root     3194:                                        }
1.1.1.35  root     3195:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
                   3196:                                                mem[0x417] |= 0x20;
1.1.1.33  root     3197:                                        } else {
1.1.1.35  root     3198:                                                mem[0x417] &= ~0x20;
                   3199:                                        }
                   3200:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
                   3201:                                                mem[0x417] |= 0x10;
                   3202:                                        } else {
                   3203:                                                mem[0x417] &= ~0x10;
1.1.1.33  root     3204:                                        }
                   3205:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43  root     3206:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3207:                                                        mouse.status_alt |= 0x80;
                   3208:                                                }
1.1.1.33  root     3209:                                                mem[0x417] |= 0x08;
                   3210:                                        } else {
                   3211:                                                mem[0x417] &= ~0x08;
                   3212:                                        }
1.1.1.35  root     3213:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43  root     3214:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3215:                                                        mouse.status_alt |= 0x40;
                   3216:                                                }
1.1.1.35  root     3217:                                                mem[0x417] |= 0x04;
1.1.1.33  root     3218:                                        } else {
1.1.1.35  root     3219:                                                mem[0x417] &= ~0x04;
1.1.1.33  root     3220:                                        }
                   3221:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43  root     3222:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3223:                                                        mouse.status_alt |= 0x20;
                   3224:                                                }
1.1.1.33  root     3225:                                                if(!(mem[0x417] & 0x03)) {
                   3226:                                                        mem[0x417] |= 0x02; // left shift
                   3227:                                                }
                   3228:                                        } else {
                   3229:                                                mem[0x417] &= ~0x03;
                   3230:                                        }
1.1.1.35  root     3231:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3232:                                                mem[0x418] |= 0x02;
                   3233:                                        } else {
                   3234:                                                mem[0x418] &= ~0x02;
                   3235:                                        }
                   3236:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3237:                                                mem[0x418] |= 0x01;
                   3238:                                        } else {
                   3239:                                                mem[0x418] &= ~0x01;
                   3240:                                        }
1.1.1.33  root     3241:                                        
1.1.1.28  root     3242:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.24  root     3243:                                        kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3244:                                        kbd_status |= 1;
                   3245:                                        
                   3246:                                        // update dos key buffer
                   3247:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3248:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
1.1.1.51  root     3249:                                        UINT8 scn_old = scn;
1.1.1.33  root     3250:                                        
                   3251:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3252:                                                // make
1.1.1.24  root     3253:                                                kbd_data &= 0x7f;
                   3254:                                                
1.1.1.33  root     3255:                                                if(chr == 0x00) {
1.1.1.24  root     3256:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3257:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3258:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3259:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3260:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3261:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3262:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3263:                                                                } else if(scn == 0x35) {
                   3264:                                                                        scn = 0xa4;             // keypad /
                   3265:                                                                }
                   3266:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3267:                                                                if(scn == 0x07) {
                   3268:                                                                        chr = 0x1e;     // Ctrl+^
                   3269:                                                                } else if(scn == 0x0c) {
                   3270:                                                                        chr = 0x1f;     // Ctrl+_
                   3271:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3272:                                                                        static const UINT8 ctrl_map[] = {
                   3273:                                                                                0x95,   // keypad /
                   3274:                                                                                0,
                   3275:                                                                                0x96,   // keypad *
                   3276:                                                                                0, 0, 0,
                   3277:                                                                                0x5e,   // F1
                   3278:                                                                                0x5f,   // F2
                   3279:                                                                                0x60,   // F3
                   3280:                                                                                0x61,   // F4
                   3281:                                                                                0x62,   // F5
                   3282:                                                                                0x63,   // F6
                   3283:                                                                                0x64,   // F7
                   3284:                                                                                0x65,   // F8
                   3285:                                                                                0x66,   // F9
                   3286:                                                                                0x67,   // F10
                   3287:                                                                                0,
                   3288:                                                                                0,
                   3289:                                                                                0x77,   // Home
                   3290:                                                                                0x8d,   // Up
                   3291:                                                                                0x84,   // PgUp
                   3292:                                                                                0x8e,   // keypad -
                   3293:                                                                                0x73,   // Left
                   3294:                                                                                0x8f,   // keypad center
                   3295:                                                                                0x74,   // Right
                   3296:                                                                                0x90,   // keyapd +
                   3297:                                                                                0x75,   // End
                   3298:                                                                                0x91,   // Down
                   3299:                                                                                0x76,   // PgDn
                   3300:                                                                                0x92,   // Insert
                   3301:                                                                                0x93,   // Delete
                   3302:                                                                                0, 0, 0,
                   3303:                                                                                0x89,   // F11
                   3304:                                                                                0x8a,   // F12
                   3305:                                                                        };
                   3306:                                                                        scn = ctrl_map[scn - 0x35];
                   3307:                                                                }
                   3308:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3309:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3310:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3311:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3312:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3313:                                                                }
                   3314:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3315:                                                                scn += 0x85 - 0x57;
                   3316:                                                        }
                   3317:                                                        // ignore shift, ctrl, alt, win and menu keys
1.1.1.51  root     3318:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && !(scn >= 0x5b && scn <= 0x5d && scn == scn_old)) {
1.1.1.32  root     3319:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3320: #ifdef USE_SERVICE_THREAD
                   3321:                                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3322: #endif
1.1.1.32  root     3323:                                                                        if(chr == 0) {
1.1.1.51  root     3324:                                                                                pcbios_set_key_buffer(0x00, ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
1.1.1.32  root     3325:                                                                        }
1.1.1.51  root     3326:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3327: #ifdef USE_SERVICE_THREAD
                   3328:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3329: #endif
1.1.1.24  root     3330:                                                                }
                   3331:                                                        }
                   3332:                                                } else {
                   3333:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3334:                                                                chr = 0;
                   3335:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3336:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3337:                                                                }
                   3338:                                                        }
1.1.1.32  root     3339:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3340: #ifdef USE_SERVICE_THREAD
                   3341:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3342: #endif
1.1.1.51  root     3343:                                                                pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3344: #ifdef USE_SERVICE_THREAD
                   3345:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3346: #endif
1.1.1.32  root     3347:                                                        }
1.1.1.24  root     3348:                                                }
1.1.1.33  root     3349:                                        } else if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
                   3350:                                                // ctrl-break, ctrl-c
                   3351:                                                if(scn == 0x46) {
                   3352:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3353: #ifdef USE_SERVICE_THREAD
                   3354:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3355: #endif
1.1.1.51  root     3356:                                                                pcbios_set_key_buffer(0x00, 0x00);
1.1.1.35  root     3357: #ifdef USE_SERVICE_THREAD
                   3358:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3359: #endif
1.1.1.33  root     3360:                                                        }
                   3361:                                                        ctrl_break_pressed = true;
                   3362:                                                        mem[0x471] = 0x80;
                   3363:                                                        raise_int_1bh = true;
                   3364:                                                } else {
                   3365:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3366: #ifdef USE_SERVICE_THREAD
                   3367:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3368: #endif
1.1.1.51  root     3369:                                                                pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3370: #ifdef USE_SERVICE_THREAD
                   3371:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3372: #endif
1.1.1.33  root     3373:                                                        }
                   3374:                                                        ctrl_c_pressed = (scn == 0x2e);
                   3375:                                                }
                   3376:                                        } else {
                   3377:                                                // break
                   3378:                                                kbd_data |= 0x80;
1.1       root     3379:                                        }
1.1.1.24  root     3380:                                        result = key_changed = true;
1.1.1.36  root     3381:                                        // IME may be on and it may causes screen scroll up and cursor position change
                   3382:                                        cursor_moved = true;
1.1       root     3383:                                }
                   3384:                        }
                   3385:                }
                   3386:        }
1.1.1.35  root     3387: #ifdef USE_SERVICE_THREAD
                   3388:        LeaveCriticalSection(&input_crit_sect);
                   3389: #endif
1.1.1.24  root     3390:        return(result);
1.1.1.8   root     3391: }
                   3392: 
1.1.1.14  root     3393: bool update_key_buffer()
1.1.1.8   root     3394: {
1.1.1.35  root     3395:        if(update_console_input()) {
                   3396:                return(true);
                   3397:        }
                   3398:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3399: #ifdef USE_SERVICE_THREAD
                   3400:                EnterCriticalSection(&key_buf_crit_sect);
                   3401: #endif
1.1.1.55  root     3402:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     3403: #ifdef USE_SERVICE_THREAD
                   3404:                LeaveCriticalSection(&key_buf_crit_sect);
                   3405: #endif
                   3406:                if(!empty) return(true);
                   3407:        }
                   3408:        return(false);
1.1.1.8   root     3409: }
                   3410: 
1.1.1.20  root     3411: /* ----------------------------------------------------------------------------
                   3412:        MS-DOS virtual machine
                   3413: ---------------------------------------------------------------------------- */
                   3414: 
1.1.1.32  root     3415: static const struct {
1.1.1.33  root     3416:        char *name;
                   3417:        DWORD lcid;
                   3418:        char *std;
                   3419:        char *dlt;
                   3420: } tz_table[] = {
                   3421:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3422: //     0       GMT             Greenwich Mean Time             GMT0
                   3423:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3424:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3425:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3426:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3427: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3428:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3429:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3430: //     3       BST             Brazil Standard Time            BST3
                   3431:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3432:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3433:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3434: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3435:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3436: //     3       GST             Greenland Standard Time         GST3
                   3437:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3438: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3439:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3440: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3441:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3442: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3443:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3444:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3445: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3446:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3447:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3448:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3449: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3450:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3451: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3452:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3453: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3454:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3455: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3456:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3457:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3458:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3459: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3460:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3461: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3462:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3463:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3464:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3465: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3466:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3467:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3468: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3469: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3470:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3471: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3472:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3473:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3474: //     11      SST             Samoa Standard Time             SST11
                   3475:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3476: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3477:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3478: //     -10     GST             Guam Standard Time              GST-10
                   3479:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3480: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3481:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3482:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3483:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3484: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3485:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3486:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3487: //     -9      JST             Japan Standard Time             JST-9
                   3488:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3489: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3490:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3491:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3492: //     -8      HKT             Hong Kong Time                  HKT-8
                   3493:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3494: //     -8      CCT             China Coast Time                CCT-8
                   3495:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3496:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3497: //     -8      SST             Singapore Standard Time         SST-8
                   3498:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3499: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3500:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3501:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3502: //     -7:30   JT              Java Standard Time              JST-7:30
                   3503: //     -7      NST             North Sumatra Time              NST-7
                   3504:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3505: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3506:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3507: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3508:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3509: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3510:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3511:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3512: //     -2      EET             Eastern Europe Time             EET-2
                   3513:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3514:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3515:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3516:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3517: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3518:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3519: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3520: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3521: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3522: //     -1      CET     CES     Central European Time           CET-1CES
                   3523:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3524:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3525:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3526:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3527: //     -1      WAT             West African Time               WAT-1
                   3528:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3529:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3530: //     0       UTC             Universal Coordinated Time      UTC0
                   3531:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3532:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3533:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3534:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3535:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3536:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3537: };
                   3538: 
1.1.1.53  root     3539: // FIXME: consider to build on non-Japanese environment :-(
                   3540: // message_japanese string must be in shift-jis
                   3541: 
1.1.1.33  root     3542: static const struct {
1.1.1.32  root     3543:        UINT16 code;
                   3544:        char *message_english;
                   3545:        char *message_japanese;
                   3546: } standard_error_table[] = {
                   3547:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3548:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3549:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3550:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3551:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3552:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3553:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3554:        {0x08,  "Insufficient memory", "������������܂���."},
                   3555:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3556:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3557:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3558:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3559:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3560:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3561:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3562:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3563:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3564:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3565:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3566:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3567:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3568:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3569:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3570:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3571:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3572:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3573:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3574:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3575:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3576:        {0x1F,  "General failure", "�G���[�ł�."},
                   3577:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3578:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3579:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3580:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3581:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3582:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3583:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3584:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3585: /*
                   3586:        {0x32,  "Network request not supported", NULL},
                   3587:        {0x33,  "Remote computer not listening", NULL},
                   3588:        {0x34,  "Duplicate name on network", NULL},
                   3589:        {0x35,  "Network name not found", NULL},
                   3590:        {0x36,  "Network busy", NULL},
                   3591:        {0x37,  "Network device no longer exists", NULL},
                   3592:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3593:        {0x39,  "Network adapter hardware error", NULL},
                   3594:        {0x3A,  "Incorrect response from network", NULL},
                   3595:        {0x3B,  "Unexpected network error", NULL},
                   3596:        {0x3C,  "Incompatible remote adapter", NULL},
                   3597:        {0x3D,  "Print queue full", NULL},
                   3598:        {0x3E,  "Queue not full", NULL},
                   3599:        {0x3F,  "Not enough space to print file", NULL},
                   3600:        {0x40,  "Network name was deleted", NULL},
                   3601:        {0x41,  "Network: Access denied", NULL},
                   3602:        {0x42,  "Network device type incorrect", NULL},
                   3603:        {0x43,  "Network name not found", NULL},
                   3604:        {0x44,  "Network name limit exceeded", NULL},
                   3605:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3606:        {0x46,  "Temporarily paused", NULL},
                   3607:        {0x47,  "Network request not accepted", NULL},
                   3608:        {0x48,  "Network print/disk redirection paused", NULL},
                   3609:        {0x49,  "Network software not installed", NULL},
                   3610:        {0x4A,  "Unexpected adapter close", NULL},
                   3611: */
                   3612:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3613:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3614:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3615:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3616:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3617:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3618:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3619:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3620:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3621:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
1.1.1.53  root     3622: #ifdef SUPPORT_MSCDEX
1.1.1.32  root     3623:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3624:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3625:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3626:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3627:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
1.1.1.53  root     3628: #endif
1.1.1.32  root     3629:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3630:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3631:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3632:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3633:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3634:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3635: };
                   3636: 
                   3637: static const struct {
                   3638:        UINT16 code;
                   3639:        char *message_english;
                   3640:        char *message_japanese;
                   3641: } param_error_table[] = {
                   3642:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3643:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3644:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3645:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3646:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3647:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3648:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3649:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3650:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3651:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3652:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3653: };
                   3654: 
                   3655: static const struct {
                   3656:        UINT16 code;
                   3657:        char *message_english;
                   3658:        char *message_japanese;
                   3659: } critical_error_table[] = {
                   3660:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3661:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3662:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3663:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3664:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3665:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3666:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3667:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3668:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3669:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3670:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3671:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3672:        {0x0C,  "General failure", "�G���[�ł�."},
                   3673:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3674:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3675:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3676:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3677:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3678:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3679:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3680:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3681:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3682: };
                   3683: 
1.1.1.20  root     3684: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3685: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3686: void msdos_putch(UINT8 data);
1.1.1.50  root     3687: void msdos_putch_fast(UINT8 data);
1.1.1.35  root     3688: #ifdef USE_SERVICE_THREAD
                   3689: void msdos_putch_tmp(UINT8 data);
                   3690: #endif
1.1.1.45  root     3691: const char *msdos_short_path(const char *path);
1.1.1.44  root     3692: bool msdos_is_valid_drive(int drv);
                   3693: bool msdos_is_removable_drive(int drv);
                   3694: bool msdos_is_cdrom_drive(int drv);
                   3695: bool msdos_is_remote_drive(int drv);
                   3696: bool msdos_is_subst_drive(int drv);
1.1.1.20  root     3697: 
1.1       root     3698: // process info
                   3699: 
                   3700: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3701: {
                   3702:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3703:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3704:                        memset(&process[i], 0, sizeof(process_t));
                   3705:                        process[i].psp = psp_seg;
                   3706:                        return(&process[i]);
                   3707:                }
                   3708:        }
                   3709:        fatalerror("too many processes\n");
                   3710:        return(NULL);
                   3711: }
                   3712: 
1.1.1.52  root     3713: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error = true)
1.1       root     3714: {
                   3715:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3716:                if(process[i].psp == psp_seg) {
                   3717:                        return(&process[i]);
                   3718:                }
                   3719:        }
1.1.1.33  root     3720:        if(show_error) {
                   3721:                fatalerror("invalid psp address\n");
                   3722:        }
1.1       root     3723:        return(NULL);
                   3724: }
                   3725: 
1.1.1.23  root     3726: void msdos_sda_update(int psp_seg)
                   3727: {
                   3728:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3729:        
                   3730:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3731:                if(process[i].psp == psp_seg) {
                   3732:                        sda->switchar = process[i].switchar;
                   3733:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3734:                        sda->current_dta.w.h = process[i].dta.w.h;
                   3735:                        sda->current_psp = process[i].psp;
                   3736:                        break;
                   3737:                }
                   3738:        }
                   3739:        sda->malloc_strategy = malloc_strategy;
                   3740:        sda->return_code = retval;
                   3741:        sda->current_drive = _getdrive();
                   3742: }
                   3743: 
1.1.1.13  root     3744: // dta info
                   3745: 
                   3746: void msdos_dta_info_init()
                   3747: {
1.1.1.14  root     3748:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     3749:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3750:        }
                   3751: }
                   3752: 
                   3753: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   3754: {
                   3755:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     3756:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3757:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   3758:                        if(free_dta == NULL) {
1.1.1.13  root     3759:                                free_dta = &dtalist[i];
                   3760:                        }
1.1.1.14  root     3761:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     3762:                        return(&dtalist[i]);
                   3763:                }
                   3764:        }
1.1.1.14  root     3765:        if(free_dta) {
1.1.1.13  root     3766:                free_dta->psp = psp_seg;
                   3767:                free_dta->dta = dta_laddr;
                   3768:                return(free_dta);
                   3769:        }
                   3770:        fatalerror("too many dta\n");
                   3771:        return(NULL);
                   3772: }
                   3773: 
                   3774: void msdos_dta_info_free(UINT16 psp_seg)
                   3775: {
1.1.1.14  root     3776:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3777:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     3778:                        FindClose(dtalist[i].find_handle);
                   3779:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3780:                }
                   3781:        }
                   3782: }
                   3783: 
1.1       root     3784: void msdos_cds_update(int drv)
                   3785: {
1.1.1.44  root     3786:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1       root     3787:        
1.1.1.44  root     3788:        memset(cds, 0, 88);
                   3789:        
                   3790:        if(msdos_is_valid_drive(drv)) {
                   3791:                char path[MAX_PATH];
                   3792:                if(msdos_is_remote_drive(drv)) {
                   3793:                        cds->drive_attrib = 0xc000;     // network drive
                   3794:                } else if(msdos_is_subst_drive(drv)) {
                   3795:                        cds->drive_attrib = 0x5000;     // subst drive
                   3796:                } else {
                   3797:                        cds->drive_attrib = 0x4000;     // physical drive
                   3798:                }
                   3799:                if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
                   3800:                        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
                   3801:                }
                   3802:        }
                   3803:        if(cds->path_name[0] == '\0') {
                   3804:                sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   3805:        }
                   3806:        cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   3807:        cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
                   3808:        cds->word_1 = cds->word_2 = 0xffff;
                   3809:        cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
                   3810:        cds->bs_offset = 2;
                   3811: }
                   3812: 
1.1.1.45  root     3813: void msdos_cds_update(int drv, const char *path)
1.1.1.44  root     3814: {
                   3815:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
                   3816:        
                   3817:        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1       root     3818: }
                   3819: 
1.1.1.17  root     3820: // nls information tables
                   3821: 
                   3822: // uppercase table (func 6502h)
                   3823: void msdos_upper_table_update()
                   3824: {
                   3825:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3826:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3827:                UINT8 c[4];
1.1.1.33  root     3828:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3829:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     3830:                c[0] = 0x80 + i;
                   3831:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   3832:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3833:        }
                   3834: }
                   3835: 
1.1.1.23  root     3836: // lowercase table (func 6503h)
                   3837: void msdos_lower_table_update()
                   3838: {
                   3839:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   3840:        for(unsigned i = 0; i < 0x80; ++i) {
                   3841:                UINT8 c[4];
1.1.1.33  root     3842:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3843:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     3844:                c[0] = 0x80 + i;
                   3845:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   3846:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3847:        }
                   3848: }
                   3849: 
1.1.1.17  root     3850: // filename uppercase table (func 6504h)
                   3851: void msdos_filename_upper_table_init()
                   3852: {
                   3853:        // depended on (file)system, not on active codepage
                   3854:        // temporary solution: just filling data
                   3855:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3856:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3857:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   3858:        }
                   3859: }
                   3860: 
                   3861: // filaname terminator table (func 6505h)
                   3862: void msdos_filename_terminator_table_init()
                   3863: {
                   3864:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   3865:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   3866:        
                   3867:        data[2] = 1;            // marker? (permissible character value)
                   3868:        data[3] = 0x00;         // 00h...FFh
                   3869:        data[4] = 0xff;
                   3870:        data[5] = 0;            // marker? (excluded character)
                   3871:        data[6] = 0x00;         // 00h...20h
                   3872:        data[7] = 0x20;
                   3873:        data[8] = 2;            // marker? (illegal characters for filename)
                   3874:        data[9] = (UINT8)strlen(illegal_chars);
                   3875:        memcpy(data + 10, illegal_chars, data[9]);
                   3876:        
                   3877:        // total length
                   3878:        *(UINT16 *)data = (10 - 2) + data[9];
                   3879: }
                   3880: 
                   3881: // collating table (func 6506h)
                   3882: void msdos_collating_table_update()
                   3883: {
                   3884:        // temporary solution: just filling data
                   3885:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     3886:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     3887:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   3888:        }
                   3889: }
                   3890: 
1.1       root     3891: // dbcs
                   3892: 
                   3893: void msdos_dbcs_table_update()
                   3894: {
                   3895:        UINT8 dbcs_data[DBCS_SIZE];
                   3896:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   3897:        
                   3898:        CPINFO info;
                   3899:        GetCPInfo(active_code_page, &info);
                   3900:        
                   3901:        if(info.MaxCharSize != 1) {
                   3902:                for(int i = 0;; i += 2) {
                   3903:                        UINT8 lo = info.LeadByte[i + 0];
                   3904:                        UINT8 hi = info.LeadByte[i + 1];
                   3905:                        dbcs_data[2 + i + 0] = lo;
                   3906:                        dbcs_data[2 + i + 1] = hi;
                   3907:                        if(lo == 0 && hi == 0) {
                   3908:                                dbcs_data[0] = i + 2;
                   3909:                                break;
                   3910:                        }
                   3911:                }
                   3912:        } else {
                   3913:                dbcs_data[0] = 2;       // ???
                   3914:        }
                   3915:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   3916: }
                   3917: 
1.1.1.17  root     3918: void msdos_dbcs_table_finish()
                   3919: {
1.1.1.32  root     3920:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     3921:                _setmbcp(system_code_page);
                   3922:        }
1.1.1.32  root     3923:        if(console_code_page != GetConsoleCP()) {
                   3924:                SetConsoleCP(console_code_page);
                   3925:                SetConsoleOutputCP(console_code_page);
                   3926:        }
1.1.1.17  root     3927: }
                   3928: 
                   3929: void msdos_nls_tables_init()
1.1       root     3930: {
1.1.1.32  root     3931:        active_code_page = console_code_page = GetConsoleCP();
                   3932:        system_code_page = _getmbcp();
                   3933:        
                   3934:        if(active_code_page != system_code_page) {
                   3935:                if(_setmbcp(active_code_page) != 0) {
                   3936:                        active_code_page = system_code_page;
                   3937:                }
                   3938:        }
                   3939:        
1.1.1.17  root     3940:        msdos_upper_table_update();
1.1.1.23  root     3941:        msdos_lower_table_update();
1.1.1.17  root     3942:        msdos_filename_terminator_table_init();
                   3943:        msdos_filename_upper_table_init();
                   3944:        msdos_collating_table_update();
1.1       root     3945:        msdos_dbcs_table_update();
                   3946: }
                   3947: 
1.1.1.17  root     3948: void msdos_nls_tables_update()
1.1       root     3949: {
1.1.1.17  root     3950:        msdos_dbcs_table_update();
                   3951:        msdos_upper_table_update();
1.1.1.23  root     3952:        msdos_lower_table_update();
                   3953: //     msdos_collating_table_update();
1.1       root     3954: }
                   3955: 
                   3956: int msdos_lead_byte_check(UINT8 code)
                   3957: {
                   3958:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   3959:        
                   3960:        for(int i = 0;; i += 2) {
                   3961:                UINT8 lo = dbcs_table[i + 0];
                   3962:                UINT8 hi = dbcs_table[i + 1];
                   3963:                if(lo == 0 && hi == 0) {
                   3964:                        break;
                   3965:                }
                   3966:                if(lo <= code && code <= hi) {
                   3967:                        return(1);
                   3968:                }
                   3969:        }
                   3970:        return(0);
                   3971: }
                   3972: 
1.1.1.20  root     3973: int msdos_ctrl_code_check(UINT8 code)
                   3974: {
1.1.1.22  root     3975:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     3976: }
                   3977: 
1.1.1.36  root     3978: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
                   3979: {
                   3980:        int is_kanji_1st = 0;
                   3981:        int is_kanji_2nd = 0;
                   3982:        
                   3983:        for(int p = 0;; p++) {
                   3984:                if(is_kanji_1st) {
                   3985:                        is_kanji_1st = 0;
                   3986:                        is_kanji_2nd = 1;
                   3987:                } else if(msdos_lead_byte_check(buf[p])) {
                   3988:                        is_kanji_1st = 1;
                   3989:                }
                   3990:                if(p == n) {
                   3991:                        return(is_kanji_2nd);
                   3992:                }
                   3993:                is_kanji_2nd = 0;
                   3994:        }
                   3995: }
                   3996: 
1.1       root     3997: // file control
                   3998: 
1.1.1.45  root     3999: const char *msdos_remove_double_quote(const char *path)
1.1.1.14  root     4000: {
                   4001:        static char tmp[MAX_PATH];
                   4002:        
                   4003:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45  root     4004:                memset(tmp, 0, sizeof(tmp));
1.1.1.14  root     4005:                memcpy(tmp, path + 1, strlen(path) - 2);
                   4006:        } else {
                   4007:                strcpy(tmp, path);
                   4008:        }
                   4009:        return(tmp);
                   4010: }
                   4011: 
1.1.1.45  root     4012: const char *msdos_remove_end_separator(const char *path)
1.1.1.32  root     4013: {
                   4014:        static char tmp[MAX_PATH];
                   4015:        
                   4016:        strcpy(tmp, path);
1.1.1.45  root     4017:        
                   4018:        // for example "C:\" case, the end separator should not be removed
                   4019:        if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
                   4020:                tmp[strlen(tmp) - 1] = '\0';
1.1.1.32  root     4021:        }
                   4022:        return(tmp);
                   4023: }
                   4024: 
1.1.1.45  root     4025: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14  root     4026: {
                   4027:        static char tmp[MAX_PATH];
1.1.1.45  root     4028:        const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14  root     4029:        
                   4030:        if(strlen(tmp_dir) == 0) {
                   4031:                strcpy(tmp, file);
                   4032:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   4033:                sprintf(tmp, "%s%s", tmp_dir, file);
                   4034:        } else {
                   4035:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   4036:        }
                   4037:        return(tmp);
                   4038: }
                   4039: 
1.1.1.45  root     4040: const char *msdos_trimmed_path(const char *path, int lfn)
1.1       root     4041: {
                   4042:        static char tmp[MAX_PATH];
                   4043:        
                   4044:        if(lfn) {
                   4045:                strcpy(tmp, path);
                   4046:        } else {
                   4047:                // remove space in the path
1.1.1.45  root     4048:                const char *src = path;
                   4049:                char *dst = tmp;
1.1       root     4050:                
                   4051:                while(*src != '\0') {
                   4052:                        if(msdos_lead_byte_check(*src)) {
                   4053:                                *dst++ = *src++;
                   4054:                                *dst++ = *src++;
                   4055:                        } else if(*src != ' ') {
                   4056:                                *dst++ = *src++;
                   4057:                        } else {
                   4058:                                src++;  // skip space
                   4059:                        }
                   4060:                }
                   4061:                *dst = '\0';
                   4062:        }
1.1.1.14  root     4063:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   4064:                // redirect C:\COMMAND.COM to comspec_path
                   4065:                strcpy(tmp, comspec_path);
                   4066:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   4067:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   4068:                static int root_drive_protected = -1;
                   4069:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   4070:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   4071:                
                   4072:                if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
                   4073:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   4074:                        strcpy(name, name_temp);
                   4075:                        name_temp[0] = '\0';
                   4076:                        
                   4077:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   4078:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   4079:                                if(root_drive_protected == -1) {
                   4080:                                        FILE *fp = NULL;
                   4081:                                        
                   4082:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   4083:                                        root_drive_protected = 1;
                   4084:                                        try {
                   4085:                                                if((fp = fopen(temp, "w")) != NULL) {
                   4086:                                                        if(fprintf(fp, "TEST") == 4) {
                   4087:                                                                root_drive_protected = 0;
                   4088:                                                        }
                   4089:                                                }
                   4090:                                        } catch(...) {
                   4091:                                        }
                   4092:                                        if(fp != NULL) {
                   4093:                                                fclose(fp);
                   4094:                                        }
                   4095:                                        if(_access(temp, 0) == 0) {
                   4096:                                                remove(temp);
                   4097:                                        }
                   4098:                                }
                   4099:                                if(root_drive_protected == 1) {
                   4100:                                        if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
                   4101:                                           GetEnvironmentVariable("TMP",  temp, MAX_PATH) != 0) {
                   4102:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   4103:                                        }
                   4104:                                }
                   4105:                        }
                   4106:                }
                   4107:        }
1.1       root     4108:        return(tmp);
                   4109: }
                   4110: 
1.1.1.45  root     4111: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28  root     4112: {
1.1.1.32  root     4113:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     4114:        static char env_path[ENV_SIZE];
                   4115:        char tmp[ENV_SIZE], *token;
                   4116:        
                   4117:        memset(env_path, 0, sizeof(env_path));
                   4118:        strcpy(tmp, src);
                   4119:        token = my_strtok(tmp, ";");
                   4120:        
                   4121:        while(token != NULL) {
                   4122:                if(token[0] != '\0') {
1.1.1.45  root     4123:                        const char *path = msdos_remove_double_quote(token);
                   4124:                        char short_path[MAX_PATH];
1.1.1.32  root     4125:                        if(path != NULL && strlen(path) != 0) {
                   4126:                                if(env_path[0] != '\0') {
                   4127:                                        strcat(env_path, ";");
                   4128:                                }
1.1.1.28  root     4129:                                if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     4130:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     4131:                                } else {
                   4132:                                        my_strupr(short_path);
1.1.1.32  root     4133:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     4134:                                }
                   4135:                        }
                   4136:                }
                   4137:                token = my_strtok(NULL, ";");
                   4138:        }
                   4139:        return(env_path);
                   4140: }
                   4141: 
1.1.1.45  root     4142: bool match(const char *text, const char *pattern)
1.1       root     4143: {
1.1.1.24  root     4144:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     4145:        switch(*pattern) {
1.1       root     4146:        case '\0':
                   4147:                return !*text;
                   4148:        case '*':
1.1.1.14  root     4149:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     4150:        case '?':
                   4151:                return *text && match(text + 1, pattern + 1);
                   4152:        default:
                   4153:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   4154:        }
                   4155: }
                   4156: 
1.1.1.45  root     4157: bool msdos_match_volume_label(const char *path, const char *volume)
1.1       root     4158: {
1.1.1.45  root     4159:        const char *p = NULL;
1.1       root     4160:        
1.1.1.14  root     4161:        if(!*volume) {
                   4162:                return false;
                   4163:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     4164:                return msdos_match_volume_label(p + 1, volume);
                   4165:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   4166:                return msdos_match_volume_label(p + 1, volume);
                   4167:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     4168:                char tmp[MAX_PATH];
                   4169:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   4170:                return match(volume, tmp);
1.1       root     4171:        } else {
                   4172:                return match(volume, path);
                   4173:        }
                   4174: }
                   4175: 
1.1.1.45  root     4176: const char *msdos_fcb_path(fcb_t *fcb)
1.1       root     4177: {
                   4178:        static char tmp[MAX_PATH];
                   4179:        char name[9], ext[4];
                   4180:        
                   4181:        memset(name, 0, sizeof(name));
                   4182:        memcpy(name, fcb->file_name, 8);
                   4183:        strcpy(name, msdos_trimmed_path(name, 0));
                   4184:        
                   4185:        memset(ext, 0, sizeof(ext));
                   4186:        memcpy(ext, fcb->file_name + 8, 3);
                   4187:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4188:        
                   4189:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4190:                strcpy(name, "*");
                   4191:        }
                   4192:        if(ext[0] == '\0') {
                   4193:                strcpy(tmp, name);
                   4194:        } else {
                   4195:                if(strcmp(ext, "???") == 0) {
                   4196:                        strcpy(ext, "*");
                   4197:                }
                   4198:                sprintf(tmp, "%s.%s", name, ext);
                   4199:        }
                   4200:        return(tmp);
                   4201: }
                   4202: 
1.1.1.45  root     4203: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1       root     4204: {
                   4205:        char *ext = my_strchr(path, '.');
                   4206:        
                   4207:        memset(fcb->file_name, 0x20, 8 + 3);
                   4208:        if(ext != NULL && path[0] != '.') {
                   4209:                *ext = '\0';
                   4210:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4211:        }
                   4212:        memcpy(fcb->file_name, path, strlen(path));
                   4213: }
                   4214: 
1.1.1.45  root     4215: const char *msdos_short_path(const char *path)
1.1       root     4216: {
                   4217:        static char tmp[MAX_PATH];
                   4218:        
1.1.1.24  root     4219:        if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
                   4220:                strcpy(tmp, path);
                   4221:        }
1.1       root     4222:        my_strupr(tmp);
                   4223:        return(tmp);
                   4224: }
                   4225: 
1.1.1.45  root     4226: const char *msdos_short_name(WIN32_FIND_DATA *fd)
1.1.1.13  root     4227: {
                   4228:        static char tmp[MAX_PATH];
1.1.1.45  root     4229:        
1.1.1.14  root     4230:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4231:                strcpy(tmp, fd->cAlternateFileName);
                   4232:        } else {
                   4233:                strcpy(tmp, fd->cFileName);
                   4234:        }
                   4235:        my_strupr(tmp);
                   4236:        return(tmp);
                   4237: }
                   4238: 
1.1.1.45  root     4239: const char *msdos_short_full_path(const char *path)
1.1       root     4240: {
                   4241:        static char tmp[MAX_PATH];
                   4242:        char full[MAX_PATH], *name;
                   4243:        
1.1.1.14  root     4244:        // Full works with non-existent files, but Short does not
1.1       root     4245:        GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14  root     4246:        *tmp = '\0';
                   4247:        if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
                   4248:                name[-1] = '\0';
                   4249:                DWORD len = GetShortPathName(full, tmp, MAX_PATH);
                   4250:                if(len == 0) {
                   4251:                        strcpy(tmp, full);
                   4252:                } else {
                   4253:                        tmp[len++] = '\\';
                   4254:                        strcpy(tmp + len, name);
                   4255:                }
                   4256:        }
1.1       root     4257:        my_strupr(tmp);
                   4258:        return(tmp);
                   4259: }
                   4260: 
1.1.1.45  root     4261: const char *msdos_short_full_dir(const char *path)
1.1       root     4262: {
                   4263:        static char tmp[MAX_PATH];
                   4264:        char full[MAX_PATH], *name;
                   4265:        
                   4266:        GetFullPathName(path, MAX_PATH, full, &name);
                   4267:        name[-1] = '\0';
1.1.1.24  root     4268:        if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
                   4269:                strcpy(tmp, full);
                   4270:        }
1.1       root     4271:        my_strupr(tmp);
                   4272:        return(tmp);
                   4273: }
                   4274: 
1.1.1.45  root     4275: const char *msdos_local_file_path(const char *path, int lfn)
1.1       root     4276: {
1.1.1.45  root     4277:        static char trimmed[MAX_PATH];
                   4278:        
                   4279:        strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14  root     4280: #if 0
                   4281:        // I have forgotten the reason of this routine... :-(
1.1       root     4282:        if(_access(trimmed, 0) != 0) {
                   4283:                process_t *process = msdos_process_info_get(current_psp);
                   4284:                static char tmp[MAX_PATH];
                   4285:                
                   4286:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4287:                if(_access(tmp, 0) == 0) {
                   4288:                        return(tmp);
                   4289:                }
                   4290:        }
1.1.1.14  root     4291: #endif
1.1       root     4292:        return(trimmed);
                   4293: }
                   4294: 
1.1.1.45  root     4295: bool msdos_is_device_path(const char *path)
1.1.1.11  root     4296: {
                   4297:        char full[MAX_PATH], *name;
                   4298:        
1.1.1.24  root     4299:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4300:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4301:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4302:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4303:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4304:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4305:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4306:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4307:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4308:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4309:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4310:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4311:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4312:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4313:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4314:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4315:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4316:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4317:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4318:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4319:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4320:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4321:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4322:                        return(true);
                   4323:                } else if(name != NULL) {
                   4324:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4325:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4326:                           _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43  root     4327: //                        _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30  root     4328:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4329:                                return(true);
                   4330:                        }
                   4331:                }
1.1.1.24  root     4332:        }
                   4333:        return(false);
1.1.1.11  root     4334: }
                   4335: 
1.1.1.45  root     4336: bool msdos_is_con_path(const char *path)
1.1.1.8   root     4337: {
1.1.1.14  root     4338:        char full[MAX_PATH], *name;
1.1.1.8   root     4339:        
1.1.1.24  root     4340:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4341:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4342:        }
                   4343:        return(false);
                   4344: }
                   4345: 
1.1.1.45  root     4346: int msdos_is_comm_path(const char *path)
1.1.1.24  root     4347: {
                   4348:        char full[MAX_PATH], *name;
                   4349:        
                   4350:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4351:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4352:                        return(1);
                   4353:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4354:                        return(2);
                   4355:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4356:                        return(3);
                   4357:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4358:                        return(4);
1.1.1.24  root     4359:                }
                   4360:        }
1.1.1.29  root     4361:        return(0);
                   4362: }
                   4363: 
1.1.1.45  root     4364: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37  root     4365: {
                   4366:        // 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     4367:        const char *p = NULL;
1.1.1.37  root     4368:        
                   4369:        if((p = strstr(path, ":")) != NULL) {
                   4370:                UINT8 selector = sio_read(sio_port - 1, 3);
                   4371:                
                   4372:                // baud rate
                   4373:                int baud = max(110, min(9600, atoi(p + 1)));
                   4374:                UINT16 divisor = 115200 / baud;
                   4375:                
                   4376:                if((p = strstr(p + 1, ",")) != NULL) {
                   4377:                        // parity
                   4378:                        if(p[1] == 'N' || p[1] == 'n') {
                   4379:                                selector = (selector & ~0x38) | 0x00;
                   4380:                        } else if(p[1] == 'O' || p[1] == 'o') {
                   4381:                                selector = (selector & ~0x38) | 0x08;
                   4382:                        } else if(p[1] == 'E' || p[1] == 'e') {
                   4383:                                selector = (selector & ~0x38) | 0x18;
                   4384:                        } else if(p[1] == 'M' || p[1] == 'm') {
                   4385:                                selector = (selector & ~0x38) | 0x28;
                   4386:                        } else if(p[1] == 'S' || p[1] == 's') {
                   4387:                                selector = (selector & ~0x38) | 0x38;
                   4388:                        }
                   4389:                        if((p = strstr(p + 1, ",")) != NULL) {
                   4390:                                // word length
                   4391:                                if(p[1] == '8') {
                   4392:                                        selector = (selector & ~0x03) | 0x03;
                   4393:                                } else if(p[1] == '7') {
                   4394:                                        selector = (selector & ~0x03) | 0x02;
                   4395:                                } else if(p[1] == '6') {
                   4396:                                        selector = (selector & ~0x03) | 0x01;
                   4397:                                } else if(p[1] == '5') {
                   4398:                                        selector = (selector & ~0x03) | 0x00;
                   4399:                                }
                   4400:                                if((p = strstr(p + 1, ",")) != NULL) {
                   4401:                                        // stop bits
                   4402:                                        float bits = atof(p + 1);
                   4403:                                        if(bits > 1.0F) {
                   4404:                                                selector |= 0x04;
                   4405:                                        } else {
                   4406:                                                selector &= ~0x04;
                   4407:                                        }
                   4408:                                }
                   4409:                        }
                   4410:                }
                   4411:                sio_write(sio_port - 1, 3, selector | 0x80);
                   4412:                sio_write(sio_port - 1, 0, divisor & 0xff);
                   4413:                sio_write(sio_port - 1, 1, divisor >> 8);
                   4414:                sio_write(sio_port - 1, 3, selector);
                   4415:        }
                   4416: }
                   4417: 
1.1.1.45  root     4418: int msdos_is_prn_path(const char *path)
1.1.1.30  root     4419: {
                   4420:        char full[MAX_PATH], *name;
                   4421:        
                   4422:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4423:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4424:                        return(1);
                   4425:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4426:                        return(1);
                   4427:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4428:                        return(2);
                   4429:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4430:                        return(3);
                   4431:                }
                   4432:        }
                   4433:        return(0);
                   4434: }
                   4435: 
1.1.1.44  root     4436: bool msdos_is_valid_drive(int drv)
                   4437: {
                   4438:        return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
                   4439: }
                   4440: 
                   4441: bool msdos_is_removable_drive(int drv)
                   4442: {
                   4443:        char volume[] = "A:\\";
                   4444:        
                   4445:        volume[0] = 'A' + drv;
                   4446:        
                   4447:        return(GetDriveType(volume) == DRIVE_REMOVABLE);
                   4448: }
                   4449: 
                   4450: bool msdos_is_cdrom_drive(int drv)
                   4451: {
                   4452:        char volume[] = "A:\\";
                   4453:        
                   4454:        volume[0] = 'A' + drv;
                   4455:        
                   4456:        return(GetDriveType(volume) == DRIVE_CDROM);
                   4457: }
                   4458: 
                   4459: bool msdos_is_remote_drive(int drv)
                   4460: {
                   4461:        char volume[] = "A:\\";
                   4462:        
                   4463:        volume[0] = 'A' + drv;
                   4464:        
                   4465:        return(GetDriveType(volume) == DRIVE_REMOTE);
                   4466: }
                   4467: 
                   4468: bool msdos_is_subst_drive(int drv)
                   4469: {
                   4470:        char device[] = "A:", path[MAX_PATH];
                   4471:        
                   4472:        device[0] = 'A' + drv;
                   4473:        
                   4474:        if(QueryDosDevice(device, path, MAX_PATH)) {
                   4475:                if(strncmp(path, "\\??\\", 4) == 0) {
                   4476:                        return(true);
                   4477:                }
                   4478:        }
                   4479:        return(false);
                   4480: }
                   4481: 
1.1.1.45  root     4482: bool msdos_is_existing_file(const char *path)
1.1.1.24  root     4483: {
                   4484:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   4485:        WIN32_FIND_DATA FindData;
                   4486:        HANDLE hFind;
                   4487:        
                   4488:        if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
                   4489:                FindClose(hFind);
                   4490:                return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
                   4491:        }
                   4492:        return(false);
1.1.1.8   root     4493: }
                   4494: 
1.1.1.45  root     4495: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9   root     4496: {
                   4497:        static char tmp[MAX_PATH];
1.1.1.28  root     4498:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4499:        
1.1.1.28  root     4500:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.9   root     4501:        if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
                   4502:                sprintf(file_name, "COMMAND.COM");
                   4503:                if(_access(tmp, 0) == 0) {
                   4504:                        return(tmp);
                   4505:                }
                   4506:        }
1.1.1.28  root     4507:        
                   4508:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.9   root     4509:        if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
                   4510:                sprintf(file_name, "COMMAND.COM");
                   4511:                if(_access(tmp, 0) == 0) {
                   4512:                        return(tmp);
                   4513:                }
                   4514:        }
1.1.1.28  root     4515:        
                   4516:        // check if COMMAND.COM is in the current directory
1.1.1.9   root     4517:        if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
                   4518:                if(_access(tmp, 0) == 0) {
                   4519:                        return(tmp);
                   4520:                }
                   4521:        }
1.1.1.28  root     4522:        
                   4523:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4524:        strcpy(path, env_path);
                   4525:        char *token = my_strtok(path, ";");
1.1.1.9   root     4526:        while(token != NULL) {
1.1.1.14  root     4527:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4528:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4529:                        if(_access(tmp, 0) == 0) {
                   4530:                                return(tmp);
                   4531:                        }
                   4532:                }
                   4533:                token = my_strtok(NULL, ";");
                   4534:        }
                   4535:        return(NULL);
                   4536: }
                   4537: 
1.1.1.14  root     4538: int msdos_drive_number(const char *path)
1.1       root     4539: {
                   4540:        char tmp[MAX_PATH], *name;
                   4541:        
1.1.1.45  root     4542:        if(GetFullPathName(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
                   4543:                if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4544:                        return(tmp[0] - 'a');
                   4545:                } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
                   4546:                        return(tmp[0] - 'A');
                   4547:                }
1.1       root     4548:        }
1.1.1.45  root     4549: //     return(msdos_drive_number("."));
                   4550:        return(_getdrive() - 1);
1.1       root     4551: }
                   4552: 
1.1.1.45  root     4553: const char *msdos_volume_label(const char *path)
1.1       root     4554: {
                   4555:        static char tmp[MAX_PATH];
                   4556:        char volume[] = "A:\\";
                   4557:        
                   4558:        if(path[1] == ':') {
                   4559:                volume[0] = path[0];
                   4560:        } else {
                   4561:                volume[0] = 'A' + _getdrive() - 1;
                   4562:        }
                   4563:        if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
                   4564:                memset(tmp, 0, sizeof(tmp));
                   4565:        }
                   4566:        return(tmp);
                   4567: }
                   4568: 
1.1.1.45  root     4569: const char *msdos_short_volume_label(const char *label)
1.1       root     4570: {
                   4571:        static char tmp[(8 + 1 + 3) + 1];
1.1.1.45  root     4572:        const char *src = label;
1.1       root     4573:        int remain = strlen(label);
                   4574:        char *dst_n = tmp;
                   4575:        char *dst_e = tmp + 9;
                   4576:        
                   4577:        strcpy(tmp, "        .   ");
                   4578:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4579:                if(msdos_lead_byte_check(*src)) {
                   4580:                        if(++i == 8) {
                   4581:                                break;
                   4582:                        }
                   4583:                        *dst_n++ = *src++;
                   4584:                        remain--;
                   4585:                }
                   4586:                *dst_n++ = *src++;
                   4587:                remain--;
                   4588:        }
                   4589:        if(remain > 0) {
                   4590:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4591:                        if(msdos_lead_byte_check(*src)) {
                   4592:                                if(++i == 3) {
                   4593:                                        break;
                   4594:                                }
                   4595:                                *dst_e++ = *src++;
                   4596:                                remain--;
                   4597:                        }
                   4598:                        *dst_e++ = *src++;
                   4599:                        remain--;
                   4600:                }
                   4601:                *dst_e = '\0';
                   4602:        } else {
                   4603:                *dst_n = '\0';
                   4604:        }
                   4605:        my_strupr(tmp);
                   4606:        return(tmp);
                   4607: }
                   4608: 
1.1.1.13  root     4609: errno_t msdos_maperr(unsigned long oserrno)
                   4610: {
                   4611:        _doserrno = oserrno;
1.1.1.14  root     4612:        switch(oserrno) {
1.1.1.13  root     4613:        case ERROR_FILE_NOT_FOUND:         // 2
                   4614:        case ERROR_PATH_NOT_FOUND:         // 3
                   4615:        case ERROR_INVALID_DRIVE:          // 15
                   4616:        case ERROR_NO_MORE_FILES:          // 18
                   4617:        case ERROR_BAD_NETPATH:            // 53
                   4618:        case ERROR_BAD_NET_NAME:           // 67
                   4619:        case ERROR_BAD_PATHNAME:           // 161
                   4620:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4621:                return ENOENT;
                   4622:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4623:                return EMFILE;
                   4624:        case ERROR_ACCESS_DENIED:          // 5
                   4625:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4626:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4627:        case ERROR_CANNOT_MAKE:            // 82
                   4628:        case ERROR_FAIL_I24:               // 83
                   4629:        case ERROR_DRIVE_LOCKED:           // 108
                   4630:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4631:        case ERROR_NOT_LOCKED:             // 158
                   4632:        case ERROR_LOCK_FAILED:            // 167
                   4633:                return EACCES;
                   4634:        case ERROR_INVALID_HANDLE:         // 6
                   4635:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4636:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4637:                return EBADF;
                   4638:        case ERROR_ARENA_TRASHED:          // 7
                   4639:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4640:        case ERROR_INVALID_BLOCK:          // 9
                   4641:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4642:                return ENOMEM;
                   4643:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4644:                return E2BIG;
                   4645:        case ERROR_BAD_FORMAT:             // 11
                   4646:                return ENOEXEC;
                   4647:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4648:                return EXDEV;
                   4649:        case ERROR_FILE_EXISTS:            // 80
                   4650:        case ERROR_ALREADY_EXISTS:         // 183
                   4651:                return EEXIST;
                   4652:        case ERROR_NO_PROC_SLOTS:          // 89
                   4653:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4654:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4655:                return EAGAIN;
                   4656:        case ERROR_BROKEN_PIPE:            // 109
                   4657:                return EPIPE;
                   4658:        case ERROR_DISK_FULL:              // 112
                   4659:                return ENOSPC;
                   4660:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4661:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4662:                return ECHILD;
                   4663:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4664:                return ENOTEMPTY;
                   4665:        }
1.1.1.14  root     4666:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4667:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4668:                return EACCES;
                   4669:        }
1.1.1.14  root     4670:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4671:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4672:                return ENOEXEC;
                   4673:        }
                   4674:        return EINVAL;
                   4675: }
                   4676: 
1.1.1.45  root     4677: int msdos_open(const char *path, int oflag)
1.1.1.13  root     4678: {
1.1.1.14  root     4679:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45  root     4680:                return(_open(path, oflag));
1.1.1.13  root     4681:        }
1.1.1.14  root     4682:        
                   4683:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4684:        DWORD disposition;
1.1.1.14  root     4685:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4686:        default:
1.1.1.13  root     4687:        case _O_EXCL:
                   4688:                disposition = OPEN_EXISTING;
                   4689:                break;
                   4690:        case _O_CREAT:
                   4691:                disposition = OPEN_ALWAYS;
                   4692:                break;
                   4693:        case _O_CREAT | _O_EXCL:
                   4694:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4695:                disposition = CREATE_NEW;
                   4696:                break;
                   4697:        case _O_TRUNC:
                   4698:        case _O_TRUNC | _O_EXCL:
                   4699:                disposition = TRUNCATE_EXISTING;
                   4700:                break;
                   4701:        case _O_CREAT | _O_TRUNC:
                   4702:                disposition = CREATE_ALWAYS;
                   4703:                break;
                   4704:        }
1.1.1.14  root     4705:        
1.1.1.45  root     4706:        HANDLE h = CreateFile(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
1.1.1.13  root     4707:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4708:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4709:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4710:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4711:                // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.45  root     4712:                h = CreateFile(path, GENERIC_READ,
1.1.1.13  root     4713:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4714:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4715:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4716:                        errno = msdos_maperr(GetLastError());
1.1.1.45  root     4717:                        return(-1);
1.1.1.13  root     4718:                }
                   4719:        }
1.1.1.14  root     4720:        
1.1.1.13  root     4721:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     4722:        if(fd == -1) {
1.1.1.13  root     4723:                CloseHandle(h);
                   4724:        }
1.1.1.45  root     4725:        return(fd);
                   4726: }
                   4727: 
                   4728: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
                   4729: {
                   4730:        int fd = -1;
                   4731:        
                   4732:        *sio_port = *lpt_port = 0;
                   4733:        
                   4734:        if(msdos_is_con_path(path)) {
                   4735:                // MODE.COM opens CON device with read/write mode :-(
                   4736:                if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
                   4737:                        oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
                   4738:                        oflag |= _O_RDONLY;
                   4739:                }
                   4740:                if((fd = msdos_open("CON", oflag)) == -1) {
                   4741: //                     fd = msdos_open("NUL", oflag);
                   4742:                }
                   4743:        } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
                   4744:                fd = msdos_open("NUL", oflag);
                   4745:                msdos_set_comm_params(*sio_port, path);
                   4746:        } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
                   4747:                fd = msdos_open("NUL", oflag);
                   4748:        } else if(msdos_is_device_path(path)) {
                   4749:                fd = msdos_open("NUL", oflag);
                   4750: //     } else if(oflag & _O_CREAT) {
                   4751: //             fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
                   4752: //     } else {
                   4753: //             fd = _open(path, oflag);
                   4754:        }
                   4755:        return(fd);
                   4756: }
                   4757: 
                   4758: UINT16 msdos_device_info(const char *path)
                   4759: {
                   4760:        if(msdos_is_con_path(path)) {
                   4761:                return(0x80d3);
                   4762:        } else if(msdos_is_comm_path(path)) {
                   4763:                return(0x80a0);
                   4764:        } else if(msdos_is_prn_path(path)) {
                   4765: //             return(0xa8c0);
                   4766:                return(0x80a0);
                   4767:        } else if(msdos_is_device_path(path)) {
                   4768:                if(strstr(path, "EMMXXXX0") != NULL) {
                   4769:                        return(0xc0c0);
                   4770:                } else if(strstr(path, "MSCD001") != NULL) {
                   4771:                        return(0xc880);
                   4772:                } else {
                   4773:                        return(0x8084);
                   4774:                }
                   4775:        } else {
                   4776:                return(msdos_drive_number(path));
                   4777:        }
1.1.1.13  root     4778: }
                   4779: 
1.1.1.52  root     4780: 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     4781: {
                   4782:        static int id = 0;
                   4783:        char full[MAX_PATH], *name;
                   4784:        
                   4785:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4786:                strcpy(file_handler[fd].path, full);
                   4787:        } else {
                   4788:                strcpy(file_handler[fd].path, path);
                   4789:        }
1.1.1.14  root     4790:        // isatty makes no distinction between CON & NUL
                   4791:        // GetFileSize fails on CON, succeeds on NUL
                   4792:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45  root     4793:                if(info == 0x80d3) {
                   4794:                        info = 0x8084;
                   4795:                }
1.1.1.14  root     4796:                atty = 0;
                   4797:        } else if(!atty && info == 0x80d3) {
1.1.1.45  root     4798: //             info = msdos_drive_number(".");
                   4799:                info = msdos_drive_number(path);
1.1.1.14  root     4800:        }
1.1       root     4801:        file_handler[fd].valid = 1;
                   4802:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37  root     4803:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     4804:        file_handler[fd].mode = mode;
                   4805:        file_handler[fd].info = info;
                   4806:        file_handler[fd].psp = psp_seg;
1.1.1.37  root     4807:        file_handler[fd].sio_port = sio_port;
                   4808:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     4809:        
                   4810:        // init system file table
                   4811:        if(fd < 20) {
                   4812:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   4813:                
                   4814:                memset(sft, 0, 0x3b);
                   4815:                
                   4816:                *(UINT16 *)(sft + 0x00) = 1;
                   4817:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
                   4818:                *(UINT8  *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
                   4819:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   4820:                
                   4821:                if(!(file_handler[fd].info & 0x80)) {
                   4822:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   4823:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   4824:                        
                   4825:                        FILETIME time, local;
                   4826:                        HANDLE hHandle;
                   4827:                        WORD dos_date = 0, dos_time = 0;
                   4828:                        DWORD file_size = 0;
                   4829:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   4830:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   4831:                                        FileTimeToLocalFileTime(&time, &local);
                   4832:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   4833:                                }
                   4834:                                file_size = GetFileSize(hHandle, NULL);
                   4835:                        }
                   4836:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   4837:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   4838:                        *(UINT32 *)(sft + 0x11) = file_size;
                   4839:                }
                   4840:                
                   4841:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   4842:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   4843:                my_strupr(fname);
                   4844:                my_strupr(ext);
                   4845:                memset(sft + 0x20, 0x20, 11);
                   4846:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   4847:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   4848:                
                   4849:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   4850:        }
1.1       root     4851: }
                   4852: 
                   4853: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   4854: {
                   4855:        strcpy(file_handler[dst].path, file_handler[src].path);
                   4856:        file_handler[dst].valid = 1;
                   4857:        file_handler[dst].id = file_handler[src].id;
                   4858:        file_handler[dst].atty = file_handler[src].atty;
                   4859:        file_handler[dst].mode = file_handler[src].mode;
                   4860:        file_handler[dst].info = file_handler[src].info;
                   4861:        file_handler[dst].psp = psp_seg;
1.1.1.37  root     4862:        file_handler[dst].sio_port = file_handler[src].sio_port;
                   4863:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     4864: }
                   4865: 
1.1.1.20  root     4866: void msdos_file_handler_close(int fd)
1.1       root     4867: {
                   4868:        file_handler[fd].valid = 0;
1.1.1.21  root     4869:        
                   4870:        if(fd < 20) {
                   4871:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   4872:        }
1.1       root     4873: }
                   4874: 
1.1.1.14  root     4875: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     4876: {
1.1.1.14  root     4877:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   4878:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   4879:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     4880: }
                   4881: 
                   4882: // find file
                   4883: 
                   4884: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   4885: {
                   4886:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4887:                return(0);      // search directory only !!!
                   4888:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   4889:                return(0);
                   4890:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   4891:                return(0);
                   4892:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4893:                return(0);
                   4894:        } else if((attribute & required_mask) != required_mask) {
                   4895:                return(0);
                   4896:        } else {
                   4897:                return(1);
                   4898:        }
                   4899: }
                   4900: 
1.1.1.13  root     4901: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
                   4902: {
1.1.1.14  root     4903:        if(fd->cAlternateFileName[0]) {
1.1.1.42  root     4904:                return(1);
1.1.1.13  root     4905:        }
                   4906:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     4907:        if(len > 12) {
1.1.1.42  root     4908:                return(0);
1.1.1.13  root     4909:        }
                   4910:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     4911:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42  root     4912:                return(0);
1.1.1.13  root     4913:        }
1.1.1.42  root     4914:        return(1);
1.1.1.13  root     4915: }
                   4916: 
1.1       root     4917: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
                   4918: {
                   4919:        FILETIME local;
                   4920:        
                   4921:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   4922:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   4923:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   4924:        
                   4925:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   4926:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   4927:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   4928:        
                   4929:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   4930:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   4931:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   4932: }
                   4933: 
                   4934: // i/o
                   4935: 
                   4936: void msdos_stdio_reopen()
                   4937: {
                   4938:        if(!file_handler[0].valid) {
                   4939:                _dup2(DUP_STDIN, 0);
                   4940:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   4941:        }
                   4942:        if(!file_handler[1].valid) {
                   4943:                _dup2(DUP_STDOUT, 1);
                   4944:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   4945:        }
                   4946:        if(!file_handler[2].valid) {
                   4947:                _dup2(DUP_STDERR, 2);
                   4948:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   4949:        }
1.1.1.21  root     4950:        if(!file_handler[3].valid) {
                   4951:                _dup2(DUP_STDAUX, 3);
                   4952:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   4953:        }
                   4954:        if(!file_handler[4].valid) {
                   4955:                _dup2(DUP_STDPRN, 4);
1.1.1.45  root     4956: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   4957:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     4958:        }
                   4959:        for(int i = 0; i < 5; i++) {
                   4960:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   4961:                        msdos_psp_set_file_table(i, i, current_psp);
                   4962:                }
                   4963:        }
1.1       root     4964: }
                   4965: 
1.1.1.37  root     4966: int msdos_read(int fd, void *buffer, unsigned int count)
                   4967: {
                   4968:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   4969:                // read from serial port
                   4970:                int read = 0;
                   4971:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   4972:                        UINT8 *buf = (UINT8 *)buffer;
                   4973:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   4974:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     4975:                        DWORD timeout = timeGetTime() + 1000;
                   4976:                        while(read < count) {
                   4977:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
                   4978:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
                   4979:                                        timeout = timeGetTime() + 1000;
                   4980:                                } else {
                   4981:                                        if(timeGetTime() > timeout) {
                   4982:                                                break;
                   4983:                                        }
                   4984:                                        Sleep(10);
1.1.1.37  root     4985:                                }
                   4986:                        }
                   4987:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   4988:                }
                   4989:                return(read);
                   4990:        }
                   4991:        return(_read(fd, buffer, count));
                   4992: }
                   4993: 
1.1       root     4994: int msdos_kbhit()
                   4995: {
                   4996:        msdos_stdio_reopen();
                   4997:        
1.1.1.20  root     4998:        process_t *process = msdos_process_info_get(current_psp);
                   4999:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5000:        
                   5001:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5002:                // stdin is redirected to file
1.1.1.20  root     5003:                return(eof(fd) == 0);
1.1       root     5004:        }
                   5005:        
                   5006:        // check keyboard status
1.1.1.35  root     5007:        if(key_recv != 0) {
1.1       root     5008:                return(1);
                   5009:        }
1.1.1.35  root     5010:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5011: #ifdef USE_SERVICE_THREAD
                   5012:                EnterCriticalSection(&key_buf_crit_sect);
                   5013: #endif
1.1.1.55  root     5014:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5015: #ifdef USE_SERVICE_THREAD
                   5016:                LeaveCriticalSection(&key_buf_crit_sect);
                   5017: #endif
                   5018:                if(!empty) return(1);
                   5019:        }
                   5020:        return(_kbhit());
1.1       root     5021: }
                   5022: 
                   5023: int msdos_getch_ex(int echo)
                   5024: {
                   5025:        static char prev = 0;
                   5026:        
                   5027:        msdos_stdio_reopen();
                   5028:        
1.1.1.20  root     5029:        process_t *process = msdos_process_info_get(current_psp);
                   5030:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5031:        
                   5032:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5033:                // stdin is redirected to file
                   5034: retry:
                   5035:                char data;
1.1.1.37  root     5036:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     5037:                        char tmp = data;
                   5038:                        if(data == 0x0a) {
                   5039:                                if(prev == 0x0d) {
                   5040:                                        goto retry; // CRLF -> skip LF
                   5041:                                } else {
                   5042:                                        data = 0x0d; // LF only -> CR
                   5043:                                }
                   5044:                        }
                   5045:                        prev = tmp;
                   5046:                        return(data);
                   5047:                }
                   5048:                return(EOF);
                   5049:        }
                   5050:        
                   5051:        // input from console
1.1.1.5   root     5052:        int key_char, key_scan;
1.1.1.33  root     5053:        if(key_recv != 0) {
1.1.1.5   root     5054:                key_char = (key_code >> 0) & 0xff;
                   5055:                key_scan = (key_code >> 8) & 0xff;
                   5056:                key_code >>= 16;
1.1.1.33  root     5057:                key_recv >>= 16;
1.1.1.5   root     5058:        } else {
1.1.1.54  root     5059:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_exit) {
1.1.1.35  root     5060:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5061: #ifdef USE_SERVICE_THREAD
                   5062:                                EnterCriticalSection(&key_buf_crit_sect);
                   5063: #endif
1.1.1.55  root     5064:                                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5065: #ifdef USE_SERVICE_THREAD
                   5066:                                LeaveCriticalSection(&key_buf_crit_sect);
                   5067: #endif
                   5068:                                if(!empty) break;
                   5069:                        }
1.1.1.23  root     5070:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   5071:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   5072:                                if(_kbhit()) {
1.1.1.32  root     5073:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5074: #ifdef USE_SERVICE_THREAD
                   5075:                                                EnterCriticalSection(&key_buf_crit_sect);
                   5076: #endif
1.1.1.51  root     5077:                                                pcbios_set_key_buffer(_getch(), 0x00);
1.1.1.35  root     5078: #ifdef USE_SERVICE_THREAD
                   5079:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   5080: #endif
1.1.1.32  root     5081:                                        }
1.1.1.23  root     5082:                                } else {
                   5083:                                        Sleep(10);
                   5084:                                }
                   5085:                        } else {
                   5086:                                if(!update_key_buffer()) {
                   5087:                                        Sleep(10);
                   5088:                                }
1.1.1.14  root     5089:                        }
                   5090:                }
1.1.1.54  root     5091:                if(m_exit) {
1.1.1.33  root     5092:                        // insert CR to terminate input loops
1.1.1.14  root     5093:                        key_char = 0x0d;
                   5094:                        key_scan = 0;
1.1.1.32  root     5095:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5096: #ifdef USE_SERVICE_THREAD
                   5097:                        EnterCriticalSection(&key_buf_crit_sect);
                   5098: #endif
1.1.1.51  root     5099:                        pcbios_get_key_buffer(&key_char, &key_scan);
1.1.1.35  root     5100: #ifdef USE_SERVICE_THREAD
                   5101:                        LeaveCriticalSection(&key_buf_crit_sect);
                   5102: #endif
1.1.1.5   root     5103:                }
1.1       root     5104:        }
                   5105:        if(echo && key_char) {
                   5106:                msdos_putch(key_char);
                   5107:        }
                   5108:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   5109: }
                   5110: 
                   5111: inline int msdos_getch()
                   5112: {
                   5113:        return(msdos_getch_ex(0));
                   5114: }
                   5115: 
                   5116: inline int msdos_getche()
                   5117: {
                   5118:        return(msdos_getch_ex(1));
                   5119: }
                   5120: 
                   5121: int msdos_write(int fd, const void *buffer, unsigned int count)
                   5122: {
1.1.1.37  root     5123:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5124:                // write to serial port
1.1.1.38  root     5125:                int written = 0;
1.1.1.37  root     5126:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5127:                        UINT8 *buf = (UINT8 *)buffer;
                   5128:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5129:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5130:                        DWORD timeout = timeGetTime() + 1000;
                   5131:                        while(written < count) {
                   5132:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
                   5133:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
                   5134:                                        timeout = timeGetTime() + 1000;
                   5135:                                } else {
                   5136:                                        if(timeGetTime() > timeout) {
                   5137:                                                break;
                   5138:                                        }
                   5139:                                        Sleep(10);
                   5140:                                }
1.1.1.37  root     5141:                        }
                   5142:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5143:                }
1.1.1.38  root     5144:                return(written);
1.1.1.37  root     5145:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   5146:                // write to printer port
                   5147:                UINT8 *buf = (UINT8 *)buffer;
                   5148:                for(unsigned int i = 0; i < count; i++) {
                   5149: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5150:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5151:                }
                   5152:                return(count);
                   5153:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     5154:                // CR+LF -> LF
1.1.1.37  root     5155:                static int is_cr = 0;
1.1       root     5156:                UINT8 *buf = (UINT8 *)buffer;
                   5157:                for(unsigned int i = 0; i < count; i++) {
                   5158:                        UINT8 data = buf[i];
                   5159:                        if(is_cr) {
                   5160:                                if(data != 0x0a) {
                   5161:                                        UINT8 tmp = 0x0d;
                   5162:                                        _write(1, &tmp, 1);
                   5163:                                }
                   5164:                                _write(1, &data, 1);
                   5165:                                is_cr = 0;
                   5166:                        } else if(data == 0x0d) {
                   5167:                                is_cr = 1;
                   5168:                        } else {
                   5169:                                _write(1, &data, 1);
                   5170:                        }
                   5171:                }
                   5172:                return(count);
                   5173:        }
1.1.1.14  root     5174:        vram_flush();
1.1       root     5175:        return(_write(fd, buffer, count));
                   5176: }
                   5177: 
                   5178: void msdos_putch(UINT8 data)
1.1.1.50  root     5179: {
                   5180:        msdos_stdio_reopen();
                   5181:        
                   5182:        process_t *process = msdos_process_info_get(current_psp);
                   5183:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5184:        
                   5185:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
                   5186:                // stdout is redirected to file
                   5187:                msdos_write(fd, &data, 1);
                   5188:                return;
                   5189:        }
                   5190:        
                   5191:        // call int 29h ?
                   5192:        if(*(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
                   5193:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   5194:                // int 29h is not hooked, no need to call int 29h
                   5195:                msdos_putch_fast(data);
                   5196: #ifdef USE_SERVICE_THREAD
                   5197:        } else if(in_service && main_thread_id != GetCurrentThreadId()) {
                   5198:                // XXX: in usually we should not reach here
                   5199:                // this is called from service thread to echo the input
                   5200:                // we can not call int 29h because it causes a critial issue to control cpu running in main thread :-(
                   5201:                msdos_putch_fast(data);
                   5202: #endif
1.1.1.51  root     5203:        } else if(in_service_29h) {
1.1.1.50  root     5204:                // disallow reentering call int 29h routine to prevent an infinite loop :-(
                   5205:                msdos_putch_fast(data);
                   5206:        } else {
                   5207:                // this is called from main thread, so we can call int 29h :-)
1.1.1.51  root     5208:                in_service_29h = true;
1.1.1.50  root     5209:                try {
                   5210:                        UINT32 tmp_pc = m_pc;
                   5211:                        UINT16 tmp_ax = REG16(AX);
                   5212:                        UINT32 tmp_bx = REG16(BX); // BX may be destroyed by some versions of DOS 3.3
                   5213:                        
                   5214:                        // call int 29h routine is at fffc:0027
                   5215:                        i386_call_far(DUMMY_TOP >> 4, 0x0027);
                   5216:                        REG8(AL) = data;
                   5217:                        
                   5218:                        // run cpu until call int 29h routine is done
1.1.1.54  root     5219:                        while(!m_exit && tmp_pc != m_pc) {
1.1.1.50  root     5220:                                try {
                   5221:                                        hardware_run_cpu();
                   5222:                                } catch(...) {
                   5223:                                }
                   5224:                        }
                   5225:                        REG16(AX) = tmp_ax;
                   5226:                        REG16(BX) = tmp_bx;
                   5227:                } catch(...) {
                   5228:                }
1.1.1.51  root     5229:                in_service_29h = false;
1.1.1.50  root     5230:        }
                   5231: }
                   5232: 
                   5233: void msdos_putch_fast(UINT8 data)
1.1.1.35  root     5234: #ifdef USE_SERVICE_THREAD
                   5235: {
                   5236:        EnterCriticalSection(&putch_crit_sect);
                   5237:        msdos_putch_tmp(data);
                   5238:        LeaveCriticalSection(&putch_crit_sect);
                   5239: }
                   5240: void msdos_putch_tmp(UINT8 data)
                   5241: #endif
1.1       root     5242: {
1.1.1.34  root     5243:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5244:        SMALL_RECT rect;
                   5245:        COORD co;
1.1       root     5246:        static int p = 0;
                   5247:        static int is_kanji = 0;
                   5248:        static int is_esc = 0;
                   5249:        static int stored_x;
                   5250:        static int stored_y;
                   5251:        static WORD stored_a;
1.1.1.20  root     5252:        static char tmp[64], out[64];
1.1       root     5253:        
1.1.1.23  root     5254:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     5255:        
                   5256:        // output to console
                   5257:        tmp[p++] = data;
                   5258:        
1.1.1.14  root     5259:        vram_flush();
                   5260:        
1.1       root     5261:        if(is_kanji) {
                   5262:                // kanji character
                   5263:                is_kanji = 0;
                   5264:        } else if(is_esc) {
                   5265:                // escape sequense
                   5266:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   5267:                        p = is_esc = 0;
                   5268:                } else if(tmp[1] == '=' && p == 4) {
                   5269:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     5270:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     5271:                        SetConsoleCursorPosition(hStdout, co);
                   5272:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5273:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     5274:                        cursor_moved = false;
                   5275:                        p = is_esc = 0;
                   5276:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
                   5277:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5278:                        co.X = csbi.dwCursorPosition.X;
                   5279:                        co.Y = csbi.dwCursorPosition.Y;
                   5280:                        WORD wAttributes = csbi.wAttributes;
                   5281:                        
                   5282:                        if(tmp[1] == 'D') {
                   5283:                                co.Y++;
                   5284:                        } else if(tmp[1] == 'E') {
                   5285:                                co.X = 0;
                   5286:                                co.Y++;
                   5287:                        } else if(tmp[1] == 'M') {
                   5288:                                co.Y--;
                   5289:                        } else if(tmp[1] == '*') {
1.1.1.14  root     5290:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5291:                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5292:                                co.X = 0;
                   5293:                                co.Y = csbi.srWindow.Top;
1.1       root     5294:                        } else if(tmp[1] == '[') {
                   5295:                                int param[256], params = 0;
                   5296:                                memset(param, 0, sizeof(param));
                   5297:                                for(int i = 2; i < p; i++) {
                   5298:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   5299:                                                param[params] *= 10;
                   5300:                                                param[params] += tmp[i] - '0';
                   5301:                                        } else {
                   5302:                                                params++;
                   5303:                                        }
                   5304:                                }
                   5305:                                if(data == 'A') {
1.1.1.14  root     5306:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     5307:                                } else if(data == 'B') {
1.1.1.14  root     5308:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     5309:                                } else if(data == 'C') {
1.1.1.14  root     5310:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     5311:                                } else if(data == 'D') {
1.1.1.14  root     5312:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     5313:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     5314:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   5315:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     5316:                                } else if(data == 'J') {
1.1.1.14  root     5317:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5318:                                        if(param[0] == 0) {
                   5319:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5320:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5321:                                                if(co.Y < csbi.srWindow.Bottom) {
                   5322:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5323:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5324:                                                }
                   5325:                                        } else if(param[0] == 1) {
1.1.1.14  root     5326:                                                if(co.Y > csbi.srWindow.Top) {
                   5327:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
                   5328:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5329:                                                }
                   5330:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5331:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5332:                                        } else if(param[0] == 2) {
1.1.1.14  root     5333:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5334:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5335:                                                co.X = co.Y = 0;
                   5336:                                        }
                   5337:                                } else if(data == 'K') {
1.1.1.14  root     5338:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5339:                                        if(param[0] == 0) {
                   5340:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5341:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5342:                                        } else if(param[0] == 1) {
                   5343:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5344:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5345:                                        } else if(param[0] == 2) {
                   5346:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5347:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5348:                                        }
                   5349:                                } else if(data == 'L') {
1.1.1.14  root     5350:                                        if(params == 0) {
                   5351:                                                param[0] = 1;
1.1       root     5352:                                        }
1.1.1.14  root     5353:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5354:                                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5355:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5356:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5357:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5358:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14  root     5359:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5360:                                        co.X = 0;
                   5361:                                } else if(data == 'M') {
1.1.1.14  root     5362:                                        if(params == 0) {
                   5363:                                                param[0] = 1;
                   5364:                                        }
                   5365:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5366:                                                clear_scr_buffer(csbi.wAttributes);
                   5367:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5368:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5369:                                        } else {
1.1.1.14  root     5370:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5371:                                                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5372:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5373:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5374:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5375:                                        }
                   5376:                                        co.X = 0;
                   5377:                                } else if(data == 'h') {
                   5378:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5379:                                                CONSOLE_CURSOR_INFO cur;
                   5380:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5381:                                                if(cur.bVisible) {
                   5382:                                                        cur.bVisible = FALSE;
1.1.1.14  root     5383: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5384:                                                }
                   5385:                                        }
                   5386:                                } else if(data == 'l') {
                   5387:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5388:                                                CONSOLE_CURSOR_INFO cur;
                   5389:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5390:                                                if(!cur.bVisible) {
                   5391:                                                        cur.bVisible = TRUE;
1.1.1.14  root     5392: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5393:                                                }
                   5394:                                        }
                   5395:                                } else if(data == 'm') {
                   5396:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5397:                                        int reverse = 0, hidden = 0;
                   5398:                                        for(int i = 0; i < params; i++) {
                   5399:                                                if(param[i] == 1) {
                   5400:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5401:                                                } else if(param[i] == 4) {
                   5402:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5403:                                                } else if(param[i] == 7) {
                   5404:                                                        reverse = 1;
                   5405:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5406:                                                        hidden = 1;
                   5407:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5408:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5409:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5410:                                                                param[i] -= 16;
                   5411:                                                        } else {
                   5412:                                                                param[i] -= 30;
                   5413:                                                        }
                   5414:                                                        if(param[i] & 1) {
                   5415:                                                                wAttributes |= FOREGROUND_RED;
                   5416:                                                        }
                   5417:                                                        if(param[i] & 2) {
                   5418:                                                                wAttributes |= FOREGROUND_GREEN;
                   5419:                                                        }
                   5420:                                                        if(param[i] & 4) {
                   5421:                                                                wAttributes |= FOREGROUND_BLUE;
                   5422:                                                        }
                   5423:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5424:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5425:                                                        if((param[i] - 40) & 1) {
                   5426:                                                                wAttributes |= BACKGROUND_RED;
                   5427:                                                        }
                   5428:                                                        if((param[i] - 40) & 2) {
                   5429:                                                                wAttributes |= BACKGROUND_GREEN;
                   5430:                                                        }
                   5431:                                                        if((param[i] - 40) & 4) {
                   5432:                                                                wAttributes |= BACKGROUND_BLUE;
                   5433:                                                        }
                   5434:                                                }
                   5435:                                        }
                   5436:                                        if(reverse) {
                   5437:                                                wAttributes &= ~0xff;
                   5438:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5439:                                        }
                   5440:                                        if(hidden) {
                   5441:                                                wAttributes &= ~0x0f;
                   5442:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5443:                                        }
                   5444:                                } else if(data == 'n') {
                   5445:                                        if(param[0] == 6) {
                   5446:                                                char tmp[16];
                   5447:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5448:                                                int len = strlen(tmp);
1.1.1.32  root     5449:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5450: #ifdef USE_SERVICE_THREAD
                   5451:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5452: #endif
1.1.1.32  root     5453:                                                        for(int i = 0; i < len; i++) {
1.1.1.51  root     5454:                                                                pcbios_set_key_buffer(tmp[i], 0x00);
1.1.1.32  root     5455:                                                        }
1.1.1.35  root     5456: #ifdef USE_SERVICE_THREAD
                   5457:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5458: #endif
1.1       root     5459:                                                }
                   5460:                                        }
                   5461:                                } else if(data == 's') {
                   5462:                                        stored_x = co.X;
                   5463:                                        stored_y = co.Y;
                   5464:                                        stored_a = wAttributes;
                   5465:                                } else if(data == 'u') {
                   5466:                                        co.X = stored_x;
                   5467:                                        co.Y = stored_y;
                   5468:                                        wAttributes = stored_a;
                   5469:                                }
                   5470:                        }
                   5471:                        if(co.X < 0) {
                   5472:                                co.X = 0;
                   5473:                        } else if(co.X >= csbi.dwSize.X) {
                   5474:                                co.X = csbi.dwSize.X - 1;
                   5475:                        }
1.1.1.14  root     5476:                        if(co.Y < csbi.srWindow.Top) {
                   5477:                                co.Y = csbi.srWindow.Top;
                   5478:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5479:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5480:                        }
                   5481:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5482:                                SetConsoleCursorPosition(hStdout, co);
                   5483:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5484:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5485:                                cursor_moved = false;
                   5486:                        }
                   5487:                        if(wAttributes != csbi.wAttributes) {
                   5488:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5489:                        }
                   5490:                        p = is_esc = 0;
                   5491:                }
                   5492:                return;
                   5493:        } else {
                   5494:                if(msdos_lead_byte_check(data)) {
                   5495:                        is_kanji = 1;
                   5496:                        return;
                   5497:                } else if(data == 0x1b) {
                   5498:                        is_esc = 1;
                   5499:                        return;
                   5500:                }
                   5501:        }
1.1.1.20  root     5502:        
                   5503:        DWORD q = 0, num;
                   5504:        is_kanji = 0;
                   5505:        for(int i = 0; i < p; i++) {
                   5506:                UINT8 c = tmp[i];
                   5507:                if(is_kanji) {
                   5508:                        is_kanji = 0;
                   5509:                } else if(msdos_lead_byte_check(data)) {
                   5510:                        is_kanji = 1;
                   5511:                } else if(msdos_ctrl_code_check(data)) {
                   5512:                        out[q++] = '^';
                   5513:                        c += 'A' - 1;
                   5514:                }
                   5515:                out[q++] = c;
                   5516:        }
1.1.1.34  root     5517:        if(q == 1 && out[0] == 0x08) {
                   5518:                // back space
                   5519:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5520:                if(csbi.dwCursorPosition.X > 0) {
                   5521:                        co.X = csbi.dwCursorPosition.X - 1;
                   5522:                        co.Y = csbi.dwCursorPosition.Y;
                   5523:                        SetConsoleCursorPosition(hStdout, co);
                   5524:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5525:                        co.X = csbi.dwSize.X - 1;
                   5526:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5527:                        SetConsoleCursorPosition(hStdout, co);
                   5528:                } else {
                   5529:                        WriteConsole(hStdout, out, q, &num, NULL); // to make sure
                   5530:                }
                   5531:        } else {
                   5532:                WriteConsole(hStdout, out, q, &num, NULL);
                   5533:        }
1.1       root     5534:        p = 0;
1.1.1.14  root     5535:        
1.1.1.15  root     5536:        if(!restore_console_on_exit) {
                   5537:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5538:                scr_top = csbi.srWindow.Top;
                   5539:        }
1.1       root     5540:        cursor_moved = true;
                   5541: }
                   5542: 
                   5543: int msdos_aux_in()
                   5544: {
1.1.1.21  root     5545:        msdos_stdio_reopen();
                   5546:        
1.1.1.20  root     5547:        process_t *process = msdos_process_info_get(current_psp);
                   5548:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5549:        
                   5550:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5551:                char data = 0;
1.1.1.37  root     5552:                msdos_read(fd, &data, 1);
1.1       root     5553:                return(data);
                   5554:        } else {
                   5555:                return(EOF);
                   5556:        }
                   5557: }
                   5558: 
                   5559: void msdos_aux_out(char data)
                   5560: {
1.1.1.21  root     5561:        msdos_stdio_reopen();
                   5562:        
1.1.1.20  root     5563:        process_t *process = msdos_process_info_get(current_psp);
                   5564:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5565:        
                   5566:        if(fd < process->max_files && file_handler[fd].valid) {
                   5567:                msdos_write(fd, &data, 1);
1.1       root     5568:        }
                   5569: }
                   5570: 
                   5571: void msdos_prn_out(char data)
                   5572: {
1.1.1.21  root     5573:        msdos_stdio_reopen();
                   5574:        
1.1.1.20  root     5575:        process_t *process = msdos_process_info_get(current_psp);
                   5576:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5577:        
                   5578:        if(fd < process->max_files && file_handler[fd].valid) {
                   5579:                msdos_write(fd, &data, 1);
1.1       root     5580:        }
                   5581: }
                   5582: 
                   5583: // memory control
                   5584: 
1.1.1.52  root     5585: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name = NULL)
1.1       root     5586: {
                   5587:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5588:        
                   5589:        mcb->mz = mz;
                   5590:        mcb->psp = psp;
1.1.1.30  root     5591:        mcb->paragraphs = paragraphs;
1.1.1.39  root     5592:        
                   5593:        if(prog_name != NULL) {
                   5594:                memset(mcb->prog_name, 0, 8);
                   5595:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
                   5596:        }
1.1       root     5597:        return(mcb);
                   5598: }
                   5599: 
                   5600: void msdos_mcb_check(mcb_t *mcb)
                   5601: {
                   5602:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5603:                #if 0
                   5604:                        // shutdown now !!!
                   5605:                        fatalerror("broken memory control block\n");
                   5606:                #else
                   5607:                        // return error code and continue
                   5608:                        throw(0x07); // broken memory control block
                   5609:                #endif
1.1       root     5610:        }
                   5611: }
                   5612: 
1.1.1.39  root     5613: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5614: {
                   5615:        int mcb_seg = seg - 1;
                   5616:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5617:        msdos_mcb_check(mcb);
                   5618:        
1.1.1.30  root     5619:        if(mcb->paragraphs > paragraphs) {
1.1       root     5620:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5621:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5622:                
                   5623:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5624:                mcb->mz = 'M';
1.1.1.30  root     5625:                mcb->paragraphs = paragraphs;
1.1       root     5626:        }
                   5627: }
                   5628: 
                   5629: void msdos_mem_merge(int seg)
                   5630: {
                   5631:        int mcb_seg = seg - 1;
                   5632:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5633:        msdos_mcb_check(mcb);
                   5634:        
                   5635:        while(1) {
                   5636:                if(mcb->mz == 'Z') {
                   5637:                        break;
                   5638:                }
1.1.1.30  root     5639:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5640:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5641:                msdos_mcb_check(next_mcb);
                   5642:                
                   5643:                if(next_mcb->psp != 0) {
                   5644:                        break;
                   5645:                }
                   5646:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5647:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5648:        }
                   5649: }
                   5650: 
1.1.1.8   root     5651: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5652: {
                   5653:        while(1) {
                   5654:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5655:                bool last_block;
1.1       root     5656:                
1.1.1.14  root     5657:                if(mcb->psp == 0) {
                   5658:                        msdos_mem_merge(mcb_seg + 1);
                   5659:                } else {
                   5660:                        msdos_mcb_check(mcb);
                   5661:                }
1.1.1.33  root     5662:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5663:                        // check if the next is dummy mcb to link to umb
                   5664:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5665:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5666:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5667:                }
                   5668:                if(!(new_process && !last_block)) {
1.1.1.30  root     5669:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5670:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5671:                                mcb->psp = current_psp;
                   5672:                                return(mcb_seg + 1);
                   5673:                        }
                   5674:                }
                   5675:                if(mcb->mz == 'Z') {
                   5676:                        break;
                   5677:                }
1.1.1.30  root     5678:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5679:        }
                   5680:        return(-1);
                   5681: }
                   5682: 
                   5683: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5684: {
                   5685:        int mcb_seg = seg - 1;
                   5686:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5687:        msdos_mcb_check(mcb);
1.1.1.30  root     5688:        int current_paragraphs = mcb->paragraphs;
1.1       root     5689:        
                   5690:        msdos_mem_merge(seg);
1.1.1.30  root     5691:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5692:                if(max_paragraphs) {
1.1.1.30  root     5693:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5694:                }
1.1       root     5695:                msdos_mem_split(seg, current_paragraphs);
                   5696:                return(-1);
                   5697:        }
                   5698:        msdos_mem_split(seg, paragraphs);
                   5699:        return(0);
                   5700: }
                   5701: 
                   5702: void msdos_mem_free(int seg)
                   5703: {
                   5704:        int mcb_seg = seg - 1;
                   5705:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5706:        msdos_mcb_check(mcb);
                   5707:        
                   5708:        mcb->psp = 0;
                   5709:        msdos_mem_merge(seg);
                   5710: }
                   5711: 
1.1.1.8   root     5712: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5713: {
                   5714:        int max_paragraphs = 0;
                   5715:        
                   5716:        while(1) {
                   5717:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5718:                bool last_block;
                   5719:                
1.1       root     5720:                msdos_mcb_check(mcb);
                   5721:                
1.1.1.33  root     5722:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5723:                        // check if the next is dummy mcb to link to umb
                   5724:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5725:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5726:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5727:                }
                   5728:                if(!(new_process && !last_block)) {
1.1.1.30  root     5729:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   5730:                                max_paragraphs = mcb->paragraphs;
1.1       root     5731:                        }
                   5732:                }
                   5733:                if(mcb->mz == 'Z') {
                   5734:                        break;
                   5735:                }
1.1.1.30  root     5736:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5737:        }
1.1.1.14  root     5738:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     5739: }
                   5740: 
1.1.1.8   root     5741: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   5742: {
                   5743:        int last_seg = -1;
                   5744:        
                   5745:        while(1) {
                   5746:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5747:                msdos_mcb_check(mcb);
                   5748:                
1.1.1.14  root     5749:                if(mcb->psp == psp) {
1.1.1.8   root     5750:                        last_seg = mcb_seg;
                   5751:                }
1.1.1.14  root     5752:                if(mcb->mz == 'Z') {
                   5753:                        break;
                   5754:                }
1.1.1.30  root     5755:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     5756:        }
                   5757:        return(last_seg);
                   5758: }
                   5759: 
1.1.1.19  root     5760: int msdos_mem_get_umb_linked()
                   5761: {
1.1.1.33  root     5762:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5763:        msdos_mcb_check(mcb);
1.1.1.19  root     5764:        
1.1.1.33  root     5765:        if(mcb->mz == 'M') {
                   5766:                return(-1);
1.1.1.19  root     5767:        }
                   5768:        return(0);
                   5769: }
                   5770: 
1.1.1.33  root     5771: void msdos_mem_link_umb()
1.1.1.19  root     5772: {
1.1.1.33  root     5773:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5774:        msdos_mcb_check(mcb);
1.1.1.19  root     5775:        
1.1.1.33  root     5776:        mcb->mz = 'M';
                   5777:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39  root     5778:        
                   5779:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     5780: }
                   5781: 
1.1.1.33  root     5782: void msdos_mem_unlink_umb()
1.1.1.19  root     5783: {
1.1.1.33  root     5784:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5785:        msdos_mcb_check(mcb);
1.1.1.19  root     5786:        
1.1.1.33  root     5787:        mcb->mz = 'Z';
                   5788:        mcb->paragraphs = 0;
1.1.1.39  root     5789:        
                   5790:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     5791: }
                   5792: 
1.1.1.29  root     5793: #ifdef SUPPORT_HMA
                   5794: 
                   5795: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   5796: {
                   5797:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5798:        
                   5799:        mcb->ms[0] = 'M';
                   5800:        mcb->ms[1] = 'S';
                   5801:        mcb->owner = owner;
                   5802:        mcb->size = size;
                   5803:        mcb->next = next;
                   5804:        return(mcb);
                   5805: }
                   5806: 
                   5807: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   5808: {
                   5809:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   5810: }
                   5811: 
                   5812: int msdos_hma_mem_split(int offset, int size)
                   5813: {
                   5814:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5815:        
                   5816:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5817:                return(-1);
                   5818:        }
                   5819:        if(mcb->size >= size + 0x10) {
                   5820:                int new_offset = offset + 0x10 + size;
                   5821:                int new_size = mcb->size - 0x10 - size;
                   5822:                
                   5823:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   5824:                mcb->size = size;
                   5825:                mcb->next = new_offset;
                   5826:                return(0);
                   5827:        }
                   5828:        return(-1);
                   5829: }
                   5830: 
                   5831: void msdos_hma_mem_merge(int offset)
                   5832: {
                   5833:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5834:        
                   5835:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5836:                return;
                   5837:        }
                   5838:        while(1) {
                   5839:                if(mcb->next == 0) {
                   5840:                        break;
                   5841:                }
                   5842:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   5843:                
                   5844:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   5845:                        return;
                   5846:                }
                   5847:                if(next_mcb->owner != 0) {
                   5848:                        break;
                   5849:                }
                   5850:                mcb->size += 0x10 + next_mcb->size;
                   5851:                mcb->next = next_mcb->next;
                   5852:        }
                   5853: }
                   5854: 
                   5855: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   5856: {
                   5857:        int offset = 0x10; // first mcb in HMA
                   5858:        
                   5859:        while(1) {
                   5860:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5861:                
                   5862:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5863:                        return(-1);
                   5864:                }
                   5865:                if(mcb->owner == 0) {
                   5866:                        msdos_hma_mem_merge(offset);
                   5867:                }
                   5868:                if(mcb->owner == 0 && mcb->size >= size) {
                   5869:                        msdos_hma_mem_split(offset, size);
                   5870:                        mcb->owner = owner;
                   5871:                        return(offset);
                   5872:                }
                   5873:                if(mcb->next == 0) {
                   5874:                        break;
                   5875:                }
                   5876:                offset = mcb->next;
                   5877:        }
                   5878:        return(-1);
                   5879: }
                   5880: 
                   5881: int msdos_hma_mem_realloc(int offset, int size)
                   5882: {
                   5883:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5884:        
                   5885:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5886:                return(-1);
                   5887:        }
                   5888:        if(mcb->size < size) {
                   5889:                return(-1);
                   5890:        }
                   5891:        msdos_hma_mem_split(offset, size);
                   5892:        return(0);
                   5893: }
                   5894: 
                   5895: void msdos_hma_mem_free(int offset)
                   5896: {
                   5897:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5898:        
                   5899:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5900:                return;
                   5901:        }
                   5902:        mcb->owner = 0;
                   5903:        msdos_hma_mem_merge(offset);
                   5904: }
                   5905: 
                   5906: int msdos_hma_mem_get_free(int *available_offset)
                   5907: {
                   5908:        int offset = 0x10; // first mcb in HMA
                   5909:        int size = 0;
                   5910:        
                   5911:        while(1) {
                   5912:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5913:                
                   5914:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5915:                        return(0);
                   5916:                }
                   5917:                if(mcb->owner == 0 && size < mcb->size) {
                   5918:                        if(available_offset != NULL) {
                   5919:                                *available_offset = offset;
                   5920:                        }
                   5921:                        size = mcb->size;
                   5922:                }
                   5923:                if(mcb->next == 0) {
                   5924:                        break;
                   5925:                }
                   5926:                offset = mcb->next;
                   5927:        }
                   5928:        return(size);
                   5929: }
                   5930: 
                   5931: #endif
                   5932: 
1.1       root     5933: // environment
                   5934: 
1.1.1.45  root     5935: void msdos_env_set_argv(int env_seg, const char *argv)
1.1       root     5936: {
                   5937:        char *dst = (char *)(mem + (env_seg << 4));
                   5938:        
                   5939:        while(1) {
                   5940:                if(dst[0] == 0) {
                   5941:                        break;
                   5942:                }
                   5943:                dst += strlen(dst) + 1;
                   5944:        }
                   5945:        *dst++ = 0; // end of environment
                   5946:        *dst++ = 1; // top of argv[0]
                   5947:        *dst++ = 0;
                   5948:        memcpy(dst, argv, strlen(argv));
                   5949:        dst += strlen(argv);
                   5950:        *dst++ = 0;
                   5951:        *dst++ = 0;
                   5952: }
                   5953: 
1.1.1.45  root     5954: const char *msdos_env_get_argv(int env_seg)
1.1       root     5955: {
                   5956:        static char env[ENV_SIZE];
                   5957:        char *src = env;
                   5958:        
                   5959:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5960:        while(1) {
                   5961:                if(src[0] == 0) {
                   5962:                        if(src[1] == 1) {
                   5963:                                return(src + 3);
                   5964:                        }
                   5965:                        break;
                   5966:                }
                   5967:                src += strlen(src) + 1;
                   5968:        }
                   5969:        return(NULL);
                   5970: }
                   5971: 
1.1.1.45  root     5972: const char *msdos_env_get(int env_seg, const char *name)
1.1       root     5973: {
                   5974:        static char env[ENV_SIZE];
                   5975:        char *src = env;
                   5976:        
                   5977:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5978:        while(1) {
                   5979:                if(src[0] == 0) {
                   5980:                        break;
                   5981:                }
                   5982:                int len = strlen(src);
                   5983:                char *n = my_strtok(src, "=");
                   5984:                char *v = src + strlen(n) + 1;
                   5985:                
                   5986:                if(_stricmp(name, n) == 0) {
                   5987:                        return(v);
                   5988:                }
                   5989:                src += len + 1;
                   5990:        }
                   5991:        return(NULL);
                   5992: }
                   5993: 
1.1.1.45  root     5994: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1       root     5995: {
                   5996:        char env[ENV_SIZE];
                   5997:        char *src = env;
                   5998:        char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45  root     5999:        const char *argv = msdos_env_get_argv(env_seg);
1.1       root     6000:        int done = 0;
                   6001:        
                   6002:        memcpy(src, dst, ENV_SIZE);
                   6003:        memset(dst, 0, ENV_SIZE);
                   6004:        while(1) {
                   6005:                if(src[0] == 0) {
                   6006:                        break;
                   6007:                }
                   6008:                int len = strlen(src);
                   6009:                char *n = my_strtok(src, "=");
                   6010:                char *v = src + strlen(n) + 1;
                   6011:                char tmp[1024];
                   6012:                
                   6013:                if(_stricmp(name, n) == 0) {
                   6014:                        sprintf(tmp, "%s=%s", n, value);
                   6015:                        done = 1;
                   6016:                } else {
                   6017:                        sprintf(tmp, "%s=%s", n, v);
                   6018:                }
                   6019:                memcpy(dst, tmp, strlen(tmp));
                   6020:                dst += strlen(tmp) + 1;
                   6021:                src += len + 1;
                   6022:        }
                   6023:        if(!done) {
                   6024:                char tmp[1024];
                   6025:                
                   6026:                sprintf(tmp, "%s=%s", name, value);
                   6027:                memcpy(dst, tmp, strlen(tmp));
                   6028:                dst += strlen(tmp) + 1;
                   6029:        }
                   6030:        if(argv) {
                   6031:                *dst++ = 0; // end of environment
                   6032:                *dst++ = 1; // top of argv[0]
                   6033:                *dst++ = 0;
                   6034:                memcpy(dst, argv, strlen(argv));
                   6035:                dst += strlen(argv);
                   6036:                *dst++ = 0;
                   6037:                *dst++ = 0;
                   6038:        }
                   6039: }
                   6040: 
                   6041: // process
                   6042: 
1.1.1.8   root     6043: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     6044: {
                   6045:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6046:        
                   6047:        memset(psp, 0, PSP_SIZE);
                   6048:        psp->exit[0] = 0xcd;
                   6049:        psp->exit[1] = 0x20;
1.1.1.8   root     6050:        psp->first_mcb = mcb_seg;
1.1.1.46  root     6051: #if 1
1.1.1.49  root     6052:        psp->call5[0] = 0xcd;   // int 30h
                   6053:        psp->call5[1] = 0x30;
1.1.1.46  root     6054:        psp->call5[2] = 0xc3;   // ret
                   6055: #else
                   6056:        psp->call5[0] = 0x8a;   // mov ah, cl
                   6057:        psp->call5[1] = 0xe1;
                   6058:        psp->call5[2] = 0xcd;   // int 21h
                   6059:        psp->call5[3] = 0x21;
                   6060:        psp->call5[4] = 0xc3;   // ret
                   6061: #endif
1.1       root     6062:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   6063:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   6064:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   6065:        psp->parent_psp = parent_psp;
1.1.1.20  root     6066:        if(parent_psp == (UINT16)-1) {
                   6067:                for(int i = 0; i < 20; i++) {
                   6068:                        if(file_handler[i].valid) {
                   6069:                                psp->file_table[i] = i;
                   6070:                        } else {
                   6071:                                psp->file_table[i] = 0xff;
                   6072:                        }
1.1       root     6073:                }
1.1.1.20  root     6074:        } else {
                   6075:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     6076:        }
                   6077:        psp->env_seg = env_seg;
                   6078:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     6079:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     6080:        psp->file_table_size = 20;
                   6081:        psp->file_table_ptr.w.l = 0x18;
                   6082:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     6083:        psp->service[0] = 0xcd;
                   6084:        psp->service[1] = 0x21;
                   6085:        psp->service[2] = 0xcb;
                   6086:        return(psp);
                   6087: }
                   6088: 
1.1.1.20  root     6089: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   6090: {
                   6091:        if(psp_seg && fd < 20) {
                   6092:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6093:                psp->file_table[fd] = value;
                   6094:        }
                   6095: }
                   6096: 
                   6097: int msdos_psp_get_file_table(int fd, int psp_seg)
                   6098: {
                   6099:        if(psp_seg && fd < 20) {
                   6100:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6101:                fd = psp->file_table[fd];
                   6102:        }
                   6103:        return fd;
                   6104: }
                   6105: 
1.1.1.52  root     6106: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al, bool first_process = false)
1.1       root     6107: {
                   6108:        // load command file
                   6109:        int fd = -1;
1.1.1.45  root     6110:        int sio_port = 0;
                   6111:        int lpt_port = 0;
1.1       root     6112:        int dos_command = 0;
1.1.1.24  root     6113:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     6114:        char pipe_stdin_path[MAX_PATH] = {0};
                   6115:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39  root     6116:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     6117:        
                   6118:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6119:        int opt_len = mem[opt_ofs];
                   6120:        memset(opt, 0, sizeof(opt));
                   6121:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6122:        
1.1.1.14  root     6123:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   6124:                // this is a batch file, run command.com
                   6125:                char tmp[MAX_PATH];
                   6126:                if(opt_len != 0) {
                   6127:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   6128:                } else {
                   6129:                        sprintf(tmp, "/C %s", cmd);
                   6130:                }
                   6131:                strcpy(opt, tmp);
                   6132:                opt_len = strlen(opt);
                   6133:                mem[opt_ofs] = opt_len;
                   6134:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6135:                strcpy(command, comspec_path);
                   6136:                strcpy(name_tmp, "COMMAND.COM");
                   6137:        } else {
                   6138:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   6139:                        // redirect C:\COMMAND.COM to comspec_path
                   6140:                        strcpy(command, comspec_path);
                   6141:                } else {
                   6142:                        strcpy(command, cmd);
                   6143:                }
1.1.1.24  root     6144:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   6145:                        return(-1);
                   6146:                }
1.1.1.14  root     6147:                memset(name_tmp, 0, sizeof(name_tmp));
                   6148:                strcpy(name_tmp, name);
                   6149:                
                   6150:                // check command.com
1.1.1.38  root     6151:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   6152:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     6153:                        if(opt_len == 0) {
                   6154: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   6155:                                process_t *current_process = NULL;
                   6156:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   6157:                                        if(process[i].psp == current_psp) {
                   6158:                                                current_process = &process[i];
                   6159:                                                break;
                   6160:                                        }
                   6161:                                }
                   6162:                                if(current_process != NULL) {
                   6163:                                        param->cmd_line.dw = current_process->dta.dw;
                   6164:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6165:                                        opt_len = mem[opt_ofs];
                   6166:                                        memset(opt, 0, sizeof(opt));
                   6167:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6168:                                }
                   6169:                        }
                   6170:                        for(int i = 0; i < opt_len; i++) {
                   6171:                                if(opt[i] == ' ') {
                   6172:                                        continue;
                   6173:                                }
                   6174:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   6175:                                        for(int j = i + 3; j < opt_len; j++) {
                   6176:                                                if(opt[j] == ' ') {
                   6177:                                                        continue;
                   6178:                                                }
                   6179:                                                char *token = my_strtok(opt + j, " ");
                   6180:                                                
1.1.1.38  root     6181:                                                strcpy(command, token);
                   6182:                                                char tmp[MAX_PATH];
                   6183:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39  root     6184:                                                strcpy(opt, "");
                   6185:                                                for(int i = 0; i < strlen(tmp); i++) {
                   6186:                                                        if(tmp[i] != ' ') {
                   6187:                                                                strcpy(opt, tmp + i);
                   6188:                                                                break;
                   6189:                                                        }
                   6190:                                                }
                   6191:                                                strcpy(tmp, opt);
1.1.1.38  root     6192:                                                
                   6193:                                                if(al == 0x00) {
1.1.1.39  root     6194:                                                        #define GET_FILE_PATH() { \
                   6195:                                                                if(token[0] != '>' && token[0] != '<') { \
                   6196:                                                                        token++; \
                   6197:                                                                } \
                   6198:                                                                token++; \
                   6199:                                                                while(*token == ' ') { \
                   6200:                                                                        token++; \
                   6201:                                                                } \
                   6202:                                                                char *ptr = token; \
                   6203:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
                   6204:                                                                        ptr++; \
                   6205:                                                                } \
                   6206:                                                                *ptr = '\0'; \
                   6207:                                                        }
                   6208:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
                   6209:                                                                GET_FILE_PATH();
1.1.1.38  root     6210:                                                                strcpy(pipe_stdin_path, token);
                   6211:                                                                strcpy(opt, tmp);
                   6212:                                                        }
1.1.1.39  root     6213:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
                   6214:                                                                GET_FILE_PATH();
1.1.1.38  root     6215:                                                                strcpy(pipe_stdout_path, token);
                   6216:                                                                strcpy(opt, tmp);
                   6217:                                                        }
1.1.1.39  root     6218:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6219:                                                                GET_FILE_PATH();
                   6220:                                                                strcpy(pipe_stderr_path, token);
                   6221:                                                                strcpy(opt, tmp);
                   6222:                                                        }
                   6223:                                                        #undef GET_FILE_PATH
                   6224:                                                        
                   6225:                                                        if((token = strstr(opt, "0<")) != NULL) {
                   6226:                                                                *token = '\0';
                   6227:                                                        }
                   6228:                                                        if((token = strstr(opt, "1>")) != NULL) {
                   6229:                                                                *token = '\0';
                   6230:                                                        }
                   6231:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6232:                                                                *token = '\0';
                   6233:                                                        }
1.1.1.38  root     6234:                                                        if((token = strstr(opt, "<")) != NULL) {
                   6235:                                                                *token = '\0';
                   6236:                                                        }
                   6237:                                                        if((token = strstr(opt, ">")) != NULL) {
                   6238:                                                                *token = '\0';
                   6239:                                                        }
1.1.1.14  root     6240:                                                }
1.1.1.39  root     6241:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
                   6242:                                                        opt[i] = '\0';
                   6243:                                                }
1.1.1.38  root     6244:                                                opt_len = strlen(opt);
                   6245:                                                mem[opt_ofs] = opt_len;
                   6246:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6247:                                                dos_command = 1;
1.1.1.14  root     6248:                                                break;
1.1       root     6249:                                        }
                   6250:                                }
1.1.1.14  root     6251:                                break;
1.1       root     6252:                        }
                   6253:                }
                   6254:        }
                   6255:        
                   6256:        // load command file
                   6257:        strcpy(path, command);
                   6258:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6259:                sprintf(path, "%s.COM", command);
                   6260:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6261:                        sprintf(path, "%s.EXE", command);
                   6262:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     6263:                                sprintf(path, "%s.BAT", command);
                   6264:                                if(_access(path, 0) == 0) {
                   6265:                                        // this is a batch file, run command.com
                   6266:                                        char tmp[MAX_PATH];
                   6267:                                        if(opt_len != 0) {
                   6268:                                                sprintf(tmp, "/C %s %s", path, opt);
                   6269:                                        } else {
                   6270:                                                sprintf(tmp, "/C %s", path);
                   6271:                                        }
                   6272:                                        strcpy(opt, tmp);
                   6273:                                        opt_len = strlen(opt);
                   6274:                                        mem[opt_ofs] = opt_len;
                   6275:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6276:                                        strcpy(path, comspec_path);
                   6277:                                        strcpy(name_tmp, "COMMAND.COM");
                   6278:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6279:                                } else {
                   6280:                                        // search path in parent environments
                   6281:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45  root     6282:                                        const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14  root     6283:                                        if(env != NULL) {
                   6284:                                                char env_path[4096];
                   6285:                                                strcpy(env_path, env);
                   6286:                                                char *token = my_strtok(env_path, ";");
                   6287:                                                
                   6288:                                                while(token != NULL) {
                   6289:                                                        if(strlen(token) != 0) {
                   6290:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   6291:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6292:                                                                        break;
                   6293:                                                                }
                   6294:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   6295:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6296:                                                                        break;
                   6297:                                                                }
                   6298:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   6299:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6300:                                                                        break;
                   6301:                                                                }
                   6302:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   6303:                                                                if(_access(path, 0) == 0) {
                   6304:                                                                        // this is a batch file, run command.com
                   6305:                                                                        char tmp[MAX_PATH];
                   6306:                                                                        if(opt_len != 0) {
                   6307:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   6308:                                                                        } else {
                   6309:                                                                                sprintf(tmp, "/C %s", path);
                   6310:                                                                        }
                   6311:                                                                        strcpy(opt, tmp);
                   6312:                                                                        opt_len = strlen(opt);
                   6313:                                                                        mem[opt_ofs] = opt_len;
                   6314:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6315:                                                                        strcpy(path, comspec_path);
                   6316:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   6317:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6318:                                                                        break;
                   6319:                                                                }
1.1.1.8   root     6320:                                                        }
1.1.1.14  root     6321:                                                        token = my_strtok(NULL, ";");
1.1       root     6322:                                                }
                   6323:                                        }
                   6324:                                }
                   6325:                        }
                   6326:                }
                   6327:        }
                   6328:        if(fd == -1) {
1.1.1.38  root     6329:                // we can not find command.com in the path, so open comspec_path
                   6330:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6331:                        strcpy(command, comspec_path);
                   6332:                        strcpy(path, command);
                   6333:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6334:                }
                   6335:        }
                   6336:        if(fd == -1) {
1.1.1.52  root     6337:                if(!first_process && al == 0 && dos_command) {
1.1       root     6338:                        // may be dos command
                   6339:                        char tmp[MAX_PATH];
1.1.1.52  root     6340:                        if(opt_len != 0) {
                   6341:                                sprintf(tmp, "%s %s", command, opt);
                   6342:                        } else {
                   6343:                                sprintf(tmp, "%s", command);
                   6344:                        }
                   6345:                        retval = system(tmp);
1.1       root     6346:                        return(0);
                   6347:                } else {
                   6348:                        return(-1);
                   6349:                }
                   6350:        }
1.1.1.52  root     6351:        memset(file_buffer, 0, sizeof(file_buffer));
1.1       root     6352:        _read(fd, file_buffer, sizeof(file_buffer));
                   6353:        _close(fd);
                   6354:        
1.1.1.52  root     6355:        // check if this is win32 program
                   6356:        if(!first_process && al == 0) {
                   6357:                UINT16 sign_dos = *(UINT16 *)(file_buffer + 0x00);
                   6358:                UINT32 e_lfanew = *(UINT32 *)(file_buffer + 0x3c);
                   6359:                if(sign_dos == IMAGE_DOS_SIGNATURE && e_lfanew >= 0x40 && e_lfanew < 0x400) {
                   6360:                        UINT32 sign_nt = *(UINT32 *)(file_buffer + e_lfanew + 0x00);
                   6361:                        UINT16 machine = *(UINT16 *)(file_buffer + e_lfanew + 0x04);
                   6362:                        if(sign_nt == IMAGE_NT_SIGNATURE && (machine == IMAGE_FILE_MACHINE_I386 || machine == IMAGE_FILE_MACHINE_AMD64)) {
                   6363:                                char tmp[MAX_PATH];
                   6364:                                if(opt_len != 0) {
                   6365:                                        sprintf(tmp, "\"%s\" %s", path, opt);
                   6366:                                } else {
                   6367:                                        sprintf(tmp, "\"%s\"", path);
                   6368:                                }
                   6369:                                retval = system(tmp);
                   6370:                                return(0);
                   6371:                        }
                   6372:                }
                   6373:        }
                   6374:        
1.1       root     6375:        // copy environment
1.1.1.29  root     6376:        int umb_linked, env_seg, psp_seg;
1.1       root     6377:        
1.1.1.29  root     6378:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6379:                msdos_mem_unlink_umb();
                   6380:        }
1.1.1.8   root     6381:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6382:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6383:                        if(umb_linked != 0) {
                   6384:                                msdos_mem_link_umb();
                   6385:                        }
                   6386:                        return(-1);
                   6387:                }
1.1       root     6388:        }
                   6389:        if(param->env_seg == 0) {
                   6390:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6391:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6392:        } else {
                   6393:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6394:        }
                   6395:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6396:        
                   6397:        // check exe header
                   6398:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6399:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6400:        UINT16 cs, ss, ip, sp;
                   6401:        
                   6402:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6403:                // memory allocation
                   6404:                int header_size = header->header_size * 16;
                   6405:                int load_size = header->pages * 512 - header_size;
                   6406:                if(header_size + load_size < 512) {
                   6407:                        load_size = 512 - header_size;
                   6408:                }
                   6409:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6410:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6411:                        msdos_mem_free(env_seg);
                   6412:                        return(-1);
                   6413:                }
                   6414:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6415:                if(paragraphs > free_paragraphs) {
                   6416:                        paragraphs = free_paragraphs;
                   6417:                }
1.1.1.8   root     6418:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6419:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6420:                                if(umb_linked != 0) {
                   6421:                                        msdos_mem_link_umb();
                   6422:                                }
                   6423:                                msdos_mem_free(env_seg);
                   6424:                                return(-1);
                   6425:                        }
1.1       root     6426:                }
                   6427:                // relocation
                   6428:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6429:                for(int i = 0; i < header->relocations; i++) {
                   6430:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6431:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6432:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6433:                }
                   6434:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6435:                // segments
                   6436:                cs = header->init_cs + start_seg;
                   6437:                ss = header->init_ss + start_seg;
                   6438:                ip = header->init_ip;
                   6439:                sp = header->init_sp - 2; // for symdeb
                   6440:        } else {
                   6441:                // memory allocation
                   6442:                paragraphs = free_paragraphs;
1.1.1.8   root     6443:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6444:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6445:                                if(umb_linked != 0) {
                   6446:                                        msdos_mem_link_umb();
                   6447:                                }
                   6448:                                msdos_mem_free(env_seg);
                   6449:                                return(-1);
                   6450:                        }
1.1       root     6451:                }
                   6452:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6453:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6454:                // segments
                   6455:                cs = ss = psp_seg;
                   6456:                ip = 0x100;
                   6457:                sp = 0xfffe;
                   6458:        }
1.1.1.29  root     6459:        if(umb_linked != 0) {
                   6460:                msdos_mem_link_umb();
                   6461:        }
1.1       root     6462:        
                   6463:        // create psp
1.1.1.3   root     6464:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6465:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6466:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6467:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6468:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6469:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6470:        
                   6471:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6472:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6473:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6474:        
1.1.1.4   root     6475:        for(int i = 0; i < 8; i++) {
                   6476:                if(name_tmp[i] == '.') {
                   6477:                        mcb_psp->prog_name[i] = '\0';
                   6478:                        break;
                   6479:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6480:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6481:                        i++;
                   6482:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6483:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6484:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6485:                } else {
                   6486:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6487:                }
                   6488:        }
                   6489:        
1.1       root     6490:        // process info
                   6491:        process_t *process = msdos_process_info_create(psp_seg);
                   6492:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6493: #ifdef USE_DEBUGGER
                   6494:        strcpy(process->module_path, path);
                   6495: #endif
1.1       root     6496:        process->dta.w.l = 0x80;
                   6497:        process->dta.w.h = psp_seg;
                   6498:        process->switchar = '/';
                   6499:        process->max_files = 20;
                   6500:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6501:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6502:        process->parent_ds = SREG(DS);
1.1.1.31  root     6503:        process->parent_es = SREG(ES);
1.1       root     6504:        
                   6505:        current_psp = psp_seg;
1.1.1.23  root     6506:        msdos_sda_update(current_psp);
1.1       root     6507:        
                   6508:        if(al == 0x00) {
                   6509:                int_10h_feh_called = int_10h_ffh_called = false;
                   6510:                
1.1.1.38  root     6511:                // pipe
                   6512:                if(pipe_stdin_path[0] != '\0') {
1.1.1.45  root     6513: //                     if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
                   6514:                        if(msdos_is_device_path(pipe_stdin_path)) {
                   6515:                                fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
                   6516:                        } else {
                   6517:                                fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
                   6518:                        }
                   6519:                        if(fd != -1) {
                   6520:                                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     6521:                                psp->file_table[0] = fd;
                   6522:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6523:                        }
                   6524:                }
                   6525:                if(pipe_stdout_path[0] != '\0') {
                   6526:                        if(_access(pipe_stdout_path, 0) == 0) {
                   6527:                                SetFileAttributes(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6528:                                DeleteFile(pipe_stdout_path);
                   6529:                        }
1.1.1.45  root     6530: //                     if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6531:                        if(msdos_is_device_path(pipe_stdout_path)) {
                   6532:                                fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6533:                        } else {
                   6534:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6535:                        }
                   6536:                        if(fd != -1) {
                   6537:                                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     6538:                                psp->file_table[1] = fd;
                   6539:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6540:                        }
                   6541:                }
1.1.1.39  root     6542:                if(pipe_stderr_path[0] != '\0') {
                   6543:                        if(_access(pipe_stderr_path, 0) == 0) {
                   6544:                                SetFileAttributes(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
                   6545:                                DeleteFile(pipe_stderr_path);
                   6546:                        }
1.1.1.45  root     6547: //                     if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6548:                        if(msdos_is_device_path(pipe_stderr_path)) {
                   6549:                                fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6550:                        } else {
                   6551:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6552:                        }
                   6553:                        if(fd != -1) {
                   6554:                                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     6555:                                psp->file_table[2] = fd;
                   6556:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6557:                        }
                   6558:                }
1.1.1.38  root     6559:                
1.1       root     6560:                // registers and segments
                   6561:                REG16(AX) = REG16(BX) = 0x00;
                   6562:                REG16(CX) = 0xff;
                   6563:                REG16(DX) = psp_seg;
                   6564:                REG16(SI) = ip;
                   6565:                REG16(DI) = sp;
                   6566:                REG16(SP) = sp;
1.1.1.3   root     6567:                SREG(DS) = SREG(ES) = psp_seg;
                   6568:                SREG(SS) = ss;
                   6569:                i386_load_segment_descriptor(DS);
                   6570:                i386_load_segment_descriptor(ES);
                   6571:                i386_load_segment_descriptor(SS);
1.1       root     6572:                
                   6573:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6574:                i386_jmp_far(cs, ip);
                   6575:        } else if(al == 0x01) {
                   6576:                // copy ss:sp and cs:ip to param block
                   6577:                param->sp = sp;
                   6578:                param->ss = ss;
                   6579:                param->ip = ip;
                   6580:                param->cs = cs;
1.1.1.31  root     6581:                
                   6582:                // the AX value to be passed to the child program is put on top of the child's stack
                   6583:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6584:        }
                   6585:        return(0);
                   6586: }
                   6587: 
                   6588: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6589: {
                   6590:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6591:        
                   6592:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6593:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6594:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6595:        
1.1.1.3   root     6596:        SREG(SS) = psp->stack.w.h;
                   6597:        i386_load_segment_descriptor(SS);
1.1       root     6598:        REG16(SP) = psp->stack.w.l;
                   6599:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6600:        
1.1.1.28  root     6601: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6602:        process_t *current_process = NULL;
                   6603:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6604:                if(process[i].psp == psp_seg) {
                   6605:                        current_process = &process[i];
                   6606:                        break;
                   6607:                }
                   6608:        }
                   6609:        if(current_process == NULL) {
                   6610:                throw(0x1f); // general failure
                   6611:        }
                   6612:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6613:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6614:        if(current_process->called_by_int2eh) {
                   6615:                REG16(AX) = ret;
                   6616:        }
                   6617:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6618:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6619:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6620:        i386_load_segment_descriptor(ES);
1.1       root     6621:        
                   6622:        if(mem_free) {
1.1.1.8   root     6623:                int mcb_seg;
                   6624:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6625:                        msdos_mem_free(mcb_seg + 1);
                   6626:                }
                   6627:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6628:                        msdos_mem_free(mcb_seg + 1);
                   6629:                }
1.1       root     6630:                
                   6631:                for(int i = 0; i < MAX_FILES; i++) {
                   6632:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6633:                                _close(i);
1.1.1.20  root     6634:                                msdos_file_handler_close(i);
                   6635:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6636:                        }
                   6637:                }
1.1.1.13  root     6638:                msdos_dta_info_free(psp_seg);
1.1       root     6639:        }
1.1.1.14  root     6640:        msdos_stdio_reopen();
1.1       root     6641:        
1.1.1.28  root     6642:        memset(current_process, 0, sizeof(process_t));
1.1       root     6643:        
                   6644:        current_psp = psp->parent_psp;
                   6645:        retval = ret;
1.1.1.23  root     6646:        msdos_sda_update(current_psp);
1.1       root     6647: }
                   6648: 
                   6649: // drive
                   6650: 
1.1.1.42  root     6651: int pcbios_update_drive_param(int drive_num, int force_update);
                   6652: 
1.1       root     6653: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6654: {
1.1.1.41  root     6655:        if(!(drive_num >= 0 && drive_num < 26)) {
                   6656:                return(0);
                   6657:        }
1.1.1.42  root     6658:        pcbios_update_drive_param(drive_num, force_update);
                   6659:        
                   6660:        drive_param_t *drive_param = &drive_params[drive_num];
1.1       root     6661:        *seg = DPB_TOP >> 4;
                   6662:        *ofs = sizeof(dpb_t) * drive_num;
                   6663:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6664:        
                   6665:        memset(dpb, 0, sizeof(dpb_t));
                   6666:        
1.1.1.41  root     6667:        dpb->drive_num = drive_num;
                   6668:        dpb->unit_num = drive_num;
1.1.1.42  root     6669:        
                   6670:        if(drive_param->valid) {
                   6671:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   6672:                
                   6673:                dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
                   6674:                dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
                   6675:                dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6676:                dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6677:                switch(geo->MediaType) {
                   6678:                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6679:                        dpb->media_type = 0xff;
                   6680:                        break;
                   6681:                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6682:                        dpb->media_type = 0xfe;
                   6683:                        break;
                   6684:                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6685:                        dpb->media_type = 0xfd;
                   6686:                        break;
                   6687:                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6688:                        dpb->media_type = 0xfc;
                   6689:                        break;
                   6690:                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6691:                case F3_1Pt2_512:
                   6692:                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6693:                case F5_720_512:
                   6694:                        dpb->media_type = 0xf9;
                   6695:                        break;
                   6696:                case FixedMedia:        // hard disk
                   6697:                case RemovableMedia:
                   6698:                case Unknown:
                   6699:                        dpb->media_type = 0xf8;
                   6700:                        break;
                   6701:                default:
                   6702:                        dpb->media_type = 0xf0;
                   6703:                        break;
                   6704:                }
                   6705:        }
1.1.1.41  root     6706:        dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
                   6707:        dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42  root     6708:        dpb->info_sector = 0xffff;
                   6709:        dpb->backup_boot_sector = 0xffff;
                   6710:        dpb->free_clusters = 0xffff;
                   6711:        dpb->free_search_cluster = 0xffffffff;
                   6712:        
                   6713:        return(drive_param->valid);
1.1       root     6714: }
                   6715: 
                   6716: // pc bios
                   6717: 
1.1.1.35  root     6718: #ifdef USE_SERVICE_THREAD
                   6719: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   6720: {
                   6721: #if defined(HAS_I386)
                   6722:        if(m_SF != 0) {
                   6723:                m_SF = 0;
1.1.1.49  root     6724:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6725:        } else {
                   6726:                m_SF = 1;
1.1.1.49  root     6727:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6728:        }
                   6729: #else
                   6730:        if(m_SignVal < 0) {
                   6731:                m_SignVal = 0;
1.1.1.49  root     6732:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6733:        } else {
                   6734:                m_SignVal = -1;
1.1.1.49  root     6735:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6736:        }
                   6737: #endif
1.1.1.49  root     6738:        i386_call_far(DUMMY_TOP >> 4, 0x0013);
1.1.1.35  root     6739:        in_service = true;
                   6740:        service_exit = false;
                   6741:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   6742: }
                   6743: 
                   6744: void finish_service_loop()
                   6745: {
                   6746:        if(in_service && service_exit) {
                   6747: #if defined(HAS_I386)
                   6748:                if(m_SF != 0) {
                   6749:                        m_SF = 0;
                   6750:                } else {
                   6751:                        m_SF = 1;
                   6752:                }
                   6753: #else
                   6754:                if(m_SignVal < 0) {
                   6755:                        m_SignVal = 0;
                   6756:                } else {
                   6757:                        m_SignVal = -1;
                   6758:                }
                   6759: #endif
                   6760:                in_service = false;
                   6761:        }
                   6762: }
                   6763: #endif
                   6764: 
1.1.1.19  root     6765: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   6766: {
                   6767:        static unsigned __int64 start_msec_since_midnight = 0;
                   6768:        static unsigned __int64 start_msec_since_hostboot = 0;
                   6769:        
                   6770:        if(start_msec_since_midnight == 0) {
                   6771:                SYSTEMTIME time;
                   6772:                GetLocalTime(&time);
                   6773:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   6774:                start_msec_since_hostboot = cur_msec;
                   6775:        }
                   6776:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   6777:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   6778:        return (UINT32)tick;
                   6779: }
                   6780: 
                   6781: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   6782: {
                   6783:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   6784:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   6785:        
                   6786:        if(prev_tick > next_tick) {
                   6787:                mem[0x470] = 1;
                   6788:        }
                   6789:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   6790: }
                   6791: 
1.1.1.14  root     6792: inline void pcbios_irq0()
                   6793: {
                   6794:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     6795:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     6796: }
                   6797: 
1.1.1.16  root     6798: int pcbios_get_text_vram_address(int page)
1.1       root     6799: {
                   6800:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6801:                return TEXT_VRAM_TOP;
1.1       root     6802:        } else {
1.1.1.14  root     6803:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     6804:        }
                   6805: }
                   6806: 
1.1.1.16  root     6807: int pcbios_get_shadow_buffer_address(int page)
1.1       root     6808: {
1.1.1.14  root     6809:        if(!int_10h_feh_called) {
1.1.1.16  root     6810:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     6811:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6812:                return SHADOW_BUF_TOP;
                   6813:        } else {
1.1.1.14  root     6814:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     6815:        }
                   6816: }
                   6817: 
1.1.1.16  root     6818: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     6819: {
1.1.1.16  root     6820:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     6821: }
                   6822: 
1.1.1.56! root     6823: bool pcbios_set_font_size(int width, int height)
        !          6824: {
        !          6825:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
        !          6826:        return(set_console_font_size(hStdout, width, height));
        !          6827: }
        !          6828: 
1.1.1.16  root     6829: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     6830: {
1.1.1.14  root     6831:        // clear the existing screen, not just the new one
                   6832:        int clr_height = max(height, scr_height);
                   6833:        
1.1.1.16  root     6834:        if(scr_width != width || scr_height != height) {
                   6835:                change_console_size(width, height);
1.1.1.14  root     6836:        }
                   6837:        mem[0x462] = 0;
                   6838:        *(UINT16 *)(mem + 0x44e) = 0;
                   6839:        
1.1.1.16  root     6840:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   6841:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   6842:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   6843:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1.1.51  root     6844:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     6845:        
1.1.1.23  root     6846:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     6847:        if(clr_screen) {
1.1.1.14  root     6848:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   6849:                        mem[ofs++] = 0x20;
                   6850:                        mem[ofs++] = 0x07;
                   6851:                }
                   6852:                
1.1.1.35  root     6853: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6854:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6855: #endif
1.1.1.14  root     6856:                for(int y = 0; y < clr_height; y++) {
                   6857:                        for(int x = 0; x < scr_width; x++) {
                   6858:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6859:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     6860:                        }
                   6861:                }
                   6862:                SMALL_RECT rect;
1.1.1.14  root     6863:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   6864:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6865:                vram_length_char = vram_last_length_char = 0;
                   6866:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     6867: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6868:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6869: #endif
1.1       root     6870:        }
1.1.1.14  root     6871:        COORD co;
                   6872:        co.X = 0;
                   6873:        co.Y = scr_top;
                   6874:        SetConsoleCursorPosition(hStdout, co);
                   6875:        cursor_moved = true;
                   6876:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     6877: }
                   6878: 
1.1.1.36  root     6879: void pcbios_update_cursor_position()
                   6880: {
                   6881:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6882:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   6883:        if(!restore_console_on_exit) {
                   6884:                scr_top = csbi.srWindow.Top;
                   6885:        }
                   6886:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   6887:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   6888: }
                   6889: 
1.1.1.16  root     6890: inline void pcbios_int_10h_00h()
                   6891: {
                   6892:        switch(REG8(AL) & 0x7f) {
                   6893:        case 0x70: // v-text mode
                   6894:        case 0x71: // extended cga v-text mode
                   6895:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   6896:                break;
                   6897:        default:
                   6898:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   6899:                break;
                   6900:        }
                   6901:        if(REG8(AL) & 0x80) {
                   6902:                mem[0x487] |= 0x80;
                   6903:        } else {
                   6904:                mem[0x487] &= ~0x80;
                   6905:        }
                   6906:        mem[0x449] = REG8(AL) & 0x7f;
                   6907: }
                   6908: 
1.1       root     6909: inline void pcbios_int_10h_01h()
                   6910: {
1.1.1.13  root     6911:        mem[0x460] = REG8(CL);
                   6912:        mem[0x461] = REG8(CH);
1.1.1.14  root     6913:        
1.1.1.23  root     6914:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6915:        CONSOLE_CURSOR_INFO ci;
                   6916:        GetConsoleCursorInfo(hStdout, &ci);
                   6917: //     ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
                   6918: //     if(ci.bVisible) {
                   6919:                int lines = max(8, REG8(CL) + 1);
                   6920:                ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
                   6921: //     }
                   6922:        SetConsoleCursorInfo(hStdout, &ci);
1.1       root     6923: }
                   6924: 
                   6925: inline void pcbios_int_10h_02h()
                   6926: {
1.1.1.14  root     6927:        // continuously setting the cursor effectively stops it blinking
                   6928:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     6929:                COORD co;
                   6930:                co.X = REG8(DL);
1.1.1.14  root     6931:                co.Y = REG8(DH) + scr_top;
                   6932:                
                   6933:                // some programs hide the cursor by moving it off screen
                   6934:                static bool hidden = false;
1.1.1.23  root     6935:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6936:                CONSOLE_CURSOR_INFO ci;
                   6937:                GetConsoleCursorInfo(hStdout, &ci);
                   6938:                
                   6939:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   6940:                        if(ci.bVisible) {
                   6941:                                ci.bVisible = FALSE;
                   6942: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6943:                                hidden = true;
                   6944:                        }
                   6945:                } else if(hidden) {
                   6946:                        if(!ci.bVisible) {
                   6947:                                ci.bVisible = TRUE;
                   6948: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6949:                        }
                   6950:                        hidden = false;
                   6951:                }
1.1       root     6952:        }
1.1.1.14  root     6953:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   6954:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     6955: }
                   6956: 
                   6957: inline void pcbios_int_10h_03h()
                   6958: {
1.1.1.14  root     6959:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6960:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6961:        REG8(CL) = mem[0x460];
                   6962:        REG8(CH) = mem[0x461];
                   6963: }
                   6964: 
                   6965: inline void pcbios_int_10h_05h()
                   6966: {
1.1.1.14  root     6967:        if(REG8(AL) >= vram_pages) {
                   6968:                return;
                   6969:        }
                   6970:        if(mem[0x462] != REG8(AL)) {
                   6971:                vram_flush();
                   6972:                
1.1.1.23  root     6973:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6974:                SMALL_RECT rect;
1.1.1.14  root     6975:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6976:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6977:                
1.1.1.16  root     6978:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     6979:                        for(int x = 0; x < scr_width; x++) {
                   6980:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6981:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6982:                        }
                   6983:                }
1.1.1.16  root     6984:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     6985:                        for(int x = 0; x < scr_width; x++) {
                   6986:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   6987:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     6988:                        }
                   6989:                }
1.1.1.14  root     6990:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6991:                
                   6992:                COORD co;
1.1.1.14  root     6993:                co.X = mem[0x450 + REG8(AL) * 2];
                   6994:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   6995:                if(co.Y < scr_top + scr_height) {
                   6996:                        SetConsoleCursorPosition(hStdout, co);
                   6997:                }
1.1       root     6998:        }
1.1.1.14  root     6999:        mem[0x462] = REG8(AL);
                   7000:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   7001:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     7002:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     7003:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     7004:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     7005:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     7006:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     7007: }
                   7008: 
                   7009: inline void pcbios_int_10h_06h()
                   7010: {
1.1.1.14  root     7011:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7012:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7013:                return;
                   7014:        }
                   7015:        vram_flush();
                   7016:        
1.1.1.23  root     7017:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7018:        SMALL_RECT rect;
1.1.1.14  root     7019:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   7020:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   7021:        
                   7022:        int right = min(REG8(DL), scr_width - 1);
                   7023:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7024:        
                   7025:        if(REG8(AL) == 0) {
1.1.1.14  root     7026:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7027:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7028:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7029:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7030:                        }
                   7031:                }
                   7032:        } else {
1.1.1.14  root     7033:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     7034:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7035:                                if(y2 <= bottom) {
                   7036:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7037:                                } else {
1.1.1.14  root     7038:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7039:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7040:                                }
1.1.1.14  root     7041:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7042:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7043:                        }
                   7044:                }
                   7045:        }
1.1.1.14  root     7046:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7047: }
                   7048: 
                   7049: inline void pcbios_int_10h_07h()
                   7050: {
1.1.1.14  root     7051:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7052:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7053:                return;
                   7054:        }
                   7055:        vram_flush();
                   7056:        
1.1.1.23  root     7057:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7058:        SMALL_RECT rect;
1.1.1.14  root     7059:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   7060:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   7061:        
                   7062:        int right = min(REG8(DL), scr_width - 1);
                   7063:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7064:        
                   7065:        if(REG8(AL) == 0) {
1.1.1.14  root     7066:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7067:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7068:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7069:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7070:                        }
                   7071:                }
                   7072:        } else {
1.1.1.14  root     7073:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     7074:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7075:                                if(y2 >= REG8(CH)) {
                   7076:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7077:                                } else {
1.1.1.14  root     7078:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7079:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7080:                                }
1.1.1.14  root     7081:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7082:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7083:                        }
                   7084:                }
                   7085:        }
1.1.1.14  root     7086:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7087: }
                   7088: 
                   7089: inline void pcbios_int_10h_08h()
                   7090: {
                   7091:        COORD co;
                   7092:        DWORD num;
                   7093:        
1.1.1.14  root     7094:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7095:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7096:        
                   7097:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7098:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7099:                co.Y += scr_top;
                   7100:                vram_flush();
1.1       root     7101:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   7102:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   7103:                REG8(AL) = scr_char[0];
                   7104:                REG8(AH) = scr_attr[0];
                   7105:        } else {
1.1.1.16  root     7106:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     7107:        }
                   7108: }
                   7109: 
                   7110: inline void pcbios_int_10h_09h()
                   7111: {
                   7112:        COORD co;
                   7113:        
1.1.1.14  root     7114:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7115:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7116:        
1.1.1.16  root     7117:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7118:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7119:        
                   7120:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7121: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7122:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7123: #endif
1.1.1.16  root     7124:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7125:                while(dest < end) {
                   7126:                        write_text_vram_char(dest - vram, REG8(AL));
                   7127:                        mem[dest++] = REG8(AL);
                   7128:                        write_text_vram_attr(dest - vram, REG8(BL));
                   7129:                        mem[dest++] = REG8(BL);
1.1       root     7130:                }
1.1.1.35  root     7131: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7132:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7133: #endif
1.1       root     7134:        } else {
1.1.1.14  root     7135:                while(dest < end) {
1.1       root     7136:                        mem[dest++] = REG8(AL);
                   7137:                        mem[dest++] = REG8(BL);
                   7138:                }
                   7139:        }
                   7140: }
                   7141: 
                   7142: inline void pcbios_int_10h_0ah()
                   7143: {
                   7144:        COORD co;
                   7145:        
1.1.1.14  root     7146:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7147:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7148:        
1.1.1.16  root     7149:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7150:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7151:        
                   7152:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7153: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7154:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7155: #endif
1.1.1.16  root     7156:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7157:                while(dest < end) {
                   7158:                        write_text_vram_char(dest - vram, REG8(AL));
                   7159:                        mem[dest++] = REG8(AL);
                   7160:                        dest++;
1.1       root     7161:                }
1.1.1.35  root     7162: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7163:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7164: #endif
1.1       root     7165:        } else {
1.1.1.14  root     7166:                while(dest < end) {
1.1       root     7167:                        mem[dest++] = REG8(AL);
                   7168:                        dest++;
                   7169:                }
                   7170:        }
                   7171: }
                   7172: 
1.1.1.40  root     7173: HDC get_console_window_device_context()
                   7174: {
                   7175:        static HWND hwndFound = 0;
                   7176:        
                   7177:        if(hwndFound == 0) {
                   7178:                // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
                   7179:                char pszNewWindowTitle[1024];
                   7180:                char pszOldWindowTitle[1024];
                   7181:                
                   7182:                GetConsoleTitle(pszOldWindowTitle, 1024);
                   7183:                wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
                   7184:                SetConsoleTitle(pszNewWindowTitle);
                   7185:                Sleep(100);
                   7186:                hwndFound = FindWindow(NULL, pszNewWindowTitle);
                   7187:                SetConsoleTitle(pszOldWindowTitle);
                   7188:        }
                   7189:        return GetDC(hwndFound);
                   7190: }
                   7191: 
                   7192: inline void pcbios_int_10h_0ch()
                   7193: {
                   7194:        HDC hdc = get_console_window_device_context();
                   7195:        
                   7196:        if(hdc != NULL) {
                   7197:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
                   7198:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
                   7199:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
                   7200:                
                   7201:                if(REG8(AL) & 0x80) {
                   7202:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7203:                        if(color != CLR_INVALID) {
                   7204:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7205:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7206:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
                   7207:                        }
                   7208:                }
                   7209:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
                   7210:        }
                   7211: }
                   7212: 
                   7213: inline void pcbios_int_10h_0dh()
                   7214: {
                   7215:        HDC hdc = get_console_window_device_context();
                   7216:        BYTE r = 0;
                   7217:        BYTE g = 0;
                   7218:        BYTE b = 0;
                   7219:        
                   7220:        if(hdc != NULL) {
                   7221:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7222:                if(color != CLR_INVALID) {
                   7223:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7224:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7225:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
                   7226:                }
                   7227:        }
                   7228:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
                   7229: }
                   7230: 
1.1       root     7231: inline void pcbios_int_10h_0eh()
                   7232: {
1.1.1.14  root     7233:        DWORD num;
                   7234:        COORD co;
                   7235:        
1.1.1.54  root     7236:        co.X = mem[0x450 + mem[0x462] * 2];
                   7237:        co.Y = mem[0x451 + mem[0x462] * 2];
1.1.1.14  root     7238:        
                   7239:        if(REG8(AL) == 7) {
                   7240:                //MessageBeep(-1);
                   7241:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   7242:                if(REG8(AL) == 10) {
                   7243:                        vram_flush();
                   7244:                }
1.1.1.23  root     7245:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     7246:                cursor_moved = true;
                   7247:        } else {
1.1.1.54  root     7248:                int dest = pcbios_get_shadow_buffer_address(mem[0x462], co.X, co.Y);
1.1.1.35  root     7249: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7250:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7251: #endif
1.1.1.54  root     7252:                int vram = pcbios_get_shadow_buffer_address(mem[0x462]);
                   7253:                write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     7254: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7255:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7256: #endif
1.1.1.54  root     7257:                
                   7258:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   7259:                if(++co.X == scr_width) {
                   7260:                        co.X = 0;
                   7261:                        if(++co.Y == scr_height) {
                   7262:                                vram_flush();
                   7263:                                WriteConsole(hStdout, "\n", 1, &num, NULL);
1.1.1.14  root     7264:                                cursor_moved = true;
                   7265:                        }
                   7266:                }
1.1.1.54  root     7267:                if(!cursor_moved) {
                   7268:                        co.Y += scr_top;
                   7269:                        SetConsoleCursorPosition(hStdout, co);
                   7270:                        cursor_moved = true;
                   7271:                }
1.1.1.14  root     7272:                mem[dest] = REG8(AL);
                   7273:        }
1.1       root     7274: }
                   7275: 
                   7276: inline void pcbios_int_10h_0fh()
                   7277: {
                   7278:        REG8(AL) = mem[0x449];
                   7279:        REG8(AH) = mem[0x44a];
                   7280:        REG8(BH) = mem[0x462];
                   7281: }
                   7282: 
1.1.1.14  root     7283: inline void pcbios_int_10h_11h()
                   7284: {
                   7285:        switch(REG8(AL)) {
1.1.1.16  root     7286:        case 0x01:
1.1.1.14  root     7287:        case 0x11:
1.1.1.56! root     7288:                if(!pcbios_set_font_size(8, 14)) {
        !          7289:                        if(active_code_page == 932) {
        !          7290:                                pcbios_set_font_size(6, 13);
        !          7291:                        } else {
        !          7292:                                pcbios_set_font_size(8, 12);
        !          7293:                        }
        !          7294:                }
        !          7295:                pcbios_set_console_size(80, 28, true); // 28 = 25 * 16 / 14
1.1.1.14  root     7296:                break;
1.1.1.16  root     7297:        case 0x02:
1.1.1.14  root     7298:        case 0x12:
1.1.1.56! root     7299:                if(!pcbios_set_font_size(8, 8)) {
        !          7300:                        if(active_code_page == 932) {
        !          7301:                                pcbios_set_font_size(5, 8);
        !          7302:                        } else {
        !          7303:                                pcbios_set_font_size(6, 8);
        !          7304:                        }
        !          7305:                }
        !          7306:                pcbios_set_console_size(80, 50, true); // 50 = 25 * 16 / 8
1.1.1.14  root     7307:                break;
1.1.1.16  root     7308:        case 0x04:
1.1.1.14  root     7309:        case 0x14:
1.1.1.56! root     7310:        case 0x18:
        !          7311:                if(!pcbios_set_font_size(8, 16)) {
        !          7312:                        if(active_code_page == 932) {
        !          7313:                                pcbios_set_font_size(8, 18);
        !          7314:                        } else {
        !          7315:                                pcbios_set_font_size(10, 18);
        !          7316:                        }
        !          7317:                }
        !          7318:                if(REG8(AL) == 0x18) {
        !          7319:                        pcbios_set_console_size(80, 50, true);
        !          7320:                } else {
        !          7321:                        pcbios_set_console_size(80, 25, true);
        !          7322:                }
1.1.1.14  root     7323:                break;
                   7324:        case 0x30:
                   7325:                SREG(ES) = 0;
                   7326:                i386_load_segment_descriptor(ES);
                   7327:                REG16(BP) = 0;
                   7328:                REG16(CX) = mem[0x485];
                   7329:                REG8(DL) = mem[0x484];
                   7330:                break;
1.1.1.54  root     7331:        default:
                   7332:                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));
                   7333:                m_CF = 1;
                   7334:                break;
1.1.1.14  root     7335:        }
                   7336: }
                   7337: 
                   7338: inline void pcbios_int_10h_12h()
                   7339: {
1.1.1.16  root     7340:        switch(REG8(BL)) {
                   7341:        case 0x10:
1.1.1.14  root     7342:                REG16(BX) = 0x0003;
                   7343:                REG16(CX) = 0x0009;
1.1.1.16  root     7344:                break;
1.1.1.14  root     7345:        }
                   7346: }
                   7347: 
1.1       root     7348: inline void pcbios_int_10h_13h()
                   7349: {
1.1.1.3   root     7350:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     7351:        COORD co;
                   7352:        DWORD num;
                   7353:        
                   7354:        co.X = REG8(DL);
1.1.1.14  root     7355:        co.Y = REG8(DH) + scr_top;
                   7356:        
                   7357:        vram_flush();
1.1       root     7358:        
                   7359:        switch(REG8(AL)) {
                   7360:        case 0x00:
                   7361:        case 0x01:
                   7362:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7363:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7364:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7365:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7366:                        SetConsoleCursorPosition(hStdout, co);
                   7367:                        
                   7368:                        if(csbi.wAttributes != REG8(BL)) {
                   7369:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   7370:                        }
1.1.1.14  root     7371:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   7372:                        
1.1       root     7373:                        if(csbi.wAttributes != REG8(BL)) {
                   7374:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7375:                        }
                   7376:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     7377:                                if(!restore_console_on_exit) {
                   7378:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7379:                                        scr_top = csbi.srWindow.Top;
                   7380:                                }
1.1.1.14  root     7381:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7382:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7383:                                SetConsoleCursorPosition(hStdout, co);
                   7384:                        } else {
                   7385:                                cursor_moved = true;
                   7386:                        }
                   7387:                } else {
1.1.1.3   root     7388:                        m_CF = 1;
1.1       root     7389:                }
                   7390:                break;
                   7391:        case 0x02:
                   7392:        case 0x03:
                   7393:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7394:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7395:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7396:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7397:                        SetConsoleCursorPosition(hStdout, co);
                   7398:                        
                   7399:                        WORD wAttributes = csbi.wAttributes;
                   7400:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7401:                                if(wAttributes != mem[ofs + 1]) {
                   7402:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7403:                                        wAttributes = mem[ofs + 1];
                   7404:                                }
1.1.1.14  root     7405:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7406:                        }
                   7407:                        if(csbi.wAttributes != wAttributes) {
                   7408:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7409:                        }
                   7410:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7411:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7412:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7413:                                SetConsoleCursorPosition(hStdout, co);
                   7414:                        } else {
                   7415:                                cursor_moved = true;
                   7416:                        }
                   7417:                } else {
1.1.1.3   root     7418:                        m_CF = 1;
1.1       root     7419:                }
                   7420:                break;
                   7421:        case 0x10:
                   7422:        case 0x11:
                   7423:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7424:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7425:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   7426:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7427:                        for(int i = 0; i < num; i++) {
                   7428:                                mem[ofs++] = scr_char[i];
                   7429:                                mem[ofs++] = scr_attr[i];
1.1.1.45  root     7430:                                if(REG8(AL) & 0x01) {
1.1       root     7431:                                        mem[ofs++] = 0;
                   7432:                                        mem[ofs++] = 0;
                   7433:                                }
                   7434:                        }
                   7435:                } else {
1.1.1.16  root     7436:                        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     7437:                                mem[ofs++] = mem[src++];
                   7438:                                mem[ofs++] = mem[src++];
1.1.1.45  root     7439:                                if(REG8(AL) & 0x01) {
1.1       root     7440:                                        mem[ofs++] = 0;
                   7441:                                        mem[ofs++] = 0;
                   7442:                                }
1.1.1.14  root     7443:                                if(++co.X == scr_width) {
                   7444:                                        if(++co.Y == scr_height) {
1.1       root     7445:                                                break;
                   7446:                                        }
                   7447:                                        co.X = 0;
                   7448:                                }
                   7449:                        }
                   7450:                }
                   7451:                break;
1.1.1.45  root     7452:        case 0x12: // ???
                   7453:        case 0x13: // ???
1.1       root     7454:        case 0x20:
                   7455:        case 0x21:
                   7456:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7457:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7458:                        int len = min(REG16(CX), scr_width * scr_height);
                   7459:                        for(int i = 0; i < len; i++) {
1.1       root     7460:                                scr_char[i] = mem[ofs++];
                   7461:                                scr_attr[i] = mem[ofs++];
1.1.1.45  root     7462:                                if(REG8(AL) & 0x01) {
1.1       root     7463:                                        ofs += 2;
                   7464:                                }
                   7465:                        }
1.1.1.14  root     7466:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7467:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7468:                } else {
1.1.1.16  root     7469:                        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     7470:                                mem[dest++] = mem[ofs++];
                   7471:                                mem[dest++] = mem[ofs++];
1.1.1.45  root     7472:                                if(REG8(AL) & 0x01) {
1.1       root     7473:                                        ofs += 2;
                   7474:                                }
1.1.1.14  root     7475:                                if(++co.X == scr_width) {
                   7476:                                        if(++co.Y == scr_height) {
1.1       root     7477:                                                break;
                   7478:                                        }
                   7479:                                        co.X = 0;
                   7480:                                }
                   7481:                        }
                   7482:                }
                   7483:                break;
                   7484:        default:
1.1.1.22  root     7485:                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     7486:                m_CF = 1;
1.1       root     7487:                break;
                   7488:        }
                   7489: }
                   7490: 
1.1.1.30  root     7491: inline void pcbios_int_10h_18h()
                   7492: {
                   7493:        switch(REG8(AL)) {
                   7494:        case 0x00:
                   7495:        case 0x01:
                   7496: //             REG8(AL) = 0x86;
                   7497:                REG8(AL) = 0x00;
                   7498:                break;
                   7499:        default:
                   7500:                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));
                   7501:                m_CF = 1;
                   7502:                break;
                   7503:        }
                   7504: }
                   7505: 
1.1.1.14  root     7506: inline void pcbios_int_10h_1ah()
                   7507: {
                   7508:        switch(REG8(AL)) {
                   7509:        case 0x00:
                   7510:                REG8(AL) = 0x1a;
                   7511:                REG8(BL) = 0x08;
                   7512:                REG8(BH) = 0x00;
                   7513:                break;
                   7514:        default:
1.1.1.22  root     7515:                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     7516:                m_CF = 1;
                   7517:                break;
                   7518:        }
                   7519: }
                   7520: 
1.1       root     7521: inline void pcbios_int_10h_1dh()
                   7522: {
                   7523:        switch(REG8(AL)) {
1.1.1.43  root     7524:        case 0x00:
                   7525:                // DOS/V Shift Status Line Control is not supported
                   7526:                m_CF = 1;
                   7527:                break;
1.1       root     7528:        case 0x01:
                   7529:                break;
                   7530:        case 0x02:
                   7531:                REG16(BX) = 0;
                   7532:                break;
                   7533:        default:
1.1.1.22  root     7534:                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));
                   7535:                m_CF = 1;
                   7536:                break;
                   7537:        }
                   7538: }
                   7539: 
                   7540: inline void pcbios_int_10h_4fh()
                   7541: {
                   7542:        switch(REG8(AL)) {
                   7543:        case 0x00:
                   7544:                REG8(AH) = 0x02; // not supported
                   7545:                break;
                   7546:        case 0x01:
                   7547:        case 0x02:
                   7548:        case 0x03:
                   7549:        case 0x04:
                   7550:        case 0x05:
                   7551:        case 0x06:
                   7552:        case 0x07:
                   7553:        case 0x08:
                   7554:        case 0x09:
                   7555:        case 0x0a:
                   7556:        case 0x0b:
                   7557:        case 0x0c:
                   7558:                REG8(AH) = 0x01; // failed
                   7559:                break;
                   7560:        default:
                   7561:                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     7562:                m_CF = 1;
1.1       root     7563:                break;
                   7564:        }
                   7565: }
                   7566: 
                   7567: inline void pcbios_int_10h_82h()
                   7568: {
                   7569:        static UINT8 mode = 0;
                   7570:        
                   7571:        switch(REG8(AL)) {
1.1.1.22  root     7572:        case 0x00:
1.1       root     7573:                if(REG8(BL) != 0xff) {
                   7574:                        mode = REG8(BL);
                   7575:                }
                   7576:                REG8(AL) = mode;
                   7577:                break;
                   7578:        default:
1.1.1.22  root     7579:                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     7580:                m_CF = 1;
1.1       root     7581:                break;
                   7582:        }
                   7583: }
                   7584: 
1.1.1.22  root     7585: inline void pcbios_int_10h_83h()
                   7586: {
                   7587:        static UINT8 mode = 0;
                   7588:        
                   7589:        switch(REG8(AL)) {
                   7590:        case 0x00:
                   7591:                REG16(AX) = 0; // offset???
                   7592:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7593:                i386_load_segment_descriptor(ES);
                   7594:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7595:                break;
                   7596:        default:
                   7597:                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));
                   7598:                m_CF = 1;
                   7599:                break;
                   7600:        }
                   7601: }
                   7602: 
                   7603: inline void pcbios_int_10h_90h()
                   7604: {
                   7605:        REG8(AL) = mem[0x449];
                   7606: }
                   7607: 
                   7608: inline void pcbios_int_10h_91h()
                   7609: {
                   7610:        REG8(AL) = 0x04; // VGA
                   7611: }
                   7612: 
                   7613: inline void pcbios_int_10h_efh()
                   7614: {
                   7615:        REG16(DX) = 0xffff;
                   7616: }
                   7617: 
1.1       root     7618: inline void pcbios_int_10h_feh()
                   7619: {
                   7620:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7621:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7622:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7623:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7624:        }
                   7625:        int_10h_feh_called = true;
                   7626: }
                   7627: 
                   7628: inline void pcbios_int_10h_ffh()
                   7629: {
                   7630:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7631:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7632:                COORD co;
                   7633:                DWORD num;
                   7634:                
1.1.1.14  root     7635:                vram_flush();
                   7636:                
                   7637:                co.X = (REG16(DI) >> 1) % scr_width;
                   7638:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7639:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7640:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7641:                int len;
                   7642:                for(len = 0; ofs < end; len++) {
                   7643:                        scr_char[len] = mem[ofs++];
                   7644:                        scr_attr[len] = mem[ofs++];
                   7645:                }
                   7646:                co.Y += scr_top;
                   7647:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7648:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7649:        }
                   7650:        int_10h_ffh_called = true;
                   7651: }
                   7652: 
1.1.1.42  root     7653: int pcbios_update_drive_param(int drive_num, int force_update)
                   7654: {
                   7655:        if(drive_num >= 0 && drive_num < 26) {
                   7656:                drive_param_t *drive_param = &drive_params[drive_num];
                   7657:                
                   7658:                if(force_update || !drive_param->initialized) {
                   7659:                        drive_param->valid = 0;
                   7660:                        char dev[64];
                   7661:                        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7662:                        
                   7663:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   7664:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7665:                                DWORD dwSize;
                   7666:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
                   7667:                                        drive_param->valid = 1;
                   7668:                                }
                   7669:                                CloseHandle(hFile);
                   7670:                        }
                   7671:                        drive_param->initialized = 1;
                   7672:                }
                   7673:                return(drive_param->valid);
                   7674:        }
                   7675:        return(0);
                   7676: }
                   7677: 
                   7678: inline void pcbios_int_13h_00h()
                   7679: {
                   7680:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7681:        
                   7682:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7683:                REG8(AH) = 0x00; // successful completion
                   7684:        } else {
                   7685:                if(REG8(DL) & 0x80) {
                   7686:                        REG8(AH) = 0x05; // reset failed (hard disk)
                   7687:                } else {
                   7688:                        REG8(AH) = 0x80; // timeout (not ready)
                   7689:                }
                   7690:                m_CF = 1;
                   7691:        }
                   7692: }
                   7693: 
                   7694: inline void pcbios_int_13h_02h()
                   7695: {
                   7696:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7697:        
                   7698:        if(REG8(AL) == 0) {
                   7699:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7700:                m_CF = 1;
                   7701:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7702:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7703:                m_CF = 1;
                   7704:        } else {
                   7705:                drive_param_t *drive_param = &drive_params[drive_num];
                   7706:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7707:                char dev[64];
                   7708:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7709:                
                   7710:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7711:                if(hFile == INVALID_HANDLE_VALUE) {
                   7712:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7713:                        m_CF = 1;
                   7714:                } else {
                   7715:                        UINT32 sector_num = REG8(AL);
                   7716:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7717:                        UINT32 head = REG8(DH);
                   7718:                        UINT32 sector = REG8(CL) & 0x3f;
                   7719:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7720:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7721:                        DWORD dwSize;
                   7722:                        
                   7723: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7724: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7725: //                             m_CF = 1;
                   7726: //                     } else 
                   7727:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7728:                                REG8(AH) = 0x04; // sector not found/read error
                   7729:                                m_CF = 1;
                   7730:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7731:                                REG8(AH) = 0x04; // sector not found/read error
                   7732:                                m_CF = 1;
                   7733:                        } else {
                   7734:                                REG8(AH) = 0x00; // successful completion
                   7735:                        }
                   7736:                        CloseHandle(hFile);
                   7737:                }
                   7738:        }
                   7739: }
                   7740: 
                   7741: inline void pcbios_int_13h_03h()
                   7742: {
                   7743:        // this operation may cause serious damage for drives, so support only floppy disk...
                   7744:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7745:        
                   7746:        if(REG8(AL) == 0) {
                   7747:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7748:                m_CF = 1;
                   7749:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7750:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7751:                m_CF = 1;
                   7752:        } else if(!drive_params[drive_num].is_fdd()) {
                   7753:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7754:                m_CF = 1;
                   7755:        } else {
                   7756:                drive_param_t *drive_param = &drive_params[drive_num];
                   7757:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7758:                char dev[64];
                   7759:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7760:                
                   7761:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7762:                if(hFile == INVALID_HANDLE_VALUE) {
                   7763:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7764:                        m_CF = 1;
                   7765:                } else {
                   7766:                        UINT32 sector_num = REG8(AL);
                   7767:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7768:                        UINT32 head = REG8(DH);
                   7769:                        UINT32 sector = REG8(CL) & 0x3f;
                   7770:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7771:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7772:                        DWORD dwSize;
                   7773:                        
                   7774: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7775: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7776: //                             m_CF = 1;
                   7777: //                     } else 
                   7778:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7779:                                REG8(AH) = 0x04; // sector not found/read error
                   7780:                                m_CF = 1;
                   7781:                        } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7782:                                REG8(AH) = 0x04; // sector not found/read error
                   7783:                                m_CF = 1;
                   7784:                        } else {
                   7785:                                REG8(AH) = 0x00; // successful completion
                   7786:                        }
                   7787:                        CloseHandle(hFile);
                   7788:                }
                   7789:        }
                   7790: }
                   7791: 
                   7792: inline void pcbios_int_13h_04h()
                   7793: {
                   7794:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7795:        
                   7796:        if(REG8(AL) == 0) {
                   7797:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7798:                m_CF = 1;
                   7799:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7800:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7801:                m_CF = 1;
                   7802:        } else {
                   7803:                drive_param_t *drive_param = &drive_params[drive_num];
                   7804:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7805:                char dev[64];
                   7806:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7807:                
                   7808:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7809:                if(hFile == INVALID_HANDLE_VALUE) {
                   7810:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7811:                        m_CF = 1;
                   7812:                } else {
                   7813:                        UINT32 sector_num = REG8(AL);
                   7814:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7815:                        UINT32 head = REG8(DH);
                   7816:                        UINT32 sector = REG8(CL) & 0x3f;
                   7817:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7818:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7819:                        DWORD dwSize;
                   7820:                        UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
                   7821:                        
                   7822: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7823: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7824: //                             m_CF = 1;
                   7825: //                     } else 
                   7826:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7827:                                REG8(AH) = 0x04; // sector not found/read error
                   7828:                                m_CF = 1;
                   7829:                        } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7830:                                REG8(AH) = 0x04; // sector not found/read error
                   7831:                                m_CF = 1;
                   7832:                        } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
                   7833:                                REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
                   7834:                                m_CF = 1;
                   7835:                        } else {
                   7836:                                REG8(AH) = 0x00; // successful completion
                   7837:                        }
                   7838:                        free(tmp_buffer);
                   7839:                        CloseHandle(hFile);
                   7840:                }
                   7841:        }
                   7842: }
                   7843: 
                   7844: inline void pcbios_int_13h_08h()
                   7845: {
                   7846:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7847:        
                   7848:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7849:                drive_param_t *drive_param = &drive_params[drive_num];
                   7850:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7851:                
                   7852:                REG16(AX) = 0x0000;
                   7853:                switch(geo->MediaType) {
                   7854:                case F5_360_512:
                   7855:                case F5_320_512:
                   7856:                case F5_320_1024:
                   7857:                case F5_180_512:
                   7858:                case F5_160_512:
                   7859:                        REG8(BL) = 0x01; // 320K/360K disk
                   7860:                        break;
                   7861:                case F5_1Pt2_512:
                   7862:                case F3_1Pt2_512:
                   7863:                case F3_1Pt23_1024:
                   7864:                case F5_1Pt23_1024:
                   7865:                        REG8(BL) = 0x02; // 1.2M disk
                   7866:                        break;
                   7867:                case F3_720_512:
                   7868:                case F3_640_512:
                   7869:                case F5_640_512:
                   7870:                case F5_720_512:
                   7871:                        REG8(BL) = 0x03; // 720K disk
                   7872:                        break;
                   7873:                case F3_1Pt44_512:
                   7874:                        REG8(BL) = 0x04; // 1.44M disk
                   7875:                        break;
                   7876:                case F3_2Pt88_512:
                   7877:                        REG8(BL) = 0x06; // 2.88M disk
                   7878:                        break;
                   7879:                case RemovableMedia:
                   7880:                        REG8(BL) = 0x10; // ATAPI Removable Media Device
                   7881:                        break;
                   7882:                default:
                   7883:                        REG8(BL) = 0x00; // unknown
                   7884:                        break;
                   7885:                }
                   7886:                if(REG8(DL) & 0x80) {
                   7887:                        switch(GetLogicalDrives() & 0x0c) {
                   7888:                        case 0x00: REG8(DL) = 0x00; break;
                   7889:                        case 0x04:
                   7890:                        case 0x08: REG8(DL) = 0x01; break;
                   7891:                        case 0x0c: REG8(DL) = 0x02; break;
                   7892:                        }
                   7893:                } else {
                   7894:                        switch(GetLogicalDrives() & 0x03) {
                   7895:                        case 0x00: REG8(DL) = 0x00; break;
                   7896:                        case 0x01:
                   7897:                        case 0x02: REG8(DL) = 0x01; break;
                   7898:                        case 0x03: REG8(DL) = 0x02; break;
                   7899:                        }
                   7900:                }
                   7901:                REG8(DH) = drive_param->head_num();
                   7902:                int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
                   7903:                int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
                   7904:                REG8(CH) = cyl & 0xff;
                   7905:                REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
                   7906:        } else {
                   7907:                REG8(AH) = 0x07;
                   7908:                m_CF = 1;
                   7909:        }
                   7910: }
                   7911: 
                   7912: inline void pcbios_int_13h_10h()
                   7913: {
                   7914:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7915:        
                   7916:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7917:                REG8(AH) = 0x00; // successful completion
                   7918:        } else {
                   7919:                if(REG8(DL) & 0x80) {
                   7920:                        REG8(AH) = 0xaa; // drive not ready (hard disk)
                   7921:                } else {
                   7922:                        REG8(AH) = 0x80; // timeout (not ready)
                   7923:                }
                   7924:                m_CF = 1;
                   7925:        }
                   7926: }
                   7927: 
                   7928: inline void pcbios_int_13h_15h()
                   7929: {
                   7930:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7931:        
                   7932:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7933:                if(REG8(DL) & 0x80) {
                   7934:                        REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
                   7935:                } else {
                   7936:                        REG8(AH) = 0x03; // hard disk
                   7937:                }
                   7938:        } else {
                   7939:                REG8(AH) = 0x00; // no such drive
                   7940:        }
                   7941: }
                   7942: 
1.1.1.43  root     7943: inline void pcbios_int_13h_41h()
                   7944: {
                   7945:        if(REG16(BX) == 0x55aa) {
                   7946:                // IBM/MS INT 13 Extensions is not installed
                   7947:                REG8(AH) = 0x01;
                   7948:                m_CF = 1;
                   7949:        } else {
                   7950:                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));
                   7951:                REG8(AH) = 0x01;
                   7952:                m_CF = 1;
                   7953:        }
                   7954: }
                   7955: 
1.1.1.25  root     7956: inline void pcbios_int_14h_00h()
                   7957: {
1.1.1.29  root     7958:        if(REG16(DX) < 4) {
1.1.1.25  root     7959:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   7960:                UINT8 selector = sio_read(REG16(DX), 3);
                   7961:                selector &= ~0x3f;
                   7962:                selector |= REG8(AL) & 0x1f;
                   7963:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   7964:                sio_write(REG16(DX), 3, selector | 0x80);
                   7965:                sio_write(REG16(DX), 0, divisor & 0xff);
                   7966:                sio_write(REG16(DX), 1, divisor >> 8);
                   7967:                sio_write(REG16(DX), 3, selector);
                   7968:                REG8(AH) = sio_read(REG16(DX), 5);
                   7969:                REG8(AL) = sio_read(REG16(DX), 6);
                   7970:        } else {
                   7971:                REG8(AH) = 0x80;
                   7972:        }
                   7973: }
                   7974: 
                   7975: inline void pcbios_int_14h_01h()
                   7976: {
1.1.1.29  root     7977:        if(REG16(DX) < 4) {
1.1.1.25  root     7978:                UINT8 selector = sio_read(REG16(DX), 3);
                   7979:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7980:                sio_write(REG16(DX), 0, REG8(AL));
                   7981:                sio_write(REG16(DX), 3, selector);
                   7982:                REG8(AH) = sio_read(REG16(DX), 5);
                   7983:        } else {
                   7984:                REG8(AH) = 0x80;
                   7985:        }
                   7986: }
                   7987: 
                   7988: inline void pcbios_int_14h_02h()
                   7989: {
1.1.1.29  root     7990:        if(REG16(DX) < 4) {
1.1.1.25  root     7991:                UINT8 selector = sio_read(REG16(DX), 3);
                   7992:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7993:                REG8(AL) = sio_read(REG16(DX), 0);
                   7994:                sio_write(REG16(DX), 3, selector);
                   7995:                REG8(AH) = sio_read(REG16(DX), 5);
                   7996:        } else {
                   7997:                REG8(AH) = 0x80;
                   7998:        }
                   7999: }
                   8000: 
                   8001: inline void pcbios_int_14h_03h()
                   8002: {
1.1.1.29  root     8003:        if(REG16(DX) < 4) {
1.1.1.25  root     8004:                REG8(AH) = sio_read(REG16(DX), 5);
                   8005:                REG8(AL) = sio_read(REG16(DX), 6);
                   8006:        } else {
                   8007:                REG8(AH) = 0x80;
                   8008:        }
                   8009: }
                   8010: 
                   8011: inline void pcbios_int_14h_04h()
                   8012: {
1.1.1.29  root     8013:        if(REG16(DX) < 4) {
1.1.1.25  root     8014:                UINT8 selector = sio_read(REG16(DX), 3);
                   8015:                if(REG8(CH) <= 0x03) {
                   8016:                        selector = (selector & ~0x03) | REG8(CH);
                   8017:                }
                   8018:                if(REG8(BL) == 0x00) {
                   8019:                        selector &= ~0x04;
                   8020:                } else if(REG8(BL) == 0x01) {
                   8021:                        selector |= 0x04;
                   8022:                }
                   8023:                if(REG8(BH) == 0x00) {
                   8024:                        selector = (selector & ~0x38) | 0x00;
                   8025:                } else if(REG8(BH) == 0x01) {
                   8026:                        selector = (selector & ~0x38) | 0x08;
                   8027:                } else if(REG8(BH) == 0x02) {
                   8028:                        selector = (selector & ~0x38) | 0x18;
                   8029:                } else if(REG8(BH) == 0x03) {
                   8030:                        selector = (selector & ~0x38) | 0x28;
                   8031:                } else if(REG8(BH) == 0x04) {
                   8032:                        selector = (selector & ~0x38) | 0x38;
                   8033:                }
                   8034:                if(REG8(AL) == 0x00) {
                   8035:                        selector |= 0x40;
                   8036:                } else if(REG8(AL) == 0x01) {
                   8037:                        selector &= ~0x40;
                   8038:                }
                   8039:                if(REG8(CL) <= 0x0b) {
                   8040:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   8041:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   8042:                        sio_write(REG16(DX), 3, selector | 0x80);
                   8043:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   8044:                        sio_write(REG16(DX), 1, divisor >> 8);
                   8045:                }
                   8046:                sio_write(REG16(DX), 3, selector);
                   8047:                REG8(AH) = sio_read(REG16(DX), 5);
                   8048:                REG8(AL) = sio_read(REG16(DX), 6);
                   8049:        } else {
                   8050:                REG8(AH) = 0x80;
                   8051:        }
                   8052: }
                   8053: 
                   8054: inline void pcbios_int_14h_05h()
                   8055: {
1.1.1.29  root     8056:        if(REG16(DX) < 4) {
1.1.1.25  root     8057:                if(REG8(AL) == 0x00) {
                   8058:                        REG8(BL) = sio_read(REG16(DX), 4);
                   8059:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8060:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8061:                } else if(REG8(AL) == 0x01) {
                   8062:                        sio_write(REG16(DX), 4, REG8(BL));
                   8063:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8064:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8065:                } else {
                   8066:                        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));
                   8067:                }
                   8068:        } else {
                   8069:                REG8(AH) = 0x80;
                   8070:        }
                   8071: }
                   8072: 
1.1.1.14  root     8073: inline void pcbios_int_15h_10h()
                   8074: {
1.1.1.22  root     8075:        switch(REG8(AL)) {
                   8076:        case 0x00:
1.1.1.14  root     8077:                Sleep(10);
1.1.1.35  root     8078:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     8079:                break;
                   8080:        default:
                   8081:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.14  root     8082:                REG8(AH) = 0x86;
                   8083:                m_CF = 1;
                   8084:        }
                   8085: }
                   8086: 
1.1       root     8087: inline void pcbios_int_15h_23h()
                   8088: {
                   8089:        switch(REG8(AL)) {
1.1.1.22  root     8090:        case 0x00:
1.1.1.8   root     8091:                REG8(CL) = cmos_read(0x2d);
                   8092:                REG8(CH) = cmos_read(0x2e);
1.1       root     8093:                break;
1.1.1.22  root     8094:        case 0x01:
1.1.1.8   root     8095:                cmos_write(0x2d, REG8(CL));
                   8096:                cmos_write(0x2e, REG8(CH));
1.1       root     8097:                break;
                   8098:        default:
1.1.1.22  root     8099:                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     8100:                REG8(AH) = 0x86;
1.1.1.3   root     8101:                m_CF = 1;
1.1       root     8102:                break;
                   8103:        }
                   8104: }
                   8105: 
                   8106: inline void pcbios_int_15h_24h()
                   8107: {
                   8108:        switch(REG8(AL)) {
1.1.1.22  root     8109:        case 0x00:
1.1.1.3   root     8110:                i386_set_a20_line(0);
1.1       root     8111:                REG8(AH) = 0;
                   8112:                break;
1.1.1.22  root     8113:        case 0x01:
1.1.1.3   root     8114:                i386_set_a20_line(1);
1.1       root     8115:                REG8(AH) = 0;
                   8116:                break;
1.1.1.22  root     8117:        case 0x02:
1.1       root     8118:                REG8(AH) = 0;
1.1.1.3   root     8119:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     8120:                REG16(CX) = 0;
                   8121:                break;
1.1.1.22  root     8122:        case 0x03:
1.1       root     8123:                REG16(AX) = 0;
                   8124:                REG16(BX) = 0;
                   8125:                break;
1.1.1.22  root     8126:        default:
                   8127:                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));
                   8128:                REG8(AH) = 0x86;
                   8129:                m_CF = 1;
                   8130:                break;
1.1       root     8131:        }
                   8132: }
                   8133: 
                   8134: inline void pcbios_int_15h_49h()
                   8135: {
1.1.1.27  root     8136:        REG8(AH) = 0x00;
                   8137:        REG8(BL) = 0x00; // DOS/V
1.1       root     8138: }
                   8139: 
1.1.1.22  root     8140: inline void pcbios_int_15h_50h()
                   8141: {
                   8142:        switch(REG8(AL)) {
                   8143:        case 0x00:
                   8144:        case 0x01:
                   8145:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   8146:                        REG8(AH) = 0x01; // invalid font type in bh
                   8147:                        m_CF = 1;
1.1.1.27  root     8148:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     8149:                        REG8(AH) = 0x02; // bl not zero
                   8150:                        m_CF = 1;
                   8151:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   8152:                        REG8(AH) = 0x04; // invalid code page
                   8153:                        m_CF = 1;
1.1.1.27  root     8154:                } else if(REG8(AL) == 0x01) {
                   8155:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     8156:                        m_CF = 1;
1.1.1.27  root     8157:                } else {
1.1.1.49  root     8158:                        // dummy font read routine is at fffc:000d
                   8159:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.27  root     8160:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     8161:                        REG16(BX) = 0x000d;
1.1.1.27  root     8162:                        REG8(AH) = 0x00; // success
1.1.1.22  root     8163:                }
                   8164:                break;
                   8165:        default:
                   8166:                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));
                   8167:                REG8(AH) = 0x86;
                   8168:                m_CF = 1;
                   8169:                break;
                   8170:        }
                   8171: }
                   8172: 
1.1.1.30  root     8173: inline void pcbios_int_15h_53h()
                   8174: {
                   8175:        switch(REG8(AL)) {
                   8176:        case 0x00:
                   8177:                // APM is not installed
                   8178:                REG8(AH) = 0x86;
                   8179:                m_CF = 1;
                   8180:                break;
                   8181:        default:
                   8182:                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));
                   8183:                REG8(AH) = 0x86;
                   8184:                m_CF = 1;
                   8185:                break;
                   8186:        }
                   8187: }
                   8188: 
1.1.1.43  root     8189: inline void pcbios_int_15h_84h()
                   8190: {
                   8191:        // joystick support (from DOSBox)
                   8192:        switch(REG16(DX)) {
                   8193:        case 0x00:
                   8194:                REG16(AX) = 0x00f0;
                   8195:                REG16(DX) = 0x0201;
                   8196:                m_CF = 1;
                   8197:                break;
                   8198:        case 0x01:
                   8199:                REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   8200:                m_CF = 1;
                   8201:                break;
                   8202:        default:
                   8203:                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));
                   8204:                REG8(AH) = 0x86;
                   8205:                m_CF = 1;
                   8206:                break;
                   8207:        }
                   8208: }
1.1.1.35  root     8209: 
                   8210: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     8211: {
                   8212:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     8213:        UINT32 msec = usec / 1000;
                   8214:        
1.1.1.54  root     8215:        while(msec && !m_exit) {
1.1.1.14  root     8216:                UINT32 tmp = min(msec, 100);
                   8217:                if(msec - tmp < 10) {
                   8218:                        tmp = msec;
                   8219:                }
                   8220:                Sleep(tmp);
                   8221:                msec -= tmp;
                   8222:        }
1.1.1.35  root     8223:        
                   8224: #ifdef USE_SERVICE_THREAD
                   8225:        service_exit = true;
                   8226: #endif
                   8227:        return(0);
                   8228: }
                   8229: 
                   8230: inline void pcbios_int_15h_86h()
                   8231: {
                   8232:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   8233: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8234:                if(!in_service && !in_service_29h) {
                   8235:                        start_service_loop(pcbios_int_15h_86h_thread);
                   8236:                } else {
                   8237: #endif
                   8238:                        pcbios_int_15h_86h_thread(NULL);
                   8239:                        REQUEST_HARDWRE_UPDATE();
                   8240: #ifdef USE_SERVICE_THREAD
                   8241:                }
1.1.1.35  root     8242: #endif
                   8243:        }
1.1       root     8244: }
                   8245: 
                   8246: inline void pcbios_int_15h_87h()
                   8247: {
                   8248:        // copy extended memory (from DOSBox)
                   8249:        int len = REG16(CX) * 2;
1.1.1.3   root     8250:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     8251:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   8252:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   8253:        memcpy(mem + dst, mem + src, len);
                   8254:        REG16(AX) = 0x00;
                   8255: }
                   8256: 
                   8257: inline void pcbios_int_15h_88h()
                   8258: {
1.1.1.17  root     8259:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     8260: }
                   8261: 
                   8262: inline void pcbios_int_15h_89h()
                   8263: {
1.1.1.21  root     8264: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     8265:        // switch to protected mode (from DOSBox)
                   8266:        write_io_byte(0x20, 0x10);
                   8267:        write_io_byte(0x21, REG8(BH));
                   8268:        write_io_byte(0x21, 0x00);
                   8269:        write_io_byte(0xa0, 0x10);
                   8270:        write_io_byte(0xa1, REG8(BL));
                   8271:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     8272:        i386_set_a20_line(1);
                   8273:        int ofs = SREG_BASE(ES) + REG16(SI);
                   8274:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   8275:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   8276:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   8277:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   8278: #if defined(HAS_I386)
                   8279:        m_cr[0] |= 1;
                   8280: #else
                   8281:        m_msw |= 1;
                   8282: #endif
                   8283:        SREG(DS) = 0x18;
                   8284:        SREG(ES) = 0x20;
                   8285:        SREG(SS) = 0x28;
                   8286:        i386_load_segment_descriptor(DS);
                   8287:        i386_load_segment_descriptor(ES);
                   8288:        i386_load_segment_descriptor(SS);
1.1.1.21  root     8289:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     8290:        REG16(SP) += 6;
1.1.1.3   root     8291: #if defined(HAS_I386)
1.1.1.21  root     8292:        UINT32 flags = get_flags();
                   8293:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8294:        set_flags(flags);
1.1.1.3   root     8295: #else
1.1.1.21  root     8296:        UINT32 flags = CompressFlags();
                   8297:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8298:        ExpandFlags(flags);
1.1.1.3   root     8299: #endif
1.1       root     8300:        REG16(AX) = 0x00;
1.1.1.21  root     8301:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     8302: #else
1.1.1.21  root     8303:        // i86/i186/v30: protected mode is not supported
1.1       root     8304:        REG8(AH) = 0x86;
1.1.1.3   root     8305:        m_CF = 1;
1.1       root     8306: #endif
                   8307: }
                   8308: 
1.1.1.21  root     8309: inline void pcbios_int_15h_8ah()
                   8310: {
                   8311:        UINT32 size = MAX_MEM - 0x100000;
                   8312:        REG16(AX) = size & 0xffff;
                   8313:        REG16(DX) = size >> 16;
                   8314: }
                   8315: 
1.1.1.54  root     8316: #ifdef EXT_BIOS_TOP
                   8317: inline void pcbios_int_15h_c1h()
                   8318: {
                   8319:        SREG(ES) = EXT_BIOS_TOP >> 4;
                   8320:        i386_load_segment_descriptor(ES);
                   8321: }
                   8322: #endif
                   8323: 
                   8324: void pcbios_read_from_ps2_mouse(UINT16 *data_1st, UINT16 *data_2nd, UINT16 *data_3rd)
                   8325: {
                   8326:        // from DOSBox DoPS2Callback()
                   8327:        UINT16 mdat = 0x08;
                   8328:        INT16 xdiff = mouse.position.x - mouse.prev_position.x;
                   8329:        INT16 ydiff = mouse.prev_position.y - mouse.position.y;
                   8330:        
                   8331:        if(mouse.buttons[0].status) {
                   8332:                mdat |= 0x01;
                   8333:        }
                   8334:        if(mouse.buttons[1].status) {
                   8335:                mdat |= 0x02;
                   8336:        }
                   8337:        mouse.prev_position.x = mouse.position.x;
                   8338:        mouse.prev_position.y = mouse.position.y;
                   8339:        if((xdiff > 0xff) || (xdiff < -0xff)) {
                   8340:                mdat |= 0x40;   // x overflow
                   8341:        }
                   8342:        if((ydiff > 0xff) || (ydiff < -0xff)) {
                   8343:                mdat |= 0x80;   // y overflow
                   8344:        }
                   8345:        xdiff %= 256;
                   8346:        ydiff %= 256;
                   8347:        if(xdiff < 0) {
                   8348:                xdiff = (0x100 + xdiff);
                   8349:                mdat |= 0x10;
                   8350:        }
                   8351:        if(ydiff < 0) {
                   8352:                ydiff = (0x100 + ydiff);
                   8353:                mdat |= 0x20;
                   8354:        }
                   8355:        *data_1st = (UINT16)mdat;
                   8356:        *data_2nd = (UINT16)(xdiff % 256);
                   8357:        *data_3rd = (UINT16)(ydiff % 256);
                   8358: }
                   8359: 
                   8360: inline void pcbios_int_15h_c2h()
                   8361: {
                   8362:        static UINT8 enabled = 0;
                   8363:        static UINT8 sampling_rate = 5;
                   8364:        static UINT8 resolution = 2;
                   8365:        static UINT8 scaling = 1;
                   8366:        
                   8367:        switch(REG8(AL)) {
                   8368:        case 0x00:
                   8369:                if(REG8(BH) == 0x00 || REG8(BH) == 0x01) {
                   8370:                        enabled = REG8(BH);
                   8371:                        REG8(AH) = 0x00; // successful
                   8372:                } else {
                   8373:                        REG8(AH) = 0x01; // invalid function
                   8374:                        m_CF = 1;
                   8375:                }
                   8376:                break;
                   8377:        case 0x01:
                   8378:                REG8(BH) = 0x00; // device id
                   8379:                REG8(BL) = 0xaa; // mouse
                   8380:        case 0x05:
                   8381:                enabled = 0;
                   8382:                sampling_rate = 5;
                   8383:                resolution = 2;
                   8384:                scaling = 1;
                   8385:                REG8(AH) = 0x00; // successful
                   8386:                break;
                   8387:        case 0x02:
                   8388:                sampling_rate = REG8(BH);
                   8389:                REG8(AH) = 0x00; // successful
                   8390:                break;
                   8391:        case 0x03:
                   8392:                resolution = REG8(BH);
                   8393:                REG8(AH) = 0x00; // successful
                   8394:                break;
                   8395:        case 0x04:
                   8396:                REG8(BH) = 0x00; // device id
                   8397:                REG8(AH) = 0x00; // successful
                   8398:                break;
                   8399:        case 0x06:
                   8400:                switch(REG8(BH)) {
                   8401:                case 0x00:
                   8402:                        REG8(BL) = 0x00;
                   8403:                        if(mouse.buttons[1].status) {
                   8404:                                REG8(BL) |= 0x01;
                   8405:                        }
                   8406:                        if(mouse.buttons[0].status) {
                   8407:                                REG8(BL) |= 0x04;
                   8408:                        }
                   8409:                        if(scaling == 2) {
                   8410:                                REG8(BL) |= 0x10;
                   8411:                        }
                   8412:                        REG8(CL) = resolution;
                   8413:                        switch(sampling_rate) {
                   8414:                        case 0:  REG8(DL) =  10; break;
                   8415:                        case 1:  REG8(DL) =  20; break;
                   8416:                        case 2:  REG8(DL) =  40; break;
                   8417:                        case 3:  REG8(DL) =  60; break;
                   8418:                        case 4:  REG8(DL) =  80; break;
                   8419: //                     case 5:  REG8(DL) = 100; break;
                   8420:                        case 6:  REG8(DL) = 200; break;
                   8421:                        default: REG8(DL) = 100; break;
                   8422:                        }
                   8423:                        REG8(AH) = 0x00; // successful
                   8424:                        break;
                   8425:                case 0x01:
                   8426:                case 0x02:
                   8427:                        scaling = REG8(BH);
                   8428:                        REG8(AH) = 0x00; // successful
                   8429:                        break;
                   8430:                default:
                   8431:                        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));
                   8432:                        REG8(AH) = 0x01; // invalid function
                   8433:                        m_CF = 1;
                   8434:                        break;
                   8435:                }
                   8436:                break;
                   8437:        case 0x07: // set device handler addr
                   8438:                mouse.call_addr_ps2.w.l = REG16(BX);
                   8439:                mouse.call_addr_ps2.w.h = SREG(ES);
                   8440:                REG8(AH) = 0x00; // successful
                   8441:                break;
                   8442:        case 0x08:
                   8443:                REG8(AH) = 0x00; // successful
                   8444:                break;
                   8445:        case 0x09:
                   8446:                {
                   8447:                        UINT16 data_1st, data_2nd, data_3rd;
                   8448:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   8449:                        REG8(BL) = (UINT8)(data_1st & 0xff);
                   8450:                        REG8(CL) = (UINT8)(data_2nd & 0xff);
                   8451:                        REG8(DL) = (UINT8)(data_3rd & 0xff);
                   8452:                }
                   8453:                REG8(AH) = 0x00; // successful
                   8454:                break;
                   8455:        default:
                   8456:                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));
                   8457: //             REG8(AH) = 0x86;
                   8458:                REG8(AH) = 0x01; // invalid function
                   8459:                m_CF = 1;
                   8460:                break;
                   8461:        }
                   8462: }
                   8463: 
1.1.1.3   root     8464: #if defined(HAS_I386)
1.1       root     8465: inline void pcbios_int_15h_c9h()
                   8466: {
                   8467:        REG8(AH) = 0x00;
                   8468:        REG8(CH) = cpu_type;
                   8469:        REG8(CL) = cpu_step;
                   8470: }
1.1.1.3   root     8471: #endif
1.1       root     8472: 
                   8473: inline void pcbios_int_15h_cah()
                   8474: {
                   8475:        switch(REG8(AL)) {
1.1.1.22  root     8476:        case 0x00:
1.1       root     8477:                if(REG8(BL) > 0x3f) {
                   8478:                        REG8(AH) = 0x03;
1.1.1.3   root     8479:                        m_CF = 1;
1.1       root     8480:                } else if(REG8(BL) < 0x0e) {
                   8481:                        REG8(AH) = 0x04;
1.1.1.3   root     8482:                        m_CF = 1;
1.1       root     8483:                } else {
1.1.1.8   root     8484:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     8485:                }
                   8486:                break;
1.1.1.22  root     8487:        case 0x01:
1.1       root     8488:                if(REG8(BL) > 0x3f) {
                   8489:                        REG8(AH) = 0x03;
1.1.1.3   root     8490:                        m_CF = 1;
1.1       root     8491:                } else if(REG8(BL) < 0x0e) {
                   8492:                        REG8(AH) = 0x04;
1.1.1.3   root     8493:                        m_CF = 1;
1.1       root     8494:                } else {
1.1.1.8   root     8495:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     8496:                }
                   8497:                break;
                   8498:        default:
1.1.1.22  root     8499:                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     8500:                REG8(AH) = 0x86;
1.1.1.3   root     8501:                m_CF = 1;
1.1       root     8502:                break;
                   8503:        }
                   8504: }
                   8505: 
1.1.1.22  root     8506: inline void pcbios_int_15h_e8h()
1.1.1.17  root     8507: {
1.1.1.22  root     8508:        switch(REG8(AL)) {
                   8509: #if defined(HAS_I386)
                   8510:        case 0x01:
                   8511:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   8512:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8513:                break;
1.1.1.17  root     8514: #endif
1.1.1.22  root     8515:        default:
                   8516:                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));
                   8517:                REG8(AH) = 0x86;
                   8518:                m_CF = 1;
                   8519:                break;
                   8520:        }
                   8521: }
1.1.1.17  root     8522: 
1.1.1.55  root     8523: bool pcbios_is_key_buffer_empty()
                   8524: {
                   8525:        return(*(UINT16 *)(mem + 0x41a) == *(UINT16 *)(mem + 0x41c));
                   8526: }
                   8527: 
1.1.1.51  root     8528: void pcbios_clear_key_buffer()
                   8529: {
                   8530:        key_buf_char->clear();
                   8531:        key_buf_scan->clear();
                   8532:        
                   8533:        // update key buffer
                   8534:        *(UINT16 *)(mem + 0x41a) = *(UINT16 *)(mem + 0x41c); // head = tail
                   8535: }
                   8536: 
                   8537: void pcbios_set_key_buffer(int key_char, int key_scan)
                   8538: {
                   8539:        // update key buffer
                   8540:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8541:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8542:        UINT16 next = tail + 2;
                   8543:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8544:                next = *(UINT16 *)(mem + 0x480);
                   8545:        }
                   8546:        if(next != head) {
                   8547:                *(UINT16 *)(mem + 0x41c) = next;
                   8548:                mem[0x400 + (tail++)] = key_char;
                   8549:                mem[0x400 + (tail++)] = key_scan;
1.1.1.55  root     8550:        } else {
                   8551:                // store to extra key buffer
                   8552:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8553:                        key_buf_char->write(key_char);
                   8554:                        key_buf_scan->write(key_scan);
                   8555:                }
1.1.1.51  root     8556:        }
                   8557: }
                   8558: 
                   8559: bool pcbios_get_key_buffer(int *key_char, int *key_scan)
                   8560: {
                   8561:        // update key buffer
                   8562:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8563:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8564:        UINT16 next = head + 2;
                   8565:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8566:                next = *(UINT16 *)(mem + 0x480);
                   8567:        }
                   8568:        if(head != tail) {
                   8569:                *(UINT16 *)(mem + 0x41a) = next;
1.1.1.55  root     8570:                *key_char = mem[0x400 + (head++)];
                   8571:                *key_scan = mem[0x400 + (head++)];
                   8572:                
                   8573:                // restore from extra key buffer
                   8574:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8575:                        if(!key_buf_char->empty()) {
                   8576:                                pcbios_set_key_buffer(key_buf_char->read(), key_buf_scan->read());
                   8577:                        }
                   8578:                }
                   8579:                return(true);
                   8580:        } else {
                   8581:                *key_char = 0x00;
                   8582:                *key_scan = 0x00;
                   8583:                return(false);
1.1.1.51  root     8584:        }
                   8585: }
                   8586: 
1.1.1.33  root     8587: void pcbios_update_key_code(bool wait)
1.1       root     8588: {
1.1.1.32  root     8589:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8590: #ifdef USE_SERVICE_THREAD
                   8591:                EnterCriticalSection(&key_buf_crit_sect);
                   8592: #endif
1.1.1.55  root     8593:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     8594: #ifdef USE_SERVICE_THREAD
                   8595:                LeaveCriticalSection(&key_buf_crit_sect);
                   8596: #endif
                   8597:                if(empty) {
1.1.1.32  root     8598:                        if(!update_key_buffer()) {
1.1.1.33  root     8599:                                if(wait) {
1.1.1.32  root     8600:                                        Sleep(10);
                   8601:                                } else {
                   8602:                                        maybe_idle();
                   8603:                                }
1.1.1.14  root     8604:                        }
                   8605:                }
1.1.1.34  root     8606:        }
                   8607:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8608: #ifdef USE_SERVICE_THREAD
                   8609:                EnterCriticalSection(&key_buf_crit_sect);
                   8610: #endif
1.1.1.51  root     8611:                int key_char, key_scan;
                   8612:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8613:                        key_code  = key_char << 0;
                   8614:                        key_code |= key_scan << 8;
1.1.1.35  root     8615:                        key_recv  = 0x0000ffff;
1.1.1.51  root     8616:                }
                   8617:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8618:                        key_code |= key_char << 16;
                   8619:                        key_code |= key_scan << 24;
1.1.1.33  root     8620:                        key_recv |= 0xffff0000;
1.1.1.32  root     8621:                }
1.1.1.35  root     8622: #ifdef USE_SERVICE_THREAD
                   8623:                LeaveCriticalSection(&key_buf_crit_sect);
                   8624: #endif
1.1       root     8625:        }
                   8626: }
                   8627: 
1.1.1.35  root     8628: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     8629: {
1.1.1.54  root     8630:        while(key_recv == 0 && !m_exit) {
1.1.1.33  root     8631:                pcbios_update_key_code(true);
1.1       root     8632:        }
1.1.1.33  root     8633:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8634:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   8635:                        if(REG8(AH) == 0x10) {
                   8636:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   8637:                        } else {
                   8638:                                key_code = ((key_code >> 16) & 0xff00);
                   8639:                        }
                   8640:                        key_recv >>= 16;
1.1       root     8641:                }
                   8642:        }
                   8643:        REG16(AX) = key_code & 0xffff;
                   8644:        key_code >>= 16;
1.1.1.33  root     8645:        key_recv >>= 16;
1.1.1.35  root     8646:        
                   8647: #ifdef USE_SERVICE_THREAD
                   8648:        service_exit = true;
                   8649: #endif
                   8650:        return(0);
                   8651: }
                   8652: 
                   8653: inline void pcbios_int_16h_00h()
                   8654: {
                   8655: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8656:        if(!in_service && !in_service_29h) {
                   8657:                start_service_loop(pcbios_int_16h_00h_thread);
                   8658:        } else {
                   8659: #endif
                   8660:                pcbios_int_16h_00h_thread(NULL);
                   8661:                REQUEST_HARDWRE_UPDATE();
                   8662: #ifdef USE_SERVICE_THREAD
                   8663:        }
1.1.1.35  root     8664: #endif
1.1       root     8665: }
                   8666: 
                   8667: inline void pcbios_int_16h_01h()
                   8668: {
1.1.1.33  root     8669:        if(key_recv == 0) {
                   8670:                pcbios_update_key_code(false);
1.1.1.5   root     8671:        }
1.1.1.33  root     8672:        if(key_recv != 0) {
                   8673:                UINT32 key_code_tmp = key_code;
                   8674:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8675:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   8676:                                if(REG8(AH) == 0x11) {
                   8677:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   8678:                                } else {
                   8679:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   8680:                                }
                   8681:                        }
1.1       root     8682:                }
1.1.1.5   root     8683:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     8684: #if defined(HAS_I386)
1.1.1.33  root     8685:                m_ZF = 0;
                   8686: #else
                   8687:                m_ZeroVal = 1;
                   8688: #endif
                   8689:        } else {
                   8690: #if defined(HAS_I386)
                   8691:                m_ZF = 1;
1.1.1.3   root     8692: #else
1.1.1.33  root     8693:                m_ZeroVal = 0;
1.1.1.3   root     8694: #endif
1.1.1.33  root     8695:        }
1.1       root     8696: }
                   8697: 
                   8698: inline void pcbios_int_16h_02h()
                   8699: {
                   8700:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   8701:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   8702:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   8703:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   8704:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   8705:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   8706:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   8707:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   8708: }
                   8709: 
                   8710: inline void pcbios_int_16h_03h()
                   8711: {
                   8712:        static UINT16 status = 0;
                   8713:        
                   8714:        switch(REG8(AL)) {
                   8715:        case 0x05:
                   8716:                status = REG16(BX);
                   8717:                break;
                   8718:        case 0x06:
                   8719:                REG16(BX) = status;
                   8720:                break;
                   8721:        default:
1.1.1.3   root     8722:                m_CF = 1;
1.1       root     8723:                break;
                   8724:        }
                   8725: }
                   8726: 
                   8727: inline void pcbios_int_16h_05h()
                   8728: {
1.1.1.32  root     8729:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8730: #ifdef USE_SERVICE_THREAD
                   8731:                EnterCriticalSection(&key_buf_crit_sect);
                   8732: #endif
1.1.1.51  root     8733:                pcbios_set_key_buffer(REG8(CL), REG8(CH));
1.1.1.35  root     8734: #ifdef USE_SERVICE_THREAD
                   8735:                LeaveCriticalSection(&key_buf_crit_sect);
                   8736: #endif
1.1.1.32  root     8737:        }
1.1       root     8738:        REG8(AL) = 0x00;
                   8739: }
                   8740: 
                   8741: inline void pcbios_int_16h_12h()
                   8742: {
                   8743:        pcbios_int_16h_02h();
                   8744:        
                   8745:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   8746:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   8747:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   8748:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   8749:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   8750:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   8751:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   8752:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   8753: }
                   8754: 
                   8755: inline void pcbios_int_16h_13h()
                   8756: {
                   8757:        static UINT16 status = 0;
                   8758:        
                   8759:        switch(REG8(AL)) {
                   8760:        case 0x00:
                   8761:                status = REG16(DX);
                   8762:                break;
                   8763:        case 0x01:
                   8764:                REG16(DX) = status;
                   8765:                break;
                   8766:        default:
1.1.1.22  root     8767:                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     8768:                m_CF = 1;
1.1       root     8769:                break;
                   8770:        }
                   8771: }
                   8772: 
                   8773: inline void pcbios_int_16h_14h()
                   8774: {
                   8775:        static UINT8 status = 0;
                   8776:        
                   8777:        switch(REG8(AL)) {
                   8778:        case 0x00:
                   8779:        case 0x01:
                   8780:                status = REG8(AL);
                   8781:                break;
                   8782:        case 0x02:
                   8783:                REG8(AL) = status;
                   8784:                break;
                   8785:        default:
1.1.1.22  root     8786:                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     8787:                m_CF = 1;
1.1       root     8788:                break;
                   8789:        }
                   8790: }
                   8791: 
1.1.1.24  root     8792: inline void pcbios_int_16h_55h()
                   8793: {
                   8794:        switch(REG8(AL)) {
                   8795:        case 0x00:
                   8796:                // keyboard tsr is not present
                   8797:                break;
                   8798:        case 0xfe:
                   8799:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   8800:                break;
                   8801:        case 0xff:
                   8802:                break;
                   8803:        default:
                   8804:                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));
                   8805:                m_CF = 1;
                   8806:                break;
                   8807:        }
                   8808: }
                   8809: 
1.1.1.30  root     8810: inline void pcbios_int_16h_6fh()
                   8811: {
                   8812:        switch(REG8(AL)) {
                   8813:        case 0x00:
                   8814:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   8815:                break;
                   8816:        default:
                   8817:                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));
                   8818:                m_CF = 1;
                   8819:                break;
                   8820:        }
                   8821: }
                   8822: 
1.1.1.37  root     8823: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   8824: {
                   8825:        UINT8 hi = jis >> 8;
                   8826:        UINT8 lo = jis & 0xff;
                   8827:        
                   8828:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   8829:        hi = (hi - 0x21) / 2 + 0x81;
                   8830:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   8831:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   8832:        
                   8833:        return((hi << 8) + lo);
                   8834: }
                   8835: 
                   8836: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   8837: {
                   8838:        UINT8 hi = sjis >> 8;
                   8839:        UINT8 lo = sjis & 0xff;
                   8840:        
                   8841:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   8842:                return(0x2121);
                   8843:        }
                   8844:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   8845:                return(0x2121);
                   8846:        }
                   8847:        if(hi >= 0xf0 && hi <= 0xf3) {
                   8848:                // gaiji
                   8849:                if(lo >= 0x40 && lo <= 0x7e) {
                   8850:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   8851:                }
                   8852:                if(lo >= 0x80 && lo <= 0x9e) {
                   8853:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   8854:                }
                   8855:                if(lo >= 0x9f && lo <= 0xfc) {
                   8856:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   8857:                }
                   8858:        }
                   8859:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   8860:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   8861:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   8862:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   8863:        
                   8864:        return((hi << 8) + lo);
                   8865: }
                   8866: 
1.1.1.38  root     8867: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   8868: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   8869: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     8870: 
                   8871: void pcbios_printer_out(int c, UINT8 data)
                   8872: {
                   8873:        if(pio[c].conv_mode) {
                   8874:                if(pio[c].sjis_hi != 0) {
                   8875:                        if(!pio[c].jis_mode) {
                   8876:                                printer_out(c, 0x1c);
                   8877:                                printer_out(c, 0x26);
                   8878:                                pio[c].jis_mode = true;
                   8879:                        }
                   8880:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   8881:                        printer_out(c, jis >> 8);
                   8882:                        printer_out(c, jis & 0xff);
                   8883:                        pio[c].sjis_hi = 0;
                   8884:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   8885:                        printer_out(c, data);
                   8886:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8887:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8888:                        }
                   8889:                        pio[c].esc_len++;
                   8890:                        
                   8891:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8892:                        case 0x33: // 1Bh 33h XX
                   8893:                        case 0x4a: // 1Bh 4Ah XX
                   8894:                        case 0x4e: // 1Bh 4Eh XX
                   8895:                        case 0x51: // 1Bh 51h XX
                   8896:                        case 0x55: // 1Bh 55h XX
                   8897:                        case 0x6c: // 1Bh 6Ch XX
                   8898:                        case 0x71: // 1Bh 71h XX
                   8899:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     8900:                                if(pio[c].esc_len == 3) {
                   8901:                                        pio[c].esc_buf[0] = 0x00;
                   8902:                                }
                   8903:                                break;
1.1.1.38  root     8904:                        case 0x24: // 1Bh 24h XX XX
                   8905:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     8906:                                if(pio[c].esc_len == 4) {
                   8907:                                        pio[c].esc_buf[0] = 0x00;
                   8908:                                }
                   8909:                                break;
1.1.1.38  root     8910:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     8911:                                if(pio[c].esc_len >= 3) {
                   8912:                                        switch(pio[c].esc_buf[2]) {
                   8913:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   8914:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   8915:                                                        pio[c].esc_buf[0] = 0x00;
                   8916:                                                }
                   8917:                                                break;
                   8918:                                        case 32: case 33: case 38: case 39: case 40:
                   8919:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   8920:                                                        pio[c].esc_buf[0] = 0x00;
                   8921:                                                }
                   8922:                                                break;
1.1.1.38  root     8923:                                        case 71: case 72: case 73:
                   8924:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   8925:                                                        pio[c].esc_buf[0] = 0x00;
                   8926:                                                }
                   8927:                                                break;
1.1.1.37  root     8928:                                        default:
                   8929:                                                pio[c].esc_buf[0] = 0x00;
                   8930:                                                break;
                   8931:                                        }
                   8932:                                }
                   8933:                                break;
1.1.1.38  root     8934:                        case 0x40: // 1Bh 40h
1.1.1.37  root     8935:                                if(pio[c].jis_mode) {
                   8936:                                        printer_out(c, 0x1c);
                   8937:                                        printer_out(c, 0x2e);
                   8938:                                        pio[c].jis_mode = false;
                   8939:                                }
                   8940:                                pio[c].esc_buf[0] = 0x00;
                   8941:                                break;
1.1.1.38  root     8942:                        case 0x42: // 1Bh 42h data 00h
                   8943:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8944:                                if(pio[c].esc_len >= 3 && data == 0) {
                   8945:                                        pio[c].esc_buf[0] = 0x00;
                   8946:                                }
                   8947:                                break;
1.1.1.38  root     8948:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     8949:                                if(pio[c].esc_len >= 3 && data != 0) {
                   8950:                                        pio[c].esc_buf[0] = 0x00;
                   8951:                                }
                   8952:                                break;
1.1.1.38  root     8953:                        default: // 1Bh XX
1.1.1.37  root     8954:                                pio[c].esc_buf[0] = 0x00;
                   8955:                                break;
                   8956:                        }
                   8957:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   8958:                        printer_out(c, data);
                   8959:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8960:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8961:                        }
                   8962:                        pio[c].esc_len++;
                   8963:                        
                   8964:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8965:                        case 0x21: // 1Ch 21h XX
                   8966:                        case 0x2d: // 1Ch 2Dh XX
                   8967:                        case 0x57: // 1Ch 57h XX
                   8968:                        case 0x6b: // 1Ch 6Bh XX
                   8969:                        case 0x72: // 1Ch 72h XX
                   8970:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     8971:                                if(pio[c].esc_len == 3) {
                   8972:                                        pio[c].esc_buf[0] = 0x00;
                   8973:                                }
                   8974:                                break;
1.1.1.38  root     8975:                        case 0x26: // 1Ch 26h
1.1.1.37  root     8976:                                pio[c].jis_mode = true;
                   8977:                                pio[c].esc_buf[0] = 0x00;
                   8978:                                break;
1.1.1.38  root     8979:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     8980:                                pio[c].jis_mode = false;
                   8981:                                pio[c].esc_buf[0] = 0x00;
                   8982:                                break;
1.1.1.38  root     8983:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     8984:                                if(pio[c].esc_len == 76) {
                   8985:                                        pio[c].esc_buf[0] = 0x00;
                   8986:                                }
                   8987:                                break;
1.1.1.38  root     8988:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8989:                                if(pio[c].esc_len == 6) {
                   8990:                                        pio[c].esc_buf[0] = 0x00;
                   8991:                                }
                   8992:                                break;
1.1.1.38  root     8993:                        case 0x53: // 1Ch 53h XX XX
                   8994:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     8995:                                if(pio[c].esc_len == 4) {
                   8996:                                        pio[c].esc_buf[0] = 0x00;
                   8997:                                }
                   8998:                                break;
1.1.1.38  root     8999:                        default: // 1Ch XX
1.1.1.37  root     9000:                                pio[c].esc_buf[0] = 0x00;
                   9001:                                break;
                   9002:                        }
                   9003:                } else if(data == 0x1b || data == 0x1c) {
                   9004:                        printer_out(c, data);
                   9005:                        pio[c].esc_buf[0] = data;
                   9006:                        pio[c].esc_len = 1;
                   9007:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   9008:                        pio[c].sjis_hi = data;
                   9009:                } else {
                   9010:                        if(pio[c].jis_mode) {
                   9011:                                printer_out(c, 0x1c);
                   9012:                                printer_out(c, 0x2e);
                   9013:                                pio[c].jis_mode = false;
                   9014:                        }
                   9015:                        printer_out(c, data);
                   9016:                }
                   9017:        } else {
                   9018:                if(pio[c].jis_mode) {
                   9019:                        printer_out(c, 0x1c);
                   9020:                        printer_out(c, 0x2e);
                   9021:                        pio[c].jis_mode = false;
                   9022:                }
                   9023:                printer_out(c, data);
                   9024:        }
                   9025: }
                   9026: 
                   9027: inline void pcbios_int_17h_00h()
                   9028: {
                   9029:        if(REG16(DX) < 3) {
                   9030:                pcbios_printer_out(REG16(DX), REG8(AL));
                   9031:                REG8(AH) = 0xd0;
                   9032:        }
                   9033: }
                   9034: 
                   9035: inline void pcbios_int_17h_01h()
                   9036: {
                   9037:        if(REG16(DX) < 3) {
                   9038:                REG8(AH) = 0xd0;
                   9039:        }
                   9040: }
                   9041: 
                   9042: inline void pcbios_int_17h_02h()
                   9043: {
                   9044:        if(REG16(DX) < 3) {
                   9045:                REG8(AH) = 0xd0;
                   9046:        }
                   9047: }
                   9048: 
                   9049: inline void pcbios_int_17h_03h()
                   9050: {
                   9051:        switch(REG8(AL)) {
                   9052:        case 0x00:
                   9053:                if(REG16(DX) < 3) {
                   9054:                        if(pio[REG16(DX)].jis_mode) {
                   9055:                                printer_out(REG16(DX), 0x1c);
                   9056:                                printer_out(REG16(DX), 0x2e);
                   9057:                                pio[REG16(DX)].jis_mode = false;
                   9058:                        }
                   9059:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   9060:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   9061:                        }
                   9062:                        REG16(CX) = 0x0000;
                   9063:                        REG8(AH) = 0xd0;
                   9064:                }
                   9065:                break;
                   9066:        default:
                   9067:                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));
                   9068:                break;
                   9069:        }
                   9070: }
                   9071: 
                   9072: inline void pcbios_int_17h_50h()
                   9073: {
                   9074:        switch(REG8(AL)) {
                   9075:        case 0x00:
                   9076:                if(REG16(DX) < 3) {
                   9077:                        if(REG16(BX) = 0x0001) {
                   9078:                                pio[REG16(DX)].conv_mode = false;
                   9079:                                REG8(AL) = 0x00;
                   9080:                        } else if(REG16(BX) = 0x0051) {
                   9081:                                pio[REG16(DX)].conv_mode = true;
                   9082:                                REG8(AL) = 0x00;
                   9083:                        } else {
                   9084:                                REG8(AL) = 0x01;
                   9085:                        }
                   9086:                } else {
                   9087:                        REG8(AL) = 0x02;
                   9088:                }
                   9089:                break;
                   9090:        case 0x01:
                   9091:                if(REG16(DX) < 3) {
                   9092:                        if(pio[REG16(DX)].conv_mode) {
                   9093:                                REG16(BX) = 0x0051;
                   9094:                        } else {
                   9095:                                REG16(BX) = 0x0001;
                   9096:                        }
                   9097:                        REG8(AL) = 0x00;
                   9098:                } else {
                   9099:                        REG8(AL) = 0x02;
                   9100:                }
                   9101:                break;
                   9102:        default:
                   9103:                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));
                   9104:                break;
                   9105:        }
                   9106: }
                   9107: 
                   9108: inline void pcbios_int_17h_51h()
                   9109: {
                   9110:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   9111:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   9112:        } else {
                   9113:                REG16(DX) = 0x0000;
                   9114:        }
                   9115: }
                   9116: 
                   9117: inline void pcbios_int_17h_52h()
                   9118: {
                   9119:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   9120:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   9121:        } else {
                   9122:                REG16(DX) = 0x0000;
                   9123:        }
                   9124: }
                   9125: 
                   9126: inline void pcbios_int_17h_84h()
                   9127: {
                   9128:        if(REG16(DX) < 3) {
                   9129:                if(pio[REG16(DX)].jis_mode) {
                   9130:                        printer_out(REG16(DX), 0x1c);
                   9131:                        printer_out(REG16(DX), 0x2e);
                   9132:                        pio[REG16(DX)].jis_mode = false;
                   9133:                }
                   9134:                printer_out(REG16(DX), REG8(AL));
                   9135:                REG8(AH) = 0xd0;
                   9136:        }
                   9137: }
                   9138: 
                   9139: inline void pcbios_int_17h_85h()
                   9140: {
                   9141:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   9142: }
                   9143: 
1.1       root     9144: inline void pcbios_int_1ah_00h()
                   9145: {
1.1.1.19  root     9146:        pcbios_update_daily_timer_counter(timeGetTime());
                   9147:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   9148:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   9149:        REG8(AL) = mem[0x470];
                   9150:        mem[0x470] = 0;
1.1       root     9151: }
                   9152: 
                   9153: inline int to_bcd(int t)
                   9154: {
                   9155:        int u = (t % 100) / 10;
                   9156:        return (u << 4) | (t % 10);
                   9157: }
                   9158: 
                   9159: inline void pcbios_int_1ah_02h()
                   9160: {
                   9161:        SYSTEMTIME time;
                   9162:        
                   9163:        GetLocalTime(&time);
                   9164:        REG8(CH) = to_bcd(time.wHour);
                   9165:        REG8(CL) = to_bcd(time.wMinute);
                   9166:        REG8(DH) = to_bcd(time.wSecond);
                   9167:        REG8(DL) = 0x00;
                   9168: }
                   9169: 
                   9170: inline void pcbios_int_1ah_04h()
                   9171: {
                   9172:        SYSTEMTIME time;
                   9173:        
                   9174:        GetLocalTime(&time);
                   9175:        REG8(CH) = to_bcd(time.wYear / 100);
                   9176:        REG8(CL) = to_bcd(time.wYear);
                   9177:        REG8(DH) = to_bcd(time.wMonth);
                   9178:        REG8(DL) = to_bcd(time.wDay);
                   9179: }
                   9180: 
                   9181: inline void pcbios_int_1ah_0ah()
                   9182: {
                   9183:        SYSTEMTIME time;
                   9184:        FILETIME file_time;
                   9185:        WORD dos_date, dos_time;
                   9186:        
                   9187:        GetLocalTime(&time);
                   9188:        SystemTimeToFileTime(&time, &file_time);
                   9189:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   9190:        REG16(CX) = dos_date;
                   9191: }
                   9192: 
                   9193: // msdos system call
                   9194: 
1.1.1.43  root     9195: inline void msdos_int_21h_56h(int lfn);
                   9196: 
1.1       root     9197: inline void msdos_int_21h_00h()
                   9198: {
1.1.1.3   root     9199:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     9200: }
                   9201: 
1.1.1.35  root     9202: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     9203: {
                   9204:        REG8(AL) = msdos_getche();
1.1.1.33  root     9205:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9206:        
1.1.1.35  root     9207: #ifdef USE_SERVICE_THREAD
                   9208:        service_exit = true;
                   9209: #endif
                   9210:        return(0);
                   9211: }
                   9212: 
                   9213: inline void msdos_int_21h_01h()
                   9214: {
                   9215: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9216:        if(!in_service && !in_service_29h &&
                   9217:           *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50  root     9218:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9219:                // msdos_putch() will be used in this service
                   9220:                // if int 29h is hooked, run this service in main thread to call int 29h
                   9221:                start_service_loop(msdos_int_21h_01h_thread);
                   9222:        } else {
                   9223: #endif
                   9224:                msdos_int_21h_01h_thread(NULL);
                   9225:                REQUEST_HARDWRE_UPDATE();
                   9226: #ifdef USE_SERVICE_THREAD
                   9227:        }
1.1.1.35  root     9228: #endif
1.1       root     9229: }
                   9230: 
                   9231: inline void msdos_int_21h_02h()
                   9232: {
1.1.1.33  root     9233:        UINT8 data = REG8(DL);
                   9234:        msdos_putch(data);
                   9235:        REG8(AL) = data;
                   9236:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9237: }
                   9238: 
                   9239: inline void msdos_int_21h_03h()
                   9240: {
                   9241:        REG8(AL) = msdos_aux_in();
                   9242: }
                   9243: 
                   9244: inline void msdos_int_21h_04h()
                   9245: {
                   9246:        msdos_aux_out(REG8(DL));
                   9247: }
                   9248: 
                   9249: inline void msdos_int_21h_05h()
                   9250: {
                   9251:        msdos_prn_out(REG8(DL));
                   9252: }
                   9253: 
                   9254: inline void msdos_int_21h_06h()
                   9255: {
                   9256:        if(REG8(DL) == 0xff) {
                   9257:                if(msdos_kbhit()) {
                   9258:                        REG8(AL) = msdos_getch();
1.1.1.3   root     9259: #if defined(HAS_I386)
                   9260:                        m_ZF = 0;
                   9261: #else
                   9262:                        m_ZeroVal = 1;
                   9263: #endif
1.1       root     9264:                } else {
                   9265:                        REG8(AL) = 0;
1.1.1.3   root     9266: #if defined(HAS_I386)
                   9267:                        m_ZF = 1;
                   9268: #else
                   9269:                        m_ZeroVal = 0;
                   9270: #endif
1.1.1.14  root     9271:                        maybe_idle();
1.1       root     9272:                }
                   9273:        } else {
1.1.1.33  root     9274:                UINT8 data = REG8(DL);
                   9275:                msdos_putch(data);
                   9276:                REG8(AL) = data;
1.1       root     9277:        }
                   9278: }
                   9279: 
1.1.1.35  root     9280: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     9281: {
                   9282:        REG8(AL) = msdos_getch();
1.1.1.26  root     9283:        
1.1.1.35  root     9284: #ifdef USE_SERVICE_THREAD
                   9285:        service_exit = true;
                   9286: #endif
                   9287:        return(0);
1.1       root     9288: }
                   9289: 
1.1.1.35  root     9290: inline void msdos_int_21h_07h()
                   9291: {
                   9292: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9293:        if(!in_service && !in_service_29h) {
                   9294:                start_service_loop(msdos_int_21h_07h_thread);
                   9295:        } else {
                   9296: #endif
                   9297:                msdos_int_21h_07h_thread(NULL);
                   9298:                REQUEST_HARDWRE_UPDATE();
                   9299: #ifdef USE_SERVICE_THREAD
                   9300:        }
1.1.1.35  root     9301: #endif
                   9302: }
                   9303: 
                   9304: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     9305: {
                   9306:        REG8(AL) = msdos_getch();
1.1.1.33  root     9307:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9308:        
1.1.1.35  root     9309: #ifdef USE_SERVICE_THREAD
                   9310:        service_exit = true;
                   9311: #endif
                   9312:        return(0);
                   9313: }
                   9314: 
                   9315: inline void msdos_int_21h_08h()
                   9316: {
                   9317: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9318:        if(!in_service && !in_service_29h) {
                   9319:                start_service_loop(msdos_int_21h_08h_thread);
                   9320:        } else {
                   9321: #endif
                   9322:                msdos_int_21h_08h_thread(NULL);
                   9323:                REQUEST_HARDWRE_UPDATE();
                   9324: #ifdef USE_SERVICE_THREAD
                   9325:        }
1.1.1.35  root     9326: #endif
1.1       root     9327: }
                   9328: 
                   9329: inline void msdos_int_21h_09h()
                   9330: {
1.1.1.21  root     9331:        msdos_stdio_reopen();
                   9332:        
1.1.1.20  root     9333:        process_t *process = msdos_process_info_get(current_psp);
                   9334:        int fd = msdos_psp_get_file_table(1, current_psp);
                   9335:        
1.1.1.14  root     9336:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   9337:        int len = 0;
1.1       root     9338:        
1.1.1.14  root     9339:        while(str[len] != '$' && len < 0x10000) {
                   9340:                len++;
                   9341:        }
1.1.1.20  root     9342:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9343:                // stdout is redirected to file
1.1.1.20  root     9344:                msdos_write(fd, str, len);
1.1       root     9345:        } else {
                   9346:                for(int i = 0; i < len; i++) {
1.1.1.14  root     9347:                        msdos_putch(str[i]);
1.1       root     9348:                }
                   9349:        }
1.1.1.33  root     9350:        REG8(AL) = '$';
                   9351:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9352: }
                   9353: 
1.1.1.35  root     9354: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     9355: {
1.1.1.3   root     9356:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     9357:        int max = mem[ofs] - 1;
                   9358:        UINT8 *buf = mem + ofs + 2;
                   9359:        int chr, p = 0;
                   9360:        
                   9361:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     9362:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     9363:                        p = 0;
1.1.1.33  root     9364:                        msdos_putch(0x03);
                   9365:                        msdos_putch(0x0d);
                   9366:                        msdos_putch(0x0a);
1.1.1.26  root     9367:                        break;
1.1.1.33  root     9368:                } else if(ctrl_break_pressed) {
                   9369:                        // skip this byte
1.1.1.26  root     9370:                } else if(chr == 0x00) {
1.1       root     9371:                        // skip 2nd byte
                   9372:                        msdos_getch();
                   9373:                } else if(chr == 0x08) {
                   9374:                        // back space
                   9375:                        if(p > 0) {
                   9376:                                p--;
1.1.1.20  root     9377:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     9378:                                        msdos_putch(0x08);
                   9379:                                        msdos_putch(0x08);
                   9380:                                        msdos_putch(0x20);
                   9381:                                        msdos_putch(0x20);
                   9382:                                        msdos_putch(0x08);
                   9383:                                        msdos_putch(0x08);
1.1.1.36  root     9384:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9385:                                        p--;
                   9386:                                        msdos_putch(0x08);
                   9387:                                        msdos_putch(0x08);
                   9388:                                        msdos_putch(0x20);
                   9389:                                        msdos_putch(0x20);
                   9390:                                        msdos_putch(0x08);
                   9391:                                        msdos_putch(0x08);
1.1.1.34  root     9392:                                } else {
                   9393:                                        msdos_putch(0x08);
                   9394:                                        msdos_putch(0x20);
                   9395:                                        msdos_putch(0x08);
                   9396:                                }
                   9397:                        }
                   9398:                } else if(chr == 0x1b) {
                   9399:                        // escape
                   9400:                        while(p > 0) {
                   9401:                                p--;
                   9402:                                if(msdos_ctrl_code_check(buf[p])) {
                   9403:                                        msdos_putch(0x08);
                   9404:                                        msdos_putch(0x08);
                   9405:                                        msdos_putch(0x20);
                   9406:                                        msdos_putch(0x20);
                   9407:                                        msdos_putch(0x08);
                   9408:                                        msdos_putch(0x08);
1.1.1.20  root     9409:                                } else {
1.1.1.34  root     9410:                                        msdos_putch(0x08);
                   9411:                                        msdos_putch(0x20);
                   9412:                                        msdos_putch(0x08);
1.1.1.20  root     9413:                                }
1.1       root     9414:                        }
                   9415:                } else if(p < max) {
                   9416:                        buf[p++] = chr;
                   9417:                        msdos_putch(chr);
                   9418:                }
                   9419:        }
                   9420:        buf[p] = 0x0d;
                   9421:        mem[ofs + 1] = p;
1.1.1.33  root     9422:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9423:        
1.1.1.35  root     9424: #ifdef USE_SERVICE_THREAD
                   9425:        service_exit = true;
                   9426: #endif
                   9427:        return(0);
                   9428: }
                   9429: 
                   9430: inline void msdos_int_21h_0ah()
                   9431: {
                   9432:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   9433: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9434:                if(!in_service && !in_service_29h &&
                   9435:                   *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50  root     9436:                   *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9437:                        // msdos_putch() will be used in this service
                   9438:                        // if int 29h is hooked, run this service in main thread to call int 29h
                   9439:                        start_service_loop(msdos_int_21h_0ah_thread);
                   9440:                } else {
                   9441: #endif
                   9442:                        msdos_int_21h_0ah_thread(NULL);
                   9443:                        REQUEST_HARDWRE_UPDATE();
                   9444: #ifdef USE_SERVICE_THREAD
                   9445:                }
1.1.1.35  root     9446: #endif
                   9447:        }
1.1       root     9448: }
                   9449: 
                   9450: inline void msdos_int_21h_0bh()
                   9451: {
                   9452:        if(msdos_kbhit()) {
                   9453:                REG8(AL) = 0xff;
                   9454:        } else {
                   9455:                REG8(AL) = 0x00;
1.1.1.14  root     9456:                maybe_idle();
1.1       root     9457:        }
1.1.1.33  root     9458:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9459: }
                   9460: 
                   9461: inline void msdos_int_21h_0ch()
                   9462: {
                   9463:        // clear key buffer
1.1.1.21  root     9464:        msdos_stdio_reopen();
                   9465:        
1.1.1.20  root     9466:        process_t *process = msdos_process_info_get(current_psp);
                   9467:        int fd = msdos_psp_get_file_table(0, current_psp);
                   9468:        
                   9469:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9470:                // stdin is redirected to file
                   9471:        } else {
                   9472:                while(msdos_kbhit()) {
                   9473:                        msdos_getch();
                   9474:                }
                   9475:        }
                   9476:        
                   9477:        switch(REG8(AL)) {
                   9478:        case 0x01:
                   9479:                msdos_int_21h_01h();
                   9480:                break;
                   9481:        case 0x06:
                   9482:                msdos_int_21h_06h();
                   9483:                break;
                   9484:        case 0x07:
                   9485:                msdos_int_21h_07h();
                   9486:                break;
                   9487:        case 0x08:
                   9488:                msdos_int_21h_08h();
                   9489:                break;
                   9490:        case 0x0a:
                   9491:                msdos_int_21h_0ah();
                   9492:                break;
                   9493:        default:
1.1.1.48  root     9494:                // the buffer is flushed but no input is attempted
1.1       root     9495:                break;
                   9496:        }
                   9497: }
                   9498: 
                   9499: inline void msdos_int_21h_0dh()
                   9500: {
                   9501: }
                   9502: 
                   9503: inline void msdos_int_21h_0eh()
                   9504: {
                   9505:        if(REG8(DL) < 26) {
                   9506:                _chdrive(REG8(DL) + 1);
                   9507:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9508:                msdos_sda_update(current_psp);
1.1       root     9509:        }
                   9510:        REG8(AL) = 26; // zdrive
                   9511: }
                   9512: 
1.1.1.14  root     9513: inline void msdos_int_21h_0fh()
                   9514: {
                   9515:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9516:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9517:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9518:        HANDLE hFile = CreateFile(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16  root     9519:        
1.1.1.14  root     9520:        if(hFile == INVALID_HANDLE_VALUE) {
                   9521:                REG8(AL) = 0xff;
                   9522:        } else {
                   9523:                REG8(AL) = 0;
                   9524:                fcb->current_block = 0;
                   9525:                fcb->record_size = 128;
                   9526:                fcb->file_size = GetFileSize(hFile, NULL);
                   9527:                fcb->handle = hFile;
                   9528:                fcb->cur_record = 0;
                   9529:        }
                   9530: }
                   9531: 
                   9532: inline void msdos_int_21h_10h()
                   9533: {
                   9534:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9535:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9536:        
                   9537:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9538: }
                   9539: 
1.1       root     9540: inline void msdos_int_21h_11h()
                   9541: {
1.1.1.3   root     9542:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9543:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9544:        
                   9545:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9546:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9547:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9548:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45  root     9549:        const char *path = msdos_fcb_path(fcb);
1.1       root     9550:        WIN32_FIND_DATA fd;
                   9551:        
1.1.1.13  root     9552:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9553:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9554:                FindClose(dtainfo->find_handle);
                   9555:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9556:        }
                   9557:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9558:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9559:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9560:        
1.1.1.14  root     9561:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9562:                dtainfo->allowable_mask &= ~8;
1.1       root     9563:        }
1.1.1.14  root     9564:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   9565:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9566:                      !msdos_find_file_has_8dot3name(&fd)) {
                   9567:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9568:                                FindClose(dtainfo->find_handle);
                   9569:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9570:                                break;
                   9571:                        }
                   9572:                }
                   9573:        }
1.1.1.13  root     9574:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9575:                if(ext_fcb->flag == 0xff) {
                   9576:                        ext_find->flag = 0xff;
                   9577:                        memset(ext_find->reserved, 0, 5);
                   9578:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9579:                }
                   9580:                find->drive = _getdrive();
1.1.1.13  root     9581:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9582:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9583:                find->nt_res = 0;
                   9584:                msdos_find_file_conv_local_time(&fd);
                   9585:                find->create_time_ms = 0;
                   9586:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9587:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9588:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9589:                find->cluster_hi = find->cluster_lo = 0;
                   9590:                find->file_size = fd.nFileSizeLow;
                   9591:                REG8(AL) = 0x00;
1.1.1.14  root     9592:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9593:                if(ext_fcb->flag == 0xff) {
                   9594:                        ext_find->flag = 0xff;
                   9595:                        memset(ext_find->reserved, 0, 5);
                   9596:                        ext_find->attribute = 8;
                   9597:                }
                   9598:                find->drive = _getdrive();
                   9599:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9600:                find->attribute = 8;
                   9601:                find->nt_res = 0;
                   9602:                msdos_find_file_conv_local_time(&fd);
                   9603:                find->create_time_ms = 0;
                   9604:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9605:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9606:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9607:                find->cluster_hi = find->cluster_lo = 0;
                   9608:                find->file_size = 0;
1.1.1.14  root     9609:                dtainfo->allowable_mask &= ~8;
1.1       root     9610:                REG8(AL) = 0x00;
                   9611:        } else {
                   9612:                REG8(AL) = 0xff;
                   9613:        }
                   9614: }
                   9615: 
                   9616: inline void msdos_int_21h_12h()
                   9617: {
1.1.1.3   root     9618:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     9619: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9620:        
                   9621:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9622:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9623:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9624:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9625:        WIN32_FIND_DATA fd;
                   9626:        
1.1.1.13  root     9627:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9628:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9629:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     9630:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9631:                              !msdos_find_file_has_8dot3name(&fd)) {
                   9632:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9633:                                        FindClose(dtainfo->find_handle);
                   9634:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9635:                                        break;
                   9636:                                }
                   9637:                        }
                   9638:                } else {
1.1.1.13  root     9639:                        FindClose(dtainfo->find_handle);
                   9640:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9641:                }
                   9642:        }
1.1.1.13  root     9643:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9644:                if(ext_fcb->flag == 0xff) {
                   9645:                        ext_find->flag = 0xff;
                   9646:                        memset(ext_find->reserved, 0, 5);
                   9647:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9648:                }
                   9649:                find->drive = _getdrive();
1.1.1.13  root     9650:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9651:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9652:                find->nt_res = 0;
                   9653:                msdos_find_file_conv_local_time(&fd);
                   9654:                find->create_time_ms = 0;
                   9655:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9656:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9657:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9658:                find->cluster_hi = find->cluster_lo = 0;
                   9659:                find->file_size = fd.nFileSizeLow;
                   9660:                REG8(AL) = 0x00;
1.1.1.14  root     9661:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9662:                if(ext_fcb->flag == 0xff) {
                   9663:                        ext_find->flag = 0xff;
                   9664:                        memset(ext_find->reserved, 0, 5);
                   9665:                        ext_find->attribute = 8;
                   9666:                }
                   9667:                find->drive = _getdrive();
                   9668:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9669:                find->attribute = 8;
                   9670:                find->nt_res = 0;
                   9671:                msdos_find_file_conv_local_time(&fd);
                   9672:                find->create_time_ms = 0;
                   9673:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9674:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9675:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9676:                find->cluster_hi = find->cluster_lo = 0;
                   9677:                find->file_size = 0;
1.1.1.14  root     9678:                dtainfo->allowable_mask &= ~8;
1.1       root     9679:                REG8(AL) = 0x00;
                   9680:        } else {
                   9681:                REG8(AL) = 0xff;
                   9682:        }
                   9683: }
                   9684: 
                   9685: inline void msdos_int_21h_13h()
                   9686: {
1.1.1.3   root     9687:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     9688:                REG8(AL) = 0xff;
                   9689:        } else {
                   9690:                REG8(AL) = 0x00;
                   9691:        }
                   9692: }
                   9693: 
1.1.1.16  root     9694: inline void msdos_int_21h_14h()
                   9695: {
                   9696:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9697:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9698:        process_t *process = msdos_process_info_get(current_psp);
                   9699:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9700:        DWORD num = 0;
                   9701:        
                   9702:        memset(mem + dta_laddr, 0, fcb->record_size);
                   9703:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9704:                REG8(AL) = 1;
                   9705:        } else {
                   9706:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9707:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9708:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9709:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9710:        }
                   9711: }
                   9712: 
                   9713: inline void msdos_int_21h_15h()
1.1.1.14  root     9714: {
                   9715:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9716:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     9717:        process_t *process = msdos_process_info_get(current_psp);
                   9718:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9719:        DWORD num = 0;
1.1.1.14  root     9720:        
1.1.1.16  root     9721:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9722:                REG8(AL) = 1;
                   9723:        } else {
                   9724:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9725:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9726:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9727:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9728:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9729:        }
                   9730: }
                   9731: 
                   9732: inline void msdos_int_21h_16h()
                   9733: {
                   9734:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9735:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9736:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9737:        HANDLE hFile = CreateFile(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     9738:        
1.1.1.14  root     9739:        if(hFile == INVALID_HANDLE_VALUE) {
                   9740:                REG8(AL) = 0xff;
                   9741:        } else {
                   9742:                REG8(AL) = 0;
                   9743:                fcb->current_block = 0;
                   9744:                fcb->record_size = 128;
                   9745:                fcb->file_size = 0;
                   9746:                fcb->handle = hFile;
                   9747:                fcb->cur_record = 0;
                   9748:        }
                   9749: }
                   9750: 
1.1.1.16  root     9751: inline void msdos_int_21h_17h()
                   9752: {
                   9753:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9754:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45  root     9755: //     const char *path_src = msdos_fcb_path(fcb_src);
                   9756:        char path_src[MAX_PATH];
                   9757:        strcpy(path_src, msdos_fcb_path(fcb_src));
                   9758:        
1.1.1.16  root     9759:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   9760:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45  root     9761: //     const char *path_dst = msdos_fcb_path(fcb_dst);
                   9762:        char path_dst[MAX_PATH];
                   9763:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     9764:        
                   9765:        if(rename(path_src, path_dst)) {
                   9766:                REG8(AL) = 0xff;
                   9767:        } else {
                   9768:                REG8(AL) = 0;
                   9769:        }
                   9770: }
                   9771: 
1.1       root     9772: inline void msdos_int_21h_18h()
                   9773: {
                   9774:        REG8(AL) = 0x00;
                   9775: }
                   9776: 
                   9777: inline void msdos_int_21h_19h()
                   9778: {
                   9779:        REG8(AL) = _getdrive() - 1;
                   9780: }
                   9781: 
                   9782: inline void msdos_int_21h_1ah()
                   9783: {
                   9784:        process_t *process = msdos_process_info_get(current_psp);
                   9785:        
                   9786:        process->dta.w.l = REG16(DX);
1.1.1.3   root     9787:        process->dta.w.h = SREG(DS);
1.1.1.23  root     9788:        msdos_sda_update(current_psp);
1.1       root     9789: }
                   9790: 
                   9791: inline void msdos_int_21h_1bh()
                   9792: {
                   9793:        int drive_num = _getdrive() - 1;
                   9794:        UINT16 seg, ofs;
                   9795:        
                   9796:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9797:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9798:                REG8(AL) = dpb->highest_sector_num + 1;
                   9799:                REG16(CX) = dpb->bytes_per_sector;
                   9800:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9801:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9802:        } else {
                   9803:                REG8(AL) = 0xff;
1.1.1.3   root     9804:                m_CF = 1;
1.1       root     9805:        }
                   9806: 
                   9807: }
                   9808: 
                   9809: inline void msdos_int_21h_1ch()
                   9810: {
1.1.1.41  root     9811:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     9812:        UINT16 seg, ofs;
                   9813:        
                   9814:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9815:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9816:                REG8(AL) = dpb->highest_sector_num + 1;
                   9817:                REG16(CX) = dpb->bytes_per_sector;
                   9818:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9819:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9820:        } else {
                   9821:                REG8(AL) = 0xff;
1.1.1.3   root     9822:                m_CF = 1;
1.1       root     9823:        }
                   9824: 
                   9825: }
                   9826: 
                   9827: inline void msdos_int_21h_1dh()
                   9828: {
                   9829:        REG8(AL) = 0;
                   9830: }
                   9831: 
                   9832: inline void msdos_int_21h_1eh()
                   9833: {
                   9834:        REG8(AL) = 0;
                   9835: }
                   9836: 
                   9837: inline void msdos_int_21h_1fh()
                   9838: {
                   9839:        int drive_num = _getdrive() - 1;
                   9840:        UINT16 seg, ofs;
                   9841:        
                   9842:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9843:                REG8(AL) = 0;
1.1.1.3   root     9844:                SREG(DS) = seg;
                   9845:                i386_load_segment_descriptor(DS);
1.1       root     9846:                REG16(BX) = ofs;
                   9847:        } else {
                   9848:                REG8(AL) = 0xff;
1.1.1.3   root     9849:                m_CF = 1;
1.1       root     9850:        }
                   9851: }
                   9852: 
                   9853: inline void msdos_int_21h_20h()
                   9854: {
                   9855:        REG8(AL) = 0;
                   9856: }
                   9857: 
1.1.1.14  root     9858: inline void msdos_int_21h_21h()
                   9859: {
                   9860:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9861:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9862:        
                   9863:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9864:                REG8(AL) = 1;
                   9865:        } else {
                   9866:                process_t *process = msdos_process_info_get(current_psp);
                   9867:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9868:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     9869:                DWORD num = 0;
1.1.1.14  root     9870:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9871:                        REG8(AL) = 1;
                   9872:                } else {
                   9873:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9874:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9875:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     9876:                }
                   9877:        }
                   9878: }
                   9879: 
                   9880: inline void msdos_int_21h_22h()
                   9881: {
                   9882:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9883:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9884:        
                   9885:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9886:                REG8(AL) = 0xff;
                   9887:        } else {
                   9888:                process_t *process = msdos_process_info_get(current_psp);
                   9889:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     9890:                DWORD num = 0;
1.1.1.14  root     9891:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   9892:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9893:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9894:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9895:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     9896:        }
                   9897: }
                   9898: 
1.1.1.16  root     9899: inline void msdos_int_21h_23h()
                   9900: {
                   9901:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9902:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9903:        const char *path = msdos_fcb_path(fcb);
1.1.1.16  root     9904:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9905:        
                   9906:        if(hFile == INVALID_HANDLE_VALUE) {
                   9907:                REG8(AL) = 0xff;
                   9908:        } else {
                   9909:                UINT32 size = GetFileSize(hFile, NULL);
                   9910:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   9911:                REG8(AL) = 0;
                   9912:        }
                   9913: }
                   9914: 
                   9915: inline void msdos_int_21h_24h()
                   9916: {
                   9917:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9918:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9919:        
                   9920:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   9921: }
                   9922: 
1.1       root     9923: inline void msdos_int_21h_25h()
                   9924: {
                   9925:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     9926:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     9927: }
                   9928: 
                   9929: inline void msdos_int_21h_26h()
                   9930: {
                   9931:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   9932:        
                   9933:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
1.1.1.48  root     9934:        psp->first_mcb = REG16(DX) + (PSP_SIZE >> 4);
1.1       root     9935:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   9936:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   9937:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   9938:        psp->parent_psp = 0;
                   9939: }
                   9940: 
1.1.1.16  root     9941: inline void msdos_int_21h_27h()
                   9942: {
                   9943:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9944:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9945:        
                   9946:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9947:                REG8(AL) = 1;
                   9948:        } else {
                   9949:                process_t *process = msdos_process_info_get(current_psp);
                   9950:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9951:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   9952:                DWORD num = 0;
                   9953:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   9954:                        REG8(AL) = 1;
                   9955:                } else {
                   9956:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9957:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9958:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9959:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9960:                }
                   9961:        }
                   9962: }
                   9963: 
                   9964: inline void msdos_int_21h_28h()
                   9965: {
                   9966:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9967:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9968:        
                   9969:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9970:                REG8(AL) = 0xff;
                   9971:        } else {
                   9972:                process_t *process = msdos_process_info_get(current_psp);
                   9973:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9974:                DWORD num = 0;
                   9975:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   9976:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9977:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9978:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9979:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9980:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9981:        }
                   9982: }
                   9983: 
1.1       root     9984: inline void msdos_int_21h_29h()
                   9985: {
1.1.1.20  root     9986:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   9987:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     9988:        UINT8 drv = 0;
                   9989:        char sep_chars[] = ":.;,=+";
                   9990:        char end_chars[] = "\\<>|/\"[]";
                   9991:        char spc_chars[] = " \t";
                   9992:        
1.1.1.20  root     9993:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   9994:        buffer[1023] = 0;
                   9995:        memset(name, 0x20, sizeof(name));
                   9996:        memset(ext, 0x20, sizeof(ext));
                   9997:        
1.1       root     9998:        if(REG8(AL) & 1) {
1.1.1.20  root     9999:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   10000:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     10001:                        ofs++;
                   10002:                }
                   10003:        }
1.1.1.20  root     10004:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     10005:        
1.1.1.24  root     10006:        if(buffer[ofs + 1] == ':') {
                   10007:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   10008:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     10009:                        ofs += 2;
1.1.1.24  root     10010:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10011:                                ofs++;
                   10012:                        }
                   10013:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   10014:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     10015:                        ofs += 2;
1.1.1.24  root     10016:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10017:                                ofs++;
                   10018:                        }
1.1       root     10019:                }
                   10020:        }
1.1.1.20  root     10021:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10022:                UINT8 c = buffer[ofs];
                   10023:                if(is_kanji) {
                   10024:                        is_kanji = 0;
                   10025:                } else if(msdos_lead_byte_check(c)) {
                   10026:                        is_kanji = 1;
                   10027:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10028:                        break;
                   10029:                } else if(c >= 'a' && c <= 'z') {
                   10030:                        c -= 0x20;
                   10031:                }
                   10032:                ofs++;
                   10033:                name[i] = c;
                   10034:        }
1.1.1.20  root     10035:        if(buffer[ofs] == '.') {
1.1       root     10036:                ofs++;
1.1.1.20  root     10037:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10038:                        UINT8 c = buffer[ofs];
                   10039:                        if(is_kanji) {
                   10040:                                is_kanji = 0;
                   10041:                        } else if(msdos_lead_byte_check(c)) {
                   10042:                                is_kanji = 1;
                   10043:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10044:                                break;
                   10045:                        } else if(c >= 'a' && c <= 'z') {
                   10046:                                c -= 0x20;
                   10047:                        }
                   10048:                        ofs++;
                   10049:                        ext[i] = c;
                   10050:                }
                   10051:        }
1.1.1.20  root     10052:        int si = REG16(SI) + ofs;
1.1.1.3   root     10053:        int ds = SREG(DS);
1.1       root     10054:        while(si > 0xffff) {
                   10055:                si -= 0x10;
                   10056:                ds++;
                   10057:        }
                   10058:        REG16(SI) = si;
1.1.1.3   root     10059:        SREG(DS) = ds;
                   10060:        i386_load_segment_descriptor(DS);
1.1       root     10061:        
1.1.1.3   root     10062:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     10063:        if(!(REG8(AL) & 2) || drv != 0) {
                   10064:                fcb[0] = drv;
                   10065:        }
                   10066:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   10067:                memcpy(fcb + 1, name, 8);
                   10068:        }
                   10069:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   10070:                memcpy(fcb + 9, ext, 3);
                   10071:        }
                   10072:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     10073:                if(fcb[i] == '*') {
                   10074:                        found_star = 1;
                   10075:                }
                   10076:                if(found_star) {
                   10077:                        fcb[i] = '?';
                   10078:                }
                   10079:        }
1.1.1.20  root     10080:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     10081:                if(fcb[i] == '*') {
                   10082:                        found_star = 1;
                   10083:                }
                   10084:                if(found_star) {
                   10085:                        fcb[i] = '?';
                   10086:                }
                   10087:        }
                   10088:        
1.1.1.44  root     10089:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     10090:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   10091:                        REG8(AL) = 0x01;
1.1.1.20  root     10092:                } else {
                   10093:                        REG8(AL) = 0x00;
1.1       root     10094:                }
                   10095:        } else {
                   10096:                REG8(AL) = 0xff;
                   10097:        }
                   10098: }
                   10099: 
                   10100: inline void msdos_int_21h_2ah()
                   10101: {
                   10102:        SYSTEMTIME sTime;
                   10103:        
                   10104:        GetLocalTime(&sTime);
                   10105:        REG16(CX) = sTime.wYear;
                   10106:        REG8(DH) = (UINT8)sTime.wMonth;
                   10107:        REG8(DL) = (UINT8)sTime.wDay;
                   10108:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   10109: }
                   10110: 
                   10111: inline void msdos_int_21h_2bh()
                   10112: {
1.1.1.14  root     10113:        REG8(AL) = 0xff;
1.1       root     10114: }
                   10115: 
                   10116: inline void msdos_int_21h_2ch()
                   10117: {
                   10118:        SYSTEMTIME sTime;
                   10119:        
                   10120:        GetLocalTime(&sTime);
                   10121:        REG8(CH) = (UINT8)sTime.wHour;
                   10122:        REG8(CL) = (UINT8)sTime.wMinute;
                   10123:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     10124:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     10125: }
                   10126: 
                   10127: inline void msdos_int_21h_2dh()
                   10128: {
                   10129:        REG8(AL) = 0x00;
                   10130: }
                   10131: 
                   10132: inline void msdos_int_21h_2eh()
                   10133: {
                   10134:        process_t *process = msdos_process_info_get(current_psp);
                   10135:        
                   10136:        process->verify = REG8(AL);
                   10137: }
                   10138: 
                   10139: inline void msdos_int_21h_2fh()
                   10140: {
                   10141:        process_t *process = msdos_process_info_get(current_psp);
                   10142:        
                   10143:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     10144:        SREG(ES) = process->dta.w.h;
                   10145:        i386_load_segment_descriptor(ES);
1.1       root     10146: }
                   10147: 
                   10148: inline void msdos_int_21h_30h()
                   10149: {
                   10150:        // Version Flag / OEM
1.1.1.27  root     10151:        if(REG8(AL) == 0x01) {
1.1.1.29  root     10152: #ifdef SUPPORT_HMA
                   10153:                REG16(BX) = 0x0000;
                   10154: #else
                   10155:                REG16(BX) = 0x1000; // DOS is in HMA
                   10156: #endif
1.1       root     10157:        } else {
1.1.1.27  root     10158:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   10159:                // but this is not correct on Windows 98 SE
                   10160: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   10161:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     10162:        }
1.1.1.27  root     10163:        REG16(CX) = 0x0000;
1.1.1.30  root     10164:        REG8(AL) = dos_major_version;   // 7
                   10165:        REG8(AH) = dos_minor_version;   // 10
1.1       root     10166: }
                   10167: 
                   10168: inline void msdos_int_21h_31h()
                   10169: {
1.1.1.29  root     10170:        try {
                   10171:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10172:        } catch(...) {
                   10173:                // recover the broken mcb
                   10174:                int mcb_seg = current_psp - 1;
                   10175:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     10176:                
1.1.1.29  root     10177:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     10178:                        mcb->mz = 'M';
                   10179:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   10180:                        
1.1.1.29  root     10181:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     10182:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     10183:                        } else {
1.1.1.39  root     10184:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     10185:                        }
                   10186:                } else {
                   10187:                        mcb->mz = 'Z';
1.1.1.30  root     10188:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     10189:                }
                   10190:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10191:        }
1.1       root     10192:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   10193: }
                   10194: 
                   10195: inline void msdos_int_21h_32h()
                   10196: {
                   10197:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   10198:        UINT16 seg, ofs;
                   10199:        
                   10200:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10201:                REG8(AL) = 0;
1.1.1.3   root     10202:                SREG(DS) = seg;
                   10203:                i386_load_segment_descriptor(DS);
1.1       root     10204:                REG16(BX) = ofs;
                   10205:        } else {
                   10206:                REG8(AL) = 0xff;
1.1.1.3   root     10207:                m_CF = 1;
1.1       root     10208:        }
                   10209: }
                   10210: 
                   10211: inline void msdos_int_21h_33h()
                   10212: {
                   10213:        char path[MAX_PATH];
1.1.1.48  root     10214:        char drive = 3; // C:
1.1       root     10215:        
                   10216:        switch(REG8(AL)) {
                   10217:        case 0x00:
1.1.1.33  root     10218:                REG8(DL) = ctrl_break_checking;
1.1       root     10219:                break;
                   10220:        case 0x01:
1.1.1.33  root     10221:                ctrl_break_checking = REG8(DL);
                   10222:                break;
                   10223:        case 0x02:
                   10224:                {
                   10225:                        UINT8 old = ctrl_break_checking;
                   10226:                        ctrl_break_checking = REG8(DL);
                   10227:                        REG8(DL) = old;
                   10228:                }
                   10229:                break;
                   10230:        case 0x03:
                   10231:        case 0x04:
                   10232:                // DOS 4.0+ - Unused
1.1       root     10233:                break;
                   10234:        case 0x05:
1.1.1.48  root     10235:                if(GetSystemDirectory(path, MAX_PATH) != 0) {
                   10236:                        if(path[0] >= 'a' && path[0] <= 'z') {
                   10237:                                drive = path[0] - 'a' + 1;
                   10238:                        } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   10239:                                drive = path[0] - 'A' + 1;
                   10240:                        }
1.1       root     10241:                }
1.1.1.48  root     10242:                REG8(DL) = (UINT8)drive;
1.1       root     10243:                break;
                   10244:        case 0x06:
1.1.1.2   root     10245:                // MS-DOS version (7.10)
1.1       root     10246:                REG8(BL) = 7;
1.1.1.2   root     10247:                REG8(BH) = 10;
1.1       root     10248:                REG8(DL) = 0;
1.1.1.29  root     10249: #ifdef SUPPORT_HMA
                   10250:                REG8(DH) = 0x00;
                   10251: #else
                   10252:                REG8(DH) = 0x10; // DOS is in HMA
                   10253: #endif
1.1       root     10254:                break;
1.1.1.6   root     10255:        case 0x07:
                   10256:                if(REG8(DL) == 0) {
                   10257:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   10258:                } else if(REG8(DL) == 1) {
                   10259:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   10260:                }
                   10261:                break;
1.1       root     10262:        default:
1.1.1.22  root     10263:                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     10264: //             REG16(AX) = 0x01;
                   10265: //             m_CF = 1;
                   10266:                REG8(AL) = 0xff;
1.1       root     10267:                break;
                   10268:        }
                   10269: }
                   10270: 
1.1.1.23  root     10271: inline void msdos_int_21h_34h()
                   10272: {
                   10273:        SREG(ES) = SDA_TOP >> 4;
                   10274:        i386_load_segment_descriptor(ES);
                   10275:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   10276: }
                   10277: 
1.1       root     10278: inline void msdos_int_21h_35h()
                   10279: {
                   10280:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     10281:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   10282:        i386_load_segment_descriptor(ES);
1.1       root     10283: }
                   10284: 
                   10285: inline void msdos_int_21h_36h()
                   10286: {
                   10287:        struct _diskfree_t df = {0};
                   10288:        
                   10289:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   10290:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   10291:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     10292:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   10293:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     10294:        } else {
                   10295:                REG16(AX) = 0xffff;
                   10296:        }
                   10297: }
                   10298: 
                   10299: inline void msdos_int_21h_37h()
                   10300: {
1.1.1.22  root     10301:        static UINT8 dev_flag = 0xff;
1.1       root     10302:        
                   10303:        switch(REG8(AL)) {
                   10304:        case 0x00:
1.1.1.22  root     10305:                {
                   10306:                        process_t *process = msdos_process_info_get(current_psp);
                   10307:                        REG8(AL) = 0x00;
                   10308:                        REG8(DL) = process->switchar;
                   10309:                }
1.1       root     10310:                break;
                   10311:        case 0x01:
1.1.1.22  root     10312:                {
                   10313:                        process_t *process = msdos_process_info_get(current_psp);
                   10314:                        REG8(AL) = 0x00;
                   10315:                        process->switchar = REG8(DL);
1.1.1.23  root     10316:                        msdos_sda_update(current_psp);
1.1.1.22  root     10317:                }
                   10318:                break;
                   10319:        case 0x02:
                   10320:                REG8(DL) = dev_flag;
                   10321:                break;
                   10322:        case 0x03:
                   10323:                dev_flag = REG8(DL);
                   10324:                break;
                   10325:        case 0xd0:
                   10326:        case 0xd1:
                   10327:        case 0xd2:
                   10328:        case 0xd3:
                   10329:        case 0xd4:
                   10330:        case 0xd5:
                   10331:        case 0xd6:
                   10332:        case 0xd7:
                   10333:        case 0xdc:
                   10334:        case 0xdd:
                   10335:        case 0xde:
                   10336:        case 0xdf:
1.1.1.48  root     10337:                // DIET v1.43e
                   10338: //             REG16(AX) = 1;
                   10339:                REG8(AL) = 0xff;
1.1       root     10340:                break;
                   10341:        default:
1.1.1.22  root     10342:                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     10343: //             REG16(AX) = 1;
                   10344:                REG8(AL) = 0xff;
1.1       root     10345:                break;
                   10346:        }
                   10347: }
                   10348: 
1.1.1.52  root     10349: int get_country_info(country_info_t *ci, LCID locale = LOCALE_USER_DEFAULT)
1.1.1.17  root     10350: {
                   10351:        char LCdata[80];
                   10352:        
1.1.1.19  root     10353:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.42  root     10354:        GetLocaleInfo(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     10355:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.42  root     10356:        GetLocaleInfo(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10357:        ci->currency_format = *LCdata - '0';
1.1.1.42  root     10358:        GetLocaleInfo(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10359:        ci->date_format = *LCdata - '0';
1.1.1.42  root     10360:        GetLocaleInfo(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10361:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.42  root     10362:        GetLocaleInfo(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10363:        *ci->date_sep = *LCdata;
1.1.1.42  root     10364:        GetLocaleInfo(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     10365:        *ci->dec_sep = *LCdata;
1.1.1.42  root     10366:        GetLocaleInfo(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     10367:        *ci->list_sep = *LCdata;
1.1.1.42  root     10368:        GetLocaleInfo(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     10369:        *ci->thou_sep = *LCdata;
1.1.1.42  root     10370:        GetLocaleInfo(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     10371:        *ci->time_sep = *LCdata;
1.1.1.42  root     10372:        GetLocaleInfo(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     10373:        if(strchr(LCdata, 'H') != NULL) {
                   10374:                ci->time_format = 1;
                   10375:        }
1.1.1.49  root     10376:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffc:000a
                   10377:        ci->case_map.w.h = DUMMY_TOP >> 4;
1.1.1.42  root     10378:        GetLocaleInfo(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     10379:        return atoi(LCdata);
                   10380: }
                   10381: 
1.1.1.42  root     10382: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   10383: {
                   10384:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   10385: }
                   10386: 
1.1.1.43  root     10387: void set_country_info(country_info_t *ci, int size)
                   10388: {
                   10389:        char LCdata[80];
                   10390:        
                   10391:        if(size >= 0x00 + 2) {
                   10392:                memset(LCdata, 0, sizeof(LCdata));
                   10393:                *LCdata = '0' + ci->date_format;
                   10394:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
                   10395:        }
                   10396:        if(size >= 0x02 + 5) {
                   10397:                memset(LCdata, 0, sizeof(LCdata));
                   10398:                memcpy(LCdata, &ci->currency_symbol, 4);
                   10399:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
                   10400:        }
                   10401:        if(size >= 0x07 + 2) {
                   10402:                memset(LCdata, 0, sizeof(LCdata));
                   10403:                *LCdata = *ci->thou_sep;
                   10404:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
                   10405:        }
                   10406:        if(size >= 0x09 + 2) {
                   10407:                memset(LCdata, 0, sizeof(LCdata));
                   10408:                *LCdata = *ci->dec_sep;
                   10409:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
                   10410:        }
                   10411:        if(size >= 0x0b + 2) {
                   10412:                memset(LCdata, 0, sizeof(LCdata));
                   10413:                *LCdata = *ci->date_sep;
                   10414:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
                   10415:        }
                   10416:        if(size >= 0x0d + 2) {
                   10417:                memset(LCdata, 0, sizeof(LCdata));
                   10418:                *LCdata = *ci->time_sep;
                   10419:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
                   10420:        }
                   10421:        if(size >= 0x0f + 1) {
                   10422:                memset(LCdata, 0, sizeof(LCdata));
                   10423:                *LCdata = '0' + ci->currency_format;
                   10424:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
                   10425:        }
                   10426:        if(size >= 0x10 + 1) {
                   10427:                sprintf(LCdata, "%d", ci->currency_dec_digits);
                   10428:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
                   10429:        }
                   10430:        if(size >= 0x11 + 1) {
                   10431:                // FIXME: is time format always H/h:mm:ss ???
                   10432:                if(ci->time_format & 1) {
                   10433:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   10434:                } else {
                   10435:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   10436:                }
                   10437:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
                   10438:        }
                   10439:        if(size >= 0x12 + 4) {
                   10440:                // 12h  DWORD   address of case map routine
                   10441:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   10442:        }
                   10443:        if(size >= 0x16 + 2) {
                   10444:                memset(LCdata, 0, sizeof(LCdata));
                   10445:                *LCdata = *ci->list_sep;
                   10446:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
                   10447:        }
                   10448: }
                   10449: 
1.1.1.42  root     10450: #ifndef SUBLANG_SWAHILI
                   10451:        #define SUBLANG_SWAHILI 0x01
                   10452: #endif
                   10453: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10454:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10455: #endif
                   10456: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10457:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10458: #endif
                   10459: #ifndef LANG_BANGLA
                   10460:        #define LANG_BANGLA 0x45
                   10461: #endif
                   10462: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10463:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10464: #endif
                   10465: 
                   10466: static const struct {
                   10467:        int code;
                   10468:        USHORT usPrimaryLanguage;
                   10469:        USHORT usSubLanguage;
                   10470: } country_table[] = {
                   10471:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10472:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10473:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10474:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10475:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10476:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10477: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10478: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10479: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10480: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10481: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10482:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10483:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10484: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10485:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10486: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10487:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10488:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10489:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10490:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10491:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10492:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10493:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10494: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10495: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10496:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10497:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10498:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10499:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10500:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10501: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10502: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10503: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10504:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10505: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10506: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10507: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10508: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10509:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10510:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10511:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10512: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10513:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10514:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10515:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10516:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10517:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10518:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10519:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10520:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10521:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10522:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10523:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10524:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10525:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10526: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10527:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10528:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10529:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10530:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10531:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10532:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10533:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10534:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10535:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10536:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10537: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10538:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10539: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10540:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10541:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10542:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10543:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10544:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10545:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10546:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10547:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10548: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10549:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10550: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10551: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10552:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   10553: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   10554:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   10555:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   10556:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   10557:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   10558:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   10559:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   10560:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   10561: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   10562: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   10563:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   10564: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   10565:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   10566:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   10567:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   10568:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   10569: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   10570: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   10571: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   10572:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   10573:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   10574:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   10575:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   10576:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   10577:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   10578:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   10579:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   10580:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   10581:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   10582:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   10583:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   10584:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   10585:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   10586:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   10587:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   10588:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   10589:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   10590:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   10591:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   10592:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     10593: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     10594:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   10595: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   10596:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   10597:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   10598:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   10599:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   10600:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   10601:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   10602:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   10603:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   10604:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   10605:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   10606:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   10607:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   10608:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   10609:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   10610:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   10611:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   10612:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   10613:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   10614:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   10615:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   10616:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   10617:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   10618:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   10619:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   10620:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   10621: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   10622:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   10623:        {-1, 0, 0},
                   10624: };
                   10625: 
1.1.1.14  root     10626: inline void msdos_int_21h_38h()
                   10627: {
                   10628:        switch(REG8(AL)) {
                   10629:        case 0x00:
1.1.1.19  root     10630:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     10631:                break;
                   10632:        default:
1.1.1.42  root     10633:                for(int i = 0;; i++) {
                   10634:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   10635:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   10636:                                break;
                   10637:                        } else if(country_table[i].code == -1) {
                   10638: //                             unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   10639: //                             REG16(AX) = 2;
                   10640: //                             m_CF = 1;
1.1.1.48  root     10641:                                // get current coutry info
1.1.1.42  root     10642:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   10643:                                break;
                   10644:                        }
                   10645:                }
1.1.1.14  root     10646:                break;
                   10647:        }
                   10648: }
                   10649: 
1.1       root     10650: inline void msdos_int_21h_39h(int lfn)
                   10651: {
1.1.1.3   root     10652:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10653:                REG16(AX) = errno;
1.1.1.3   root     10654:                m_CF = 1;
1.1       root     10655:        }
                   10656: }
                   10657: 
                   10658: inline void msdos_int_21h_3ah(int lfn)
                   10659: {
1.1.1.3   root     10660:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10661:                REG16(AX) = errno;
1.1.1.3   root     10662:                m_CF = 1;
1.1       root     10663:        }
                   10664: }
                   10665: 
                   10666: inline void msdos_int_21h_3bh(int lfn)
                   10667: {
1.1.1.45  root     10668:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     10669:        
                   10670:        if(_chdir(path)) {
1.1.1.17  root     10671:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     10672:                m_CF = 1;
1.1.1.44  root     10673:        } else {
                   10674:                int drv = _getdrive() - 1;
                   10675:                if(path[1] == ':') {
                   10676:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   10677:                                drv = path[0] - 'A';
                   10678:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   10679:                                drv = path[0] - 'a';
                   10680:                        }
                   10681:                }
                   10682:                msdos_cds_update(drv, path);
1.1       root     10683:        }
                   10684: }
                   10685: 
                   10686: inline void msdos_int_21h_3ch()
                   10687: {
1.1.1.45  root     10688:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10689:        int attr = GetFileAttributes(path);
1.1.1.37  root     10690:        int fd = -1;
                   10691:        int sio_port = 0;
                   10692:        int lpt_port = 0;
1.1       root     10693:        
1.1.1.45  root     10694:        if(msdos_is_device_path(path)) {
                   10695:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     10696:        } else {
                   10697:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10698:        }
                   10699:        if(fd != -1) {
                   10700:                if(attr == -1) {
                   10701:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   10702:                }
                   10703:                SetFileAttributes(path, attr);
                   10704:                REG16(AX) = fd;
1.1.1.45  root     10705:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10706:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10707:        } else {
                   10708:                REG16(AX) = errno;
1.1.1.3   root     10709:                m_CF = 1;
1.1       root     10710:        }
                   10711: }
                   10712: 
                   10713: inline void msdos_int_21h_3dh()
                   10714: {
1.1.1.45  root     10715:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10716:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     10717:        int fd = -1;
                   10718:        int sio_port = 0;
                   10719:        int lpt_port = 0;
1.1       root     10720:        
                   10721:        if(mode < 0x03) {
1.1.1.45  root     10722:                if(msdos_is_device_path(path)) {
                   10723:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     10724:                } else {
1.1.1.13  root     10725:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     10726:                }
1.1       root     10727:                if(fd != -1) {
                   10728:                        REG16(AX) = fd;
1.1.1.45  root     10729:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10730:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10731:                } else {
                   10732:                        REG16(AX) = errno;
1.1.1.3   root     10733:                        m_CF = 1;
1.1       root     10734:                }
                   10735:        } else {
                   10736:                REG16(AX) = 0x0c;
1.1.1.3   root     10737:                m_CF = 1;
1.1       root     10738:        }
                   10739: }
                   10740: 
                   10741: inline void msdos_int_21h_3eh()
                   10742: {
                   10743:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10744:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10745:        
1.1.1.20  root     10746:        if(fd < process->max_files && file_handler[fd].valid) {
                   10747:                _close(fd);
                   10748:                msdos_file_handler_close(fd);
                   10749:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     10750:        } else {
                   10751:                REG16(AX) = 0x06;
1.1.1.3   root     10752:                m_CF = 1;
1.1       root     10753:        }
                   10754: }
                   10755: 
1.1.1.35  root     10756: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   10757: {
                   10758:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   10759:        int max = REG16(CX);
                   10760:        int p = 0;
                   10761:        
                   10762:        while(max > p) {
                   10763:                int chr = msdos_getch();
                   10764:                
                   10765:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   10766:                        p = 0;
                   10767:                        buf[p++] = 0x0d;
                   10768:                        if(max > p) {
                   10769:                                buf[p++] = 0x0a;
                   10770:                        }
                   10771:                        msdos_putch(0x03);
                   10772:                        msdos_putch(0x0d);
                   10773:                        msdos_putch(0x0a);
                   10774:                        break;
                   10775:                } else if(ctrl_break_pressed) {
                   10776:                        // skip this byte
                   10777:                } else if(chr == 0x00) {
                   10778:                        // skip 2nd byte
                   10779:                        msdos_getch();
                   10780:                } else if(chr == 0x0d) {
                   10781:                        // carriage return
                   10782:                        buf[p++] = 0x0d;
                   10783:                        if(max > p) {
                   10784:                                buf[p++] = 0x0a;
                   10785:                        }
                   10786:                        msdos_putch('\n');
                   10787:                        break;
                   10788:                } else if(chr == 0x08) {
                   10789:                        // back space
                   10790:                        if(p > 0) {
                   10791:                                p--;
                   10792:                                if(msdos_ctrl_code_check(buf[p])) {
                   10793:                                        msdos_putch(0x08);
                   10794:                                        msdos_putch(0x08);
                   10795:                                        msdos_putch(0x20);
                   10796:                                        msdos_putch(0x20);
                   10797:                                        msdos_putch(0x08);
                   10798:                                        msdos_putch(0x08);
1.1.1.36  root     10799:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   10800:                                        p--;
                   10801:                                        msdos_putch(0x08);
                   10802:                                        msdos_putch(0x08);
                   10803:                                        msdos_putch(0x20);
                   10804:                                        msdos_putch(0x20);
                   10805:                                        msdos_putch(0x08);
                   10806:                                        msdos_putch(0x08);
1.1.1.35  root     10807:                                } else {
                   10808:                                        msdos_putch(0x08);
                   10809:                                        msdos_putch(0x20);
                   10810:                                        msdos_putch(0x08);
                   10811:                                }
                   10812:                        }
                   10813:                } else if(chr == 0x1b) {
                   10814:                        // escape
                   10815:                        while(p > 0) {
                   10816:                                p--;
                   10817:                                if(msdos_ctrl_code_check(buf[p])) {
                   10818:                                        msdos_putch(0x08);
                   10819:                                        msdos_putch(0x08);
                   10820:                                        msdos_putch(0x20);
                   10821:                                        msdos_putch(0x20);
                   10822:                                        msdos_putch(0x08);
                   10823:                                        msdos_putch(0x08);
                   10824:                                } else {
                   10825:                                        msdos_putch(0x08);
                   10826:                                        msdos_putch(0x20);
                   10827:                                        msdos_putch(0x08);
                   10828:                                }
                   10829:                        }
                   10830:                } else {
                   10831:                        buf[p++] = chr;
                   10832:                        msdos_putch(chr);
                   10833:                }
                   10834:        }
                   10835:        REG16(AX) = p;
                   10836:        
                   10837: #ifdef USE_SERVICE_THREAD
                   10838:        service_exit = true;
                   10839: #endif
                   10840:        return(0);
                   10841: }
                   10842: 
1.1       root     10843: inline void msdos_int_21h_3fh()
                   10844: {
                   10845:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10846:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10847:        
1.1.1.20  root     10848:        if(fd < process->max_files && file_handler[fd].valid) {
                   10849:                if(file_mode[file_handler[fd].mode].in) {
                   10850:                        if(file_handler[fd].atty) {
1.1       root     10851:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     10852:                                if(REG16(CX) != 0) {
                   10853: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     10854:                                        if(!in_service && !in_service_29h &&
                   10855:                                           *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50  root     10856:                                           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   10857:                                                // msdos_putch() will be used in this service
                   10858:                                                // if int 29h is hooked, run this service in main thread to call int 29h
                   10859:                                                start_service_loop(msdos_int_21h_3fh_thread);
                   10860:                                        } else {
                   10861: #endif
                   10862:                                                msdos_int_21h_3fh_thread(NULL);
                   10863:                                                REQUEST_HARDWRE_UPDATE();
                   10864: #ifdef USE_SERVICE_THREAD
                   10865:                                        }
1.1.1.35  root     10866: #endif
                   10867:                                } else {
                   10868:                                        REG16(AX) = 0;
1.1       root     10869:                                }
                   10870:                        } else {
1.1.1.37  root     10871:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10872:                        }
                   10873:                } else {
                   10874:                        REG16(AX) = 0x05;
1.1.1.3   root     10875:                        m_CF = 1;
1.1       root     10876:                }
                   10877:        } else {
                   10878:                REG16(AX) = 0x06;
1.1.1.3   root     10879:                m_CF = 1;
1.1       root     10880:        }
                   10881: }
                   10882: 
                   10883: inline void msdos_int_21h_40h()
                   10884: {
                   10885:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10886:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10887:        
1.1.1.20  root     10888:        if(fd < process->max_files && file_handler[fd].valid) {
                   10889:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     10890:                        if(REG16(CX)) {
1.1.1.20  root     10891:                                if(file_handler[fd].atty) {
1.1       root     10892:                                        // BX is stdout/stderr or is redirected to stdout
                   10893:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     10894:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     10895:                                        }
                   10896:                                        REG16(AX) = REG16(CX);
                   10897:                                } else {
1.1.1.20  root     10898:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10899:                                }
                   10900:                        } else {
1.1.1.20  root     10901:                                UINT32 pos = _tell(fd);
                   10902:                                _lseek(fd, 0, SEEK_END);
                   10903:                                UINT32 size = _tell(fd);
1.1.1.12  root     10904:                                if(pos < size) {
1.1.1.20  root     10905:                                        _lseek(fd, pos, SEEK_SET);
                   10906:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     10907:                                } else {
                   10908:                                        for(UINT32 i = size; i < pos; i++) {
                   10909:                                                UINT8 tmp = 0;
1.1.1.23  root     10910:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     10911:                                        }
1.1.1.20  root     10912:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     10913:                                }
1.1.1.23  root     10914:                                REG16(AX) = 0;
1.1       root     10915:                        }
                   10916:                } else {
                   10917:                        REG16(AX) = 0x05;
1.1.1.3   root     10918:                        m_CF = 1;
1.1       root     10919:                }
                   10920:        } else {
                   10921:                REG16(AX) = 0x06;
1.1.1.3   root     10922:                m_CF = 1;
1.1       root     10923:        }
                   10924: }
                   10925: 
                   10926: inline void msdos_int_21h_41h(int lfn)
                   10927: {
1.1.1.3   root     10928:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10929:                REG16(AX) = errno;
1.1.1.3   root     10930:                m_CF = 1;
1.1       root     10931:        }
                   10932: }
                   10933: 
                   10934: inline void msdos_int_21h_42h()
                   10935: {
                   10936:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10937:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10938:        
1.1.1.20  root     10939:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     10940:                if(REG8(AL) < 0x03) {
1.1.1.35  root     10941:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     10942:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   10943:                        UINT32 pos = _tell(fd);
1.1       root     10944:                        REG16(AX) = pos & 0xffff;
                   10945:                        REG16(DX) = (pos >> 16);
                   10946:                } else {
                   10947:                        REG16(AX) = 0x01;
1.1.1.3   root     10948:                        m_CF = 1;
1.1       root     10949:                }
                   10950:        } else {
                   10951:                REG16(AX) = 0x06;
1.1.1.3   root     10952:                m_CF = 1;
1.1       root     10953:        }
                   10954: }
                   10955: 
                   10956: inline void msdos_int_21h_43h(int lfn)
                   10957: {
1.1.1.45  root     10958:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     10959:        int attr;
                   10960:        
1.1.1.14  root     10961:        if(!lfn && REG8(AL) > 2) {
                   10962:                REG16(AX) = 0x01;
                   10963:                m_CF = 1;
                   10964:                return;
                   10965:        }
                   10966:        switch(REG8(lfn ? BL : AL)) {
1.1       root     10967:        case 0x00:
                   10968:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     10969:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   10970:                } else {
                   10971:                        REG16(AX) = (UINT16)GetLastError();
                   10972:                        m_CF = 1;
                   10973:                }
                   10974:                break;
                   10975:        case 0x01:
                   10976:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   10977:                        REG16(AX) = (UINT16)GetLastError();
                   10978:                        m_CF = 1;
                   10979:                }
                   10980:                break;
                   10981:        case 0x02:
                   10982:                {
1.1.1.45  root     10983:                        DWORD compressed_size = GetCompressedFileSize(path, NULL), file_size = 0;
                   10984:                        if(compressed_size != INVALID_FILE_SIZE) {
                   10985:                                if(compressed_size != 0) {
                   10986:                                        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10987:                                        if(hFile != INVALID_HANDLE_VALUE) {
                   10988:                                                file_size = GetFileSize(hFile, NULL);
                   10989:                                                CloseHandle(hFile);
                   10990:                                        }
                   10991:                                        if(compressed_size == file_size) {
                   10992:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   10993:                                                // this isn't correct if the file is in the NTFS MFT
                   10994:                                                if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   10995:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   10996:                                                }
1.1.1.14  root     10997:                                        }
                   10998:                                }
1.1.1.45  root     10999:                                REG16(AX) = LOWORD(compressed_size);
                   11000:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     11001:                        } else {
                   11002:                                REG16(AX) = (UINT16)GetLastError();
                   11003:                                m_CF = 1;
1.1       root     11004:                        }
1.1.1.14  root     11005:                }
                   11006:                break;
                   11007:        case 0x03:
                   11008:        case 0x05:
                   11009:        case 0x07:
1.1.1.48  root     11010:                if(lfn) {
1.1.1.14  root     11011:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   11012:                        if(hFile != INVALID_HANDLE_VALUE) {
                   11013:                                FILETIME local, time;
                   11014:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   11015:                                if(REG8(BL) == 7) {
                   11016:                                        ULARGE_INTEGER hund;
                   11017:                                        hund.LowPart = local.dwLowDateTime;
                   11018:                                        hund.HighPart = local.dwHighDateTime;
                   11019:                                        hund.QuadPart += REG16(SI) * 100000;
                   11020:                                        local.dwLowDateTime = hund.LowPart;
                   11021:                                        local.dwHighDateTime = hund.HighPart;
                   11022:                                }
                   11023:                                LocalFileTimeToFileTime(&local, &time);
                   11024:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   11025:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   11026:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   11027:                                        REG16(AX) = (UINT16)GetLastError();
                   11028:                                        m_CF = 1;
                   11029:                                }
                   11030:                                CloseHandle(hFile);
                   11031:                        } else {
                   11032:                                REG16(AX) = (UINT16)GetLastError();
                   11033:                                m_CF = 1;
1.1       root     11034:                        }
1.1.1.48  root     11035:                } else {
                   11036:                        // 214303 DR DOS 3.41+ internal - Set Access Rights And Password
                   11037:                        // 214305 DR DOS 5.0-6.0 internal - Set Extended File Attributes
                   11038:                        // 214307 DR DOS 6.0 - Set File Owner
                   11039: //                     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));
                   11040:                        REG16(AX) = 0x01;
                   11041:                        m_CF = 1;
1.1.1.14  root     11042:                }
                   11043:                break;
                   11044:        case 0x04:
                   11045:        case 0x06:
                   11046:        case 0x08:
1.1.1.48  root     11047:                if(lfn) {
1.1.1.14  root     11048:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   11049:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   11050:                                FILETIME *time, local;
                   11051:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   11052:                                                   0x06 ? &fad.ftLastAccessTime :
                   11053:                                                          &fad.ftCreationTime;
                   11054:                                FileTimeToLocalFileTime(time, &local);
                   11055:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   11056:                                if(REG8(BL) == 0x08) {
                   11057:                                        ULARGE_INTEGER hund;
                   11058:                                        hund.LowPart = local.dwLowDateTime;
                   11059:                                        hund.HighPart = local.dwHighDateTime;
                   11060:                                        hund.QuadPart /= 100000;
                   11061:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   11062:                                }
                   11063:                        } else {
                   11064:                                REG16(AX) = (UINT16)GetLastError();
                   11065:                                m_CF = 1;
1.1       root     11066:                        }
1.1.1.48  root     11067:                } else {
                   11068:                        // 214304 DR DOS 5.0-6.0 internal - Get Encrypted Password
                   11069:                        // 214306 DR DOS 6.0 - Get File Owner
                   11070: //                     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));
                   11071:                        REG16(AX) = 0x01;
                   11072:                        m_CF = 1;
1.1.1.14  root     11073:                }
                   11074:                break;
1.1.1.43  root     11075:        case 0xff:
1.1.1.48  root     11076:                if(!lfn && REG16(BP) == 0x5053) {
1.1.1.43  root     11077:                        if(REG8(CL) == 0x39) {
                   11078:                                msdos_int_21h_39h(1);
                   11079:                                break;
                   11080:                        } else if(REG8(CL) == 0x56) {
                   11081:                                msdos_int_21h_56h(1);
                   11082:                                break;
                   11083:                        }
                   11084:                }
1.1.1.14  root     11085:        default:
1.1.1.22  root     11086:                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     11087:                REG16(AX) = lfn ? 0x7100 : 0x01;
1.1.1.14  root     11088:                m_CF = 1;
                   11089:                break;
                   11090:        }
                   11091: }
                   11092: 
                   11093: inline void msdos_int_21h_44h()
                   11094: {
1.1.1.22  root     11095:        static UINT16 iteration_count = 0;
                   11096:        
1.1.1.44  root     11097:        process_t *process;
                   11098:        int fd, drv;
1.1.1.14  root     11099:        
                   11100:        switch(REG8(AL)) {
                   11101:        case 0x00:
                   11102:        case 0x01:
                   11103:        case 0x02:
                   11104:        case 0x03:
                   11105:        case 0x04:
                   11106:        case 0x05:
                   11107:        case 0x06:
                   11108:        case 0x07:
1.1.1.44  root     11109:                process = msdos_process_info_get(current_psp);
                   11110:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     11111:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   11112:                        REG16(AX) = 0x06;
                   11113:                        m_CF = 1;
                   11114:                        return;
1.1.1.14  root     11115:                }
                   11116:                break;
                   11117:        case 0x08:
                   11118:        case 0x09:
1.1.1.44  root     11119:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11120:                if(!msdos_is_valid_drive(drv)) {
                   11121:                        // invalid drive
1.1.1.14  root     11122:                        REG16(AX) = 0x0f;
                   11123:                        m_CF = 1;
                   11124:                        return;
1.1       root     11125:                }
                   11126:                break;
                   11127:        }
                   11128:        switch(REG8(AL)) {
1.1.1.48  root     11129:        case 0x00: // Get Device Information
1.1.1.20  root     11130:                REG16(DX) = file_handler[fd].info;
1.1       root     11131:                break;
1.1.1.48  root     11132:        case 0x01: // Set Device Information
1.1.1.45  root     11133:                if(REG8(DH) != 0) {
                   11134: //                     REG16(AX) = 0x0d; // data invalid
                   11135: //                     m_CF = 1;
                   11136:                        file_handler[fd].info = REG16(DX);
                   11137:                } else {
                   11138:                        file_handler[fd].info &= 0xff00;
                   11139:                        file_handler[fd].info |= REG8(DL);
                   11140:                }
1.1       root     11141:                break;
1.1.1.48  root     11142:        case 0x02: // Read From Character Device Control Channel
1.1.1.45  root     11143:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
                   11144:                        // from DOSBox
                   11145:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
                   11146:                        case 0x00:
                   11147:                                if(REG16(CX) >= 6) {
                   11148:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
                   11149:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
                   11150:                                        REG16(AX) = 6; // number of bytes actually read
                   11151:                                } else {
                   11152:                                        REG16(AX) = 0x0d; // data invalid
                   11153:                                        m_CF = 1;
                   11154:                                }
                   11155:                                break;
                   11156:                        case 0x01:
                   11157:                                if(REG16(CX) >= 6) {
                   11158:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
                   11159:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
                   11160:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
                   11161:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
                   11162:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
                   11163:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
                   11164:                                                        int page = (addr - EMS_TOP) / 0x4000;
                   11165:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
                   11166:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11167:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
                   11168:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
                   11169:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
                   11170:                                                } else {
                   11171:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
                   11172:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11173:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
                   11174:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
                   11175:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
                   11176:                                                }
                   11177:                                        }
                   11178:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
                   11179:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
                   11180:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
                   11181:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
                   11182:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
                   11183:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
                   11184:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
                   11185:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
                   11186:                                        
                   11187:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
                   11188:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
                   11189:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
                   11190:                                        REG16(AX) = 6; // number of bytes actually read
                   11191:                                } else {
                   11192:                                        REG16(AX) = 0x0d; // data invalid
                   11193:                                        m_CF = 1;
                   11194:                                }
                   11195:                                break;
                   11196:                        case 0x02:
                   11197:                                if(REG16(CX) >= 2) {
                   11198:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
                   11199:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
                   11200:                                        REG16(AX) = 2; // number of bytes actually read
                   11201:                                } else {
                   11202:                                        REG16(AX) = 0x0d; // data invalid
                   11203:                                        m_CF = 1;
                   11204:                                }
                   11205:                                break;
                   11206:                        case 0x03:
                   11207:                                if(REG16(CX) >= 4) {
                   11208:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
                   11209:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
                   11210:                                        REG16(AX) = 4; // number of bytes actually read
                   11211:                                } else {
                   11212:                                        REG16(AX) = 0x0d; // data invalid
                   11213:                                        m_CF = 1;
                   11214:                                }
                   11215:                                break;
                   11216:                        default:
                   11217:                                REG16(AX) = 0x01; // function number invalid
                   11218:                                m_CF = 1;
                   11219:                        }
                   11220:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
                   11221:                        if(REG16(CX) >= 5) {
                   11222:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
                   11223:                                REG16(AX) = 5; // number of bytes actually read
                   11224:                        } else {
                   11225:                                REG16(AX) = 0x0d; // data invalid
                   11226:                                m_CF = 1;
                   11227:                        }
                   11228:                } else {
                   11229: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
                   11230: //                     REG16(AX) = REG16(CX);
                   11231:                        REG16(AX) = 0x05; // access denied
                   11232:                        m_CF = 1;
                   11233:                }
                   11234:                break;
1.1.1.48  root     11235:        case 0x03: // Write To Character Device Control Channel
1.1.1.45  root     11236: //             REG16(AX) = 0x05;
                   11237: //             m_CF = 1;
                   11238:                REG16(AX) = 0x00; // success
                   11239:                break;
1.1.1.48  root     11240:        case 0x04: // Read From Block Device Control Channel
                   11241:        case 0x05: // Write To Block Device Control Channel
1.1       root     11242:                REG16(AX) = 0x05;
1.1.1.3   root     11243:                m_CF = 1;
1.1       root     11244:                break;
1.1.1.48  root     11245:        case 0x06: // Get Input Status
1.1.1.20  root     11246:                if(file_mode[file_handler[fd].mode].in) {
                   11247:                        if(file_handler[fd].atty) {
1.1.1.14  root     11248:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     11249:                        } else {
1.1.1.20  root     11250:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     11251:                        }
1.1.1.14  root     11252:                } else {
                   11253:                        REG8(AL) = 0x00;
1.1       root     11254:                }
                   11255:                break;
1.1.1.48  root     11256:        case 0x07: // Get Output Status
1.1.1.20  root     11257:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     11258:                        REG8(AL) = 0xff;
                   11259:                } else {
                   11260:                        REG8(AL) = 0x00;
1.1       root     11261:                }
                   11262:                break;
1.1.1.48  root     11263:        case 0x08: // Check If Block Device Removable
1.1.1.44  root     11264:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     11265:                        // removable drive
                   11266:                        REG16(AX) = 0x00;
1.1       root     11267:                } else {
1.1.1.14  root     11268:                        // fixed drive
                   11269:                        REG16(AX) = 0x01;
1.1       root     11270:                }
                   11271:                break;
1.1.1.48  root     11272:        case 0x09: // Check If Block Device Remote
1.1.1.44  root     11273:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     11274:                        // remote drive
                   11275:                        REG16(DX) = 0x1000;
1.1.1.44  root     11276:                } else if(msdos_is_subst_drive(drv)) {
                   11277:                        // subst drive
                   11278:                        REG16(DX) = 0x8000;
1.1       root     11279:                } else {
1.1.1.14  root     11280:                        // local drive
1.1.1.44  root     11281:                        REG16(DX) = 0x0000;
1.1       root     11282:                }
                   11283:                break;
1.1.1.48  root     11284:        case 0x0a: // Check If Handle Is Remote
1.1.1.45  root     11285:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
                   11286:                        REG16(DX) = 0x8000;
                   11287:                } else {
                   11288:                        REG16(DX) = 0x0000;
                   11289:                }
1.1.1.21  root     11290:                break;
1.1.1.48  root     11291:        case 0x0b: // Set Sharing Retry Count
1.1       root     11292:                break;
1.1.1.48  root     11293:        case 0x0c: // Generic Character Device Request
1.1.1.22  root     11294:                if(REG8(CL) == 0x45) {
                   11295:                        // set iteration (retry) count
                   11296:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   11297:                } else if(REG8(CL) == 0x4a) {
                   11298:                        // select code page
                   11299:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   11300:                        msdos_nls_tables_update();
1.1.1.44  root     11301:                } else if(REG8(CL) == 0x4c) {
                   11302:                        // start code-page preparation
                   11303:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11304:                        int count = 1, offset = 0;
                   11305:                        if(active_code_page != 437) {
                   11306:                                ids[count++] = active_code_page;
                   11307:                        }
                   11308:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11309:                                ids[count++] = system_code_page;
                   11310:                        }
                   11311:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   11312:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   11313:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11314:                        for(int i = 0; i < count; i++) {
                   11315:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11316:                        }
                   11317:                } else if(REG8(CL) == 0x4d) {
                   11318:                        // end code-page preparation
                   11319:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   11320:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.50  root     11321:                } else if(REG8(CL) == 0x5f) {
                   11322:                        // set display information
                   11323:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   11324:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   11325:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   11326:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   11327:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   11328:                                
                   11329:                                if(cur_width != new_width || cur_height != new_height) {
                   11330:                                        pcbios_set_console_size(new_width, new_height, true);
                   11331:                                }
                   11332:                        }
1.1.1.22  root     11333:                } else if(REG8(CL) == 0x65) {
                   11334:                        // get iteration (retry) count
                   11335:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   11336:                } else if(REG8(CL) == 0x6a) {
                   11337:                        // query selected code page
                   11338:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   11339:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   11340:                        
                   11341:                        CPINFO info;
                   11342:                        GetCPInfo(active_code_page, &info);
                   11343:                        
                   11344:                        if(info.MaxCharSize != 1) {
                   11345:                                for(int i = 0;; i++) {
                   11346:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   11347:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   11348:                                        
                   11349:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   11350:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   11351:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   11352:                                        
                   11353:                                        if(lo == 0 && hi == 0) {
                   11354:                                                break;
                   11355:                                        }
                   11356:                                }
                   11357:                        }
1.1.1.44  root     11358:                } else if(REG8(CL) == 0x6b) {
                   11359:                        // query prepare list
                   11360:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11361:                        int count = 1, offset = 0;
                   11362:                        if(active_code_page != 437) {
                   11363:                                ids[count++] = active_code_page;
                   11364:                        }
                   11365:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11366:                                ids[count++] = system_code_page;
                   11367:                        }
                   11368:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   11369:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11370:                        for(int i = 0; i < count; i++) {
                   11371:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11372:                        }
                   11373:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11374:                        for(int i = 0; i < count; i++) {
                   11375:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11376:                        }
1.1.1.22  root     11377:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     11378:                        // get display information
1.1.1.50  root     11379:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   11380:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   11381:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   11382:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   11383:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   11384:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   11385:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   11386:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   11387:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   11388:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   11389:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.22  root     11390:                } else {
                   11391:                        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));
                   11392:                        REG16(AX) = 0x01; // invalid function
                   11393:                        m_CF = 1;
                   11394:                }
                   11395:                break;
1.1.1.48  root     11396:        case 0x0d: // Generic Block Device Request
1.1.1.22  root     11397:                if(REG8(CL) == 0x40) {
                   11398:                        // set device parameters
1.1.1.48  root     11399: //             } else if(REG8(CL) == 0x41) {
                   11400: //                     // write logical device track
                   11401: //             } else if(REG8(CL) == 0x42) {
                   11402: //                     // format and verify logical device track
1.1.1.22  root     11403:                } else if(REG8(CL) == 0x46) {
                   11404:                        // set volume serial number
1.1.1.48  root     11405:                } else if(REG8(CL) == 0x47) {
                   11406:                        // set access flag
                   11407: //             } else if(REG8(CL) == 0x48) {
                   11408: //                     // set media lock state
                   11409: //             } else if(REG8(CL) == 0x49) {
                   11410: //                     // eject media in drive
1.1.1.22  root     11411:                } else if(REG8(CL) == 0x4a) {
                   11412:                        // lock logical volume
                   11413:                } else if(REG8(CL) == 0x4b) {
                   11414:                        // lock physical volume
                   11415:                } else if(REG8(CL) == 0x60) {
                   11416:                        // get device parameters
1.1.1.42  root     11417:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11418:                        
1.1.1.42  root     11419:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11420:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11421:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11422:                                
                   11423:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   11424:                                switch(geo->MediaType) {
                   11425:                                case F5_360_512:
                   11426:                                case F5_320_512:
                   11427:                                case F5_320_1024:
                   11428:                                case F5_180_512:
                   11429:                                case F5_160_512:
                   11430:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   11431:                                        break;
                   11432:                                case F5_1Pt2_512:
                   11433:                                case F3_1Pt2_512:
                   11434:                                case F3_1Pt23_1024:
                   11435:                                case F5_1Pt23_1024:
                   11436:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   11437:                                        break;
                   11438:                                case F3_720_512:
                   11439:                                case F3_640_512:
                   11440:                                case F5_640_512:
                   11441:                                case F5_720_512:
                   11442:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   11443:                                        break;
                   11444:                                case F8_256_128:
                   11445:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   11446:                                        break;
                   11447:                                case FixedMedia:
                   11448:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11449:                                        break;
                   11450:                                case F3_1Pt44_512:
                   11451:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11452:                                        break;
                   11453:                                case F3_2Pt88_512:
                   11454:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   11455:                                        break;
                   11456:                                default:
                   11457:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11458: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11459:                                        break;
1.1.1.22  root     11460:                                }
1.1.1.42  root     11461:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   11462:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   11463:                                switch(geo->MediaType) {
                   11464:                                case F5_360_512:
                   11465:                                case F5_320_512:
                   11466:                                case F5_320_1024:
                   11467:                                case F5_180_512:
                   11468:                                case F5_160_512:
                   11469:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   11470:                                        break;
                   11471:                                default:
                   11472:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   11473:                                        break;
                   11474:                                }
                   11475:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   11476:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   11477:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   11478:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   11479:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   11480:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   11481:                                switch(geo->MediaType) {
                   11482:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   11483:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   11484:                                        break;
                   11485:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   11486:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   11487:                                        break;
                   11488:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   11489:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11490:                                        break;
                   11491:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11492:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11493:                                        break;
                   11494:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11495:                                case F3_1Pt2_512:
                   11496:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11497:                                case F5_720_512:
                   11498:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11499:                                        break;
                   11500:                                case FixedMedia:        // hard disk
                   11501:                                case RemovableMedia:
                   11502:                                case Unknown:
                   11503:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11504:                                        break;
                   11505:                                default:
                   11506:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11507:                                        break;
                   11508:                                }
                   11509:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11510:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11511:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11512:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11513:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11514:                                // 21h  BYTE    device type
                   11515:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11516:                        } else {
                   11517:                                REG16(AX) = 0x0f; // invalid drive
                   11518:                                m_CF = 1;
                   11519:                        }
1.1.1.48  root     11520: //             } else if(REG8(CL) == 0x61) {
                   11521: //                     // read logical device track
                   11522: //             } else if(REG8(CL) == 0x62) {
                   11523: //                     // verify logical device track
1.1.1.22  root     11524:                } else if(REG8(CL) == 0x66) {
                   11525:                        // get volume serial number
                   11526:                        char path[] = "A:\\";
                   11527:                        char volume_label[MAX_PATH];
                   11528:                        DWORD serial_number = 0;
                   11529:                        char file_system[MAX_PATH];
                   11530:                        
                   11531:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11532:                        
                   11533:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   11534:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11535:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11536:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11537:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11538:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11539:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11540:                        } else {
                   11541:                                REG16(AX) = 0x0f; // invalid drive
                   11542:                                m_CF = 1;
                   11543:                        }
                   11544:                } else if(REG8(CL) == 0x67) {
                   11545:                        // get access flag
                   11546:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11547:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11548:                } else if(REG8(CL) == 0x68) {
                   11549:                        // sense media type
1.1.1.42  root     11550:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11551:                        
1.1.1.42  root     11552:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11553:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11554:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11555:                                
                   11556:                                switch(geo->MediaType) {
                   11557:                                case F3_720_512:
                   11558:                                case F5_720_512:
                   11559:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11560:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   11561:                                        break;
                   11562:                                case F3_1Pt44_512:
                   11563:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11564:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   11565:                                        break;
                   11566:                                case F3_2Pt88_512:
                   11567:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11568:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   11569:                                        break;
                   11570:                                default:
                   11571:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   11572:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   11573:                                        break;
1.1.1.22  root     11574:                                }
                   11575:                        } else {
                   11576:                                REG16(AX) = 0x0f; // invalid drive
                   11577:                                m_CF = 1;
                   11578:                        }
                   11579:                } else if(REG8(CL) == 0x6a) {
                   11580:                        // unlock logical volume
                   11581:                } else if(REG8(CL) == 0x6b) {
                   11582:                        // unlock physical volume
1.1.1.48  root     11583: //             } else if(REG8(CL) == 0x6c) {
                   11584: //                     // get lock flag
                   11585: //             } else if(REG8(CL) == 0x6d) {
                   11586: //                     // enumerate open files
                   11587: //             } else if(REG8(CL) == 0x6e) {
                   11588: //                     // find swap file
                   11589: //             } else if(REG8(CL) == 0x6f) {
                   11590: //                     // get drive map information
                   11591: //             } else if(REG8(CL) == 0x70) {
                   11592: //                     // get current lock state
                   11593: //             } else if(REG8(CL) == 0x71) {
                   11594: //                     // get first cluster
1.1.1.22  root     11595:                } else {
                   11596:                        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));
                   11597:                        REG16(AX) = 0x01; // invalid function
                   11598:                        m_CF = 1;
                   11599:                }
                   11600:                break;
1.1.1.48  root     11601:        case 0x0e: // Get Lgical Drive Map
1.1.1.44  root     11602:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11603:                        REG16(AX) = 0x0f; // invalid drive
                   11604:                        m_CF = 1;
                   11605:                } else {
                   11606:                        REG8(AL) = 0;
1.1.1.22  root     11607:                }
                   11608:                break;
1.1.1.48  root     11609:        case 0x0f: // Set Logical Drive Map
1.1.1.44  root     11610:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11611:                        REG16(AX) = 0x0f; // invalid drive
                   11612:                        m_CF = 1;
1.1.1.22  root     11613:                }
                   11614:                break;
1.1.1.48  root     11615:        case 0x10: // Query Generic IOCTRL Capability (Handle)
1.1.1.22  root     11616:                switch(REG8(CL)) {
                   11617:                case 0x45:
                   11618:                case 0x4a:
1.1.1.48  root     11619:                case 0x4c:
                   11620:                case 0x4d:
1.1.1.22  root     11621:                case 0x65:
                   11622:                case 0x6a:
1.1.1.48  root     11623:                case 0x6b:
1.1.1.22  root     11624:                case 0x7f:
                   11625:                        REG16(AX) = 0x0000; // supported
                   11626:                        break;
                   11627:                default:
                   11628:                        REG8(AL) = 0x01; // ioctl capability not available
                   11629:                        m_CF = 1;
                   11630:                        break;
                   11631:                }
                   11632:                break;
1.1.1.48  root     11633:        case 0x11: // Query Generic IOCTRL Capability (Drive)
1.1.1.22  root     11634:                switch(REG8(CL)) {
                   11635:                case 0x40:
                   11636:                case 0x46:
                   11637:                case 0x4a:
                   11638:                case 0x4b:
                   11639:                case 0x60:
                   11640:                case 0x66:
                   11641:                case 0x67:
                   11642:                case 0x68:
                   11643:                case 0x6a:
                   11644:                case 0x6b:
1.1.1.48  root     11645:                        if(REG8(CH) == 0x00 || REG8(CH) == 0x01 || REG8(CH) == 0x03 || REG8(CH) == 0x05) {
                   11646:                                // CH = 00h     Unknown
                   11647:                                // CH = 01h     COMn:
                   11648:                                // CH = 03h     CON
                   11649:                                // CH = 05h     LPTn:
                   11650:                                REG16(AX) = 0x0000; // supported
                   11651:                                break;
                   11652:                        }
1.1.1.22  root     11653:                default:
                   11654:                        REG8(AL) = 0x01; // ioctl capability not available
                   11655:                        m_CF = 1;
                   11656:                        break;
                   11657:                }
                   11658:                break;
1.1.1.48  root     11659:        case 0x12: // DR DOS 5.0-6.0 - Determine DOS Type
                   11660:        case 0x14: // DR DOS 5.0-6.0 - Set Global Password
                   11661:        case 0x16: // DR DOS 5.0-6.0 - History Buffer, Share, And Hiload Control
                   11662:        case 0x51: // Concurrent DOS v3.2+ - Installation Check
                   11663:        case 0x52: // DR DOS 3.41+ - Determine DOS tTpe/Get DR DOS Version
                   11664:        case 0x54: // DR DOS 3.41+ - Set Global Password
                   11665:        case 0x56: // DR DOS 5.0+ - History Buffer Control
                   11666:        case 0x57: // DR DOS 5.0-6.0 - Share/Hiload Control
                   11667:        case 0x58: // DR DOS 5.0+ internal - Get Pointer To Internal Variable Table
                   11668:        case 0x59: // DR Multiuser DOS 5.0 - API
                   11669:                REG16(AX) = 0x01; // this is not DR-DOS
1.1.1.22  root     11670:                m_CF = 1;
                   11671:                break;
1.1       root     11672:        default:
1.1.1.22  root     11673:                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     11674:                REG16(AX) = 0x01;
1.1.1.3   root     11675:                m_CF = 1;
1.1       root     11676:                break;
                   11677:        }
                   11678: }
                   11679: 
                   11680: inline void msdos_int_21h_45h()
                   11681: {
                   11682:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11683:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11684:        
1.1.1.20  root     11685:        if(fd < process->max_files && file_handler[fd].valid) {
                   11686:                int dup_fd = _dup(fd);
                   11687:                if(dup_fd != -1) {
                   11688:                        REG16(AX) = dup_fd;
                   11689:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11690: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11691:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11692:                } else {
                   11693:                        REG16(AX) = errno;
1.1.1.3   root     11694:                        m_CF = 1;
1.1       root     11695:                }
                   11696:        } else {
                   11697:                REG16(AX) = 0x06;
1.1.1.3   root     11698:                m_CF = 1;
1.1       root     11699:        }
                   11700: }
                   11701: 
                   11702: inline void msdos_int_21h_46h()
                   11703: {
                   11704:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11705:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11706:        int dup_fd = REG16(CX);
                   11707:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     11708:        
1.1.1.20  root     11709:        if(REG16(BX) == REG16(CX)) {
                   11710:                REG16(AX) = 0x06;
                   11711:                m_CF = 1;
                   11712:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   11713:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   11714:                        _close(tmp_fd);
                   11715:                        msdos_file_handler_close(tmp_fd);
                   11716:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   11717:                }
                   11718:                if(_dup2(fd, dup_fd) != -1) {
                   11719:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11720: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11721:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11722:                } else {
                   11723:                        REG16(AX) = errno;
1.1.1.3   root     11724:                        m_CF = 1;
1.1       root     11725:                }
                   11726:        } else {
                   11727:                REG16(AX) = 0x06;
1.1.1.3   root     11728:                m_CF = 1;
1.1       root     11729:        }
                   11730: }
                   11731: 
                   11732: inline void msdos_int_21h_47h(int lfn)
                   11733: {
                   11734:        char path[MAX_PATH];
                   11735:        
                   11736:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45  root     11737:                if(!lfn) {
                   11738:                        strcpy(path, msdos_short_path(path));
                   11739:                }
1.1       root     11740:                if(path[1] == ':') {
                   11741:                        // the returned path does not include a drive or the initial backslash
1.1.1.45  root     11742:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     11743:                } else {
1.1.1.45  root     11744:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     11745:                }
                   11746:        } else {
                   11747:                REG16(AX) = errno;
1.1.1.3   root     11748:                m_CF = 1;
1.1       root     11749:        }
                   11750: }
                   11751: 
                   11752: inline void msdos_int_21h_48h()
                   11753: {
1.1.1.19  root     11754:        int seg, umb_linked;
1.1       root     11755:        
1.1.1.8   root     11756:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     11757:                // unlink umb not to allocate memory in umb
                   11758:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   11759:                        msdos_mem_unlink_umb();
                   11760:                }
1.1.1.8   root     11761:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11762:                        REG16(AX) = seg;
                   11763:                } else {
                   11764:                        REG16(AX) = 0x08;
                   11765:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   11766:                        m_CF = 1;
                   11767:                }
1.1.1.19  root     11768:                if(umb_linked != 0) {
                   11769:                        msdos_mem_link_umb();
                   11770:                }
1.1.1.8   root     11771:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   11772:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11773:                        REG16(AX) = seg;
                   11774:                } else {
                   11775:                        REG16(AX) = 0x08;
                   11776:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   11777:                        m_CF = 1;
                   11778:                }
                   11779:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   11780:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11781:                        REG16(AX) = seg;
                   11782:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11783:                        REG16(AX) = seg;
                   11784:                } else {
                   11785:                        REG16(AX) = 0x08;
                   11786:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   11787:                        m_CF = 1;
                   11788:                }
1.1       root     11789:        }
                   11790: }
                   11791: 
                   11792: inline void msdos_int_21h_49h()
                   11793: {
1.1.1.14  root     11794:        int mcb_seg = SREG(ES) - 1;
                   11795:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   11796:        
                   11797:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11798:                msdos_mem_free(SREG(ES));
                   11799:        } else {
1.1.1.33  root     11800:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     11801:                m_CF = 1;
                   11802:        }
1.1       root     11803: }
                   11804: 
                   11805: inline void msdos_int_21h_4ah()
                   11806: {
1.1.1.14  root     11807:        int mcb_seg = SREG(ES) - 1;
                   11808:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     11809:        int max_paragraphs;
                   11810:        
1.1.1.14  root     11811:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11812:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   11813:                        REG16(AX) = 0x08;
                   11814:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   11815:                        m_CF = 1;
                   11816:                }
                   11817:        } else {
1.1.1.33  root     11818:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     11819:                m_CF = 1;
1.1       root     11820:        }
                   11821: }
                   11822: 
                   11823: inline void msdos_int_21h_4bh()
                   11824: {
1.1.1.3   root     11825:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11826:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     11827:        
                   11828:        switch(REG8(AL)) {
                   11829:        case 0x00:
                   11830:        case 0x01:
                   11831:                if(msdos_process_exec(command, param, REG8(AL))) {
                   11832:                        REG16(AX) = 0x02;
1.1.1.3   root     11833:                        m_CF = 1;
1.1       root     11834:                }
                   11835:                break;
1.1.1.14  root     11836:        case 0x03:
                   11837:                {
                   11838:                        int fd;
                   11839:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   11840:                                REG16(AX) = 0x02;
                   11841:                                m_CF = 1;
                   11842:                                break;
                   11843:                        }
                   11844:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   11845:                        _close(fd);
                   11846:                        
                   11847:                        UINT16 *overlay = (UINT16 *)param;
                   11848:                        
                   11849:                        // check exe header
                   11850:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   11851:                        int header_size = 0;
                   11852:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   11853:                                header_size = header->header_size * 16;
                   11854:                                // relocation
                   11855:                                int start_seg = overlay[1];
                   11856:                                for(int i = 0; i < header->relocations; i++) {
                   11857:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   11858:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   11859:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   11860:                                }
                   11861:                        }
                   11862:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   11863:                }
                   11864:                break;
1.1.1.48  root     11865:        case 0x04:
                   11866:                // Load And Execute In Background (European MS-DOS 4.0 only)
                   11867: //     case 0x05:
                   11868: //             // DOS 5+ - Set Execution State
                   11869:        case 0x80:
                   11870:                // DR DOS v3.41 - Run Already-Loaded Kernel File
                   11871:        case 0xf0:
                   11872:        case 0xf1:
                   11873:                // DIET v1.10+
1.1.1.43  root     11874:        case 0xfd:
                   11875:        case 0xfe:
                   11876:                // unknown function called in FreeCOM
                   11877:                REG16(AX) = 0x01;
                   11878:                m_CF = 1;
                   11879:                break;
1.1       root     11880:        default:
1.1.1.22  root     11881:                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     11882:                REG16(AX) = 0x01;
1.1.1.3   root     11883:                m_CF = 1;
1.1       root     11884:                break;
                   11885:        }
                   11886: }
                   11887: 
                   11888: inline void msdos_int_21h_4ch()
                   11889: {
                   11890:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   11891: }
                   11892: 
                   11893: inline void msdos_int_21h_4dh()
                   11894: {
                   11895:        REG16(AX) = retval;
                   11896: }
                   11897: 
                   11898: inline void msdos_int_21h_4eh()
                   11899: {
                   11900:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11901:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11902:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45  root     11903:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11904:        WIN32_FIND_DATA fd;
                   11905:        
1.1.1.14  root     11906:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   11907:        find->find_magic = FIND_MAGIC;
                   11908:        find->dta_index = dtainfo - dtalist;
1.1       root     11909:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     11910:        dtainfo->allowable_mask = REG8(CL);
                   11911:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     11912:        
1.1.1.14  root     11913:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   11914:                dtainfo->allowable_mask &= ~8;
1.1       root     11915:        }
1.1.1.14  root     11916:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   11917:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11918:                      !msdos_find_file_has_8dot3name(&fd)) {
                   11919:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11920:                                FindClose(dtainfo->find_handle);
                   11921:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11922:                                break;
                   11923:                        }
                   11924:                }
                   11925:        }
1.1.1.13  root     11926:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11927:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11928:                msdos_find_file_conv_local_time(&fd);
                   11929:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11930:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11931:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11932:                REG16(AX) = 0;
1.1.1.14  root     11933:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11934:                find->attrib = 8;
                   11935:                find->size = 0;
                   11936:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11937:                dtainfo->allowable_mask &= ~8;
1.1       root     11938:                REG16(AX) = 0;
                   11939:        } else {
                   11940:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     11941:                m_CF = 1;
1.1       root     11942:        }
                   11943: }
                   11944: 
                   11945: inline void msdos_int_21h_4fh()
                   11946: {
                   11947:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11948:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11949:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     11950:        WIN32_FIND_DATA fd;
                   11951:        
1.1.1.14  root     11952:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   11953:                REG16(AX) = 0x12;
                   11954:                m_CF = 1;
                   11955:                return;
                   11956:        }
                   11957:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     11958:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11959:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     11960:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11961:                              !msdos_find_file_has_8dot3name(&fd)) {
                   11962:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11963:                                        FindClose(dtainfo->find_handle);
                   11964:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11965:                                        break;
                   11966:                                }
                   11967:                        }
                   11968:                } else {
1.1.1.13  root     11969:                        FindClose(dtainfo->find_handle);
                   11970:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11971:                }
                   11972:        }
1.1.1.13  root     11973:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11974:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11975:                msdos_find_file_conv_local_time(&fd);
                   11976:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11977:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11978:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11979:                REG16(AX) = 0;
1.1.1.14  root     11980:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11981:                find->attrib = 8;
                   11982:                find->size = 0;
                   11983:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11984:                dtainfo->allowable_mask &= ~8;
1.1       root     11985:                REG16(AX) = 0;
                   11986:        } else {
                   11987:                REG16(AX) = 0x12;
1.1.1.3   root     11988:                m_CF = 1;
1.1       root     11989:        }
                   11990: }
                   11991: 
                   11992: inline void msdos_int_21h_50h()
                   11993: {
1.1.1.8   root     11994:        if(current_psp != REG16(BX)) {
                   11995:                process_t *process = msdos_process_info_get(current_psp);
                   11996:                if(process != NULL) {
                   11997:                        process->psp = REG16(BX);
                   11998:                }
                   11999:                current_psp = REG16(BX);
1.1.1.23  root     12000:                msdos_sda_update(current_psp);
1.1.1.8   root     12001:        }
1.1       root     12002: }
                   12003: 
                   12004: inline void msdos_int_21h_51h()
                   12005: {
                   12006:        REG16(BX) = current_psp;
                   12007: }
                   12008: 
                   12009: inline void msdos_int_21h_52h()
                   12010: {
1.1.1.25  root     12011:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     12012:        i386_load_segment_descriptor(ES);
1.1.1.25  root     12013:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     12014: }
                   12015: 
1.1.1.43  root     12016: inline void msdos_int_21h_53h()
                   12017: {
                   12018:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   12019:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   12020:        
                   12021:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   12022:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   12023:        dpb->shift_count = 0;
                   12024:        dpb->reserved_sectors = 0;
                   12025:        dpb->fat_num = bpb->fat_num;
                   12026:        dpb->root_entries = bpb->root_entries;
                   12027:        dpb->first_data_sector = 0;
                   12028:        if(bpb->sectors_per_cluster != 0) {
                   12029:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   12030:        } else {
                   12031:                dpb->highest_cluster_num = 0;
                   12032:        }
                   12033:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   12034:        dpb->first_dir_sector = 0;
                   12035:        dpb->device_driver_header = 0;
                   12036:        dpb->media_type = bpb->media_type;
                   12037:        dpb->drive_accessed = 0;
                   12038:        dpb->next_dpb_ofs = 0xffff;
                   12039:        dpb->next_dpb_seg = 0xffff;
                   12040:        dpb->first_free_cluster = 0;
                   12041:        dpb->free_clusters = 0xffff;
                   12042: }
                   12043: 
1.1       root     12044: inline void msdos_int_21h_54h()
                   12045: {
                   12046:        process_t *process = msdos_process_info_get(current_psp);
                   12047:        
                   12048:        REG8(AL) = process->verify;
                   12049: }
                   12050: 
                   12051: inline void msdos_int_21h_55h()
                   12052: {
                   12053:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   12054:        
                   12055:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   12056:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   12057:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   12058:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   12059:        psp->parent_psp = current_psp;
                   12060: }
                   12061: 
                   12062: inline void msdos_int_21h_56h(int lfn)
                   12063: {
                   12064:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     12065:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   12066:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     12067:        
                   12068:        if(rename(src, dst)) {
                   12069:                REG16(AX) = errno;
1.1.1.3   root     12070:                m_CF = 1;
1.1       root     12071:        }
                   12072: }
                   12073: 
                   12074: inline void msdos_int_21h_57h()
                   12075: {
                   12076:        FILETIME time, local;
1.1.1.14  root     12077:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     12078:        HANDLE hHandle;
1.1       root     12079:        
1.1.1.21  root     12080:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     12081:                REG16(AX) = (UINT16)GetLastError();
                   12082:                m_CF = 1;
                   12083:                return;
                   12084:        }
                   12085:        ctime = atime = mtime = NULL;
                   12086:        
1.1       root     12087:        switch(REG8(AL)) {
                   12088:        case 0x00:
1.1.1.6   root     12089:        case 0x01:
1.1.1.14  root     12090:                mtime = &time;
1.1.1.6   root     12091:                break;
                   12092:        case 0x04:
                   12093:        case 0x05:
1.1.1.14  root     12094:                atime = &time;
1.1       root     12095:                break;
1.1.1.6   root     12096:        case 0x06:
                   12097:        case 0x07:
1.1.1.14  root     12098:                ctime = &time;
                   12099:                break;
                   12100:        default:
1.1.1.22  root     12101:                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     12102:                REG16(AX) = 0x01;
                   12103:                m_CF = 1;
                   12104:                return;
                   12105:        }
                   12106:        if(REG8(AL) & 1) {
1.1       root     12107:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   12108:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     12109:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     12110:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12111:                        m_CF = 1;
1.1       root     12112:                }
1.1.1.14  root     12113:        } else {
1.1.1.21  root     12114:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     12115:                        // assume a device and use the current time
                   12116:                        GetSystemTimeAsFileTime(&time);
                   12117:                }
                   12118:                FileTimeToLocalFileTime(&time, &local);
                   12119:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     12120:        }
                   12121: }
                   12122: 
                   12123: inline void msdos_int_21h_58h()
                   12124: {
                   12125:        switch(REG8(AL)) {
                   12126:        case 0x00:
1.1.1.7   root     12127:                REG16(AX) = malloc_strategy;
                   12128:                break;
                   12129:        case 0x01:
1.1.1.24  root     12130: //             switch(REG16(BX)) {
                   12131:                switch(REG8(BL)) {
1.1.1.7   root     12132:                case 0x0000:
                   12133:                case 0x0001:
                   12134:                case 0x0002:
                   12135:                case 0x0040:
                   12136:                case 0x0041:
                   12137:                case 0x0042:
                   12138:                case 0x0080:
                   12139:                case 0x0081:
                   12140:                case 0x0082:
                   12141:                        malloc_strategy = REG16(BX);
1.1.1.23  root     12142:                        msdos_sda_update(current_psp);
1.1.1.7   root     12143:                        break;
                   12144:                default:
1.1.1.22  root     12145:                        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     12146:                        REG16(AX) = 0x01;
                   12147:                        m_CF = 1;
                   12148:                        break;
                   12149:                }
                   12150:                break;
                   12151:        case 0x02:
1.1.1.19  root     12152:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     12153:                break;
                   12154:        case 0x03:
1.1.1.24  root     12155: //             switch(REG16(BX)) {
                   12156:                switch(REG8(BL)) {
1.1.1.7   root     12157:                case 0x0000:
1.1.1.19  root     12158:                        msdos_mem_unlink_umb();
                   12159:                        break;
1.1.1.7   root     12160:                case 0x0001:
1.1.1.19  root     12161:                        msdos_mem_link_umb();
1.1.1.7   root     12162:                        break;
                   12163:                default:
1.1.1.22  root     12164:                        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     12165:                        REG16(AX) = 0x01;
                   12166:                        m_CF = 1;
                   12167:                        break;
                   12168:                }
1.1       root     12169:                break;
                   12170:        default:
1.1.1.22  root     12171:                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     12172:                REG16(AX) = 0x01;
1.1.1.3   root     12173:                m_CF = 1;
1.1       root     12174:                break;
                   12175:        }
                   12176: }
                   12177: 
                   12178: inline void msdos_int_21h_59h()
                   12179: {
1.1.1.47  root     12180:        if(REG16(BX) == 0x0000) {
                   12181:                sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   12182:                
                   12183:                REG16(AX) = sda->extended_error_code;
                   12184:                REG8(BH)  = sda->error_class;
                   12185:                REG8(BL)  = sda->suggested_action;
                   12186:                REG8(CH)  = sda->locus_of_last_error;
                   12187:                // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
                   12188:                if(sda->int21h_5d0ah_called != 0) {
                   12189:                        REG8(CL)  = sda->int21h_5d0ah_cl;
                   12190:                        REG16(DX) = sda->int21h_5d0ah_dx;
                   12191: //                     REG16(SI) = sda->int21h_5d0ah_si;
                   12192:                        REG16(DI) = sda->last_error_pointer.w.l;
                   12193: //                     SREG(DS)  = sda->int21h_5d0ah_ds;
                   12194: //                     i386_load_segment_descriptor(DS);
                   12195:                        SREG(ES)  = sda->last_error_pointer.w.h;
                   12196:                        i386_load_segment_descriptor(ES);
                   12197:                }
                   12198:                sda->int21h_5d0ah_called = 0;
                   12199: //     } else if(REG16(BX) == 0x0001) {
                   12200: //             // European MS-DOS 4.0 - Get Hard Error Information
                   12201:        } else {
                   12202:                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));
                   12203:                REG16(AX) = 0x01;
                   12204:                m_CF = 1;
                   12205:        }
1.1       root     12206: }
                   12207: 
                   12208: inline void msdos_int_21h_5ah()
                   12209: {
1.1.1.3   root     12210:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12211:        int len = strlen(path);
                   12212:        char tmp[MAX_PATH];
                   12213:        
                   12214:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   12215:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12216:                
                   12217:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12218:                REG16(AX) = fd;
                   12219:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12220:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12221:                
                   12222:                strcpy(path, tmp);
                   12223:                int dx = REG16(DX) + len;
1.1.1.3   root     12224:                int ds = SREG(DS);
1.1       root     12225:                while(dx > 0xffff) {
                   12226:                        dx -= 0x10;
                   12227:                        ds++;
                   12228:                }
                   12229:                REG16(DX) = dx;
1.1.1.3   root     12230:                SREG(DS) = ds;
                   12231:                i386_load_segment_descriptor(DS);
1.1       root     12232:        } else {
                   12233:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12234:                m_CF = 1;
1.1       root     12235:        }
                   12236: }
                   12237: 
                   12238: inline void msdos_int_21h_5bh()
                   12239: {
1.1.1.45  root     12240:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     12241:        
1.1.1.45  root     12242: //     if(msdos_is_existing_file(path)) {
                   12243:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12244:                // already exists
                   12245:                REG16(AX) = 0x50;
1.1.1.3   root     12246:                m_CF = 1;
1.1       root     12247:        } else {
                   12248:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12249:                
                   12250:                if(fd != -1) {
                   12251:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12252:                        REG16(AX) = fd;
                   12253:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12254:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12255:                } else {
                   12256:                        REG16(AX) = errno;
1.1.1.3   root     12257:                        m_CF = 1;
1.1       root     12258:                }
                   12259:        }
                   12260: }
                   12261: 
                   12262: inline void msdos_int_21h_5ch()
                   12263: {
                   12264:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12265:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12266:        
1.1.1.20  root     12267:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     12268:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     12269:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     12270:                        UINT32 pos = _tell(fd);
                   12271:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   12272:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     12273:                                REG16(AX) = errno;
1.1.1.3   root     12274:                                m_CF = 1;
1.1       root     12275:                        }
1.1.1.20  root     12276:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     12277:                        
1.1       root     12278:                        // some seconds may be passed in _locking()
1.1.1.35  root     12279:                        REQUEST_HARDWRE_UPDATE();
1.1       root     12280:                } else {
                   12281:                        REG16(AX) = 0x01;
1.1.1.3   root     12282:                        m_CF = 1;
1.1       root     12283:                }
                   12284:        } else {
                   12285:                REG16(AX) = 0x06;
1.1.1.3   root     12286:                m_CF = 1;
1.1       root     12287:        }
                   12288: }
                   12289: 
1.1.1.22  root     12290: inline void msdos_int_21h_5dh()
                   12291: {
                   12292:        switch(REG8(AL)) {
1.1.1.45  root     12293:        case 0x00:
                   12294:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
                   12295:                        // current system
                   12296:                        static bool reenter = false;
                   12297:                        if(!reenter) {
                   12298:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
                   12299:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
                   12300:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
                   12301:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
                   12302:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
                   12303:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
                   12304:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
                   12305:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
                   12306:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
                   12307:                                i386_load_segment_descriptor(DS);
                   12308:                                i386_load_segment_descriptor(ES);
                   12309:                                reenter = true;
                   12310:                                try {
                   12311:                                        msdos_syscall(0x21);
                   12312:                                } catch(...) {
                   12313:                                }
                   12314:                                reenter = false;
                   12315:                        }
                   12316:                } else {
                   12317:                        REG16(AX) = 0x49; //  network software not installed
                   12318:                        m_CF = 1;
                   12319:                }
                   12320:                break;
1.1.1.22  root     12321:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     12322:                SREG(DS) = (SDA_TOP >> 4);
                   12323:                i386_load_segment_descriptor(DS);
                   12324:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   12325:                REG16(CX) = 0x80;
                   12326:                REG16(DX) = 0x1a;
                   12327:                break;
1.1.1.45  root     12328:        case 0x07: // get redirected printer mode
                   12329:        case 0x08: // set redirected printer mode
                   12330:        case 0x09: // flush redirected printer output
                   12331:                REG16(AX) = 0x49; //  network software not installed
                   12332:                m_CF = 1;
                   12333:                break;
1.1.1.43  root     12334:        case 0x0a: // set extended error information
                   12335:                {
                   12336:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     12337:                        sda->int21h_5d0ah_called    = 1;
1.1.1.43  root     12338:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47  root     12339:                        // XXX: which one is correct ???
                   12340: #if 1
                   12341:                        // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43  root     12342:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   12343:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47  root     12344:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43  root     12345:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47  root     12346: #else
                   12347:                        // PC DOS 7 Technical Update
                   12348:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
                   12349:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
                   12350:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
                   12351:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
                   12352: #endif
                   12353:                        sda->int21h_5d0ah_dx        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   12354: //                     sda->int21h_5d0ah_si        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43  root     12355:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47  root     12356: //                     sda->int21h_5d0ah_ds        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43  root     12357:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   12358:                }
                   12359:                break;
1.1.1.23  root     12360:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     12361:                REG16(AX) = 0x01;
                   12362:                m_CF = 1;
                   12363:                break;
                   12364:        default:
                   12365:                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));
                   12366:                REG16(AX) = 0x01;
                   12367:                m_CF = 1;
                   12368:                break;
                   12369:        }
                   12370: }
                   12371: 
1.1.1.42  root     12372: inline void msdos_int_21h_5eh()
                   12373: {
                   12374:        switch(REG8(AL)) {
                   12375:        case 0x00:
                   12376:                {
                   12377:                        char name[256] = {0};
                   12378:                        DWORD dwSize = 256;
                   12379:                        
                   12380:                        if(GetComputerName(name, &dwSize)) {
                   12381:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12382:                                for(int i = 0; i < 15; i++) {
                   12383:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   12384:                                }
                   12385:                                dest[15] = '\0';
                   12386:                                REG8(CH) = 0x01; // nonzero valid
                   12387:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   12388:                        } else {
                   12389:                                REG16(AX) = 0x01;
                   12390:                                m_CF = 1;
                   12391:                        }
                   12392:                }
                   12393:                break;
                   12394:        default:
1.1.1.45  root     12395: //             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));
                   12396: //             REG16(AX) = 0x01;
                   12397:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     12398:                m_CF = 1;
                   12399:                break;
                   12400:        }
                   12401: }
                   12402: 
1.1.1.30  root     12403: inline void msdos_int_21h_5fh()
                   12404: {
                   12405:        switch(REG8(AL)) {
1.1.1.42  root     12406:        case 0x05:
1.1.1.44  root     12407:                REG16(BP) = 0;
                   12408:                for(int i = 0; i < 26; i++) {
                   12409:                        if(msdos_is_remote_drive(i)) {
                   12410:                                REG16(BP)++;
1.1.1.42  root     12411:                        }
                   12412:                }
1.1.1.30  root     12413:        case 0x02:
1.1.1.44  root     12414:                for(int i = 0, index = 0; i < 26; i++) {
                   12415:                        if(msdos_is_remote_drive(i)) {
                   12416:                                if(index == REG16(BX)) {
                   12417:                                        char volume[] = "A:";
1.1.1.30  root     12418:                                        volume[0] = 'A' + i;
1.1.1.44  root     12419:                                        DWORD dwSize = 128;
                   12420:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   12421:                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   12422:                                        REG8(BH) = 0x00; // valid
                   12423:                                        REG8(BL) = 0x04; // disk drive
                   12424:                                        REG16(CX) = 0x00; // LANtastic
                   12425:                                        return;
1.1.1.30  root     12426:                                }
1.1.1.44  root     12427:                                index++;
1.1.1.30  root     12428:                        }
                   12429:                }
                   12430:                REG16(AX) = 0x12; // no more files
                   12431:                m_CF = 1;
                   12432:                break;
1.1.1.44  root     12433:        case 0x07:
                   12434:                if(msdos_is_valid_drive(REG8(DL))) {
                   12435:                        msdos_cds_update(REG8(DL));
                   12436:                } else {
                   12437:                        REG16(AX) = 0x0f; // invalid drive
                   12438:                        m_CF = 1;
                   12439:                }
                   12440:                break;
                   12441:        case 0x08:
                   12442:                if(msdos_is_valid_drive(REG8(DL))) {
                   12443:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   12444:                        cds->drive_attrib = 0x0000;
                   12445:                } else {
                   12446:                        REG16(AX) = 0x0f; // invalid drive
                   12447:                        m_CF = 1;
                   12448:                }
                   12449:                break;
1.1.1.30  root     12450:        default:
1.1.1.45  root     12451: //             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));
                   12452: //             REG16(AX) = 0x01;
                   12453:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     12454:                m_CF = 1;
                   12455:                break;
                   12456:        }
                   12457: }
                   12458: 
1.1       root     12459: inline void msdos_int_21h_60h(int lfn)
                   12460: {
1.1.1.45  root     12461:        char full[MAX_PATH];
                   12462:        const char *path = NULL;
1.1.1.14  root     12463:        
1.1       root     12464:        if(lfn) {
1.1.1.14  root     12465:                char *name;
                   12466:                *full = '\0';
1.1.1.3   root     12467:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     12468:                switch(REG8(CL)) {
                   12469:                case 1:
                   12470:                        GetShortPathName(full, full, MAX_PATH);
                   12471:                        my_strupr(full);
                   12472:                        break;
                   12473:                case 2:
                   12474:                        GetLongPathName(full, full, MAX_PATH);
                   12475:                        break;
                   12476:                }
                   12477:                path = full;
                   12478:        } else {
                   12479:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12480:        }
                   12481:        if(*path != '\0') {
                   12482:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     12483:        } else {
1.1.1.14  root     12484:                REG16(AX) = (UINT16)GetLastError();
                   12485:                m_CF = 1;
1.1       root     12486:        }
                   12487: }
                   12488: 
                   12489: inline void msdos_int_21h_61h()
                   12490: {
                   12491:        REG8(AL) = 0;
                   12492: }
                   12493: 
                   12494: inline void msdos_int_21h_62h()
                   12495: {
                   12496:        REG16(BX) = current_psp;
                   12497: }
                   12498: 
                   12499: inline void msdos_int_21h_63h()
                   12500: {
                   12501:        switch(REG8(AL)) {
                   12502:        case 0x00:
1.1.1.3   root     12503:                SREG(DS) = (DBCS_TABLE >> 4);
                   12504:                i386_load_segment_descriptor(DS);
1.1       root     12505:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   12506:                REG8(AL) = 0x00;
                   12507:                break;
1.1.1.22  root     12508:        case 0x01: // set korean input mode
                   12509:        case 0x02: // get korean input mode
                   12510:                REG8(AL) = 0xff; // not supported
                   12511:                break;
1.1       root     12512:        default:
1.1.1.22  root     12513:                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     12514:                REG16(AX) = 0x01;
1.1.1.3   root     12515:                m_CF = 1;
1.1       root     12516:                break;
                   12517:        }
                   12518: }
                   12519: 
1.1.1.25  root     12520: UINT16 get_extended_country_info(UINT8 func)
1.1       root     12521: {
1.1.1.25  root     12522:        switch(func) {
1.1.1.17  root     12523:        case 0x01:
                   12524:                if(REG16(CX) >= 5) {
1.1.1.19  root     12525:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     12526:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   12527:                                REG16(CX) = sizeof(data);
                   12528:                        ZeroMemory(data, sizeof(data));
                   12529:                        data[0] = 0x01;
                   12530:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     12531:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     12532:                        *(UINT16 *)(data + 5) = active_code_page;
                   12533:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     12534: //                     REG16(AX) = active_code_page;
1.1.1.17  root     12535:                } else {
1.1.1.25  root     12536:                        return(0x08); // insufficient memory
1.1.1.17  root     12537:                }
                   12538:                break;
                   12539:        case 0x02:
                   12540:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12541:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   12542:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     12543: //             REG16(AX) = active_code_page;
1.1.1.17  root     12544:                REG16(CX) = 0x05;
                   12545:                break;
1.1.1.23  root     12546:        case 0x03:
                   12547:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12548:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   12549:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     12550: //             REG16(AX) = active_code_page;
1.1.1.23  root     12551:                REG16(CX) = 0x05;
                   12552:                break;
1.1.1.17  root     12553:        case 0x04:
                   12554:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   12555:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   12556:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     12557: //             REG16(AX) = active_code_page;
1.1.1.17  root     12558:                REG16(CX) = 0x05;
                   12559:                break;
                   12560:        case 0x05:
                   12561:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   12562:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   12563:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     12564: //             REG16(AX) = active_code_page;
1.1.1.17  root     12565:                REG16(CX) = 0x05;
                   12566:                break;
                   12567:        case 0x06:
                   12568:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   12569:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   12570:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     12571: //             REG16(AX) = active_code_page;
1.1.1.17  root     12572:                REG16(CX) = 0x05;
                   12573:                break;
1.1       root     12574:        case 0x07:
1.1.1.3   root     12575:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   12576:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   12577:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     12578: //             REG16(AX) = active_code_page;
1.1       root     12579:                REG16(CX) = 0x05;
                   12580:                break;
1.1.1.25  root     12581:        default:
                   12582:                return(0x01); // function number invalid
                   12583:        }
                   12584:        return(0x00);
                   12585: }
                   12586: 
                   12587: inline void msdos_int_21h_65h()
                   12588: {
                   12589:        char tmp[0x10000];
                   12590:        
                   12591:        switch(REG8(AL)) {
1.1.1.43  root     12592:        case 0x00:
                   12593:                if(REG16(CX) >= 7) {
                   12594:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   12595:                        REG16(AX) = system_code_page;
                   12596:                } else {
                   12597:                        REG16(AX) = 0x0c;
                   12598:                        m_CF = 1;
                   12599:                }
                   12600:                break;
1.1.1.25  root     12601:        case 0x01:
                   12602:        case 0x02:
                   12603:        case 0x03:
                   12604:        case 0x04:
                   12605:        case 0x05:
                   12606:        case 0x06:
                   12607:        case 0x07:
                   12608:                {
                   12609:                        UINT16 result = get_extended_country_info(REG8(AL));
                   12610:                        if(result) {
                   12611:                                REG16(AX) = result;
                   12612:                                m_CF = 1;
                   12613:                        } else {
                   12614:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   12615:                        }
                   12616:                }
                   12617:                break;
1.1       root     12618:        case 0x20:
1.1.1.25  root     12619:        case 0xa0:
1.1.1.19  root     12620:                memset(tmp, 0, sizeof(tmp));
                   12621:                tmp[0] = REG8(DL);
1.1       root     12622:                my_strupr(tmp);
                   12623:                REG8(DL) = tmp[0];
                   12624:                break;
                   12625:        case 0x21:
1.1.1.25  root     12626:        case 0xa1:
1.1       root     12627:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12628:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     12629:                my_strupr(tmp);
1.1.1.3   root     12630:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     12631:                break;
                   12632:        case 0x22:
1.1.1.25  root     12633:        case 0xa2:
1.1.1.3   root     12634:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     12635:                break;
1.1.1.25  root     12636:        case 0x23:
                   12637:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45  root     12638:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     12639:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45  root     12640:                        REG16(AX) = 0x00;
                   12641:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   12642:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     12643:                        REG16(AX) = 0x01;
                   12644:                } else {
                   12645:                        REG16(AX) = 0x02;
                   12646:                }
                   12647:                break;
1.1       root     12648:        default:
1.1.1.22  root     12649:                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     12650:                REG16(AX) = 0x01;
1.1.1.3   root     12651:                m_CF = 1;
1.1       root     12652:                break;
                   12653:        }
                   12654: }
                   12655: 
                   12656: inline void msdos_int_21h_66h()
                   12657: {
                   12658:        switch(REG8(AL)) {
                   12659:        case 0x01:
                   12660:                REG16(BX) = active_code_page;
                   12661:                REG16(DX) = system_code_page;
                   12662:                break;
                   12663:        case 0x02:
                   12664:                if(active_code_page == REG16(BX)) {
                   12665:                        REG16(AX) = 0xeb41;
                   12666:                } else if(_setmbcp(REG16(BX)) == 0) {
                   12667:                        active_code_page = REG16(BX);
1.1.1.17  root     12668:                        msdos_nls_tables_update();
1.1       root     12669:                        REG16(AX) = 0xeb41;
1.1.1.32  root     12670:                        SetConsoleCP(active_code_page);
                   12671:                        SetConsoleOutputCP(active_code_page);
1.1       root     12672:                } else {
                   12673:                        REG16(AX) = 0x25;
1.1.1.3   root     12674:                        m_CF = 1;
1.1       root     12675:                }
                   12676:                break;
                   12677:        default:
1.1.1.22  root     12678:                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     12679:                REG16(AX) = 0x01;
1.1.1.3   root     12680:                m_CF = 1;
1.1       root     12681:                break;
                   12682:        }
                   12683: }
                   12684: 
                   12685: inline void msdos_int_21h_67h()
                   12686: {
                   12687:        process_t *process = msdos_process_info_get(current_psp);
                   12688:        
                   12689:        if(REG16(BX) <= MAX_FILES) {
                   12690:                process->max_files = max(REG16(BX), 20);
                   12691:        } else {
                   12692:                REG16(AX) = 0x08;
1.1.1.3   root     12693:                m_CF = 1;
1.1       root     12694:        }
                   12695: }
                   12696: 
                   12697: inline void msdos_int_21h_68h()
                   12698: {
                   12699:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12700:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12701:        
1.1.1.20  root     12702:        if(fd < process->max_files && file_handler[fd].valid) {
                   12703:                // fflush(_fdopen(fd, ""));
1.1       root     12704:        } else {
                   12705:                REG16(AX) = 0x06;
1.1.1.3   root     12706:                m_CF = 1;
1.1       root     12707:        }
                   12708: }
                   12709: 
                   12710: inline void msdos_int_21h_69h()
                   12711: {
1.1.1.3   root     12712:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12713:        char path[] = "A:\\";
                   12714:        char volume_label[MAX_PATH];
                   12715:        DWORD serial_number = 0;
                   12716:        char file_system[MAX_PATH];
                   12717:        
                   12718:        if(REG8(BL) == 0) {
                   12719:                path[0] = 'A' + _getdrive() - 1;
                   12720:        } else {
                   12721:                path[0] = 'A' + REG8(BL) - 1;
                   12722:        }
                   12723:        
                   12724:        switch(REG8(AL)) {
                   12725:        case 0x00:
                   12726:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   12727:                        info->info_level = 0;
                   12728:                        info->serial_number = serial_number;
                   12729:                        memset(info->volume_label, 0x20, 11);
                   12730:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   12731:                        memset(info->file_system, 0x20, 8);
                   12732:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   12733:                } else {
                   12734:                        REG16(AX) = errno;
1.1.1.3   root     12735:                        m_CF = 1;
1.1       root     12736:                }
                   12737:                break;
                   12738:        case 0x01:
                   12739:                REG16(AX) = 0x03;
1.1.1.3   root     12740:                m_CF = 1;
1.1.1.45  root     12741:                break;
                   12742:        default:
                   12743:                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));
                   12744:                REG16(AX) = 0x01;
                   12745:                m_CF = 1;
                   12746:                break;
1.1       root     12747:        }
                   12748: }
                   12749: 
                   12750: inline void msdos_int_21h_6ah()
                   12751: {
                   12752:        REG8(AH) = 0x68;
                   12753:        msdos_int_21h_68h();
                   12754: }
                   12755: 
                   12756: inline void msdos_int_21h_6bh()
                   12757: {
1.1.1.45  root     12758:        REG8(AL) = 0x00;
1.1       root     12759: }
                   12760: 
                   12761: inline void msdos_int_21h_6ch(int lfn)
                   12762: {
1.1.1.45  root     12763:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     12764:        int mode = REG8(BL) & 0x03;
                   12765:        
                   12766:        if(mode < 0x03) {
1.1.1.29  root     12767:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12768:                        // file exists
                   12769:                        if(REG8(DL) & 1) {
1.1.1.37  root     12770:                                int fd = -1;
                   12771:                                int sio_port = 0;
                   12772:                                int lpt_port = 0;
1.1       root     12773:                                
1.1.1.45  root     12774:                                if(msdos_is_device_path(path)) {
                   12775:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     12776:                                } else {
1.1.1.13  root     12777:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     12778:                                }
1.1       root     12779:                                if(fd != -1) {
                   12780:                                        REG16(AX) = fd;
                   12781:                                        REG16(CX) = 1;
1.1.1.45  root     12782:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12783:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12784:                                } else {
                   12785:                                        REG16(AX) = errno;
1.1.1.3   root     12786:                                        m_CF = 1;
1.1       root     12787:                                }
                   12788:                        } else if(REG8(DL) & 2) {
                   12789:                                int attr = GetFileAttributes(path);
1.1.1.37  root     12790:                                int fd = -1;
                   12791:                                int sio_port = 0;
                   12792:                                int lpt_port = 0;
1.1       root     12793:                                
1.1.1.45  root     12794:                                if(msdos_is_device_path(path)) {
                   12795:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     12796:                                } else {
                   12797:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12798:                                }
                   12799:                                if(fd != -1) {
                   12800:                                        if(attr == -1) {
                   12801:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   12802:                                        }
                   12803:                                        SetFileAttributes(path, attr);
                   12804:                                        REG16(AX) = fd;
                   12805:                                        REG16(CX) = 3;
1.1.1.45  root     12806:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12807:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12808:                                } else {
                   12809:                                        REG16(AX) = errno;
1.1.1.3   root     12810:                                        m_CF = 1;
1.1       root     12811:                                }
                   12812:                        } else {
                   12813:                                REG16(AX) = 0x50;
1.1.1.3   root     12814:                                m_CF = 1;
1.1       root     12815:                        }
                   12816:                } else {
                   12817:                        // file not exists
                   12818:                        if(REG8(DL) & 0x10) {
                   12819:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12820:                                
                   12821:                                if(fd != -1) {
                   12822:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12823:                                        REG16(AX) = fd;
                   12824:                                        REG16(CX) = 2;
                   12825:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12826:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12827:                                } else {
                   12828:                                        REG16(AX) = errno;
1.1.1.3   root     12829:                                        m_CF = 1;
1.1       root     12830:                                }
                   12831:                        } else {
                   12832:                                REG16(AX) = 0x02;
1.1.1.3   root     12833:                                m_CF = 1;
1.1       root     12834:                        }
                   12835:                }
                   12836:        } else {
                   12837:                REG16(AX) = 0x0c;
1.1.1.3   root     12838:                m_CF = 1;
1.1       root     12839:        }
                   12840: }
                   12841: 
1.1.1.43  root     12842: inline void msdos_int_21h_70h()
                   12843: {
                   12844:        switch(REG8(AL)) {
1.1.1.48  root     12845:        case 0x00: // get ??? info
                   12846:        case 0x01: // set above info
                   12847: //             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));
                   12848:                REG16(AX) = 0x7000;
                   12849:                m_CF = 1;
                   12850:                break;
                   12851:        case 0x02: // set general internationalization info
1.1.1.43  root     12852:                if(REG16(CX) >= 7) {
                   12853:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   12854:                        msdos_nls_tables_update();
                   12855:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   12856:                        REG16(AX) = system_code_page;
                   12857:                } else {
                   12858:                        REG16(AX) = 0x0c;
                   12859:                        m_CF = 1;
                   12860:                }
                   12861:                break;
                   12862:        default:
                   12863:                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     12864:                REG16(AX) = 0x7000;
1.1.1.43  root     12865:                m_CF = 1;
                   12866:                break;
                   12867:        }
                   12868: }
                   12869: 
1.1       root     12870: inline void msdos_int_21h_710dh()
                   12871: {
                   12872:        // reset drive
                   12873: }
                   12874: 
1.1.1.48  root     12875: inline void msdos_int_21h_7141h()
1.1.1.17  root     12876: {
                   12877:        if(REG16(SI) == 0) {
1.1.1.48  root     12878:                msdos_int_21h_41h(1);
1.1.1.17  root     12879:                return;
                   12880:        }
                   12881:        if(REG16(SI) != 1) {
                   12882:                REG16(AX) = 5;
                   12883:                m_CF = 1;
                   12884:        }
                   12885:        /* wild card and matching attributes... */
                   12886:        char tmp[MAX_PATH * 2];
                   12887:        // copy search pathname (and quick check overrun)
                   12888:        ZeroMemory(tmp, sizeof(tmp));
                   12889:        tmp[MAX_PATH - 1] = '\0';
                   12890:        tmp[MAX_PATH] = 1;
                   12891:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   12892:        
                   12893:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   12894:                REG16(AX) = 1;
                   12895:                m_CF = 1;
                   12896:                return;
                   12897:        }
                   12898:        for(char *s = tmp; *s; ++s) {
                   12899:                if(*s == '/') {
                   12900:                        *s = '\\';
                   12901:                }
                   12902:        }
                   12903:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   12904:        if(tmp_name) {
                   12905:                ++tmp_name;
                   12906:        } else {
                   12907:                tmp_name = strchr(tmp, ':');
                   12908:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   12909:        }
                   12910:        
                   12911:        WIN32_FIND_DATAA fd;
                   12912:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   12913:        if(fh == INVALID_HANDLE_VALUE) {
                   12914:                REG16(AX) = 2;
                   12915:                m_CF = 1;
                   12916:                return;
                   12917:        }
                   12918:        do {
                   12919:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   12920:                        strcpy(tmp_name, fd.cFileName);
1.1.1.48  root     12921:                        if(remove(msdos_trimmed_path(tmp, 1))) {
1.1.1.17  root     12922:                                REG16(AX) = 5;
                   12923:                                m_CF = 1;
                   12924:                                break;
                   12925:                        }
                   12926:                }
                   12927:        } while(FindNextFileA(fh, &fd));
                   12928:        if(!m_CF) {
                   12929:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   12930:                        m_CF = 1;
                   12931:                        REG16(AX) = 2;
                   12932:                }
                   12933:        }
                   12934:        FindClose(fh);
                   12935: }
                   12936: 
1.1       root     12937: inline void msdos_int_21h_714eh()
                   12938: {
                   12939:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12940:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   12941:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12942:        WIN32_FIND_DATA fd;
                   12943:        
1.1.1.13  root     12944:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12945:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12946:                FindClose(dtainfo->find_handle);
                   12947:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12948:        }
                   12949:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12950:        dtainfo->allowable_mask = REG8(CL);
                   12951:        dtainfo->required_mask = REG8(CH);
                   12952:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12953:        
1.1.1.14  root     12954:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12955:                dtainfo->allowable_mask &= ~8;
1.1       root     12956:        }
1.1.1.14  root     12957:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   12958:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12959:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12960:                                FindClose(dtainfo->find_handle);
                   12961:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12962:                                break;
                   12963:                        }
                   12964:                }
                   12965:        }
1.1.1.13  root     12966:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12967:                find->attrib = fd.dwFileAttributes;
                   12968:                msdos_find_file_conv_local_time(&fd);
                   12969:                if(REG16(SI) == 0) {
                   12970:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12971:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12972:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12973:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12974:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12975:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12976:                } else {
                   12977:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12978:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12979:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12980:                }
                   12981:                find->size_hi = fd.nFileSizeHigh;
                   12982:                find->size_lo = fd.nFileSizeLow;
                   12983:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12984:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12985:                REG16(AX) = dtainfo - dtalist + 1;
                   12986:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12987:                // volume label
                   12988:                find->attrib = 8;
                   12989:                find->size_hi = find->size_lo = 0;
                   12990:                strcpy(find->full_name, process->volume_label);
                   12991:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12992:                dtainfo->allowable_mask &= ~8;
                   12993:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     12994:        } else {
                   12995:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12996:                m_CF = 1;
1.1       root     12997:        }
                   12998: }
                   12999: 
                   13000: inline void msdos_int_21h_714fh()
                   13001: {
                   13002:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13003:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     13004:        WIN32_FIND_DATA fd;
                   13005:        
1.1.1.14  root     13006:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13007:                REG16(AX) = 6;
1.1.1.13  root     13008:                m_CF = 1;
                   13009:                return;
                   13010:        }
1.1.1.14  root     13011:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13012:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13013:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     13014:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     13015:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   13016:                                        FindClose(dtainfo->find_handle);
                   13017:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13018:                                        break;
                   13019:                                }
                   13020:                        }
                   13021:                } else {
1.1.1.13  root     13022:                        FindClose(dtainfo->find_handle);
                   13023:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13024:                }
                   13025:        }
1.1.1.13  root     13026:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13027:                find->attrib = fd.dwFileAttributes;
                   13028:                msdos_find_file_conv_local_time(&fd);
                   13029:                if(REG16(SI) == 0) {
                   13030:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13031:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13032:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13033:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13034:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13035:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13036:                } else {
                   13037:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13038:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13039:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13040:                }
                   13041:                find->size_hi = fd.nFileSizeHigh;
                   13042:                find->size_lo = fd.nFileSizeLow;
                   13043:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13044:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13045:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13046:                // volume label
                   13047:                find->attrib = 8;
                   13048:                find->size_hi = find->size_lo = 0;
                   13049:                strcpy(find->full_name, process->volume_label);
                   13050:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13051:                dtainfo->allowable_mask &= ~8;
1.1       root     13052:        } else {
                   13053:                REG16(AX) = 0x12;
1.1.1.3   root     13054:                m_CF = 1;
1.1       root     13055:        }
                   13056: }
                   13057: 
                   13058: inline void msdos_int_21h_71a0h()
                   13059: {
                   13060:        DWORD max_component_len, file_sys_flag;
                   13061:        
1.1.1.14  root     13062:        if(GetVolumeInformation((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))) {
                   13063:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   13064:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     13065:                REG16(CX) = (UINT16)max_component_len;          // 255
                   13066:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   13067:        } else {
                   13068:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13069:                m_CF = 1;
1.1       root     13070:        }
                   13071: }
                   13072: 
                   13073: inline void msdos_int_21h_71a1h()
                   13074: {
1.1.1.14  root     13075:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13076:                REG16(AX) = 6;
1.1.1.13  root     13077:                m_CF = 1;
                   13078:                return;
                   13079:        }
1.1.1.14  root     13080:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13081:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13082:                FindClose(dtainfo->find_handle);
                   13083:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13084:        }
                   13085: }
                   13086: 
                   13087: inline void msdos_int_21h_71a6h()
                   13088: {
                   13089:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     13090:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13091:        
1.1.1.3   root     13092:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13093:        struct _stat64 status;
                   13094:        DWORD serial_number = 0;
                   13095:        
1.1.1.20  root     13096:        if(fd < process->max_files && file_handler[fd].valid) {
                   13097:                if(_fstat64(fd, &status) == 0) {
                   13098:                        if(file_handler[fd].path[1] == ':') {
1.1       root     13099:                                // NOTE: we need to consider the network file path "\\host\share\"
                   13100:                                char volume[] = "A:\\";
1.1.1.20  root     13101:                                volume[0] = file_handler[fd].path[1];
1.1       root     13102:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   13103:                        }
1.1.1.20  root     13104:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     13105:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   13106:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   13107:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   13108:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   13109:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   13110:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   13111:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   13112:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   13113:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   13114:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     13115:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     13116:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     13117:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     13118:                } else {
                   13119:                        REG16(AX) = errno;
1.1.1.3   root     13120:                        m_CF = 1;
1.1       root     13121:                }
                   13122:        } else {
                   13123:                REG16(AX) = 0x06;
1.1.1.3   root     13124:                m_CF = 1;
1.1       root     13125:        }
                   13126: }
                   13127: 
                   13128: inline void msdos_int_21h_71a7h()
                   13129: {
                   13130:        switch(REG8(BL)) {
                   13131:        case 0x00:
1.1.1.3   root     13132:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     13133:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13134:                        m_CF = 1;
1.1       root     13135:                }
                   13136:                break;
                   13137:        case 0x01:
                   13138:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     13139:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     13140:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13141:                        m_CF = 1;
1.1       root     13142:                }
                   13143:                break;
                   13144:        default:
1.1.1.22  root     13145:                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     13146:                REG16(AX) = 0x7100;
1.1.1.3   root     13147:                m_CF = 1;
1.1       root     13148:                break;
                   13149:        }
                   13150: }
                   13151: 
                   13152: inline void msdos_int_21h_71a8h()
                   13153: {
                   13154:        if(REG8(DH) == 0) {
                   13155:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     13156:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13157:                memset(fcb, 0x20, sizeof(fcb));
                   13158:                int len = strlen(tmp);
1.1.1.21  root     13159:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     13160:                        if(tmp[i] == '.') {
                   13161:                                pos = 8;
                   13162:                        } else {
                   13163:                                if(msdos_lead_byte_check(tmp[i])) {
                   13164:                                        fcb[pos++] = tmp[i++];
                   13165:                                }
                   13166:                                fcb[pos++] = tmp[i];
                   13167:                        }
                   13168:                }
1.1.1.3   root     13169:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     13170:        } else {
1.1.1.3   root     13171:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13172:        }
                   13173: }
                   13174: 
1.1.1.22  root     13175: inline void msdos_int_21h_71aah()
                   13176: {
                   13177:        char drv[] = "A:", path[MAX_PATH];
                   13178:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13179:        
                   13180:        if(REG8(BL) == 0) {
                   13181:                drv[0] = 'A' + _getdrive() - 1;
                   13182:        } else {
                   13183:                drv[0] = 'A' + REG8(BL) - 1;
                   13184:        }
                   13185:        switch(REG8(BH)) {
                   13186:        case 0x00:
1.1.1.44  root     13187:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13188:                        REG16(AX) = 0x0f; // invalid drive
                   13189:                        m_CF = 1;
                   13190:                } else if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   13191:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     13192:                        m_CF = 1;
                   13193:                }
                   13194:                break;
                   13195:        case 0x01:
1.1.1.44  root     13196:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13197:                        REG16(AX) = 0x0f; // invalid drive
                   13198:                        m_CF = 1;
                   13199:                } else if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     13200:                        REG16(AX) = 0x0f; // invalid drive
                   13201:                        m_CF = 1;
                   13202:                }
                   13203:                break;
                   13204:        case 0x02:
1.1.1.44  root     13205:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13206:                        REG16(AX) = 0x0f; // invalid drive
                   13207:                        m_CF = 1;
                   13208:                } else if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     13209:                        REG16(AX) = 0x0f; // invalid drive
                   13210:                        m_CF = 1;
                   13211:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   13212:                        REG16(AX) = 0x0f; // invalid drive
                   13213:                        m_CF = 1;
                   13214:                } else {
                   13215:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   13216:                }
                   13217:                break;
                   13218:        default:
                   13219:                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     13220:                REG16(AX) = 0x7100;
1.1.1.22  root     13221:                m_CF = 1;
                   13222:                break;
                   13223:        }
                   13224: }
                   13225: 
1.1.1.14  root     13226: inline void msdos_int_21h_7300h()
                   13227: {
1.1.1.44  root     13228:        REG8(AL) = REG8(CL);
                   13229:        REG8(AH) = 0;
1.1.1.14  root     13230: }
                   13231: 
                   13232: inline void msdos_int_21h_7302h()
                   13233: {
                   13234:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   13235:        UINT16 seg, ofs;
                   13236:        
                   13237:        if(REG16(CX) < 0x3f) {
                   13238:                REG8(AL) = 0x18;
                   13239:                m_CF = 1;
                   13240:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   13241:                REG8(AL) = 0xff;
                   13242:                m_CF = 1;
                   13243:        } else {
                   13244:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   13245:        }
                   13246: }
                   13247: 
1.1       root     13248: inline void msdos_int_21h_7303h()
                   13249: {
1.1.1.3   root     13250:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13251:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     13252:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   13253:        
                   13254:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   13255:                info->size_of_structure = sizeof(ext_space_info_t);
                   13256:                info->structure_version = 0;
                   13257:                info->sectors_per_cluster = sectors_per_cluster;
                   13258:                info->bytes_per_sector = bytes_per_sector;
                   13259:                info->available_clusters_on_drive = free_clusters;
                   13260:                info->total_clusters_on_drive = total_clusters;
                   13261:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   13262:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   13263:                info->available_allocation_units = free_clusters;       // ???
                   13264:                info->total_allocation_units = total_clusters;          // ???
                   13265:        } else {
                   13266:                REG16(AX) = errno;
1.1.1.3   root     13267:                m_CF = 1;
1.1       root     13268:        }
                   13269: }
                   13270: 
1.1.1.30  root     13271: inline void msdos_int_21h_dbh()
                   13272: {
                   13273:        // Novell NetWare - Workstation - Get Number of Local Drives
                   13274:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   13275:        REG8(AL) = dos_info->last_drive;
                   13276: }
                   13277: 
                   13278: inline void msdos_int_21h_dch()
                   13279: {
                   13280:        // Novell NetWare - Connection Services - Get Connection Number
                   13281:        REG8(AL) = 0x00;
                   13282: }
                   13283: 
1.1.1.32  root     13284: inline void msdos_int_24h()
                   13285: {
                   13286:        const char *message = NULL;
                   13287:        int key = 0;
                   13288:        
                   13289:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   13290:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   13291:                        if(active_code_page == 932) {
                   13292:                                message = critical_error_table[i].message_japanese;
                   13293:                        }
                   13294:                        if(message == NULL) {
                   13295:                                message = critical_error_table[i].message_english;
                   13296:                        }
                   13297:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   13298:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   13299:                        
                   13300:                        SREG(ES) = WORK_TOP >> 4;
                   13301:                        i386_load_segment_descriptor(ES);
                   13302:                        REG16(DI) = 0x0000;
                   13303:                        break;
                   13304:                }
                   13305:        }
                   13306:        fprintf(stderr, "\n%s", message);
                   13307:        if(!(REG8(AH) & 0x80)) {
                   13308:                if(REG8(AH) & 0x01) {
                   13309:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   13310:                } else {
                   13311:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   13312:                }
                   13313:        }
                   13314:        fprintf(stderr, "\n");
                   13315:        
1.1.1.33  root     13316:        {
1.1.1.32  root     13317:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     13318:        }
1.1.1.32  root     13319:        if(REG8(AH) & 0x10) {
                   13320:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   13321:        }
                   13322:        if(REG8(AH) & 0x20) {
                   13323:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   13324:        }
                   13325:        if(REG8(AH) & 0x08) {
                   13326:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   13327:        }
                   13328:        fprintf(stderr, "? ");
                   13329:        
                   13330:        while(1) {
                   13331:                while(!_kbhit()) {
                   13332:                        Sleep(10);
                   13333:                }
                   13334:                key = _getch();
                   13335:                
                   13336:                if(key == 'I' || key == 'i') {
                   13337:                        if(REG8(AH) & 0x20) {
                   13338:                                REG8(AL) = 0;
                   13339:                                break;
                   13340:                        }
                   13341:                } else if(key == 'R' || key == 'r') {
                   13342:                        if(REG8(AH) & 0x10) {
                   13343:                                REG8(AL) = 1;
                   13344:                                break;
                   13345:                        }
                   13346:                } else if(key == 'A' || key == 'a') {
                   13347:                        REG8(AL) = 2;
                   13348:                        break;
                   13349:                } else if(key == 'F' || key == 'f') {
                   13350:                        if(REG8(AH) & 0x08) {
                   13351:                                REG8(AL) = 3;
                   13352:                                break;
                   13353:                        }
                   13354:                }
                   13355:        }
                   13356:        fprintf(stderr, "%c\n", key);
                   13357: }
                   13358: 
1.1       root     13359: inline void msdos_int_25h()
                   13360: {
                   13361:        UINT16 seg, ofs;
                   13362:        DWORD dwSize;
                   13363:        
1.1.1.3   root     13364: #if defined(HAS_I386)
                   13365:        I386OP(pushf)();
                   13366: #else
                   13367:        PREFIX86(_pushf());
                   13368: #endif
1.1       root     13369:        
                   13370:        if(!(REG8(AL) < 26)) {
                   13371:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13372:                m_CF = 1;
1.1       root     13373:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13374:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13375:                m_CF = 1;
1.1       root     13376:        } else {
                   13377:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13378:                char dev[64];
                   13379:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13380:                
                   13381:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   13382:                if(hFile == INVALID_HANDLE_VALUE) {
                   13383:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13384:                        m_CF = 1;
1.1       root     13385:                } else {
1.1.1.19  root     13386:                        UINT32 top_sector  = REG16(DX);
                   13387:                        UINT16 sector_num  = REG16(CX);
                   13388:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13389:                        
                   13390:                        if(sector_num == 0xffff) {
                   13391:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13392:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13393:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13394:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13395:                                buffer_addr = (seg << 4) + ofs;
                   13396:                        }
                   13397: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13398: //                             REG8(AL) = 0x02; // drive not ready
                   13399: //                             m_CF = 1;
                   13400: //                     } else 
                   13401:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13402:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13403:                                m_CF = 1;
1.1.1.19  root     13404:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13405:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     13406:                                m_CF = 1;
1.1       root     13407:                        }
                   13408:                        CloseHandle(hFile);
                   13409:                }
                   13410:        }
                   13411: }
                   13412: 
                   13413: inline void msdos_int_26h()
                   13414: {
1.1.1.42  root     13415:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     13416:        UINT16 seg, ofs;
                   13417:        DWORD dwSize;
                   13418:        
1.1.1.3   root     13419: #if defined(HAS_I386)
                   13420:        I386OP(pushf)();
                   13421: #else
                   13422:        PREFIX86(_pushf());
                   13423: #endif
1.1       root     13424:        
                   13425:        if(!(REG8(AL) < 26)) {
                   13426:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13427:                m_CF = 1;
1.1       root     13428:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13429:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13430:                m_CF = 1;
1.1       root     13431:        } else {
                   13432:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13433:                char dev[64];
                   13434:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13435:                
                   13436:                if(dpb->media_type == 0xf8) {
                   13437:                        // this drive is not a floppy
1.1.1.6   root     13438: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   13439: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   13440: //                     }
1.1       root     13441:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13442:                        m_CF = 1;
1.1       root     13443:                } else {
                   13444:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   13445:                        if(hFile == INVALID_HANDLE_VALUE) {
                   13446:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13447:                                m_CF = 1;
1.1       root     13448:                        } else {
1.1.1.19  root     13449:                                UINT32 top_sector  = REG16(DX);
                   13450:                                UINT16 sector_num  = REG16(CX);
                   13451:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13452:                                
                   13453:                                if(sector_num == 0xffff) {
                   13454:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13455:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13456:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13457:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13458:                                        buffer_addr = (seg << 4) + ofs;
                   13459:                                }
1.1       root     13460:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13461:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13462:                                        m_CF = 1;
1.1.1.19  root     13463:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13464:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13465:                                        m_CF = 1;
1.1.1.19  root     13466:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13467:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     13468:                                        m_CF = 1;
1.1       root     13469:                                }
                   13470:                                CloseHandle(hFile);
                   13471:                        }
                   13472:                }
                   13473:        }
                   13474: }
                   13475: 
                   13476: inline void msdos_int_27h()
                   13477: {
1.1.1.29  root     13478:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   13479:        try {
                   13480:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13481:        } catch(...) {
                   13482:                // recover the broken mcb
                   13483:                int mcb_seg = SREG(CS) - 1;
                   13484:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     13485:                
1.1.1.29  root     13486:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     13487:                        mcb->mz = 'M';
                   13488:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   13489:                        
1.1.1.29  root     13490:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     13491:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     13492:                        } else {
1.1.1.39  root     13493:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     13494:                        }
                   13495:                } else {
                   13496:                        mcb->mz = 'Z';
1.1.1.30  root     13497:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     13498:                }
                   13499:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13500:        }
1.1.1.3   root     13501:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     13502: }
                   13503: 
                   13504: inline void msdos_int_29h()
                   13505: {
1.1.1.50  root     13506:        msdos_putch_fast(REG8(AL));
1.1       root     13507: }
                   13508: 
                   13509: inline void msdos_int_2eh()
                   13510: {
                   13511:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   13512:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13513:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     13514:        char *token = my_strtok(tmp, " ");
                   13515:        strcpy(command, token);
                   13516:        strcpy(opt, token + strlen(token) + 1);
                   13517:        
                   13518:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   13519:        param->env_seg = 0;
                   13520:        param->cmd_line.w.l = 44;
                   13521:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13522:        param->fcb1.w.l = 24;
                   13523:        param->fcb1.w.h = (WORK_TOP >> 4);
                   13524:        param->fcb2.w.l = 24;
                   13525:        param->fcb2.w.h = (WORK_TOP >> 4);
                   13526:        
                   13527:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   13528:        
                   13529:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   13530:        cmd_line->len = strlen(opt);
                   13531:        strcpy(cmd_line->cmd, opt);
                   13532:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   13533:        
1.1.1.28  root     13534:        try {
                   13535:                if(msdos_process_exec(command, param, 0)) {
                   13536:                        REG16(AX) = 0xffff; // error before processing command
                   13537:                } else {
                   13538:                        // set flag to set retval to ax when the started process is terminated
                   13539:                        process_t *process = msdos_process_info_get(current_psp);
                   13540:                        process->called_by_int2eh = true;
                   13541:                }
                   13542:        } catch(...) {
                   13543:                REG16(AX) = 0xffff; // error before processing command
                   13544:        }
1.1       root     13545: }
                   13546: 
1.1.1.29  root     13547: inline void msdos_int_2fh_05h()
                   13548: {
                   13549:        switch(REG8(AL)) {
                   13550:        case 0x00:
1.1.1.49  root     13551:                // critical error handler is installed
1.1.1.32  root     13552:                REG8(AL) = 0xff;
                   13553:                break;
                   13554:        case 0x01:
                   13555:        case 0x02:
                   13556:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   13557:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   13558:                                const char *message = NULL;
                   13559:                                if(active_code_page == 932) {
                   13560:                                        message = standard_error_table[i].message_japanese;
                   13561:                                }
                   13562:                                if(message == NULL) {
                   13563:                                        message = standard_error_table[i].message_english;
                   13564:                                }
                   13565:                                strcpy((char *)(mem + WORK_TOP), message);
                   13566:                                
                   13567:                                SREG(ES) = WORK_TOP >> 4;
                   13568:                                i386_load_segment_descriptor(ES);
                   13569:                                REG16(DI) = 0x0000;
                   13570:                                REG8(AL) = 0x01;
                   13571:                                break;
                   13572:                        }
                   13573:                }
1.1.1.29  root     13574:                break;
                   13575:        default:
                   13576:                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     13577:                REG16(AX) = 0x01;
1.1.1.29  root     13578:                m_CF = 1;
                   13579:        }
                   13580: }
                   13581: 
1.1.1.44  root     13582: inline void msdos_int_2fh_06h()
                   13583: {
                   13584:        switch(REG8(AL)) {
                   13585:        case 0x00:
                   13586:                // ASSIGN is not installed
1.1.1.49  root     13587: //             REG8(AL) = 0x00;
1.1.1.44  root     13588:                break;
                   13589:        case 0x01:
                   13590:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   13591:                REG16(AX) = 0x01;
                   13592:                m_CF = 1;
                   13593:                break;
                   13594:        default:
                   13595:                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));
                   13596:                REG16(AX) = 0x01;
                   13597:                m_CF = 1;
                   13598:                break;
                   13599:        }
                   13600: }
                   13601: 
1.1.1.22  root     13602: inline void msdos_int_2fh_11h()
                   13603: {
                   13604:        switch(REG8(AL)) {
                   13605:        case 0x00:
1.1.1.29  root     13606:                if(i386_read_stack() == 0xdada) {
1.1.1.53  root     13607: #ifdef SUPPORT_MSCDEX
                   13608:                        // MSCDEX is installed
                   13609:                        REG8(AL) = 0xff;
                   13610:                        i386_write_stack(0xadad);
                   13611: #else
1.1.1.29  root     13612:                        // MSCDEX is not installed
                   13613: //                     REG8(AL) = 0x00;
1.1.1.53  root     13614: #endif
1.1.1.29  root     13615:                } else {
                   13616:                        // Network Redirector is not installed
                   13617: //                     REG8(AL) = 0x00;
                   13618:                }
1.1.1.22  root     13619:                break;
                   13620:        default:
1.1.1.43  root     13621: //             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     13622:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     13623:                m_CF = 1;
                   13624:                break;
                   13625:        }
                   13626: }
                   13627: 
1.1.1.21  root     13628: inline void msdos_int_2fh_12h()
                   13629: {
                   13630:        switch(REG8(AL)) {
1.1.1.22  root     13631:        case 0x00:
1.1.1.29  root     13632:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     13633:                REG8(AL) = 0xff;
                   13634:                break;
1.1.1.29  root     13635: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   13636:        case 0x02:
                   13637:                {
                   13638:                        UINT16 stack = i386_read_stack();
                   13639:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   13640:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   13641:                        i386_load_segment_descriptor(ES);
                   13642:                }
                   13643:                break;
1.1.1.30  root     13644:        case 0x03:
                   13645:                SREG(DS) = (DEVICE_TOP >> 4);
                   13646:                i386_load_segment_descriptor(DS);
                   13647:                break;
1.1.1.29  root     13648:        case 0x04:
                   13649:                {
                   13650:                        UINT16 stack = i386_read_stack();
                   13651:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   13652: #if defined(HAS_I386)
                   13653:                        m_ZF = (REG8(AL) == '\\');
                   13654: #else
                   13655:                        m_ZeroVal = (REG8(AL) != '\\');
                   13656: #endif
                   13657:                }
                   13658:                break;
                   13659:        case 0x05:
1.1.1.49  root     13660:                {
                   13661:                        UINT16 c = i386_read_stack();
                   13662:                        if((c >> 0) & 0xff) {
                   13663:                                msdos_putch((c >> 0) & 0xff);
                   13664:                        }
                   13665:                        if((c >> 8) & 0xff) {
                   13666:                                msdos_putch((c >> 8) & 0xff);
                   13667:                        }
                   13668:                }
1.1.1.29  root     13669:                break;
1.1.1.49  root     13670: //     case 0x06: // DOS 3.0+ internal - Invoke Critical Error
1.1.1.29  root     13671: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   13672: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
1.1.1.49  root     13673: //     case 0x09: // DOS 3.0+ internal - Flush and Free Disk Buffer
1.1.1.29  root     13674: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   13675: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   13676: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   13677:        case 0x0d:
                   13678:                {
                   13679:                        SYSTEMTIME time;
                   13680:                        FILETIME file_time;
                   13681:                        WORD dos_date, dos_time;
                   13682:                        GetLocalTime(&time);
                   13683:                        SystemTimeToFileTime(&time, &file_time);
                   13684:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   13685:                        REG16(AX) = dos_date;
                   13686:                        REG16(DX) = dos_time;
                   13687:                }
                   13688:                break;
                   13689: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   13690: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   13691: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   13692:        case 0x11:
                   13693:                {
                   13694:                        char path[MAX_PATH], *p;
                   13695:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13696:                        my_strupr(path);
                   13697:                        while((p = my_strchr(path, '/')) != NULL) {
                   13698:                                *p = '\\';
                   13699:                        }
                   13700:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   13701:                }
                   13702:                break;
                   13703:        case 0x12:
                   13704:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13705:                break;
                   13706:        case 0x13:
                   13707:                {
                   13708:                        char tmp[2] = {0};
                   13709:                        tmp[0] = i386_read_stack();
                   13710:                        my_strupr(tmp);
                   13711:                        REG8(AL) = tmp[0];
                   13712:                }
                   13713:                break;
                   13714:        case 0x14:
                   13715: #if defined(HAS_I386)
                   13716:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   13717: #else
                   13718:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13719: #endif
                   13720:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13721:                break;
                   13722: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     13723:        case 0x16:
                   13724:                if(REG16(BX) < 20) {
                   13725:                        SREG(ES) = SFT_TOP >> 4;
                   13726:                        i386_load_segment_descriptor(ES);
                   13727:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   13728:                        
                   13729:                        // update system file table
                   13730:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   13731:                        if(file_handler[REG16(BX)].valid) {
                   13732:                                int count = 0;
                   13733:                                for(int i = 0; i < 20; i++) {
                   13734:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   13735:                                                count++;
                   13736:                                        }
                   13737:                                }
                   13738:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   13739:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   13740:                                _lseek(REG16(BX), 0, SEEK_END);
                   13741:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   13742:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   13743:                        } else {
                   13744:                                memset(sft, 0, 0x3b);
                   13745:                        }
                   13746:                } else {
                   13747:                        REG16(AX) = 0x06;
                   13748:                        m_CF = 1;
                   13749:                }
                   13750:                break;
1.1.1.49  root     13751:        case 0x17:
                   13752:                {
                   13753:                        UINT16 drive = i386_read_stack();
                   13754:                        if(msdos_is_valid_drive(drive)) {
                   13755:                                msdos_cds_update(drive);
                   13756:                        }
                   13757:                        REG16(SI) = 88 * drive;
                   13758:                        SREG(DS) = (CDS_TOP >> 4);
                   13759:                        i386_load_segment_descriptor(DS);
                   13760:                }
                   13761:                break;
1.1.1.29  root     13762: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   13763: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   13764:        case 0x1a:
                   13765:                {
                   13766:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   13767:                        if(path[1] == ':') {
                   13768:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   13769:                                        REG8(AL) = path[0] - 'a' + 1;
                   13770:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   13771:                                        REG8(AL) = path[0] - 'A' + 1;
                   13772:                                } else {
                   13773:                                        REG8(AL) = 0xff; // invalid
                   13774:                                }
                   13775:                                strcpy(full, path);
                   13776:                                strcpy(path, full + 2);
                   13777:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   13778:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   13779:                                        REG8(AL) = full[0] - 'a' + 1;
                   13780:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   13781:                                        REG8(AL) = full[0] - 'A' + 1;
                   13782:                                } else {
                   13783:                                        REG8(AL) = 0xff; // invalid
                   13784:                                }
                   13785:                        } else {
                   13786:                                REG8(AL) = 0x00; // default
                   13787:                        }
                   13788:                }
                   13789:                break;
                   13790:        case 0x1b:
                   13791:                {
                   13792:                        int year = REG16(CX) + 1980;
                   13793:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   13794:                }
                   13795:                break;
                   13796: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   13797: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   13798:        case 0x1e:
                   13799:                {
                   13800:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   13801:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   13802:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   13803: #if defined(HAS_I386)
                   13804:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   13805: #else
                   13806:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   13807: #endif
                   13808:                        } else {
                   13809: #if defined(HAS_I386)
                   13810:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   13811: #else
                   13812:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   13813: #endif
                   13814:                        }
                   13815:                }
                   13816:                break;
1.1.1.49  root     13817:        case 0x1f:
                   13818:                {
                   13819:                        UINT16 drive = i386_read_stack();
                   13820:                        if(msdos_is_valid_drive(drive)) {
                   13821:                                msdos_cds_update(drive);
                   13822:                        }
                   13823:                        REG16(SI) = 88 * drive;
                   13824:                        SREG(ES) = (CDS_TOP >> 4);
                   13825:                        i386_load_segment_descriptor(ES);
                   13826:                }
                   13827:                break;
1.1.1.21  root     13828:        case 0x20:
                   13829:                {
                   13830:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13831:                        
                   13832:                        if(fd < 20) {
                   13833:                                SREG(ES) = current_psp;
                   13834:                                i386_load_segment_descriptor(ES);
                   13835:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   13836:                        } else {
                   13837:                                REG16(AX) = 0x06;
                   13838:                                m_CF = 1;
                   13839:                        }
                   13840:                }
                   13841:                break;
1.1.1.29  root     13842:        case 0x21:
                   13843:                msdos_int_21h_60h(0);
                   13844:                break;
1.1.1.49  root     13845:        case 0x22:
                   13846:                {
                   13847:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13848:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00) != 0xff) {
                   13849:                                sda->extended_error_code = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00);
                   13850:                        }
                   13851:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01) != 0xff) {
                   13852:                                sda->error_class         = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01);
                   13853:                        }
                   13854:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02) != 0xff) {
                   13855:                                sda->suggested_action    = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02);
                   13856:                        }
                   13857:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03) != 0xff) {
                   13858:                                sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03);
                   13859:                        }
                   13860:                }
                   13861:                break;
1.1.1.29  root     13862: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   13863: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   13864:        case 0x25:
                   13865:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13866:                break;
                   13867:        case 0x26:
                   13868:                REG8(AL) = REG8(CL);
                   13869:                msdos_int_21h_3dh();
                   13870:                break;
                   13871:        case 0x27:
                   13872:                msdos_int_21h_3eh();
                   13873:                break;
                   13874:        case 0x28:
                   13875:                REG16(AX) = REG16(BP);
                   13876:                msdos_int_21h_42h();
                   13877:                break;
                   13878:        case 0x29:
                   13879:                msdos_int_21h_3fh();
                   13880:                break;
                   13881: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   13882:        case 0x2b:
                   13883:                REG16(AX) = REG16(BP);
                   13884:                msdos_int_21h_44h();
                   13885:                break;
                   13886:        case 0x2c:
                   13887:                REG16(BX) = DEVICE_TOP >> 4;
                   13888:                REG16(AX) = 22;
                   13889:                break;
                   13890:        case 0x2d:
                   13891:                {
                   13892:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13893:                        REG16(AX) = sda->extended_error_code;
                   13894:                }
                   13895:                break;
                   13896:        case 0x2e:
                   13897:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     13898:                        SREG(ES) = 0x0001;
                   13899:                        i386_load_segment_descriptor(ES);
                   13900:                        REG16(DI) = 0x00;
                   13901:                } else if(REG8(DL) == 0x08) {
1.1.1.49  root     13902:                        // dummy parameter error message read routine is at fffc:0010
                   13903:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.22  root     13904:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     13905:                        REG16(DI) = 0x0010;
1.1.1.22  root     13906:                }
                   13907:                break;
1.1.1.29  root     13908:        case 0x2f:
                   13909:                if(REG16(DX) != 0) {
1.1.1.30  root     13910:                        dos_major_version = REG8(DL);
                   13911:                        dos_minor_version = REG8(DH);
1.1.1.29  root     13912:                } else {
                   13913:                        REG8(DL) = 7;
                   13914:                        REG8(DH) = 10;
                   13915:                }
                   13916:                break;
                   13917: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   13918: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     13919:        default:
                   13920:                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));
                   13921:                REG16(AX) = 0x01;
                   13922:                m_CF = 1;
                   13923:                break;
                   13924:        }
                   13925: }
                   13926: 
1.1.1.30  root     13927: inline void msdos_int_2fh_13h()
                   13928: {
                   13929:        static UINT16 prevDS = 0, prevDX = 0;
                   13930:        static UINT16 prevES = 0, prevBX = 0;
                   13931:        UINT16 tmp;
                   13932:        
                   13933:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   13934:        i386_load_segment_descriptor(DS);
                   13935:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   13936:        
                   13937:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   13938:        i386_load_segment_descriptor(ES);
                   13939:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   13940: }
                   13941: 
1.1.1.22  root     13942: inline void msdos_int_2fh_14h()
                   13943: {
                   13944:        switch(REG8(AL)) {
                   13945:        case 0x00:
1.1.1.29  root     13946:                // NLSFUNC.COM is installed
                   13947:                REG8(AL) = 0xff;
1.1.1.25  root     13948:                break;
                   13949:        case 0x01:
                   13950:        case 0x03:
                   13951:                REG8(AL) = 0x00;
                   13952:                active_code_page = REG16(BX);
                   13953:                msdos_nls_tables_update();
                   13954:                break;
                   13955:        case 0x02:
                   13956:                REG8(AL) = get_extended_country_info(REG16(BP));
                   13957:                break;
                   13958:        case 0x04:
1.1.1.42  root     13959:                for(int i = 0;; i++) {
                   13960:                        if(country_table[i].code == REG16(DX)) {
                   13961:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   13962:                                break;
                   13963:                        } else if(country_table[i].code == -1) {
                   13964:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13965:                                break;
                   13966:                        }
                   13967:                }
1.1.1.25  root     13968:                REG8(AL) = 0x00;
1.1.1.22  root     13969:                break;
                   13970:        default:
                   13971:                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));
                   13972:                REG16(AX) = 0x01;
                   13973:                m_CF = 1;
                   13974:                break;
                   13975:        }
                   13976: }
                   13977: 
                   13978: inline void msdos_int_2fh_15h()
                   13979: {
                   13980:        switch(REG8(AL)) {
1.1.1.29  root     13981:        case 0x00: // CD-ROM - Installation Check
                   13982:                if(REG16(BX) == 0x0000) {
1.1.1.53  root     13983: #ifdef SUPPORT_MSCDEX
1.1.1.43  root     13984:                        // MSCDEX is installed
                   13985:                        REG16(BX) = 0;
                   13986:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     13987:                                if(msdos_is_cdrom_drive(i)) {
                   13988:                                        if(REG16(BX) == 0) {
                   13989:                                                REG16(CX) = i;
1.1.1.43  root     13990:                                        }
1.1.1.44  root     13991:                                        REG16(BX)++;
1.1.1.43  root     13992:                                }
                   13993:                        }
                   13994: #else
1.1.1.29  root     13995:                        // MSCDEX is not installed
                   13996: //                     REG8(AL) = 0x00;
1.1.1.43  root     13997: #endif
1.1.1.29  root     13998:                } else {
                   13999:                        // GRAPHICS.COM is not installed
                   14000: //                     REG8(AL) = 0x00;
                   14001:                }
1.1.1.22  root     14002:                break;
1.1.1.43  root     14003:        case 0x0b:
1.1.1.44  root     14004:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   14005:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   14006:                REG16(BX) = 0xadad;
1.1.1.43  root     14007:                break;
                   14008:        case 0x0d:
1.1.1.44  root     14009:                for(int i = 0, n = 0; i < 26; i++) {
                   14010:                        if(msdos_is_cdrom_drive(i)) {
                   14011:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     14012:                        }
                   14013:                }
                   14014:                break;
1.1.1.22  root     14015:        case 0xff:
1.1.1.29  root     14016:                if(REG16(BX) == 0x0000) {
                   14017:                        // CORELCDX is not installed
                   14018:                } else {
                   14019:                        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));
                   14020:                        REG16(AX) = 0x01;
                   14021:                        m_CF = 1;
                   14022:                }
1.1.1.22  root     14023:                break;
1.1.1.21  root     14024:        default:
1.1.1.22  root     14025:                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     14026:                REG16(AX) = 0x01;
                   14027:                m_CF = 1;
                   14028:                break;
                   14029:        }
                   14030: }
                   14031: 
1.1       root     14032: inline void msdos_int_2fh_16h()
                   14033: {
                   14034:        switch(REG8(AL)) {
                   14035:        case 0x00:
1.1.1.14  root     14036:                if(no_windows) {
1.1.1.29  root     14037:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   14038: //                     REG8(AL) = 0x00;
1.1.1.14  root     14039:                } else {
1.1.1.30  root     14040:                        REG8(AL) = win_major_version;
                   14041:                        REG8(AH) = win_minor_version;
1.1       root     14042:                }
                   14043:                break;
1.1.1.43  root     14044:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     14045:                // from DOSBox
                   14046:                i386_set_a20_line(1);
                   14047:                break;
1.1.1.49  root     14048:        case 0x06: // Windows Enhanced Mode & 286 DOSX Exit Broadcast
1.1.1.43  root     14049:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   14050:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   14051:                break;
                   14052:        case 0x07:
                   14053:                // Virtual Device Call API
                   14054:                break;
1.1.1.22  root     14055:        case 0x0a:
                   14056:                if(!no_windows) {
                   14057:                        REG16(AX) = 0x0000;
1.1.1.30  root     14058:                        REG8(BH) = win_major_version;
                   14059:                        REG8(BL) = win_minor_version;
1.1.1.49  root     14060: //                     REG16(CX) = 0x0002; // standard
1.1.1.22  root     14061:                        REG16(CX) = 0x0003; // enhanced
                   14062:                }
                   14063:                break;
1.1.1.30  root     14064:        case 0x0b:
                   14065:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     14066:        case 0x0e:
                   14067:        case 0x0f:
1.1.1.30  root     14068:        case 0x10:
1.1.1.22  root     14069:        case 0x11:
                   14070:        case 0x12:
                   14071:        case 0x13:
                   14072:        case 0x14:
1.1.1.30  root     14073:        case 0x15:
1.1.1.43  root     14074:        case 0x81:
                   14075:        case 0x82:
1.1.1.44  root     14076:        case 0x84:
1.1.1.49  root     14077:        case 0x85:
1.1.1.33  root     14078:        case 0x86:
1.1.1.22  root     14079:        case 0x87:
1.1.1.30  root     14080:        case 0x89:
1.1.1.33  root     14081:        case 0x8a:
1.1.1.22  root     14082:                // function not supported, do not clear AX
                   14083:                break;
1.1.1.14  root     14084:        case 0x80:
                   14085:                Sleep(10);
1.1.1.35  root     14086:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     14087:                REG8(AL) = 0x00;
1.1.1.14  root     14088:                break;
1.1.1.33  root     14089:        case 0x83:
                   14090:                REG16(BX) = 0x01; // system vm id
                   14091:                break;
1.1.1.22  root     14092:        case 0x8e:
                   14093:                REG16(AX) = 0x00; // failed
                   14094:                break;
1.1.1.20  root     14095:        case 0x8f:
                   14096:                switch(REG8(DH)) {
                   14097:                case 0x01:
1.1.1.49  root     14098: //                     REG16(AX) = 0x0000; // close command selected but not yet acknowledged
                   14099: //                     REG16(AX) = 0x0001; // close command issued and acknowledged
                   14100:                        REG16(AX) = 0x168f; // close command not selected -- application should continue
                   14101:                        break;
                   14102:                default:
                   14103:                        REG16(AX) = 0x0000; // successful
1.1.1.20  root     14104:                        break;
                   14105:                }
                   14106:                break;
1.1       root     14107:        default:
1.1.1.22  root     14108:                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));
                   14109:                REG16(AX) = 0x01;
                   14110:                m_CF = 1;
                   14111:                break;
                   14112:        }
                   14113: }
                   14114: 
                   14115: inline void msdos_int_2fh_19h()
                   14116: {
                   14117:        switch(REG8(AL)) {
                   14118:        case 0x00:
1.1.1.29  root     14119:                // SHELLB.COM is not installed
                   14120: //             REG8(AL) = 0x00;
1.1.1.22  root     14121:                break;
                   14122:        case 0x01:
                   14123:        case 0x02:
                   14124:        case 0x03:
                   14125:        case 0x04:
                   14126:                REG16(AX) = 0x01;
                   14127:                m_CF = 1;
                   14128:                break;
1.1.1.29  root     14129:        case 0x80:
                   14130:                // IBM ROM-DOS v4.0 is not installed
                   14131: //             REG8(AL) = 0x00;
                   14132:                break;
1.1.1.22  root     14133:        default:
                   14134:                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     14135:                REG16(AX) = 0x01;
1.1.1.3   root     14136:                m_CF = 1;
1.1       root     14137:                break;
                   14138:        }
                   14139: }
                   14140: 
                   14141: inline void msdos_int_2fh_1ah()
                   14142: {
                   14143:        switch(REG8(AL)) {
                   14144:        case 0x00:
1.1.1.29  root     14145:                // ANSI.SYS is installed
1.1       root     14146:                REG8(AL) = 0xff;
                   14147:                break;
1.1.1.49  root     14148:        case 0x01:
1.1.1.50  root     14149:                if(REG8(CL) == 0x5f) {
                   14150:                        // set display information
                   14151:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   14152:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   14153:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   14154:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   14155:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   14156:                                
                   14157:                                if(cur_width != new_width || cur_height != new_height) {
                   14158:                                        pcbios_set_console_size(new_width, new_height, true);
                   14159:                                }
                   14160:                        }
                   14161:                } else if(REG8(CL) == 0x7f) {
1.1.1.49  root     14162:                        // get display information
1.1.1.50  root     14163:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   14164:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   14165:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   14166:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   14167:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   14168:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   14169:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   14170:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   14171:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   14172:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   14173:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.49  root     14174:                } else {
                   14175:                        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));
                   14176:                        REG16(AX) = 0x01;
                   14177:                        m_CF = 1;
                   14178:                }
                   14179:                break;
1.1       root     14180:        default:
1.1.1.22  root     14181:                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));
                   14182:                REG16(AX) = 0x01;
                   14183:                m_CF = 1;
                   14184:                break;
                   14185:        }
                   14186: }
                   14187: 
1.1.1.30  root     14188: inline void msdos_int_2fh_40h()
1.1.1.22  root     14189: {
                   14190:        switch(REG8(AL)) {
                   14191:        case 0x00:
1.1.1.30  root     14192:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   14193:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     14194:                break;
1.1.1.43  root     14195:        case 0x10:
                   14196:                // OS/2 v2.0+ - Installation Check
                   14197:                REG16(AX) = 0x01;
                   14198:                m_CF = 1;
                   14199:                break;
1.1.1.22  root     14200:        default:
                   14201:                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     14202:                REG16(AX) = 0x01;
1.1.1.3   root     14203:                m_CF = 1;
1.1       root     14204:                break;
                   14205:        }
                   14206: }
                   14207: 
                   14208: inline void msdos_int_2fh_43h()
                   14209: {
                   14210:        switch(REG8(AL)) {
                   14211:        case 0x00:
1.1.1.29  root     14212:                // XMS is installed ?
1.1.1.19  root     14213: #ifdef SUPPORT_XMS
                   14214:                if(support_xms) {
                   14215:                        REG8(AL) = 0x80;
1.1.1.44  root     14216:                }
                   14217: #endif
                   14218:                break;
                   14219:        case 0x08:
                   14220: #ifdef SUPPORT_XMS
                   14221:                if(support_xms) {
                   14222:                        REG8(AL) = 0x43;
                   14223:                        REG8(BL) = 0x01; // IBM PC/AT
                   14224:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   14225:                }
1.1.1.19  root     14226: #endif
                   14227:                break;
                   14228:        case 0x10:
                   14229:                SREG(ES) = XMS_TOP >> 4;
                   14230:                i386_load_segment_descriptor(ES);
1.1.1.26  root     14231:                REG16(BX) = 0x15;
1.1       root     14232:                break;
1.1.1.44  root     14233:        case 0xe0:
                   14234:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   14235:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   14236:                        break;
                   14237:                }
1.1       root     14238:        default:
1.1.1.22  root     14239:                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));
                   14240:                REG16(AX) = 0x01;
                   14241:                m_CF = 1;
                   14242:                break;
                   14243:        }
                   14244: }
                   14245: 
                   14246: inline void msdos_int_2fh_46h()
                   14247: {
                   14248:        switch(REG8(AL)) {
                   14249:        case 0x80:
1.1.1.29  root     14250:                // Windows v3.0 is not installed
                   14251: //             REG8(AL) = 0x00;
1.1.1.22  root     14252:                break;
                   14253:        default:
                   14254:                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));
                   14255:                REG16(AX) = 0x01;
                   14256:                m_CF = 1;
                   14257:                break;
                   14258:        }
                   14259: }
                   14260: 
                   14261: inline void msdos_int_2fh_48h()
                   14262: {
                   14263:        switch(REG8(AL)) {
                   14264:        case 0x00:
1.1.1.29  root     14265:                // DOSKEY is not installed
                   14266: //             REG8(AL) = 0x00;
1.1.1.22  root     14267:                break;
                   14268:        case 0x10:
                   14269:                msdos_int_21h_0ah();
                   14270:                REG16(AX) = 0x00;
                   14271:                break;
                   14272:        default:
                   14273:                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     14274:                REG16(AX) = 0x01;
1.1.1.3   root     14275:                m_CF = 1;
1.1       root     14276:                break;
                   14277:        }
                   14278: }
                   14279: 
                   14280: inline void msdos_int_2fh_4ah()
                   14281: {
                   14282:        switch(REG8(AL)) {
1.1.1.29  root     14283: #ifdef SUPPORT_HMA
                   14284:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   14285:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14286:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14287:                                // restore first free mcb in high memory area
                   14288:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14289:                        }
                   14290:                        int offset = 0xffff;
                   14291:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   14292:                                REG16(DI) = offset + 0x10;
                   14293:                        } else {
                   14294:                                REG16(DI) = 0xffff;
                   14295:                        }
                   14296:                } else {
                   14297:                        // HMA is already used
                   14298:                        REG16(BX) = 0;
                   14299:                        REG16(DI) = 0xffff;
                   14300:                }
                   14301:                SREG(ES) = 0xffff;
                   14302:                i386_load_segment_descriptor(ES);
                   14303:                break;
                   14304:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   14305:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14306:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14307:                                // restore first free mcb in high memory area
                   14308:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14309:                        }
                   14310:                        int size = REG16(BX), offset;
                   14311:                        if((size % 16) != 0) {
                   14312:                                size &= ~15;
                   14313:                                size += 16;
                   14314:                        }
                   14315:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   14316:                                REG16(BX) = size;
                   14317:                                REG16(DI) = offset + 0x10;
                   14318:                                is_hma_used_by_int_2fh = true;
                   14319:                        } else {
                   14320:                                REG16(BX) = 0;
                   14321:                                REG16(DI) = 0xffff;
                   14322:                        }
                   14323:                } else {
                   14324:                        // HMA is already used
                   14325:                        REG16(BX) = 0;
                   14326:                        REG16(DI) = 0xffff;
                   14327:                }
                   14328:                SREG(ES) = 0xffff;
                   14329:                i386_load_segment_descriptor(ES);
                   14330:                break;
                   14331:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   14332:                if(REG8(DL) == 0x00) {
                   14333:                        if(!is_hma_used_by_xms) {
                   14334:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14335:                                        // restore first free mcb in high memory area
                   14336:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14337:                                        is_hma_used_by_int_2fh = false;
                   14338:                                }
                   14339:                                int size = REG16(BX), offset;
                   14340:                                if((size % 16) != 0) {
                   14341:                                        size &= ~15;
                   14342:                                        size += 16;
                   14343:                                }
                   14344:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   14345: //                                     REG16(BX) = size;
                   14346:                                        SREG(ES) = 0xffff;
                   14347:                                        i386_load_segment_descriptor(ES);
                   14348:                                        REG16(DI) = offset + 0x10;
                   14349:                                        is_hma_used_by_int_2fh = true;
                   14350:                                } else {
                   14351:                                        REG16(DI) = 0xffff;
                   14352:                                }
                   14353:                        } else {
                   14354:                                REG16(DI) = 0xffff;
                   14355:                        }
                   14356:                } else if(REG8(DL) == 0x01) {
                   14357:                        if(!is_hma_used_by_xms) {
                   14358:                                int size = REG16(BX);
                   14359:                                if((size % 16) != 0) {
                   14360:                                        size &= ~15;
                   14361:                                        size += 16;
                   14362:                                }
                   14363:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   14364:                                        // memory block address is not changed
                   14365:                                } else {
                   14366:                                        REG16(DI) = 0xffff;
                   14367:                                }
                   14368:                        } else {
                   14369:                                REG16(DI) = 0xffff;
                   14370:                        }
                   14371:                } else if(REG8(DL) == 0x02) {
                   14372:                        if(!is_hma_used_by_xms) {
                   14373:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14374:                                        // restore first free mcb in high memory area
                   14375:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14376:                                        is_hma_used_by_int_2fh = false;
                   14377:                                } else {
                   14378:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   14379:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   14380:                                                is_hma_used_by_int_2fh = false;
                   14381:                                        }
                   14382:                                }
                   14383:                        }
                   14384:                } else {
                   14385:                        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));
                   14386:                        REG16(AX) = 0x01;
                   14387:                        m_CF = 1;
                   14388:                }
                   14389:                break;
                   14390:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   14391:                if(!is_hma_used_by_xms) {
                   14392:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14393:                                // restore first free mcb in high memory area
                   14394:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14395:                                is_hma_used_by_int_2fh = false;
                   14396:                        }
                   14397:                        REG16(AX) = 0x0000;
                   14398:                        SREG(ES) = 0xffff;
                   14399:                        i386_load_segment_descriptor(ES);
                   14400:                        REG16(DI) = 0x10;
                   14401:                }
                   14402:                break;
                   14403: #else
1.1       root     14404:        case 0x01:
                   14405:        case 0x02:
1.1.1.29  root     14406:                // HMA is already used
1.1.1.27  root     14407:                REG16(BX) = 0x0000;
1.1.1.3   root     14408:                SREG(ES) = 0xffff;
                   14409:                i386_load_segment_descriptor(ES);
1.1       root     14410:                REG16(DI) = 0xffff;
                   14411:                break;
1.1.1.19  root     14412:        case 0x03:
                   14413:                // unable to allocate
                   14414:                REG16(DI) = 0xffff;
                   14415:                break;
                   14416:        case 0x04:
                   14417:                // function not supported, do not clear AX
                   14418:                break;
1.1.1.29  root     14419: #endif
                   14420:        case 0x10:
1.1.1.42  root     14421:                switch(REG16(BX)) {
                   14422:                case 0x0000:
                   14423:                case 0x0001:
                   14424:                case 0x0002:
                   14425:                case 0x0003:
                   14426:                case 0x0004:
                   14427:                case 0x0005:
                   14428:                case 0x0006:
                   14429:                case 0x0007:
                   14430:                case 0x0008:
                   14431:                case 0x000a:
                   14432:                case 0x1234:
                   14433:                        // SMARTDRV v4.00+ is not installed
                   14434:                        break;
                   14435:                default:
1.1.1.29  root     14436:                        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));
                   14437:                        REG16(AX) = 0x01;
                   14438:                        m_CF = 1;
1.1.1.42  root     14439:                        break;
1.1.1.29  root     14440:                }
                   14441:                break;
                   14442:        case 0x11:
1.1.1.42  root     14443:                switch(REG16(BX)) {
                   14444:                case 0x0000:
                   14445:                case 0x0001:
                   14446:                case 0x0002:
                   14447:                case 0x0003:
                   14448:                case 0x0004:
                   14449:                case 0x0005:
                   14450:                case 0x0006:
                   14451:                case 0x0007:
                   14452:                case 0x0008:
                   14453:                case 0x0009:
                   14454:                case 0x000a:
                   14455:                case 0x000b:
                   14456:                case 0xfffe:
                   14457:                case 0xffff:
1.1.1.29  root     14458:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     14459:                        break;
                   14460:                default:
                   14461:                        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));
                   14462:                        REG16(AX) = 0x01;
                   14463:                        m_CF = 1;
                   14464:                        break;
                   14465:                }
                   14466:                break;
                   14467:        case 0x12:
                   14468:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   14469:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   14470:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   14471:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     14472:                } else {
                   14473:                        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));
                   14474:                        REG16(AX) = 0x01;
                   14475:                        m_CF = 1;
                   14476:                }
1.1.1.22  root     14477:                break;
1.1.1.42  root     14478:        case 0x13:
                   14479:                // DBLSPACE.BIN is not installed
                   14480:                break;
1.1.1.22  root     14481:        default:
                   14482:                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));
                   14483:                REG16(AX) = 0x01;
                   14484:                m_CF = 1;
                   14485:                break;
                   14486:        }
                   14487: }
                   14488: 
                   14489: inline void msdos_int_2fh_4bh()
                   14490: {
                   14491:        switch(REG8(AL)) {
1.1.1.24  root     14492:        case 0x01:
1.1.1.22  root     14493:        case 0x02:
1.1.1.29  root     14494:                // Task Switcher is not installed
1.1.1.24  root     14495:                break;
                   14496:        case 0x03:
                   14497:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   14498:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     14499:                break;
1.1.1.30  root     14500:        case 0x04:
                   14501:                REG16(BX) = 0x0000; // free switcher id successfully
                   14502:                break;
1.1.1.43  root     14503:        case 0x05:
                   14504:                REG16(BX) = 0x0000; // no instance data chain
                   14505:                SREG(ES) = 0x0000;
                   14506:                i386_load_segment_descriptor(ES);
                   14507:                break;
1.1       root     14508:        default:
1.1.1.22  root     14509:                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     14510:                REG16(AX) = 0x01;
1.1.1.3   root     14511:                m_CF = 1;
1.1       root     14512:                break;
                   14513:        }
                   14514: }
                   14515: 
1.1.1.44  root     14516: inline void msdos_int_2fh_4dh()
                   14517: {
                   14518:        switch(REG8(AL)) {
                   14519:        case 0x00:
                   14520:                // KKCFUNC is not installed ???
                   14521:                break;
                   14522:        default:
                   14523: //             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));
                   14524:                REG16(AX) = 0x01; // invalid function
                   14525:                m_CF = 1;
                   14526:                break;
                   14527:        }
                   14528: }
                   14529: 
1.1       root     14530: inline void msdos_int_2fh_4fh()
                   14531: {
                   14532:        switch(REG8(AL)) {
                   14533:        case 0x00:
1.1.1.29  root     14534:                // BILING is installed
1.1.1.27  root     14535:                REG16(AX) = 0x0000;
                   14536:                REG8(DL) = 0x01;        // major version
                   14537:                REG8(DH) = 0x00;        // minor version
1.1       root     14538:                break;
                   14539:        case 0x01:
1.1.1.27  root     14540:                REG16(AX) = 0x0000;
1.1       root     14541:                REG16(BX) = active_code_page;
                   14542:                break;
                   14543:        default:
1.1.1.22  root     14544:                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));
                   14545:                REG16(AX) = 0x01;
                   14546:                m_CF = 1;
                   14547:                break;
                   14548:        }
                   14549: }
                   14550: 
                   14551: inline void msdos_int_2fh_55h()
                   14552: {
                   14553:        switch(REG8(AL)) {
                   14554:        case 0x00:
                   14555:        case 0x01:
                   14556: //             unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14557:                break;
                   14558:        default:
                   14559:                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     14560:                REG16(AX) = 0x01;
1.1.1.3   root     14561:                m_CF = 1;
1.1       root     14562:                break;
                   14563:        }
                   14564: }
                   14565: 
1.1.1.44  root     14566: inline void msdos_int_2fh_56h()
                   14567: {
                   14568:        switch(REG8(AL)) {
                   14569:        case 0x00:
                   14570:                // INTERLNK is not installed
                   14571:                break;
                   14572:        case 0x01:
                   14573:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   14574: //             if(msdos_is_remote_drive(REG8(BH))) {
                   14575: //                     REG8(AL) = 0x00;
                   14576: //             }
                   14577:                break;
                   14578:        default:
                   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: 
1.1.1.24  root     14586: inline void msdos_int_2fh_adh()
                   14587: {
                   14588:        switch(REG8(AL)) {
                   14589:        case 0x00:
1.1.1.29  root     14590:                // DISPLAY.SYS is installed
1.1.1.24  root     14591:                REG8(AL) = 0xff;
                   14592:                REG16(BX) = 0x100; // ???
                   14593:                break;
                   14594:        case 0x01:
                   14595:                active_code_page = REG16(BX);
                   14596:                msdos_nls_tables_update();
                   14597:                REG16(AX) = 0x01;
                   14598:                break;
                   14599:        case 0x02:
                   14600:                REG16(BX) = active_code_page;
                   14601:                break;
                   14602:        case 0x03:
                   14603:                // FIXME
                   14604:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   14605:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   14606:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   14607:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   14608:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   14609:                break;
                   14610:        case 0x80:
1.1.1.49  root     14611:                // KEYB.COM is not installed
                   14612:                break;
1.1.1.24  root     14613:        default:
                   14614:                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));
                   14615:                REG16(AX) = 0x01;
                   14616:                m_CF = 1;
                   14617:                break;
                   14618:        }
                   14619: }
                   14620: 
1.1       root     14621: inline void msdos_int_2fh_aeh()
                   14622: {
                   14623:        switch(REG8(AL)) {
                   14624:        case 0x00:
1.1.1.28  root     14625:                // FIXME: we need to check the given command line
                   14626:                REG8(AL) = 0x00; // the command should be executed as usual
                   14627: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     14628:                break;
                   14629:        case 0x01:
                   14630:                {
                   14631:                        char command[MAX_PATH];
                   14632:                        memset(command, 0, sizeof(command));
1.1.1.3   root     14633:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     14634:                        
                   14635:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   14636:                        param->env_seg = 0;
                   14637:                        param->cmd_line.w.l = 44;
                   14638:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   14639:                        param->fcb1.w.l = 24;
                   14640:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   14641:                        param->fcb2.w.l = 24;
                   14642:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   14643:                        
                   14644:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   14645:                        
                   14646:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     14647:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   14648:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     14649:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   14650:                        
1.1.1.28  root     14651:                        try {
                   14652:                                msdos_process_exec(command, param, 0);
                   14653:                        } catch(...) {
                   14654:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     14655:                        }
                   14656:                }
                   14657:                break;
                   14658:        default:
1.1.1.22  root     14659:                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     14660:                REG16(AX) = 0x01;
1.1.1.3   root     14661:                m_CF = 1;
1.1       root     14662:                break;
                   14663:        }
                   14664: }
                   14665: 
1.1.1.34  root     14666: inline void msdos_int_2fh_b7h()
                   14667: {
                   14668:        switch(REG8(AL)) {
                   14669:        case 0x00:
                   14670:                // APPEND is not installed
                   14671: //             REG8(AL) = 0x00;
                   14672:                break;
1.1.1.44  root     14673:        case 0x06:
                   14674:                REG16(BX) = 0x0000;
                   14675:                break;
1.1.1.34  root     14676:        case 0x07:
1.1.1.43  root     14677:        case 0x11:
1.1.1.34  root     14678:                // COMMAND.COM calls this service without checking APPEND is installed
                   14679:                break;
                   14680:        default:
                   14681:                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));
                   14682:                REG16(AX) = 0x01;
                   14683:                m_CF = 1;
                   14684:                break;
                   14685:        }
                   14686: }
                   14687: 
1.1.1.24  root     14688: inline void msdos_int_33h_0000h()
                   14689: {
                   14690:        REG16(AX) = 0xffff; // hardware/driver installed
                   14691:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14692: }
                   14693: 
                   14694: inline void msdos_int_33h_0001h()
                   14695: {
1.1.1.34  root     14696:        if(mouse.hidden > 0) {
                   14697:                mouse.hidden--;
                   14698:        }
                   14699:        if(mouse.hidden == 0) {
1.1.1.24  root     14700:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   14701:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   14702:                }
                   14703:                pic[1].imr &= ~0x10;    // enable irq12
                   14704:        }
                   14705: }
                   14706: 
                   14707: inline void msdos_int_33h_0002h()
                   14708: {
1.1.1.34  root     14709:        mouse.hidden++;
                   14710: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
                   14711:        pic[1].imr |= 0x10;     // enable irq12
1.1.1.24  root     14712: }
                   14713: 
                   14714: inline void msdos_int_33h_0003h()
                   14715: {
1.1.1.34  root     14716: //     if(mouse.hidden > 0) {
                   14717:                update_console_input();
                   14718: //     }
1.1.1.24  root     14719:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     14720:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   14721:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   14722: }
                   14723: 
                   14724: inline void msdos_int_33h_0004h()
                   14725: {
                   14726:        mouse.position.x = REG16(CX);
                   14727:        mouse.position.x = REG16(DX);
1.1.1.24  root     14728: }
                   14729: 
                   14730: inline void msdos_int_33h_0005h()
                   14731: {
1.1.1.34  root     14732: //     if(mouse.hidden > 0) {
                   14733:                update_console_input();
                   14734: //     }
1.1.1.24  root     14735:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14736:                int idx = REG16(BX);
1.1.1.34  root     14737:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   14738:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   14739:                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     14740:                mouse.buttons[idx].pressed_times = 0;
                   14741:        } else {
                   14742:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14743:        }
                   14744:        REG16(AX) = mouse.get_buttons();
                   14745: }
                   14746: 
                   14747: inline void msdos_int_33h_0006h()
                   14748: {
1.1.1.34  root     14749: //     if(mouse.hidden > 0) {
                   14750:                update_console_input();
                   14751: //     }
1.1.1.24  root     14752:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14753:                int idx = REG16(BX);
1.1.1.34  root     14754:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   14755:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   14756:                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     14757:                mouse.buttons[idx].released_times = 0;
                   14758:        } else {
                   14759:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14760:        }
                   14761:        REG16(AX) = mouse.get_buttons();
                   14762: }
                   14763: 
                   14764: inline void msdos_int_33h_0007h()
                   14765: {
                   14766:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   14767:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   14768: }
                   14769: 
                   14770: inline void msdos_int_33h_0008h()
                   14771: {
                   14772:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   14773:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   14774: }
                   14775: 
                   14776: inline void msdos_int_33h_0009h()
                   14777: {
                   14778:        mouse.hot_spot[0] = REG16(BX);
                   14779:        mouse.hot_spot[1] = REG16(CX);
                   14780: }
                   14781: 
1.1.1.49  root     14782: inline void msdos_int_33h_000ah()
                   14783: {
                   14784:        mouse.screen_mask = REG16(CX);
                   14785:        mouse.cursor_mask = REG16(DX);
                   14786: }
                   14787: 
1.1.1.24  root     14788: inline void msdos_int_33h_000bh()
                   14789: {
1.1.1.34  root     14790: //     if(mouse.hidden > 0) {
                   14791:                update_console_input();
                   14792: //     }
1.1.1.24  root     14793:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14794:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   14795:        mouse.prev_position.x = mouse.position.x;
                   14796:        mouse.prev_position.y = mouse.position.y;
                   14797:        REG16(CX) = dx;
                   14798:        REG16(DX) = dy;
                   14799: }
                   14800: 
                   14801: inline void msdos_int_33h_000ch()
                   14802: {
                   14803:        mouse.call_mask = REG16(CX);
                   14804:        mouse.call_addr.w.l = REG16(DX);
                   14805:        mouse.call_addr.w.h = SREG(ES);
                   14806: }
                   14807: 
                   14808: inline void msdos_int_33h_000fh()
                   14809: {
                   14810:        mouse.mickey.x = REG16(CX);
                   14811:        mouse.mickey.y = REG16(DX);
                   14812: }
                   14813: 
                   14814: inline void msdos_int_33h_0011h()
                   14815: {
                   14816:        REG16(AX) = 0xffff;
                   14817:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14818: }
                   14819: 
                   14820: inline void msdos_int_33h_0014h()
                   14821: {
                   14822:        UINT16 old_mask = mouse.call_mask;
                   14823:        UINT16 old_ofs = mouse.call_addr.w.l;
                   14824:        UINT16 old_seg = mouse.call_addr.w.h;
                   14825:        
                   14826:        mouse.call_mask = REG16(CX);
                   14827:        mouse.call_addr.w.l = REG16(DX);
                   14828:        mouse.call_addr.w.h = SREG(ES);
                   14829:        
                   14830:        REG16(CX) = old_mask;
                   14831:        REG16(DX) = old_ofs;
                   14832:        SREG(ES) = old_seg;
                   14833:        i386_load_segment_descriptor(ES);
                   14834: }
                   14835: 
                   14836: inline void msdos_int_33h_0015h()
                   14837: {
                   14838:        REG16(BX) = sizeof(mouse);
                   14839: }
                   14840: 
                   14841: inline void msdos_int_33h_0016h()
                   14842: {
                   14843:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   14844: }
                   14845: 
                   14846: inline void msdos_int_33h_0017h()
                   14847: {
                   14848:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   14849: }
                   14850: 
1.1.1.43  root     14851: inline void msdos_int_33h_0018h()
                   14852: {
                   14853:        for(int i = 0; i < 8; i++) {
                   14854:                if(REG16(CX) & (1 << i)) {
                   14855:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   14856:                                // event handler already exists
                   14857:                                REG16(AX) = 0xffff;
                   14858:                                break;
                   14859:                        }
                   14860:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   14861:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   14862:                }
                   14863:        }
                   14864: }
                   14865: 
                   14866: inline void msdos_int_33h_0019h()
                   14867: {
                   14868:        UINT16 call_mask = REG16(CX);
                   14869:        
                   14870:        REG16(CX) = 0;
                   14871:        
                   14872:        for(int i = 0; i < 8; i++) {
                   14873:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   14874:                        for(int j = 0; j < 8; j++) {
                   14875:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   14876:                                        REG16(CX) |= (1 << j);
                   14877:                                }
                   14878:                        }
                   14879:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   14880:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   14881:                        break;
                   14882:                }
                   14883:        }
                   14884: }
                   14885: 
1.1.1.24  root     14886: inline void msdos_int_33h_001ah()
                   14887: {
                   14888:        mouse.sensitivity[0] = REG16(BX);
                   14889:        mouse.sensitivity[1] = REG16(CX);
                   14890:        mouse.sensitivity[2] = REG16(DX);
                   14891: }
                   14892: 
                   14893: inline void msdos_int_33h_001bh()
                   14894: {
                   14895:        REG16(BX) = mouse.sensitivity[0];
                   14896:        REG16(CX) = mouse.sensitivity[1];
                   14897:        REG16(DX) = mouse.sensitivity[2];
                   14898: }
                   14899: 
                   14900: inline void msdos_int_33h_001dh()
                   14901: {
                   14902:        mouse.display_page = REG16(BX);
                   14903: }
                   14904: 
                   14905: inline void msdos_int_33h_001eh()
                   14906: {
                   14907:        REG16(BX) = mouse.display_page;
                   14908: }
                   14909: 
1.1.1.34  root     14910: inline void msdos_int_33h_001fh()
                   14911: {
                   14912:        // from DOSBox
                   14913:        REG16(BX) = 0x0000;
                   14914:        SREG(ES) = 0x0000;
                   14915:        i386_load_segment_descriptor(ES);
                   14916:        mouse.enabled = false;
                   14917:        mouse.old_hidden = mouse.hidden;
                   14918:        mouse.hidden = 1;
                   14919: }
                   14920: 
                   14921: inline void msdos_int_33h_0020h()
                   14922: {
                   14923:        // from DOSBox
                   14924:        mouse.enabled = true;
                   14925:        mouse.hidden = mouse.old_hidden;
                   14926: }
                   14927: 
1.1.1.24  root     14928: inline void msdos_int_33h_0021h()
                   14929: {
                   14930:        REG16(AX) = 0xffff;
                   14931:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14932: }
                   14933: 
                   14934: inline void msdos_int_33h_0022h()
                   14935: {
                   14936:        mouse.language = REG16(BX);
                   14937: }
                   14938: 
                   14939: inline void msdos_int_33h_0023h()
                   14940: {
                   14941:        REG16(BX) = mouse.language;
                   14942: }
                   14943: 
                   14944: inline void msdos_int_33h_0024h()
                   14945: {
                   14946:        REG16(BX) = 0x0805; // V8.05
                   14947:        REG16(CX) = 0x0400; // PS/2
                   14948: }
                   14949: 
1.1.1.49  root     14950: inline void msdos_int_33h_0025h()
                   14951: {
                   14952:        REG16(AX) = 0x8000; // driver (not TSR), software text cursor
                   14953: }
                   14954: 
1.1.1.24  root     14955: inline void msdos_int_33h_0026h()
                   14956: {
                   14957:        REG16(BX) = 0x0000;
                   14958:        REG16(CX) = mouse.max_position.x;
                   14959:        REG16(DX) = mouse.max_position.y;
                   14960: }
                   14961: 
1.1.1.49  root     14962: inline void msdos_int_33h_0027h()
                   14963: {
                   14964: //     if(mouse.hidden > 0) {
                   14965:                update_console_input();
                   14966: //     }
                   14967:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14968:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   14969:        mouse.prev_position.x = mouse.position.x;
                   14970:        mouse.prev_position.y = mouse.position.y;
                   14971:        REG16(AX) = mouse.screen_mask;
                   14972:        REG16(BX) = mouse.cursor_mask;
                   14973:        REG16(CX) = dx;
                   14974:        REG16(DX) = dy;
                   14975: }
                   14976: 
                   14977: inline void msdos_int_33h_0028h()
                   14978: {
                   14979:        if(REG16(CX) != 0) {
                   14980:                UINT8 tmp = REG8(AL);
                   14981:                REG8(AL) = REG8(CL);
                   14982:                pcbios_int_10h_00h();
                   14983:                REG8(AL) = tmp;
                   14984:        }
                   14985:        REG8(CL) = 0x00; // successful
                   14986: }
                   14987: 
                   14988: inline void msdos_int_33h_0029h()
                   14989: {
                   14990:        switch(REG16(CX)) {
                   14991:        case 0x0000:
                   14992:                REG16(CX) = 0x0003;
                   14993:                sprintf((char *)(mem + WORK_TOP), "TEXT Mode (80x25)$");
                   14994:                break;
                   14995:        case 0x0003:
                   14996:                REG16(CX) = 0x0070;
                   14997:                sprintf((char *)(mem + WORK_TOP), "V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   14998:                break;
                   14999:        case 0x0070:
                   15000:                REG16(CX) = 0x0071;
                   15001:                sprintf((char *)(mem + WORK_TOP), "Extended CGA V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15002:                break;
                   15003:        case 0x0071:
                   15004:                REG16(CX) = 0x0073;
                   15005:                sprintf((char *)(mem + WORK_TOP), "Extended CGA TEXT Mode (80x25)$");
                   15006:                break;
                   15007:        default:
                   15008:                REG16(CX) = 0x0000;
                   15009:                break;
                   15010:        }
                   15011:        if(REG16(CX) != 0) {
                   15012:                SREG(DS) = (WORK_TOP >> 4);
                   15013:        } else {
                   15014:                SREG(DS) = 0x0000;
                   15015:        }
                   15016:        i386_load_segment_descriptor(DS);
                   15017:        REG16(DX) = 0x0000;
                   15018: }
                   15019: 
1.1.1.24  root     15020: inline void msdos_int_33h_002ah()
                   15021: {
1.1.1.34  root     15022:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     15023:        REG16(BX) = mouse.hot_spot[0];
                   15024:        REG16(CX) = mouse.hot_spot[1];
                   15025:        REG16(DX) = 4; // PS/2
                   15026: }
                   15027: 
                   15028: inline void msdos_int_33h_0031h()
                   15029: {
                   15030:        REG16(AX) = mouse.min_position.x;
                   15031:        REG16(BX) = mouse.min_position.y;
                   15032:        REG16(CX) = mouse.max_position.x;
                   15033:        REG16(DX) = mouse.max_position.y;
                   15034: }
                   15035: 
                   15036: inline void msdos_int_33h_0032h()
                   15037: {
                   15038:        REG16(AX) = 0;
1.1.1.49  root     15039:        REG16(AX) |= 0x8000; // 0025h
1.1.1.24  root     15040:        REG16(AX) |= 0x4000; // 0026h
1.1.1.49  root     15041:        REG16(AX) |= 0x2000; // 0027h
1.1.1.24  root     15042: //     REG16(AX) |= 0x1000; // 0028h
                   15043: //     REG16(AX) |= 0x0800; // 0029h
                   15044:        REG16(AX) |= 0x0400; // 002ah
                   15045: //     REG16(AX) |= 0x0200; // 002bh
                   15046: //     REG16(AX) |= 0x0100; // 002ch
                   15047: //     REG16(AX) |= 0x0080; // 002dh
                   15048: //     REG16(AX) |= 0x0040; // 002eh
                   15049:        REG16(AX) |= 0x0020; // 002fh
                   15050: //     REG16(AX) |= 0x0010; // 0030h
                   15051:        REG16(AX) |= 0x0008; // 0031h
                   15052:        REG16(AX) |= 0x0004; // 0032h
                   15053: //     REG16(AX) |= 0x0002; // 0033h
                   15054: //     REG16(AX) |= 0x0001; // 0034h
                   15055: }
                   15056: 
1.1.1.49  root     15057: inline void msdos_int_33h_004dh()
                   15058: {
                   15059:        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), "Copyright 2017 MS-DOS Player");
                   15060: }
                   15061: 
                   15062: inline void msdos_int_33h_006dh()
                   15063: {
                   15064:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x08; // V8.05
                   15065:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = 0x05;
                   15066: }
                   15067: 
1.1.1.19  root     15068: inline void msdos_int_67h_40h()
                   15069: {
                   15070:        if(!support_ems) {
                   15071:                REG8(AH) = 0x84;
                   15072:        } else {
                   15073:                REG8(AH) = 0x00;
                   15074:        }
                   15075: }
                   15076: 
                   15077: inline void msdos_int_67h_41h()
                   15078: {
                   15079:        if(!support_ems) {
                   15080:                REG8(AH) = 0x84;
                   15081:        } else {
                   15082:                REG8(AH) = 0x00;
                   15083:                REG16(BX) = EMS_TOP >> 4;
                   15084:        }
                   15085: }
                   15086: 
                   15087: inline void msdos_int_67h_42h()
                   15088: {
                   15089:        if(!support_ems) {
                   15090:                REG8(AH) = 0x84;
                   15091:        } else {
                   15092:                REG8(AH) = 0x00;
                   15093:                REG16(BX) = free_ems_pages;
                   15094:                REG16(DX) = MAX_EMS_PAGES;
                   15095:        }
                   15096: }
                   15097: 
                   15098: inline void msdos_int_67h_43h()
                   15099: {
                   15100:        if(!support_ems) {
                   15101:                REG8(AH) = 0x84;
                   15102:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15103:                REG8(AH) = 0x87;
                   15104:        } else if(REG16(BX) > free_ems_pages) {
                   15105:                REG8(AH) = 0x88;
                   15106:        } else if(REG16(BX) == 0) {
                   15107:                REG8(AH) = 0x89;
                   15108:        } else {
1.1.1.31  root     15109:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15110:                        if(!ems_handles[i].allocated) {
                   15111:                                ems_allocate_pages(i, REG16(BX));
                   15112:                                REG8(AH) = 0x00;
                   15113:                                REG16(DX) = i;
                   15114:                                return;
                   15115:                        }
                   15116:                }
                   15117:                REG8(AH) = 0x85;
                   15118:        }
                   15119: }
                   15120: 
                   15121: inline void msdos_int_67h_44h()
                   15122: {
                   15123:        if(!support_ems) {
                   15124:                REG8(AH) = 0x84;
1.1.1.31  root     15125:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15126:                REG8(AH) = 0x83;
                   15127:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   15128:                REG8(AH) = 0x8a;
                   15129: //     } else if(!(REG8(AL) < 4)) {
                   15130: //             REG8(AH) = 0x8b;
                   15131:        } else if(REG16(BX) == 0xffff) {
                   15132:                ems_unmap_page(REG8(AL) & 3);
                   15133:                REG8(AH) = 0x00;
                   15134:        } else {
                   15135:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   15136:                REG8(AH) = 0x00;
                   15137:        }
                   15138: }
                   15139: 
                   15140: inline void msdos_int_67h_45h()
                   15141: {
                   15142:        if(!support_ems) {
                   15143:                REG8(AH) = 0x84;
1.1.1.31  root     15144:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15145:                REG8(AH) = 0x83;
                   15146:        } else {
                   15147:                ems_release_pages(REG16(DX));
                   15148:                REG8(AH) = 0x00;
                   15149:        }
                   15150: }
                   15151: 
                   15152: inline void msdos_int_67h_46h()
                   15153: {
                   15154:        if(!support_ems) {
                   15155:                REG8(AH) = 0x84;
                   15156:        } else {
1.1.1.29  root     15157: //             REG16(AX) = 0x0032; // EMS 3.2
                   15158:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     15159:        }
                   15160: }
                   15161: 
                   15162: inline void msdos_int_67h_47h()
                   15163: {
                   15164:        // NOTE: the map data should be stored in the specified ems page, not process data
                   15165:        process_t *process = msdos_process_info_get(current_psp);
                   15166:        
                   15167:        if(!support_ems) {
                   15168:                REG8(AH) = 0x84;
1.1.1.31  root     15169: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15170: //             REG8(AH) = 0x83;
                   15171:        } else if(process->ems_pages_stored) {
                   15172:                REG8(AH) = 0x8d;
                   15173:        } else {
                   15174:                for(int i = 0; i < 4; i++) {
                   15175:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   15176:                        process->ems_pages[i].page   = ems_pages[i].page;
                   15177:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   15178:                }
                   15179:                process->ems_pages_stored = true;
                   15180:                REG8(AH) = 0x00;
                   15181:        }
                   15182: }
                   15183: 
                   15184: inline void msdos_int_67h_48h()
                   15185: {
                   15186:        // NOTE: the map data should be restored from the specified ems page, not process data
                   15187:        process_t *process = msdos_process_info_get(current_psp);
                   15188:        
                   15189:        if(!support_ems) {
                   15190:                REG8(AH) = 0x84;
1.1.1.31  root     15191: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15192: //             REG8(AH) = 0x83;
                   15193:        } else if(!process->ems_pages_stored) {
                   15194:                REG8(AH) = 0x8e;
                   15195:        } else {
                   15196:                for(int i = 0; i < 4; i++) {
                   15197:                        if(process->ems_pages[i].mapped) {
                   15198:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   15199:                        } else {
                   15200:                                ems_unmap_page(i);
                   15201:                        }
                   15202:                }
                   15203:                process->ems_pages_stored = false;
                   15204:                REG8(AH) = 0x00;
                   15205:        }
                   15206: }
                   15207: 
                   15208: inline void msdos_int_67h_4bh()
                   15209: {
                   15210:        if(!support_ems) {
                   15211:                REG8(AH) = 0x84;
                   15212:        } else {
                   15213:                REG8(AH) = 0x00;
                   15214:                REG16(BX) = 0;
1.1.1.31  root     15215:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15216:                        if(ems_handles[i].allocated) {
                   15217:                                REG16(BX)++;
                   15218:                        }
                   15219:                }
                   15220:        }
                   15221: }
                   15222: 
                   15223: inline void msdos_int_67h_4ch()
                   15224: {
                   15225:        if(!support_ems) {
                   15226:                REG8(AH) = 0x84;
1.1.1.31  root     15227:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15228:                REG8(AH) = 0x83;
                   15229:        } else {
                   15230:                REG8(AH) = 0x00;
                   15231:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   15232:        }
                   15233: }
                   15234: 
                   15235: inline void msdos_int_67h_4dh()
                   15236: {
                   15237:        if(!support_ems) {
                   15238:                REG8(AH) = 0x84;
                   15239:        } else {
                   15240:                REG8(AH) = 0x00;
                   15241:                REG16(BX) = 0;
1.1.1.31  root     15242:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15243:                        if(ems_handles[i].allocated) {
                   15244:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   15245:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   15246:                                REG16(BX)++;
                   15247:                        }
                   15248:                }
                   15249:        }
                   15250: }
                   15251: 
1.1.1.20  root     15252: inline void msdos_int_67h_4eh()
                   15253: {
                   15254:        if(!support_ems) {
                   15255:                REG8(AH) = 0x84;
                   15256:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15257:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   15258:                        // save page map
                   15259:                        for(int i = 0; i < 4; i++) {
                   15260:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15261:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15262:                        }
                   15263:                }
                   15264:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15265:                        // restore page map
                   15266:                        for(int i = 0; i < 4; i++) {
                   15267:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15268:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15269:                                
1.1.1.31  root     15270:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     15271:                                        ems_map_page(i, handle, page);
                   15272:                                } else {
                   15273:                                        ems_unmap_page(i);
                   15274:                                }
                   15275:                        }
                   15276:                }
                   15277:                REG8(AH) = 0x00;
                   15278:        } else if(REG8(AL) == 0x03) {
                   15279:                REG8(AH) = 0x00;
1.1.1.21  root     15280:                REG8(AL) = 4 * 4;
                   15281:        } else {
1.1.1.22  root     15282:                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     15283:                REG8(AH) = 0x8f;
                   15284:        }
                   15285: }
                   15286: 
                   15287: inline void msdos_int_67h_4fh()
                   15288: {
                   15289:        if(!support_ems) {
                   15290:                REG8(AH) = 0x84;
                   15291:        } else if(REG8(AL) == 0x00) {
                   15292:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15293:                
                   15294:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   15295:                for(int i = 0; i < count; i++) {
                   15296:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   15297:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15298:                        
                   15299: //                     if(!(physical < 4)) {
                   15300: //                             REG8(AH) = 0x8b;
                   15301: //                             return;
                   15302: //                     }
                   15303:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     15304:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   15305:                        *(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     15306:                }
                   15307:                REG8(AH) = 0x00;
                   15308:        } else if(REG8(AL) == 0x01) {
                   15309:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15310:                
                   15311:                for(int i = 0; i < count; i++) {
                   15312:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   15313:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15314:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   15315:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   15316:                        
                   15317: //                     if(!(physical < 4)) {
                   15318: //                             REG8(AH) = 0x8b;
                   15319: //                             return;
                   15320: //                     } else
1.1.1.41  root     15321:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     15322:                                ems_map_page(physical & 3, handle, logical);
                   15323:                        } else {
1.1.1.41  root     15324:                                ems_unmap_page(physical & 3);
1.1.1.21  root     15325:                        }
                   15326:                }
                   15327:                REG8(AH) = 0x00;
                   15328:        } else if(REG8(AL) == 0x02) {
                   15329:                REG8(AH) = 0x00;
                   15330:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     15331:        } else {
1.1.1.22  root     15332:                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     15333:                REG8(AH) = 0x8f;
                   15334:        }
                   15335: }
                   15336: 
                   15337: inline void msdos_int_67h_50h()
                   15338: {
                   15339:        if(!support_ems) {
                   15340:                REG8(AH) = 0x84;
1.1.1.31  root     15341:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     15342:                REG8(AH) = 0x83;
                   15343:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15344:                for(int i = 0; i < REG16(CX); i++) {
                   15345:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15346:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15347:                        
                   15348:                        if(REG8(AL) == 0x01) {
                   15349:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15350:                        }
                   15351: //                     if(!(physical < 4)) {
                   15352: //                             REG8(AH) = 0x8b;
                   15353: //                             return;
                   15354: //                     } else
                   15355:                        if(logical == 0xffff) {
                   15356:                                ems_unmap_page(physical & 3);
                   15357:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15358:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15359:                        } else {
                   15360:                                REG8(AH) = 0x8a;
                   15361:                                return;
                   15362:                        }
                   15363:                }
                   15364:                REG8(AH) = 0x00;
                   15365:        } else {
1.1.1.22  root     15366:                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     15367:                REG8(AH) = 0x8f;
                   15368:        }
                   15369: }
                   15370: 
1.1.1.19  root     15371: inline void msdos_int_67h_51h()
                   15372: {
                   15373:        if(!support_ems) {
                   15374:                REG8(AH) = 0x84;
1.1.1.31  root     15375:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15376:                REG8(AH) = 0x83;
                   15377:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15378:                REG8(AH) = 0x87;
                   15379:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   15380:                REG8(AH) = 0x88;
                   15381:        } else {
                   15382:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   15383:                REG8(AH) = 0x00;
                   15384:        }
                   15385: }
                   15386: 
1.1.1.20  root     15387: inline void msdos_int_67h_52h()
                   15388: {
                   15389:        if(!support_ems) {
                   15390:                REG8(AH) = 0x84;
1.1.1.31  root     15391: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15392: //             REG8(AH) = 0x83;
1.1.1.20  root     15393:        } else if(REG8(AL) == 0x00) {
                   15394:                REG8(AL) = 0x00; // handle is volatile
                   15395:                REG8(AH) = 0x00;
                   15396:        } else if(REG8(AL) == 0x01) {
                   15397:                if(REG8(BL) == 0x00) {
                   15398:                        REG8(AH) = 0x00;
                   15399:                } else {
                   15400:                        REG8(AH) = 0x90; // undefined attribute type
                   15401:                }
                   15402:        } else if(REG8(AL) == 0x02) {
                   15403:                REG8(AL) = 0x00; // only volatile handles supported
                   15404:                REG8(AH) = 0x00;
                   15405:        } else {
1.1.1.22  root     15406:                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     15407:                REG8(AH) = 0x8f;
                   15408:        }
                   15409: }
                   15410: 
1.1.1.19  root     15411: inline void msdos_int_67h_53h()
                   15412: {
                   15413:        if(!support_ems) {
                   15414:                REG8(AH) = 0x84;
1.1.1.31  root     15415:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15416:                REG8(AH) = 0x83;
                   15417:        } else if(REG8(AL) == 0x00) {
                   15418:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   15419:                REG8(AH) = 0x00;
                   15420:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     15421:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15422:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15423:                                REG8(AH) = 0xa1;
                   15424:                                return;
                   15425:                        }
                   15426:                }
                   15427:                REG8(AH) = 0x00;
                   15428:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   15429:        } else {
1.1.1.22  root     15430:                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     15431:                REG8(AH) = 0x8f;
1.1.1.19  root     15432:        }
                   15433: }
                   15434: 
                   15435: inline void msdos_int_67h_54h()
                   15436: {
                   15437:        if(!support_ems) {
                   15438:                REG8(AH) = 0x84;
                   15439:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     15440:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15441:                        if(ems_handles[i].allocated) {
                   15442:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   15443:                        } else {
                   15444:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   15445:                        }
                   15446:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   15447:                }
                   15448:                REG8(AH) = 0x00;
                   15449:                REG8(AL) = MAX_EMS_HANDLES;
                   15450:        } else if(REG8(AL) == 0x01) {
                   15451:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     15452:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15453:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15454:                                REG8(AH) = 0x00;
                   15455:                                REG16(DX) = i;
                   15456:                                break;
                   15457:                        }
                   15458:                }
                   15459:        } else if(REG8(AL) == 0x02) {
                   15460:                REG8(AH) = 0x00;
                   15461:                REG16(BX) = MAX_EMS_HANDLES;
                   15462:        } else {
1.1.1.22  root     15463:                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     15464:                REG8(AH) = 0x8f;
                   15465:        }
                   15466: }
                   15467: 
1.1.1.49  root     15468: inline void msdos_int_67h_55h()
                   15469: {
                   15470:        if(!support_ems) {
                   15471:                REG8(AH) = 0x84;
                   15472:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15473:                REG8(AH) = 0x83;
                   15474:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15475:                UINT16 jump_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
                   15476:                UINT16 jump_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
                   15477:                UINT8  entries  = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
                   15478:                UINT16 map_ofs  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   15479:                UINT16 map_seg  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
                   15480:                
                   15481:                for(int i = 0; i < (int)entries; i++) {
                   15482:                        int logical  = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 0);
                   15483:                        int physical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 2);
                   15484:                        
                   15485:                        if(REG8(AL) == 0x01) {
                   15486:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15487:                        }
                   15488: //                     if(!(physical < 4)) {
                   15489: //                             REG8(AH) = 0x8b;
                   15490: //                             return;
                   15491: //                     } else
                   15492:                        if(logical == 0xffff) {
                   15493:                                ems_unmap_page(physical & 3);
                   15494:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15495:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15496:                        } else {
                   15497:                                REG8(AH) = 0x8a;
                   15498:                                return;
                   15499:                        }
                   15500:                }
                   15501:                i386_jmp_far(jump_seg, jump_ofs);
                   15502:                REG8(AH) = 0x00;
                   15503:        } else {
                   15504:                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));
                   15505:                REG8(AH) = 0x8f;
                   15506:        }
                   15507: }
                   15508: 
                   15509: inline void msdos_int_67h_56h()
                   15510: {
                   15511:        if(!support_ems) {
                   15512:                REG8(AH) = 0x84;
                   15513:        } else if(REG8(AL) == 0x02) {
                   15514:                REG16(BX) = (2 + 2) * 4;
                   15515:                REG8(AH) = 0x00;
                   15516:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15517:                REG8(AH) = 0x83;
                   15518:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15519:                UINT16 call_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  0);
                   15520:                UINT16 call_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  2);
                   15521:                UINT8  new_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  4);
                   15522:                UINT16 new_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  5);
                   15523:                UINT16 new_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  7);
                   15524: #if 0
                   15525:                UINT8  old_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  9);
                   15526:                UINT16 old_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 10);
                   15527:                UINT16 old_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 12);
                   15528: #endif
                   15529:                UINT16 handles[4], pages[4];
                   15530:                
                   15531:                // alter page map and call routine is at fffc:001f
                   15532:                if(!(call_seg == 0 && call_ofs == 0)) {
                   15533:                        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   15534:                        mem[DUMMY_TOP + 0x20] = (call_ofs >> 0) & 0xff;
                   15535:                        mem[DUMMY_TOP + 0x21] = (call_ofs >> 8) & 0xff;
                   15536:                        mem[DUMMY_TOP + 0x22] = (call_seg >> 0) & 0xff;
                   15537:                        mem[DUMMY_TOP + 0x23] = (call_seg >> 8) & 0xff;
                   15538:                } else {
                   15539:                        // invalid call addr :-(
                   15540:                        mem[DUMMY_TOP + 0x1f] = 0x90;   // nop
                   15541:                        mem[DUMMY_TOP + 0x20] = 0x90;   // nop
                   15542:                        mem[DUMMY_TOP + 0x21] = 0x90;   // nop
                   15543:                        mem[DUMMY_TOP + 0x22] = 0x90;   // nop
                   15544:                        mem[DUMMY_TOP + 0x23] = 0x90;   // nop
                   15545:                }
                   15546:                // do call far (push cs/ip) in old mapping
                   15547:                i386_call_far(DUMMY_TOP >> 4, 0x001f);
                   15548:                
                   15549:                // get old mapping data
                   15550: #if 0
                   15551:                for(int i = 0; i < (int)old_entries; i++) {
                   15552:                        int logical  = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 0);
                   15553:                        int physical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 2);
                   15554:                        
                   15555:                        if(REG8(AL) == 0x01) {
                   15556:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15557:                        }
                   15558: //                     if(!(physical < 4)) {
                   15559: //                             REG8(AH) = 0x8b;
                   15560: //                             return;
                   15561: //                     } else
                   15562:                        if(logical == 0xffff) {
                   15563:                                ems_unmap_page(physical & 3);
                   15564:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15565:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15566:                        } else {
                   15567:                                REG8(AH) = 0x8a;
                   15568:                                return;
                   15569:                        }
                   15570:                }
                   15571: #endif
                   15572:                for(int i = 0; i < 4; i++) {
                   15573:                        handles[i] = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15574:                        pages  [i] = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15575:                }
                   15576:                
                   15577:                // set new mapping
                   15578:                for(int i = 0; i < (int)new_entries; i++) {
                   15579:                        int logical  = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 0);
                   15580:                        int physical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 2);
                   15581:                        
                   15582:                        if(REG8(AL) == 0x01) {
                   15583:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15584:                        }
                   15585: //                     if(!(physical < 4)) {
                   15586: //                             REG8(AH) = 0x8b;
                   15587: //                             return;
                   15588: //                     } else
                   15589:                        if(logical == 0xffff) {
                   15590:                                ems_unmap_page(physical & 3);
                   15591:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15592:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15593:                        } else {
                   15594:                                REG8(AH) = 0x8a;
                   15595:                                return;
                   15596:                        }
                   15597:                }
                   15598:                
                   15599:                // push old mapping data in new mapping
                   15600:                for(int i = 0; i < 4; i++) {
                   15601:                        i386_push16(handles[i]);
                   15602:                        i386_push16(pages  [i]);
                   15603:                }
                   15604:                REG8(AH) = 0x00;
                   15605:        } else {
                   15606:                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));
                   15607:                REG8(AH) = 0x8f;
                   15608:        }
                   15609: }
                   15610: 
1.1.1.20  root     15611: inline void msdos_int_67h_57h_tmp()
                   15612: {
                   15613:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15614:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15615:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   15616:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   15617:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   15618:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   15619:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15620:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   15621:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   15622:        
1.1.1.32  root     15623:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     15624:        UINT32 src_addr, dest_addr;
                   15625:        UINT32 src_addr_max, dest_addr_max;
                   15626:        
                   15627:        if(src_type == 0) {
                   15628:                src_buffer = mem;
                   15629:                src_addr = (src_seg << 4) + src_ofs;
                   15630:                src_addr_max = MAX_MEM;
                   15631:        } else {
1.1.1.31  root     15632:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     15633:                        REG8(AH) = 0x83;
                   15634:                        return;
                   15635:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   15636:                        REG8(AH) = 0x8a;
                   15637:                        return;
                   15638:                }
1.1.1.32  root     15639:                if(ems_handles[src_handle].buffer != NULL) {
                   15640:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   15641:                }
1.1.1.20  root     15642:                src_addr = src_ofs;
1.1.1.32  root     15643:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     15644:        }
                   15645:        if(dest_type == 0) {
                   15646:                dest_buffer = mem;
                   15647:                dest_addr = (dest_seg << 4) + dest_ofs;
                   15648:                dest_addr_max = MAX_MEM;
                   15649:        } else {
1.1.1.31  root     15650:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     15651:                        REG8(AH) = 0x83;
                   15652:                        return;
                   15653:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   15654:                        REG8(AH) = 0x8a;
                   15655:                        return;
                   15656:                }
1.1.1.32  root     15657:                if(ems_handles[dest_handle].buffer != NULL) {
                   15658:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   15659:                }
1.1.1.20  root     15660:                dest_addr = dest_ofs;
1.1.1.32  root     15661:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     15662:        }
1.1.1.32  root     15663:        if(src_buffer != NULL && dest_buffer != NULL) {
                   15664:                for(int i = 0; i < copy_length; i++) {
                   15665:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   15666:                                if(REG8(AL) == 0x00) {
                   15667:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   15668:                                } else if(REG8(AL) == 0x01) {
                   15669:                                        UINT8 tmp = dest_buffer[dest_addr];
                   15670:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   15671:                                        src_buffer[src_addr++] = tmp;
                   15672:                                }
                   15673:                        } else {
                   15674:                                REG8(AH) = 0x93;
                   15675:                                return;
1.1.1.20  root     15676:                        }
                   15677:                }
1.1.1.32  root     15678:                REG8(AH) = 0x00;
                   15679:        } else {
                   15680:                REG8(AH) = 0x80;
1.1.1.20  root     15681:        }
                   15682: }
                   15683: 
                   15684: inline void msdos_int_67h_57h()
                   15685: {
                   15686:        if(!support_ems) {
                   15687:                REG8(AH) = 0x84;
                   15688:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15689:                struct {
                   15690:                        UINT16 handle;
                   15691:                        UINT16 page;
                   15692:                        bool mapped;
                   15693:                } tmp_pages[4];
                   15694:                
                   15695:                // unmap pages to copy memory data to ems buffer
                   15696:                for(int i = 0; i < 4; i++) {
                   15697:                        tmp_pages[i].handle = ems_pages[i].handle;
                   15698:                        tmp_pages[i].page   = ems_pages[i].page;
                   15699:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   15700:                        ems_unmap_page(i);
                   15701:                }
                   15702:                
                   15703:                // run move/exchange operation
                   15704:                msdos_int_67h_57h_tmp();
                   15705:                
                   15706:                // restore unmapped pages
                   15707:                for(int i = 0; i < 4; i++) {
                   15708:                        if(tmp_pages[i].mapped) {
                   15709:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   15710:                        }
                   15711:                }
                   15712:        } else {
1.1.1.22  root     15713:                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     15714:                REG8(AH) = 0x8f;
                   15715:        }
                   15716: }
                   15717: 
                   15718: inline void msdos_int_67h_58h()
                   15719: {
                   15720:        if(!support_ems) {
                   15721:                REG8(AH) = 0x84;
                   15722:        } else if(REG8(AL) == 0x00) {
                   15723:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     15724:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   15725:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     15726:                }
                   15727:                REG8(AH) = 0x00;
                   15728:                REG16(CX) = 4;
                   15729:        } else if(REG8(AL) == 0x01) {
                   15730:                REG8(AH) = 0x00;
                   15731:                REG16(CX) = 4;
                   15732:        } else {
1.1.1.22  root     15733:                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     15734:                REG8(AH) = 0x8f;
                   15735:        }
                   15736: }
                   15737: 
1.1.1.42  root     15738: inline void msdos_int_67h_59h()
                   15739: {
                   15740:        if(!support_ems) {
                   15741:                REG8(AH) = 0x84;
                   15742:        } else if(REG8(AL) == 0x00) {
1.1.1.49  root     15743:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1024;
                   15744:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 0;
                   15745:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 4 * 4;
                   15746:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = 0;
                   15747:                REG8(AH) = 0x00;
                   15748: //             REG8(AH) = 0xa4; // access denied by operating system
1.1.1.42  root     15749:        } else if(REG8(AL) == 0x01) {
                   15750:                REG8(AH) = 0x00;
                   15751:                REG16(BX) = free_ems_pages;
                   15752:                REG16(DX) = MAX_EMS_PAGES;
                   15753:        } else {
                   15754:                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));
                   15755:                REG8(AH) = 0x8f;
                   15756:        }
                   15757: }
                   15758: 
1.1.1.20  root     15759: inline void msdos_int_67h_5ah()
                   15760: {
                   15761:        if(!support_ems) {
1.1.1.19  root     15762:                REG8(AH) = 0x84;
1.1.1.20  root     15763:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15764:                REG8(AH) = 0x87;
                   15765:        } else if(REG16(BX) > free_ems_pages) {
                   15766:                REG8(AH) = 0x88;
                   15767: //     } else if(REG16(BX) == 0) {
                   15768: //             REG8(AH) = 0x89;
                   15769:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     15770:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     15771:                        if(!ems_handles[i].allocated) {
                   15772:                                ems_allocate_pages(i, REG16(BX));
                   15773:                                REG8(AH) = 0x00;
                   15774:                                REG16(DX) = i;
                   15775:                                return;
                   15776:                        }
                   15777:                }
                   15778:                REG8(AH) = 0x85;
                   15779:        } else {
1.1.1.22  root     15780:                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     15781:                REG8(AH) = 0x8f;
1.1.1.19  root     15782:        }
                   15783: }
                   15784: 
1.1.1.49  root     15785: inline void msdos_int_67h_5bh()
                   15786: {
                   15787:        static UINT8  stored_bl = 0x00;
                   15788:        static UINT16 stored_es = 0x0000;
                   15789:        static UINT16 stored_di = 0x0000;
                   15790:        
                   15791:        if(!support_ems) {
                   15792:                REG8(AH) = 0x84;
                   15793:        } else if(REG8(AL) == 0x00) {
                   15794:                if(stored_bl == 0x00) {
                   15795:                        if(!(stored_es == 0 && stored_di == 0)) {
                   15796:                                for(int i = 0; i < 4; i++) {
                   15797:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15798:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15799:                                }
                   15800:                        }
                   15801:                        SREG(ES) = stored_es;
                   15802:                        i386_load_segment_descriptor(ES);
                   15803:                        REG16(DI) = stored_di;
                   15804:                } else {
                   15805:                        REG8(BL) = stored_bl;
                   15806:                }
                   15807:                REG8(AH) = 0x00;
                   15808:        } else if(REG8(AL) == 0x01) {
                   15809:                if(REG8(BL) == 0x00) {
                   15810:                        if(!(SREG(ES) == 0 && REG16(DI) == 0)) {
                   15811:                                for(int i = 0; i < 4; i++) {
                   15812:                                        UINT16 handle = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0);
                   15813:                                        UINT16 page   = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2);
                   15814:                                        
                   15815:                                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   15816:                                                ems_map_page(i, handle, page);
                   15817:                                        } else {
                   15818:                                                ems_unmap_page(i);
                   15819:                                        }
                   15820:                                }
                   15821:                        }
                   15822:                }
                   15823:                stored_bl = REG8(BL);
                   15824:                stored_es = SREG(ES);
                   15825:                stored_di = REG16(DI);
                   15826:                REG8(AH) = 0x00;
                   15827:        } else if(REG8(AL) == 0x02) {
                   15828:                REG16(DX) = 4 * 4;
                   15829:                REG8(AH) = 0x00;
                   15830:        } else if(REG8(AL) == 0x03) {
                   15831:                REG8(BL) = 0x00; // not supported
                   15832:                REG8(AH) = 0x00;
                   15833:        } else if(REG8(AL) == 0x04) {
                   15834:                REG8(AH) = 0x00;
                   15835:        } else if(REG8(AL) == 0x05) {
                   15836:                REG8(BL) = 0x00; // not supported
                   15837:                REG8(AH) = 0x00;
                   15838:        } else {
                   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));
                   15840:                REG8(AH) = 0x8f;
                   15841:        }
                   15842: }
                   15843: 
1.1.1.43  root     15844: inline void msdos_int_67h_5dh()
                   15845: {
                   15846:        if(!support_ems) {
                   15847:                REG8(AH) = 0x84;
                   15848:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15849:                REG8(AH) = 0xa4; // operating system denied access
                   15850:        } else {
                   15851:                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));
                   15852:                REG8(AH) = 0x8f;
                   15853:        }
                   15854: }
                   15855: 
1.1.1.49  root     15856: inline void msdos_int_67h_70h()
                   15857: {
                   15858:        if(!support_ems) {
                   15859:                REG8(AH) = 0x84;
                   15860:        } else if(REG8(AL) == 0x00) {
                   15861:                REG8(AL) = 0x00;
                   15862:                REG8(AH) = 0x00;
                   15863:        } else if(REG8(AL) == 0x01) {
                   15864:                REG8(AL) = 0x00;
                   15865: //             REG8(AH) = (REG8(BL) == 0x00) ? 0x00 : 0x80;
                   15866:                REG8(AH) = 0x00;
                   15867:        } else {
                   15868:                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));
                   15869:                REG8(AH) = 0x8f;
                   15870:        }
                   15871: }
                   15872: 
1.1.1.30  root     15873: inline void msdos_int_67h_deh()
                   15874: {
                   15875:        REG8(AH) = 0x84;
                   15876: }
                   15877: 
1.1.1.19  root     15878: #ifdef SUPPORT_XMS
                   15879: 
1.1.1.32  root     15880: void msdos_xms_init()
1.1.1.26  root     15881: {
1.1.1.30  root     15882:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   15883:        emb_handle_top->address = EMB_TOP;
                   15884:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     15885:        xms_a20_local_enb_count = 0;
                   15886: }
                   15887: 
1.1.1.32  root     15888: void msdos_xms_finish()
                   15889: {
                   15890:        msdos_xms_release();
                   15891: }
                   15892: 
                   15893: void msdos_xms_release()
1.1.1.30  root     15894: {
                   15895:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   15896:                emb_handle_t *next_handle = emb_handle->next;
                   15897:                free(emb_handle);
                   15898:                emb_handle = next_handle;
                   15899:        }
                   15900: }
                   15901: 
                   15902: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   15903: {
                   15904:        if(handle != 0) {
                   15905:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15906:                        if(emb_handle->handle == handle) {
                   15907:                                return(emb_handle);
                   15908:                        }
                   15909:                }
                   15910:        }
                   15911:        return(NULL);
                   15912: }
                   15913: 
                   15914: int msdos_xms_get_unused_emb_handle_id()
                   15915: {
                   15916:        for(int handle = 1;; handle++) {
                   15917:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   15918:                        return(handle);
                   15919:                }
                   15920:        }
                   15921:        return(0);
                   15922: }
                   15923: 
                   15924: int msdos_xms_get_unused_emb_handle_count()
                   15925: {
                   15926:        int count = 64; //255;
                   15927:        
                   15928:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15929:                if(emb_handle->handle != 0) {
                   15930:                        if(--count == 1) {
                   15931:                                break;
                   15932:                        }
                   15933:                }
                   15934:        }
                   15935:        return(count);
                   15936: }
                   15937: 
                   15938: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   15939: {
                   15940:        if(emb_handle->size_kb > size_kb) {
                   15941:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   15942:                
                   15943:                new_handle->address = emb_handle->address + size_kb * 1024;
                   15944:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   15945:                emb_handle->size_kb = size_kb;
                   15946:                
                   15947:                new_handle->prev = emb_handle;
                   15948:                new_handle->next = emb_handle->next;
                   15949:                if(emb_handle->next != NULL) {
                   15950:                        emb_handle->next->prev = new_handle;
                   15951:                }
                   15952:                emb_handle->next = new_handle;
                   15953:        }
                   15954: }
                   15955: 
                   15956: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   15957: {
                   15958:        emb_handle_t *next_handle = emb_handle->next;
                   15959:        
                   15960:        if(next_handle != NULL) {
                   15961:                emb_handle->size_kb += next_handle->size_kb;
                   15962:                
                   15963:                if(next_handle->next != NULL) {
                   15964:                        next_handle->next->prev = emb_handle;
                   15965:                }
                   15966:                emb_handle->next = next_handle->next;
                   15967:                free(next_handle);
                   15968:        }
                   15969: }
                   15970: 
                   15971: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   15972: {
                   15973:        emb_handle_t *target_handle = NULL;
                   15974:        
                   15975:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15976:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   15977:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   15978:                                target_handle = emb_handle;
                   15979:                        }
                   15980:                }
                   15981:        }
                   15982:        if(target_handle != NULL) {
                   15983:                if(target_handle->size_kb > size_kb) {
                   15984:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   15985:                }
                   15986: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   15987:                return(target_handle);
                   15988:        }
                   15989:        return(NULL);
                   15990: }
                   15991: 
                   15992: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   15993: {
                   15994:        emb_handle_t *prev_handle = emb_handle->prev;
                   15995:        emb_handle_t *next_handle = emb_handle->next;
                   15996:        
                   15997:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   15998:                msdos_xms_combine_emb_handles(prev_handle);
                   15999:                emb_handle = prev_handle;
                   16000:        }
                   16001:        if(next_handle != NULL && next_handle->handle == 0) {
                   16002:                msdos_xms_combine_emb_handles(emb_handle);
                   16003:        }
                   16004:        emb_handle->handle = 0;
                   16005: }
                   16006: 
1.1.1.19  root     16007: inline void msdos_call_xms_00h()
                   16008: {
1.1.1.29  root     16009: #if defined(HAS_I386)
                   16010:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.45  root     16011: //     REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
1.1.1.29  root     16012:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   16013: #else
                   16014:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   16015:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   16016: #endif
                   16017: //     REG16(DX) = 0x0000; // HMA does not exist
                   16018:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     16019: }
                   16020: 
                   16021: inline void msdos_call_xms_01h()
                   16022: {
1.1.1.29  root     16023:        if(REG8(AL) == 0x40) {
                   16024:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   16025:                // DX=KB free extended memory returned by last call of function 08h
                   16026:                REG16(AX) = 0x0000;
                   16027:                REG8(BL) = 0x91;
                   16028:                REG16(DX) = xms_dx_after_call_08h;
                   16029:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16030:                REG16(AX) = 0x0000;
                   16031:                REG8(BL) = 0x81; // Vdisk was detected
                   16032: #ifdef SUPPORT_HMA
                   16033:        } else if(is_hma_used_by_int_2fh) {
                   16034:                REG16(AX) = 0x0000;
                   16035:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16036:        } else if(is_hma_used_by_xms) {
                   16037:                REG16(AX) = 0x0000;
                   16038:                REG8(BL) = 0x91; // HMA is already in use
                   16039:        } else {
                   16040:                REG16(AX) = 0x0001;
                   16041:                is_hma_used_by_xms = true;
                   16042: #else
                   16043:        } else {
                   16044:                REG16(AX) = 0x0000;
                   16045:                REG8(BL) = 0x91; // HMA is already in use
                   16046: #endif
                   16047:        }
1.1.1.19  root     16048: }
                   16049: 
                   16050: inline void msdos_call_xms_02h()
                   16051: {
1.1.1.29  root     16052:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16053:                REG16(AX) = 0x0000;
                   16054:                REG8(BL) = 0x81; // Vdisk was detected
                   16055: #ifdef SUPPORT_HMA
                   16056:        } else if(is_hma_used_by_int_2fh) {
                   16057:                REG16(AX) = 0x0000;
                   16058:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16059:        } else if(!is_hma_used_by_xms) {
                   16060:                REG16(AX) = 0x0000;
                   16061:                REG8(BL) = 0x93; // HMA is not allocated
                   16062:        } else {
                   16063:                REG16(AX) = 0x0001;
                   16064:                is_hma_used_by_xms = false;
                   16065:                // restore first free mcb in high memory area
                   16066:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   16067: #else
                   16068:        } else {
                   16069:                REG16(AX) = 0x0000;
                   16070:                REG8(BL) = 0x91; // HMA is already in use
                   16071: #endif
                   16072:        }
1.1.1.19  root     16073: }
                   16074: 
                   16075: inline void msdos_call_xms_03h()
                   16076: {
                   16077:        i386_set_a20_line(1);
                   16078:        REG16(AX) = 0x0001;
                   16079:        REG8(BL) = 0x00;
                   16080: }
                   16081: 
                   16082: inline void msdos_call_xms_04h()
                   16083: {
1.1.1.21  root     16084:        i386_set_a20_line(0);
                   16085:        REG16(AX) = 0x0001;
                   16086:        REG8(BL) = 0x00;
1.1.1.19  root     16087: }
                   16088: 
                   16089: inline void msdos_call_xms_05h()
                   16090: {
                   16091:        i386_set_a20_line(1);
                   16092:        REG16(AX) = 0x0001;
                   16093:        REG8(BL) = 0x00;
1.1.1.21  root     16094:        xms_a20_local_enb_count++;
1.1.1.19  root     16095: }
                   16096: 
                   16097: void msdos_call_xms_06h()
                   16098: {
1.1.1.21  root     16099:        if(xms_a20_local_enb_count > 0) {
1.1.1.45  root     16100:                if(--xms_a20_local_enb_count == 0) {
                   16101:                        i386_set_a20_line(0);
                   16102:                        REG16(AX) = 0x0001;
                   16103:                        REG8(BL) = 0x00;
                   16104:                } else {
                   16105:                        REG16(AX) = 0x0000;
                   16106:                        REG8(BL) = 0x94;
                   16107:                }
1.1.1.21  root     16108:        } else {
1.1.1.45  root     16109:                i386_set_a20_line(0);
1.1.1.21  root     16110:                REG16(AX) = 0x0001;
                   16111:                REG8(BL) = 0x00;
1.1.1.19  root     16112:        }
                   16113: }
                   16114: 
                   16115: inline void msdos_call_xms_07h()
                   16116: {
                   16117:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   16118:        REG8(BL) = 0x00;
                   16119: }
                   16120: 
                   16121: inline void msdos_call_xms_08h()
                   16122: {
1.1.1.45  root     16123:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     16124:        
1.1.1.30  root     16125:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16126:                if(emb_handle->handle == 0) {
1.1.1.45  root     16127:                        if(eax < emb_handle->size_kb) {
                   16128:                                eax = emb_handle->size_kb;
1.1.1.19  root     16129:                        }
1.1.1.45  root     16130:                        edx += emb_handle->size_kb;
1.1.1.19  root     16131:                }
                   16132:        }
1.1.1.45  root     16133:        if(eax > 65535) {
                   16134:                eax = 65535;
                   16135:        }
                   16136:        if(edx > 65535) {
                   16137:                edx = 65535;
                   16138:        }
                   16139:        if(eax == 0 && edx == 0) {
1.1.1.19  root     16140:                REG8(BL) = 0xa0;
                   16141:        } else {
                   16142:                REG8(BL) = 0x00;
                   16143:        }
1.1.1.45  root     16144: #if defined(HAS_I386)
                   16145:        REG32(EAX) = eax;
                   16146:        REG32(EDX) = edx;
                   16147: #else
                   16148:        REG16(AX) = (UINT16)eax;
                   16149:        REG16(DX) = (UINT16)edx;
                   16150: #endif
1.1.1.29  root     16151:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     16152: }
                   16153: 
1.1.1.30  root     16154: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     16155: {
1.1.1.30  root     16156:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   16157:        
                   16158:        if(emb_handle != NULL) {
                   16159:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16160:                
                   16161:                REG16(AX) = 0x0001;
                   16162:                REG16(DX) = emb_handle->handle;
                   16163:                REG8(BL) = 0x00;
                   16164:        } else {
                   16165:                REG16(AX) = REG16(DX) = 0x0000;
                   16166:                REG8(BL) = 0xa0;
1.1.1.19  root     16167:        }
1.1.1.30  root     16168: }
                   16169: 
                   16170: inline void msdos_call_xms_09h()
                   16171: {
                   16172:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     16173: }
                   16174: 
                   16175: inline void msdos_call_xms_0ah()
                   16176: {
1.1.1.30  root     16177:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16178:        
                   16179:        if(emb_handle == NULL) {
1.1.1.19  root     16180:                REG16(AX) = 0x0000;
                   16181:                REG8(BL) = 0xa2;
1.1.1.45  root     16182: //     } else if(emb_handle->lock > 0) {
                   16183: //             REG16(AX) = 0x0000;
                   16184: //             REG8(BL) = 0xab;
1.1.1.19  root     16185:        } else {
1.1.1.30  root     16186:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     16187:                
                   16188:                REG16(AX) = 0x0001;
                   16189:                REG8(BL) = 0x00;
                   16190:        }
                   16191: }
                   16192: 
                   16193: inline void msdos_call_xms_0bh()
                   16194: {
                   16195:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   16196:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   16197:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   16198:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   16199:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   16200:        
                   16201:        UINT8 *src_buffer, *dest_buffer;
                   16202:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     16203:        emb_handle_t *emb_handle;
1.1.1.19  root     16204:        
                   16205:        if(src_handle == 0) {
                   16206:                src_buffer = mem;
                   16207:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   16208:                src_addr_max = MAX_MEM;
                   16209:        } else {
1.1.1.30  root     16210:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     16211:                        REG16(AX) = 0x0000;
                   16212:                        REG8(BL) = 0xa3;
                   16213:                        return;
                   16214:                }
1.1.1.30  root     16215:                src_buffer = mem + emb_handle->address;
                   16216:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16217:        }
                   16218:        if(dest_handle == 0) {
                   16219:                dest_buffer = mem;
                   16220:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   16221:                dest_addr_max = MAX_MEM;
                   16222:        } else {
1.1.1.30  root     16223:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     16224:                        REG16(AX) = 0x0000;
                   16225:                        REG8(BL) = 0xa5;
                   16226:                        return;
                   16227:                }
1.1.1.30  root     16228:                dest_buffer = mem + emb_handle->address;
                   16229:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16230:        }
                   16231:        for(int i = 0; i < copy_length; i++) {
                   16232:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   16233:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   16234:                } else {
                   16235:                        break;
                   16236:                }
                   16237:        }
                   16238:        REG16(AX) = 0x0001;
                   16239:        REG8(BL) = 0x00;
                   16240: }
                   16241: 
                   16242: inline void msdos_call_xms_0ch()
                   16243: {
1.1.1.30  root     16244:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16245:        
                   16246:        if(emb_handle == NULL) {
1.1.1.19  root     16247:                REG16(AX) = 0x0000;
                   16248:                REG8(BL) = 0xa2;
                   16249:        } else {
1.1.1.45  root     16250:                if(emb_handle->lock < 255) {
                   16251:                        emb_handle->lock++;
                   16252:                }
1.1.1.19  root     16253:                REG16(AX) = 0x0001;
1.1.1.30  root     16254:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   16255:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     16256:        }
                   16257: }
                   16258: 
                   16259: inline void msdos_call_xms_0dh()
                   16260: {
1.1.1.30  root     16261:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16262:        
                   16263:        if(emb_handle == NULL) {
1.1.1.19  root     16264:                REG16(AX) = 0x0000;
                   16265:                REG8(BL) = 0xa2;
1.1.1.30  root     16266:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     16267:                REG16(AX) = 0x0000;
                   16268:                REG8(BL) = 0xaa;
                   16269:        } else {
1.1.1.30  root     16270:                emb_handle->lock--;
1.1.1.19  root     16271:                REG16(AX) = 0x0001;
                   16272:                REG8(BL) = 0x00;
                   16273:        }
                   16274: }
                   16275: 
                   16276: inline void msdos_call_xms_0eh()
                   16277: {
1.1.1.30  root     16278:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16279:        
                   16280:        if(emb_handle == NULL) {
1.1.1.19  root     16281:                REG16(AX) = 0x0000;
                   16282:                REG8(BL) = 0xa2;
                   16283:        } else {
                   16284:                REG16(AX) = 0x0001;
1.1.1.30  root     16285:                REG8(BH) = emb_handle->lock;
                   16286:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   16287:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     16288:        }
                   16289: }
                   16290: 
1.1.1.30  root     16291: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     16292: {
1.1.1.30  root     16293:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16294:        
                   16295:        if(emb_handle == NULL) {
1.1.1.19  root     16296:                REG16(AX) = 0x0000;
                   16297:                REG8(BL) = 0xa2;
1.1.1.30  root     16298:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     16299:                REG16(AX) = 0x0000;
                   16300:                REG8(BL) = 0xab;
                   16301:        } else {
1.1.1.30  root     16302:                if(emb_handle->size_kb < size_kb) {
                   16303:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   16304:                                msdos_xms_combine_emb_handles(emb_handle);
                   16305:                                if(emb_handle->size_kb > size_kb) {
                   16306:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16307:                                }
                   16308:                        } else {
                   16309:                                int old_handle = emb_handle->handle;
                   16310:                                int old_size_kb = emb_handle->size_kb;
                   16311:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   16312:                                
                   16313:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   16314:                                msdos_xms_free_emb_handle(emb_handle);
                   16315:                                
                   16316:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   16317:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   16318:                                }
                   16319:                                emb_handle->handle = old_handle;
                   16320:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   16321:                                free(buffer);
                   16322:                        }
                   16323:                } else if(emb_handle->size_kb > size_kb) {
                   16324:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16325:                }
                   16326:                if(emb_handle->size_kb != size_kb) {
                   16327:                        REG16(AX) = 0x0000;
                   16328:                        REG8(BL) = 0xa0;
                   16329:                } else {
                   16330:                        REG16(AX) = 0x0001;
                   16331:                        REG8(BL) = 0x00;
                   16332:                }
1.1.1.19  root     16333:        }
                   16334: }
                   16335: 
1.1.1.30  root     16336: inline void msdos_call_xms_0fh()
                   16337: {
                   16338:        msdos_call_xms_0fh(REG16(BX));
                   16339: }
                   16340: 
1.1.1.19  root     16341: inline void msdos_call_xms_10h()
                   16342: {
                   16343:        int seg;
                   16344:        
                   16345:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   16346:                REG16(AX) = 0x0001;
                   16347:                REG16(BX) = seg;
                   16348:        } else {
                   16349:                REG16(AX) = 0x0000;
                   16350:                REG8(BL) = 0xb0;
                   16351:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   16352:        }
                   16353: }
                   16354: 
                   16355: inline void msdos_call_xms_11h()
                   16356: {
                   16357:        int mcb_seg = REG16(DX) - 1;
                   16358:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16359:        
                   16360:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16361:                msdos_mem_free(REG16(DX));
                   16362:                REG16(AX) = 0x0001;
                   16363:                REG8(BL) = 0x00;
                   16364:        } else {
                   16365:                REG16(AX) = 0x0000;
                   16366:                REG8(BL) = 0xb2;
                   16367:        }
                   16368: }
                   16369: 
                   16370: inline void msdos_call_xms_12h()
                   16371: {
                   16372:        int mcb_seg = REG16(DX) - 1;
                   16373:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16374:        int max_paragraphs;
                   16375:        
                   16376:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16377:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   16378:                        REG16(AX) = 0x0001;
                   16379:                        REG8(BL) = 0x00;
                   16380:                } else {
                   16381:                        REG16(AX) = 0x0000;
                   16382:                        REG8(BL) = 0xb0;
                   16383:                        REG16(DX) = max_paragraphs;
                   16384:                }
                   16385:        } else {
                   16386:                REG16(AX) = 0x0000;
                   16387:                REG8(BL) = 0xb2;
                   16388:        }
                   16389: }
                   16390: 
1.1.1.29  root     16391: #if defined(HAS_I386)
                   16392: 
                   16393: inline void msdos_call_xms_88h()
                   16394: {
                   16395:        REG32(EAX) = REG32(EDX) = 0x0000;
                   16396:        
1.1.1.30  root     16397:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16398:                if(emb_handle->handle == 0) {
                   16399:                        if(REG32(EAX) < emb_handle->size_kb) {
                   16400:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     16401:                        }
1.1.1.30  root     16402:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     16403:                }
                   16404:        }
                   16405:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   16406:                REG8(BL) = 0xa0;
                   16407:        } else {
                   16408:                REG8(BL) = 0x00;
                   16409:        }
                   16410:        REG32(ECX) = EMB_END - 1;
                   16411: }
                   16412: 
                   16413: inline void msdos_call_xms_89h()
                   16414: {
1.1.1.30  root     16415:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     16416: }
                   16417: 
                   16418: inline void msdos_call_xms_8eh()
                   16419: {
1.1.1.30  root     16420:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16421:        
                   16422:        if(emb_handle == NULL) {
1.1.1.29  root     16423:                REG16(AX) = 0x0000;
                   16424:                REG8(BL) = 0xa2;
                   16425:        } else {
                   16426:                REG16(AX) = 0x0001;
1.1.1.30  root     16427:                REG8(BH) = emb_handle->lock;
                   16428:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   16429:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     16430:        }
                   16431: }
                   16432: 
                   16433: inline void msdos_call_xms_8fh()
                   16434: {
1.1.1.30  root     16435:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     16436: }
                   16437: 
                   16438: #endif
1.1.1.19  root     16439: #endif
                   16440: 
1.1.1.26  root     16441: UINT16 msdos_get_equipment()
                   16442: {
                   16443:        static UINT16 equip = 0;
                   16444:        
                   16445:        if(equip == 0) {
                   16446: #ifdef SUPPORT_FPU
                   16447:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   16448: #endif
                   16449:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   16450:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   16451: //             equip |= (1 << 8);      // 0 if DMA installed
                   16452:                equip |= (2 << 9);      // number of serial ports
                   16453:                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     16454:                
                   16455:                // check only A: and B: if it is floppy drive
                   16456:                int n = 0;
                   16457:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     16458:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   16459:                                n++;
1.1.1.28  root     16460:                        }
                   16461:                }
                   16462:                if(n != 0) {
                   16463:                        equip |= (1 << 0);      // floppy disk(s) installed
                   16464:                        n--;
                   16465:                        equip |= (n << 6);      // number of floppies installed less 1
                   16466:                }
                   16467: //             if(joyGetNumDevs() != 0) {
                   16468: //                     equip |= (1 << 12);     // game port installed
                   16469: //             }
1.1.1.26  root     16470:        }
                   16471:        return(equip);
                   16472: }
                   16473: 
1.1       root     16474: void msdos_syscall(unsigned num)
                   16475: {
1.1.1.22  root     16476: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     16477:        if(num == 0x08 || num == 0x1c) {
                   16478:                // don't log the timer interrupts
1.1.1.45  root     16479: //             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     16480:        } else if(num == 0x30) {
                   16481:                // dummy interrupt for call 0005h (call near)
                   16482:                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.43  root     16483:        } else if(num == 0x68) {
1.1.1.22  root     16484:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     16485:                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.22  root     16486:        } else if(num == 0x69) {
                   16487:                // dummy interrupt for XMS (call far)
1.1.1.33  root     16488:                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.50  root     16489:        } else if(num >= 0x6a && num <= 0x6f) {
1.1.1.45  root     16490:                // dummy interrupt
1.1.1.22  root     16491:        } else {
1.1.1.33  root     16492:                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     16493:        }
                   16494: #endif
1.1.1.36  root     16495:        // update cursor position
                   16496:        if(cursor_moved) {
                   16497:                pcbios_update_cursor_position();
                   16498:                cursor_moved = false;
                   16499:        }
1.1.1.50  root     16500: #ifdef USE_SERVICE_THREAD
                   16501:        // this is called from dummy loop to wait until a serive that waits input is done
                   16502:        if(!in_service)
                   16503: #endif
1.1.1.33  root     16504:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     16505:        
1.1       root     16506:        switch(num) {
                   16507:        case 0x00:
1.1.1.28  root     16508:                try {
                   16509:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16510:                        error("division by zero\n");
                   16511:                } catch(...) {
                   16512:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   16513:                }
1.1       root     16514:                break;
                   16515:        case 0x04:
1.1.1.28  root     16516:                try {
                   16517:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16518:                        error("overflow\n");
                   16519:                } catch(...) {
                   16520:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   16521:                }
1.1       root     16522:                break;
                   16523:        case 0x06:
                   16524:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     16525:                if(!ignore_illegal_insn) {
1.1.1.28  root     16526:                        try {
                   16527:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16528:                                error("illegal instruction\n");
                   16529:                        } catch(...) {
                   16530:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   16531:                        }
1.1.1.14  root     16532:                } else {
                   16533: #if defined(HAS_I386)
1.1.1.39  root     16534:                        m_eip = m_int6h_skip_eip;
                   16535: #elif defined(HAS_I286)
                   16536:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     16537: #else
1.1.1.39  root     16538:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     16539: #endif
                   16540:                }
1.1       root     16541:                break;
1.1.1.33  root     16542:        case 0x09:
                   16543:                // ctrl-break is pressed
                   16544:                if(raise_int_1bh) {
                   16545: #if defined(HAS_I386)
                   16546:                        m_ext = 0; // not an external interrupt
                   16547:                        i386_trap(0x1b, 1, 0);
                   16548:                        m_ext = 1;
                   16549: #else
                   16550:                        PREFIX86(_interrupt)(0x1b);
                   16551: #endif
                   16552:                        raise_int_1bh = false;
                   16553:                }
1.1.1.8   root     16554:        case 0x08:
1.1.1.14  root     16555: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     16556:        case 0x0b:
                   16557:        case 0x0c:
                   16558:        case 0x0d:
                   16559:        case 0x0e:
                   16560:        case 0x0f:
                   16561:                // EOI
                   16562:                pic[0].isr &= ~(1 << (num - 0x08));
                   16563:                pic_update();
                   16564:                break;
1.1       root     16565:        case 0x10:
                   16566:                // PC BIOS - Video
1.1.1.14  root     16567:                if(!restore_console_on_exit) {
1.1.1.15  root     16568:                        change_console_size(scr_width, scr_height);
1.1       root     16569:                }
1.1.1.3   root     16570:                m_CF = 0;
1.1       root     16571:                switch(REG8(AH)) {
1.1.1.16  root     16572:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     16573:                case 0x01: pcbios_int_10h_01h(); break;
                   16574:                case 0x02: pcbios_int_10h_02h(); break;
                   16575:                case 0x03: pcbios_int_10h_03h(); break;
                   16576:                case 0x05: pcbios_int_10h_05h(); break;
                   16577:                case 0x06: pcbios_int_10h_06h(); break;
                   16578:                case 0x07: pcbios_int_10h_07h(); break;
                   16579:                case 0x08: pcbios_int_10h_08h(); break;
                   16580:                case 0x09: pcbios_int_10h_09h(); break;
                   16581:                case 0x0a: pcbios_int_10h_0ah(); break;
                   16582:                case 0x0b: break;
1.1.1.40  root     16583:                case 0x0c: pcbios_int_10h_0ch(); break;
                   16584:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     16585:                case 0x0e: pcbios_int_10h_0eh(); break;
                   16586:                case 0x0f: pcbios_int_10h_0fh(); break;
                   16587:                case 0x10: break;
1.1.1.14  root     16588:                case 0x11: pcbios_int_10h_11h(); break;
                   16589:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     16590:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     16591:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     16592:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     16593:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   16594:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     16595:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     16596:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   16597:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     16598:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     16599:                case 0x6f: break;
1.1.1.22  root     16600:                case 0x80: m_CF = 1; break; // unknown
                   16601:                case 0x81: m_CF = 1; break; // unknown
1.1       root     16602:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     16603:                case 0x83: pcbios_int_10h_83h(); break;
                   16604:                case 0x8b: break;
                   16605:                case 0x8c: m_CF = 1; break; // unknown
                   16606:                case 0x8d: m_CF = 1; break; // unknown
                   16607:                case 0x8e: m_CF = 1; break; // unknown
                   16608:                case 0x90: pcbios_int_10h_90h(); break;
                   16609:                case 0x91: pcbios_int_10h_91h(); break;
                   16610:                case 0x92: break;
                   16611:                case 0x93: break;
                   16612:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     16613:                case 0xfa: break; // ega register interface library is not installed
1.1       root     16614:                case 0xfe: pcbios_int_10h_feh(); break;
                   16615:                case 0xff: pcbios_int_10h_ffh(); break;
                   16616:                default:
1.1.1.22  root     16617:                        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));
                   16618:                        m_CF = 1;
1.1       root     16619:                        break;
                   16620:                }
                   16621:                break;
                   16622:        case 0x11:
                   16623:                // PC BIOS - Get Equipment List
1.1.1.26  root     16624:                REG16(AX) = msdos_get_equipment();
1.1       root     16625:                break;
                   16626:        case 0x12:
                   16627:                // PC BIOS - Get Memory Size
1.1.1.33  root     16628:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     16629:                break;
                   16630:        case 0x13:
1.1.1.42  root     16631:                // PC BIOS - Disk I/O
                   16632:                {
                   16633:                        static UINT8 last = 0x00;
                   16634:                        switch(REG8(AH)) {
                   16635:                        case 0x00: pcbios_int_13h_00h(); break;
                   16636:                        case 0x01: // get last status
                   16637:                                REG8(AH) = last;
                   16638:                                break;
                   16639:                        case 0x02: pcbios_int_13h_02h(); break;
                   16640:                        case 0x03: pcbios_int_13h_03h(); break;
                   16641:                        case 0x04: pcbios_int_13h_04h(); break;
                   16642:                        case 0x08: pcbios_int_13h_08h(); break;
                   16643:                        case 0x0a: pcbios_int_13h_02h(); break;
                   16644:                        case 0x0b: pcbios_int_13h_03h(); break;
                   16645:                        case 0x0d: pcbios_int_13h_00h(); break;
                   16646:                        case 0x10: pcbios_int_13h_10h(); break;
                   16647:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     16648:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     16649:                        case 0x05: // format
                   16650:                        case 0x06:
                   16651:                        case 0x07:
                   16652:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   16653:                                m_CF = 1;
                   16654:                                break;
                   16655:                        case 0x09:
                   16656:                        case 0x0c: // seek
                   16657:                        case 0x11: // recalib
                   16658:                        case 0x14:
                   16659:                        case 0x17:
                   16660:                                REG8(AH) = 0x00; // successful completion
                   16661:                                break;
1.1.1.43  root     16662:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   16663:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   16664:                                REG8(AH) = 0x01; // invalid function
                   16665:                                m_CF = 1;
                   16666:                                break;
1.1.1.42  root     16667:                        default:
                   16668:                                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));
                   16669:                                REG8(AH) = 0x01; // invalid function
                   16670:                                m_CF = 1;
                   16671:                                break;
                   16672:                        }
                   16673:                        last = REG8(AH);
                   16674:                }
1.1       root     16675:                break;
                   16676:        case 0x14:
                   16677:                // PC BIOS - Serial I/O
1.1.1.25  root     16678:                switch(REG8(AH)) {
                   16679:                case 0x00: pcbios_int_14h_00h(); break;
                   16680:                case 0x01: pcbios_int_14h_01h(); break;
                   16681:                case 0x02: pcbios_int_14h_02h(); break;
                   16682:                case 0x03: pcbios_int_14h_03h(); break;
                   16683:                case 0x04: pcbios_int_14h_04h(); break;
                   16684:                case 0x05: pcbios_int_14h_05h(); break;
                   16685:                default:
                   16686:                        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));
                   16687:                        break;
                   16688:                }
1.1       root     16689:                break;
                   16690:        case 0x15:
                   16691:                // PC BIOS
1.1.1.3   root     16692:                m_CF = 0;
1.1       root     16693:                switch(REG8(AH)) {
1.1.1.14  root     16694:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     16695:                case 0x23: pcbios_int_15h_23h(); break;
                   16696:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     16697:                case 0x41: break;
1.1       root     16698:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     16699:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     16700:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     16701:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     16702:                case 0x86: pcbios_int_15h_86h(); break;
                   16703:                case 0x87: pcbios_int_15h_87h(); break;
                   16704:                case 0x88: pcbios_int_15h_88h(); break;
                   16705:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     16706:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     16707:                case 0xc0: // PS/2 ???
1.1.1.54  root     16708: #ifndef EXT_BIOS_TOP
1.1.1.22  root     16709:                case 0xc1:
1.1.1.54  root     16710: #endif
1.1.1.30  root     16711:                case 0xc3: // PS50+ ???
                   16712:                case 0xc4:
1.1.1.22  root     16713:                        REG8(AH) = 0x86;
                   16714:                        m_CF = 1;
                   16715:                        break;
1.1.1.54  root     16716: #ifdef EXT_BIOS_TOP
                   16717:                case 0xc1: pcbios_int_15h_c1h(); break;
                   16718: #endif
                   16719:                case 0xc2: pcbios_int_15h_c2h(); break;
1.1.1.3   root     16720: #if defined(HAS_I386)
1.1       root     16721:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     16722: #endif
1.1       root     16723:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     16724:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     16725:                default:
1.1.1.22  root     16726:                        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));
                   16727:                        REG8(AH) = 0x86;
1.1.1.3   root     16728:                        m_CF = 1;
1.1       root     16729:                        break;
                   16730:                }
                   16731:                break;
                   16732:        case 0x16:
                   16733:                // PC BIOS - Keyboard
1.1.1.3   root     16734:                m_CF = 0;
1.1       root     16735:                switch(REG8(AH)) {
                   16736:                case 0x00: pcbios_int_16h_00h(); break;
                   16737:                case 0x01: pcbios_int_16h_01h(); break;
                   16738:                case 0x02: pcbios_int_16h_02h(); break;
                   16739:                case 0x03: pcbios_int_16h_03h(); break;
                   16740:                case 0x05: pcbios_int_16h_05h(); break;
                   16741:                case 0x10: pcbios_int_16h_00h(); break;
                   16742:                case 0x11: pcbios_int_16h_01h(); break;
                   16743:                case 0x12: pcbios_int_16h_12h(); break;
                   16744:                case 0x13: pcbios_int_16h_13h(); break;
                   16745:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     16746:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     16747:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     16748:                case 0xda: break; // unknown
1.1.1.43  root     16749:                case 0xdb: break; // unknown
1.1.1.22  root     16750:                case 0xff: break; // unknown
1.1       root     16751:                default:
1.1.1.22  root     16752:                        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     16753:                        break;
                   16754:                }
                   16755:                break;
                   16756:        case 0x17:
                   16757:                // PC BIOS - Printer
1.1.1.37  root     16758:                m_CF = 0;
                   16759:                switch(REG8(AH)) {
                   16760:                case 0x00: pcbios_int_17h_00h(); break;
                   16761:                case 0x01: pcbios_int_17h_01h(); break;
                   16762:                case 0x02: pcbios_int_17h_02h(); break;
                   16763:                case 0x03: pcbios_int_17h_03h(); break;
                   16764:                case 0x50: pcbios_int_17h_50h(); break;
                   16765:                case 0x51: pcbios_int_17h_51h(); break;
                   16766:                case 0x52: pcbios_int_17h_52h(); break;
                   16767:                case 0x84: pcbios_int_17h_84h(); break;
                   16768:                case 0x85: pcbios_int_17h_85h(); break;
                   16769:                default:
                   16770:                        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));
                   16771:                        break;
                   16772:                }
1.1       root     16773:                break;
                   16774:        case 0x1a:
                   16775:                // PC BIOS - Timer
1.1.1.3   root     16776:                m_CF = 0;
1.1       root     16777:                switch(REG8(AH)) {
                   16778:                case 0x00: pcbios_int_1ah_00h(); break;
                   16779:                case 0x01: break;
                   16780:                case 0x02: pcbios_int_1ah_02h(); break;
                   16781:                case 0x03: break;
                   16782:                case 0x04: pcbios_int_1ah_04h(); break;
                   16783:                case 0x05: break;
                   16784:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   16785:                case 0x0b: break;
1.1.1.14  root     16786:                case 0x35: break; // Word Perfect Third Party Interface?
                   16787:                case 0x36: break; // Word Perfect Third Party Interface
                   16788:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     16789:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     16790:                case 0xb1: break; // PCI BIOS v2.0c+
                   16791:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     16792:                default:
1.1.1.22  root     16793:                        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     16794:                        break;
                   16795:                }
                   16796:                break;
1.1.1.33  root     16797:        case 0x1b:
                   16798:                mem[0x471] = 0x00;
                   16799:                break;
1.1       root     16800:        case 0x20:
1.1.1.28  root     16801:                try {
                   16802:                        msdos_process_terminate(SREG(CS), retval, 1);
                   16803:                } catch(...) {
                   16804:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   16805:                }
1.1       root     16806:                break;
1.1.1.49  root     16807:        case 0x30:
1.1.1.46  root     16808:                // dummy interrupt for case map routine pointed in the country info
                   16809: //             if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
                   16810: //                     REG8(AL) = 0x00;
                   16811: //                     break;
                   16812: //             }
1.1       root     16813:        case 0x21:
                   16814:                // MS-DOS System Call
1.1.1.3   root     16815:                m_CF = 0;
1.1.1.28  root     16816:                try {
1.1.1.46  root     16817:                        switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28  root     16818:                        case 0x00: msdos_int_21h_00h(); break;
                   16819:                        case 0x01: msdos_int_21h_01h(); break;
                   16820:                        case 0x02: msdos_int_21h_02h(); break;
                   16821:                        case 0x03: msdos_int_21h_03h(); break;
                   16822:                        case 0x04: msdos_int_21h_04h(); break;
                   16823:                        case 0x05: msdos_int_21h_05h(); break;
                   16824:                        case 0x06: msdos_int_21h_06h(); break;
                   16825:                        case 0x07: msdos_int_21h_07h(); break;
                   16826:                        case 0x08: msdos_int_21h_08h(); break;
                   16827:                        case 0x09: msdos_int_21h_09h(); break;
                   16828:                        case 0x0a: msdos_int_21h_0ah(); break;
                   16829:                        case 0x0b: msdos_int_21h_0bh(); break;
                   16830:                        case 0x0c: msdos_int_21h_0ch(); break;
                   16831:                        case 0x0d: msdos_int_21h_0dh(); break;
                   16832:                        case 0x0e: msdos_int_21h_0eh(); break;
                   16833:                        case 0x0f: msdos_int_21h_0fh(); break;
                   16834:                        case 0x10: msdos_int_21h_10h(); break;
                   16835:                        case 0x11: msdos_int_21h_11h(); break;
                   16836:                        case 0x12: msdos_int_21h_12h(); break;
                   16837:                        case 0x13: msdos_int_21h_13h(); break;
                   16838:                        case 0x14: msdos_int_21h_14h(); break;
                   16839:                        case 0x15: msdos_int_21h_15h(); break;
                   16840:                        case 0x16: msdos_int_21h_16h(); break;
                   16841:                        case 0x17: msdos_int_21h_17h(); break;
                   16842:                        case 0x18: msdos_int_21h_18h(); break;
                   16843:                        case 0x19: msdos_int_21h_19h(); break;
                   16844:                        case 0x1a: msdos_int_21h_1ah(); break;
                   16845:                        case 0x1b: msdos_int_21h_1bh(); break;
                   16846:                        case 0x1c: msdos_int_21h_1ch(); break;
                   16847:                        case 0x1d: msdos_int_21h_1dh(); break;
                   16848:                        case 0x1e: msdos_int_21h_1eh(); break;
                   16849:                        case 0x1f: msdos_int_21h_1fh(); break;
                   16850:                        case 0x20: msdos_int_21h_20h(); break;
                   16851:                        case 0x21: msdos_int_21h_21h(); break;
                   16852:                        case 0x22: msdos_int_21h_22h(); break;
                   16853:                        case 0x23: msdos_int_21h_23h(); break;
                   16854:                        case 0x24: msdos_int_21h_24h(); break;
                   16855:                        case 0x25: msdos_int_21h_25h(); break;
                   16856:                        case 0x26: msdos_int_21h_26h(); break;
                   16857:                        case 0x27: msdos_int_21h_27h(); break;
                   16858:                        case 0x28: msdos_int_21h_28h(); break;
                   16859:                        case 0x29: msdos_int_21h_29h(); break;
                   16860:                        case 0x2a: msdos_int_21h_2ah(); break;
                   16861:                        case 0x2b: msdos_int_21h_2bh(); break;
                   16862:                        case 0x2c: msdos_int_21h_2ch(); break;
                   16863:                        case 0x2d: msdos_int_21h_2dh(); break;
                   16864:                        case 0x2e: msdos_int_21h_2eh(); break;
                   16865:                        case 0x2f: msdos_int_21h_2fh(); break;
                   16866:                        case 0x30: msdos_int_21h_30h(); break;
                   16867:                        case 0x31: msdos_int_21h_31h(); break;
                   16868:                        case 0x32: msdos_int_21h_32h(); break;
                   16869:                        case 0x33: msdos_int_21h_33h(); break;
                   16870:                        case 0x34: msdos_int_21h_34h(); break;
                   16871:                        case 0x35: msdos_int_21h_35h(); break;
                   16872:                        case 0x36: msdos_int_21h_36h(); break;
                   16873:                        case 0x37: msdos_int_21h_37h(); break;
                   16874:                        case 0x38: msdos_int_21h_38h(); break;
                   16875:                        case 0x39: msdos_int_21h_39h(0); break;
                   16876:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   16877:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   16878:                        case 0x3c: msdos_int_21h_3ch(); break;
                   16879:                        case 0x3d: msdos_int_21h_3dh(); break;
                   16880:                        case 0x3e: msdos_int_21h_3eh(); break;
                   16881:                        case 0x3f: msdos_int_21h_3fh(); break;
                   16882:                        case 0x40: msdos_int_21h_40h(); break;
                   16883:                        case 0x41: msdos_int_21h_41h(0); break;
                   16884:                        case 0x42: msdos_int_21h_42h(); break;
                   16885:                        case 0x43: msdos_int_21h_43h(0); break;
                   16886:                        case 0x44: msdos_int_21h_44h(); break;
                   16887:                        case 0x45: msdos_int_21h_45h(); break;
                   16888:                        case 0x46: msdos_int_21h_46h(); break;
                   16889:                        case 0x47: msdos_int_21h_47h(0); break;
                   16890:                        case 0x48: msdos_int_21h_48h(); break;
                   16891:                        case 0x49: msdos_int_21h_49h(); break;
                   16892:                        case 0x4a: msdos_int_21h_4ah(); break;
                   16893:                        case 0x4b: msdos_int_21h_4bh(); break;
                   16894:                        case 0x4c: msdos_int_21h_4ch(); break;
                   16895:                        case 0x4d: msdos_int_21h_4dh(); break;
                   16896:                        case 0x4e: msdos_int_21h_4eh(); break;
                   16897:                        case 0x4f: msdos_int_21h_4fh(); break;
                   16898:                        case 0x50: msdos_int_21h_50h(); break;
                   16899:                        case 0x51: msdos_int_21h_51h(); break;
                   16900:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     16901:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     16902:                        case 0x54: msdos_int_21h_54h(); break;
                   16903:                        case 0x55: msdos_int_21h_55h(); break;
                   16904:                        case 0x56: msdos_int_21h_56h(0); break;
                   16905:                        case 0x57: msdos_int_21h_57h(); break;
                   16906:                        case 0x58: msdos_int_21h_58h(); break;
                   16907:                        case 0x59: msdos_int_21h_59h(); break;
                   16908:                        case 0x5a: msdos_int_21h_5ah(); break;
                   16909:                        case 0x5b: msdos_int_21h_5bh(); break;
                   16910:                        case 0x5c: msdos_int_21h_5ch(); break;
                   16911:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     16912:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     16913:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     16914:                        case 0x60: msdos_int_21h_60h(0); break;
                   16915:                        case 0x61: msdos_int_21h_61h(); break;
                   16916:                        case 0x62: msdos_int_21h_62h(); break;
                   16917:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     16918:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     16919:                        case 0x65: msdos_int_21h_65h(); break;
                   16920:                        case 0x66: msdos_int_21h_66h(); break;
                   16921:                        case 0x67: msdos_int_21h_67h(); break;
                   16922:                        case 0x68: msdos_int_21h_68h(); break;
                   16923:                        case 0x69: msdos_int_21h_69h(); break;
                   16924:                        case 0x6a: msdos_int_21h_6ah(); break;
                   16925:                        case 0x6b: msdos_int_21h_6bh(); break;
                   16926:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.48  root     16927:                        case 0x6d: // Find First ROM Program
                   16928:                        case 0x6e: // Find Next ROM Program
                   16929:                        case 0x6f: // Get/Set ROM Scan Start Address
                   16930:                                REG8(AL) = 0x00; // if not supported (DOS <5, MS-DOS 5+ non-ROM versions)
                   16931:                                break;
1.1.1.43  root     16932:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.48  root     16933:                        case 0x71: // Windows95 - Long Filename Functions
1.1.1.28  root     16934:                                switch(REG8(AL)) {
                   16935:                                case 0x0d: msdos_int_21h_710dh(); break;
                   16936:                                case 0x39: msdos_int_21h_39h(1); break;
                   16937:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   16938:                                case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.48  root     16939:                                case 0x41: msdos_int_21h_7141h(); break;
1.1.1.28  root     16940:                                case 0x43: msdos_int_21h_43h(1); break;
                   16941:                                case 0x47: msdos_int_21h_47h(1); break;
                   16942:                                case 0x4e: msdos_int_21h_714eh(); break;
                   16943:                                case 0x4f: msdos_int_21h_714fh(); break;
                   16944:                                case 0x56: msdos_int_21h_56h(1); break;
                   16945:                                case 0x60: msdos_int_21h_60h(1); break;
                   16946:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   16947:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   16948:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   16949:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   16950:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   16951:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45  root     16952:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     16953:                                case 0xaa: msdos_int_21h_71aah(); break;
                   16954:                                default:
                   16955:                                        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));
                   16956:                                        REG16(AX) = 0x7100;
                   16957:                                        m_CF = 1;
                   16958:                                        break;
                   16959:                                }
                   16960:                                break;
1.1.1.48  root     16961:                        case 0x72: // Windows95 beta - LFN FindClose
                   16962: //                             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));
                   16963:                                REG16(AX) = 0x7200;
                   16964:                                m_CF = 1;
                   16965:                                break;
                   16966:                        case 0x73: // Windows95 - FAT32 Functions
1.1.1.28  root     16967:                                switch(REG8(AL)) {
                   16968:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     16969:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     16970:                                case 0x02: msdos_int_21h_7302h(); break;
                   16971:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     16972:                                // 0x04: Set DPB to Use for Formatting
                   16973:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     16974:                                default:
                   16975:                                        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));
                   16976:                                        REG16(AX) = 0x7300;
                   16977:                                        m_CF = 1;
                   16978:                                        break;
                   16979:                                }
1.1       root     16980:                                break;
1.1.1.30  root     16981:                        case 0xdb: msdos_int_21h_dbh(); break;
                   16982:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     16983:                        default:
1.1.1.22  root     16984:                                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     16985:                                REG16(AX) = 0x01;
1.1.1.3   root     16986:                                m_CF = 1;
1.1       root     16987:                                break;
                   16988:                        }
1.1.1.28  root     16989:                } catch(int error) {
                   16990:                        REG16(AX) = error;
                   16991:                        m_CF = 1;
                   16992:                } catch(...) {
                   16993:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     16994:                        m_CF = 1;
1.1       root     16995:                }
1.1.1.3   root     16996:                if(m_CF) {
1.1.1.23  root     16997:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     16998:                        sda->int21h_5d0ah_called = 0;
1.1.1.23  root     16999:                        sda->extended_error_code = REG16(AX);
                   17000:                        switch(sda->extended_error_code) {
                   17001:                        case  4: // Too many open files
                   17002:                        case  8: // Insufficient memory
                   17003:                                sda->error_class = 1; // Out of resource
                   17004:                                break;
                   17005:                        case  5: // Access denied
                   17006:                                sda->error_class = 3; // Authorization
                   17007:                                break;
                   17008:                        case  7: // Memory control block destroyed
                   17009:                                sda->error_class = 4; // Internal
                   17010:                                break;
                   17011:                        case  2: // File not found
                   17012:                        case  3: // Path not found
                   17013:                        case 15: // Invaid drive specified
                   17014:                        case 18: // No more files
                   17015:                                sda->error_class = 8; // Not found
                   17016:                                break;
                   17017:                        case 32: // Sharing violation
                   17018:                        case 33: // Lock violation
                   17019:                                sda->error_class = 10; // Locked
                   17020:                                break;
                   17021: //                     case 16: // Removal of current directory attempted
                   17022:                        case 19: // Attempted write on protected disk
                   17023:                        case 21: // Drive not ready
                   17024: //                     case 29: // Write failure
                   17025: //                     case 30: // Read failure
                   17026: //                     case 82: // Cannot create subdirectory
                   17027:                                sda->error_class = 11; // Media
                   17028:                                break;
                   17029:                        case 80: // File already exists
                   17030:                                sda->error_class = 12; // Already exist
                   17031:                                break;
                   17032:                        default:
                   17033:                                sda->error_class = 13; // Unknown
                   17034:                                break;
                   17035:                        }
                   17036:                        sda->suggested_action = 1; // Retry
                   17037:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     17038:                }
1.1.1.33  root     17039:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     17040:                        // raise int 23h
                   17041: #if defined(HAS_I386)
                   17042:                        m_ext = 0; // not an external interrupt
                   17043:                        i386_trap(0x23, 1, 0);
                   17044:                        m_ext = 1;
                   17045: #else
                   17046:                        PREFIX86(_interrupt)(0x23);
                   17047: #endif
                   17048:                }
1.1       root     17049:                break;
                   17050:        case 0x22:
                   17051:                fatalerror("int 22h (terminate address)\n");
                   17052:        case 0x23:
1.1.1.28  root     17053:                try {
                   17054:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   17055:                } catch(...) {
                   17056:                        fatalerror("failed to terminate the current process by int 23h\n");
                   17057:                }
1.1       root     17058:                break;
                   17059:        case 0x24:
1.1.1.32  root     17060: /*
1.1.1.28  root     17061:                try {
                   17062:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17063:                } catch(...) {
                   17064:                        fatalerror("failed to terminate the current process by int 24h\n");
                   17065:                }
1.1.1.32  root     17066: */
                   17067:                msdos_int_24h();
1.1       root     17068:                break;
                   17069:        case 0x25:
                   17070:                msdos_int_25h();
                   17071:                break;
                   17072:        case 0x26:
                   17073:                msdos_int_26h();
                   17074:                break;
                   17075:        case 0x27:
1.1.1.28  root     17076:                try {
                   17077:                        msdos_int_27h();
                   17078:                } catch(...) {
                   17079:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   17080:                }
1.1       root     17081:                break;
                   17082:        case 0x28:
                   17083:                Sleep(10);
1.1.1.35  root     17084:                REQUEST_HARDWRE_UPDATE();
1.1       root     17085:                break;
                   17086:        case 0x29:
                   17087:                msdos_int_29h();
                   17088:                break;
                   17089:        case 0x2e:
                   17090:                msdos_int_2eh();
                   17091:                break;
                   17092:        case 0x2f:
                   17093:                // multiplex interrupt
                   17094:                switch(REG8(AH)) {
1.1.1.22  root     17095:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     17096:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     17097:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     17098:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     17099:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     17100:                case 0x14: msdos_int_2fh_14h(); break;
                   17101:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     17102:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     17103:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     17104:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     17105:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     17106:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     17107:                case 0x46: msdos_int_2fh_46h(); break;
                   17108:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     17109:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     17110:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     17111:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     17112:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     17113:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     17114:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     17115:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     17116:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     17117:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     17118:                default:
1.1.1.30  root     17119:                        switch(REG8(AL)) {
                   17120:                        case 0x00:
                   17121:                                // This is not installed
                   17122: //                             REG8(AL) = 0x00;
                   17123:                                break;
1.1.1.33  root     17124:                        case 0x01:
1.1.1.42  root     17125:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   17126:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   17127:                                        break;
                   17128:                                }
1.1.1.33  root     17129:                                // Banyan VINES v4+ is not installed
                   17130:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   17131:                                        break;
                   17132:                                }
1.1.1.42  root     17133:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   17134:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   17135:                                        break;
                   17136:                                }
1.1.1.30  root     17137:                        default:
1.1.1.42  root     17138:                                // NORTON UTILITIES 5.0+
                   17139:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   17140:                                        break;
                   17141:                                }
1.1.1.30  root     17142:                                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     17143:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     17144:                                m_CF = 1;
                   17145:                                break;
                   17146:                        }
                   17147:                        break;
1.1       root     17148:                }
                   17149:                break;
1.1.1.24  root     17150:        case 0x33:
                   17151:                switch(REG8(AH)) {
                   17152:                case 0x00:
                   17153:                        // Mouse
1.1.1.49  root     17154:                        switch(REG16(AX)) {
                   17155:                        case 0x0000: msdos_int_33h_0000h(); break;
                   17156:                        case 0x0001: msdos_int_33h_0001h(); break;
                   17157:                        case 0x0002: msdos_int_33h_0002h(); break;
                   17158:                        case 0x0003: msdos_int_33h_0003h(); break;
                   17159:                        case 0x0004: msdos_int_33h_0004h(); break;
                   17160:                        case 0x0005: msdos_int_33h_0005h(); break;
                   17161:                        case 0x0006: msdos_int_33h_0006h(); break;
                   17162:                        case 0x0007: msdos_int_33h_0007h(); break;
                   17163:                        case 0x0008: msdos_int_33h_0008h(); break;
                   17164:                        case 0x0009: msdos_int_33h_0009h(); break;
                   17165:                        case 0x000a: msdos_int_33h_000ah(); break;
                   17166:                        case 0x000b: msdos_int_33h_000bh(); break;
                   17167:                        case 0x000c: msdos_int_33h_000ch(); break;
                   17168:                        case 0x000d: break; // MS MOUSE v1.0+ - Light Pen Emulation On
                   17169:                        case 0x000e: break; // MS MOUSE v1.0+ - Light Pen Emulation Off
                   17170:                        case 0x000f: msdos_int_33h_000fh(); break;
                   17171:                        case 0x0010: break; // MS MOUSE v1.0+ - Define Screen Region for Updating
                   17172:                        case 0x0011: msdos_int_33h_0011h(); break;
                   17173:                        case 0x0012: REG16(AX) = 0xffff; break; // MS MOUSE - Set Large Graphics Cursor Block
                   17174:                        case 0x0013: break; // MS MOUSE v5.0+ - Define Double-Speed Threshold
                   17175:                        case 0x0014: msdos_int_33h_0014h(); break;
                   17176:                        case 0x0015: msdos_int_33h_0015h(); break;
                   17177:                        case 0x0016: msdos_int_33h_0016h(); break;
                   17178:                        case 0x0017: msdos_int_33h_0017h(); break;
                   17179:                        case 0x0018: msdos_int_33h_0018h(); break;
                   17180:                        case 0x0019: msdos_int_33h_0019h(); break;
                   17181:                        case 0x001a: msdos_int_33h_001ah(); break;
                   17182:                        case 0x001b: msdos_int_33h_001bh(); break;
                   17183:                        case 0x001c: break; // MS MOUSE v6.0+ - Set Interrupt Rate
                   17184:                        case 0x001d: msdos_int_33h_001dh(); break;
                   17185:                        case 0x001e: msdos_int_33h_001eh(); break;
                   17186:                        case 0x001f: msdos_int_33h_001fh(); break;
                   17187:                        case 0x0020: msdos_int_33h_0020h(); break;
                   17188:                        case 0x0021: msdos_int_33h_0021h(); break;
                   17189:                        case 0x0022: msdos_int_33h_0022h(); break;
                   17190:                        case 0x0023: msdos_int_33h_0023h(); break;
                   17191:                        case 0x0024: msdos_int_33h_0024h(); break;
                   17192:                        case 0x0025: msdos_int_33h_0025h(); break;
                   17193:                        case 0x0026: msdos_int_33h_0026h(); break;
                   17194:                        case 0x0027: msdos_int_33h_0027h(); break;
                   17195:                        case 0x0028: msdos_int_33h_0028h(); break;
                   17196:                        case 0x0029: msdos_int_33h_0029h(); break;
                   17197:                        case 0x002a: msdos_int_33h_002ah(); break;
                   17198:                        // 0x002b: MS MOUSE v7.0+ - Load Acceleration Profiles
                   17199:                        // 0x002c: MS MOUSE v7.0+ - Get Acceleration Profiles
                   17200:                        // 0x002d: MS MOUSE v7.0+ - Select Acceleration Profile
                   17201:                        // 0x002e: MS MOUSE v8.10+ - Set Acceleration Profile Names
                   17202:                        case 0x002f: break; // Mouse Hardware Reset
                   17203:                        // 0x0030: MS MOUSE v7.04+ - Get/Set BallPoint Information
                   17204:                        case 0x0031: msdos_int_33h_0031h(); break;
                   17205:                        case 0x0032: msdos_int_33h_0032h(); break;
                   17206:                        // 0x0033: MS MOUSE v7.05+ - Get Switch Settings And Acceleration Profile Data
                   17207:                        // 0x0034: MS MOUSE v8.0+ - Get Initialization File
                   17208:                        // 0x0035: MS MOUSE v8.10+ - LCD Screen Large Pointer Support
                   17209:                        case 0x004d: msdos_int_33h_004dh(); break;
                   17210:                        case 0x006d: msdos_int_33h_006dh(); break;
1.1.1.24  root     17211:                        default:
                   17212:                                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));
                   17213:                                break;
                   17214:                        }
                   17215:                        break;
                   17216:                default:
                   17217:                        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));
                   17218:                        break;
                   17219:                }
                   17220:                break;
1.1.1.50  root     17221: /*
                   17222:        case 0x67:
                   17223:                // int 67h handler is in EMS device driver (EMMXXXX0) and it calls int 68h
                   17224:                // NOTE: some softwares get address of int 67h handler and recognize the address is in EMS device driver
                   17225:                break;
                   17226: */
1.1.1.19  root     17227:        case 0x68:
                   17228:                // dummy interrupt for EMS (int 67h)
                   17229:                switch(REG8(AH)) {
                   17230:                case 0x40: msdos_int_67h_40h(); break;
                   17231:                case 0x41: msdos_int_67h_41h(); break;
                   17232:                case 0x42: msdos_int_67h_42h(); break;
                   17233:                case 0x43: msdos_int_67h_43h(); break;
                   17234:                case 0x44: msdos_int_67h_44h(); break;
                   17235:                case 0x45: msdos_int_67h_45h(); break;
                   17236:                case 0x46: msdos_int_67h_46h(); break;
                   17237:                case 0x47: msdos_int_67h_47h(); break;
                   17238:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     17239:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   17240:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     17241:                case 0x4b: msdos_int_67h_4bh(); break;
                   17242:                case 0x4c: msdos_int_67h_4ch(); break;
                   17243:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     17244:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     17245:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     17246:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     17247:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     17248:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     17249:                case 0x53: msdos_int_67h_53h(); break;
                   17250:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.49  root     17251:                case 0x55: msdos_int_67h_55h(); break;
                   17252:                case 0x56: msdos_int_67h_56h(); break;
1.1.1.20  root     17253:                case 0x57: msdos_int_67h_57h(); break;
                   17254:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     17255:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     17256:                case 0x5a: msdos_int_67h_5ah(); break;
1.1.1.49  root     17257:                case 0x5b: msdos_int_67h_5bh(); break;
1.1.1.43  root     17258:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   17259:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.49  root     17260:                case 0x70: msdos_int_67h_70h(); break;
1.1.1.31  root     17261:                // 0xde: VCPI
1.1.1.30  root     17262:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     17263:                default:
1.1.1.22  root     17264:                        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     17265:                        REG8(AH) = 0x84;
                   17266:                        break;
                   17267:                }
                   17268:                break;
                   17269: #ifdef SUPPORT_XMS
                   17270:        case 0x69:
                   17271:                // dummy interrupt for XMS (call far)
1.1.1.28  root     17272:                try {
                   17273:                        switch(REG8(AH)) {
                   17274:                        case 0x00: msdos_call_xms_00h(); break;
                   17275:                        case 0x01: msdos_call_xms_01h(); break;
                   17276:                        case 0x02: msdos_call_xms_02h(); break;
                   17277:                        case 0x03: msdos_call_xms_03h(); break;
                   17278:                        case 0x04: msdos_call_xms_04h(); break;
                   17279:                        case 0x05: msdos_call_xms_05h(); break;
                   17280:                        case 0x06: msdos_call_xms_06h(); break;
                   17281:                        case 0x07: msdos_call_xms_07h(); break;
                   17282:                        case 0x08: msdos_call_xms_08h(); break;
                   17283:                        case 0x09: msdos_call_xms_09h(); break;
                   17284:                        case 0x0a: msdos_call_xms_0ah(); break;
                   17285:                        case 0x0b: msdos_call_xms_0bh(); break;
                   17286:                        case 0x0c: msdos_call_xms_0ch(); break;
                   17287:                        case 0x0d: msdos_call_xms_0dh(); break;
                   17288:                        case 0x0e: msdos_call_xms_0eh(); break;
                   17289:                        case 0x0f: msdos_call_xms_0fh(); break;
                   17290:                        case 0x10: msdos_call_xms_10h(); break;
                   17291:                        case 0x11: msdos_call_xms_11h(); break;
                   17292:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     17293: #if defined(HAS_I386)
                   17294:                        case 0x88: msdos_call_xms_88h(); break;
                   17295:                        case 0x89: msdos_call_xms_89h(); break;
                   17296:                        case 0x8e: msdos_call_xms_8eh(); break;
                   17297:                        case 0x8f: msdos_call_xms_8fh(); break;
                   17298: #endif
1.1.1.28  root     17299:                        default:
                   17300:                                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));
                   17301:                                REG16(AX) = 0x0000;
                   17302:                                REG8(BL) = 0x80; // function not implemented
                   17303:                                break;
                   17304:                        }
                   17305:                } catch(...) {
1.1.1.19  root     17306:                        REG16(AX) = 0x0000;
1.1.1.28  root     17307:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     17308:                }
                   17309:                break;
                   17310: #endif
                   17311:        case 0x6a:
1.1.1.24  root     17312:                // irq12 (mouse)
                   17313:                mouse_push_ax = REG16(AX);
                   17314:                mouse_push_bx = REG16(BX);
                   17315:                mouse_push_cx = REG16(CX);
                   17316:                mouse_push_dx = REG16(DX);
                   17317:                mouse_push_si = REG16(SI);
                   17318:                mouse_push_di = REG16(DI);
                   17319:                
1.1.1.43  root     17320:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     17321:                        REG16(AX) = mouse.status_irq;
                   17322:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     17323:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17324:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     17325:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17326:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17327:                        
1.1.1.49  root     17328:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17329:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr.w.l & 0xff;
                   17330:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr.w.l >> 8;
                   17331:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr.w.h & 0xff;
                   17332:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.43  root     17333:                        break;
1.1.1.24  root     17334:                }
1.1.1.43  root     17335:                for(int i = 0; i < 8; i++) {
                   17336:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17337:                                REG16(AX) = mouse.status_irq_alt;
                   17338:                                REG16(BX) = mouse.get_buttons();
                   17339:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17340:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   17341:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17342:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17343:                                
1.1.1.49  root     17344:                                mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17345:                                mem[DUMMY_TOP + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   17346:                                mem[DUMMY_TOP + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   17347:                                mem[DUMMY_TOP + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   17348:                                mem[DUMMY_TOP + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
1.1.1.43  root     17349:                                break;
                   17350:                        }
                   17351:                }
1.1.1.54  root     17352:                if(mouse.status_irq_ps2 && mouse.call_addr_ps2.dw) {
                   17353:                        UINT16 data_1st, data_2nd, data_3rd;
                   17354:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   17355:                        i386_push16(data_1st);
                   17356:                        i386_push16(data_2nd);
                   17357:                        i386_push16(data_3rd);
                   17358:                        i386_push16(0x0000);
                   17359:                        
                   17360:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17361:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr_ps2.w.l & 0xff;
                   17362:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr_ps2.w.l >> 8;
                   17363:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr_ps2.w.h & 0xff;
                   17364:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr_ps2.w.h >> 8;
                   17365:                        break;
                   17366:                }
1.1.1.43  root     17367:                // invalid call addr :-(
1.1.1.49  root     17368:                mem[DUMMY_TOP + 0x02] = 0x90;   // nop
                   17369:                mem[DUMMY_TOP + 0x03] = 0x90;   // nop
                   17370:                mem[DUMMY_TOP + 0x04] = 0x90;   // nop
                   17371:                mem[DUMMY_TOP + 0x05] = 0x90;   // nop
                   17372:                mem[DUMMY_TOP + 0x06] = 0x90;   // nop
1.1.1.24  root     17373:                break;
                   17374:        case 0x6b:
                   17375:                // end of irq12 (mouse)
                   17376:                REG16(AX) = mouse_push_ax;
                   17377:                REG16(BX) = mouse_push_bx;
                   17378:                REG16(CX) = mouse_push_cx;
                   17379:                REG16(DX) = mouse_push_dx;
                   17380:                REG16(SI) = mouse_push_si;
                   17381:                REG16(DI) = mouse_push_di;
                   17382:                
                   17383:                // EOI
                   17384:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   17385:                        pic[0].isr &= ~(1 << 2); // master
                   17386:                }
                   17387:                pic_update();
                   17388:                break;
                   17389:        case 0x6c:
1.1.1.19  root     17390:                // dummy interrupt for case map routine pointed in the country info
                   17391:                if(REG8(AL) >= 0x80) {
                   17392:                        char tmp[2] = {0};
                   17393:                        tmp[0] = REG8(AL);
                   17394:                        my_strupr(tmp);
                   17395:                        REG8(AL) = tmp[0];
                   17396:                }
                   17397:                break;
1.1.1.27  root     17398:        case 0x6d:
                   17399:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   17400:                REG8(AL) = 0x86; // not supported
                   17401:                m_CF = 1;
                   17402:                break;
1.1.1.32  root     17403:        case 0x6e:
                   17404:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   17405:                {
                   17406:                        UINT16 code = REG16(AX);
                   17407:                        if(code & 0xf0) {
                   17408:                                code = (code & 7) | ((code & 0x10) >> 1);
                   17409:                        }
                   17410:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   17411:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   17412:                                        const char *message = NULL;
                   17413:                                        if(active_code_page == 932) {
                   17414:                                                message = param_error_table[i].message_japanese;
                   17415:                                        }
                   17416:                                        if(message == NULL) {
                   17417:                                                message = param_error_table[i].message_english;
                   17418:                                        }
                   17419:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   17420:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   17421:                                        
                   17422:                                        SREG(ES) = WORK_TOP >> 4;
                   17423:                                        i386_load_segment_descriptor(ES);
                   17424:                                        REG16(DI) = 0x0000;
                   17425:                                        break;
                   17426:                                }
                   17427:                        }
                   17428:                }
                   17429:                break;
1.1.1.49  root     17430:        case 0x6f:
                   17431:                // dummy interrupt for end of alter page map and call
                   17432:                {
                   17433:                        UINT16 handles[4], pages[4];
                   17434:                        
                   17435:                        // pop old mapping data in new mapping
                   17436:                        for(int i = 0; i < 4; i++) {
                   17437:                                pages  [3 - i] = i386_pop16();
                   17438:                                handles[3 - i] = i386_pop16();
                   17439:                        }
                   17440:                        
                   17441:                        // restore old mapping
                   17442:                        for(int i = 0; i < 4; i++) {
                   17443:                                UINT16 handle = handles[i];
                   17444:                                UINT16 page   = pages  [i];
                   17445:                                
                   17446:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   17447:                                        ems_map_page(i, handle, page);
                   17448:                                } else {
                   17449:                                        ems_unmap_page(i);
                   17450:                                }
                   17451:                        }
                   17452:                        // do ret_far (pop cs/ip) in old mapping
                   17453:                }
                   17454:                break;
1.1.1.8   root     17455:        case 0x70:
                   17456:        case 0x71:
                   17457:        case 0x72:
                   17458:        case 0x73:
                   17459:        case 0x74:
                   17460:        case 0x75:
                   17461:        case 0x76:
                   17462:        case 0x77:
                   17463:                // EOI
                   17464:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   17465:                        pic[0].isr &= ~(1 << 2); // master
                   17466:                }
                   17467:                pic_update();
                   17468:                break;
1.1       root     17469:        default:
1.1.1.22  root     17470: //             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     17471:                break;
                   17472:        }
                   17473:        
                   17474:        // update cursor position
                   17475:        if(cursor_moved) {
1.1.1.36  root     17476:                pcbios_update_cursor_position();
1.1       root     17477:                cursor_moved = false;
                   17478:        }
                   17479: }
                   17480: 
                   17481: // init
                   17482: 
                   17483: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   17484: {
                   17485:        // init file handler
                   17486:        memset(file_handler, 0, sizeof(file_handler));
                   17487:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   17488:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   17489:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     17490: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     17491:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     17492: #else
                   17493:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   17494: #endif
                   17495:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     17496:        }
1.1.1.21  root     17497:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45  root     17498: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   17499:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     17500:        }
1.1       root     17501:        _dup2(0, DUP_STDIN);
                   17502:        _dup2(1, DUP_STDOUT);
                   17503:        _dup2(2, DUP_STDERR);
1.1.1.21  root     17504:        _dup2(3, DUP_STDAUX);
                   17505:        _dup2(4, DUP_STDPRN);
1.1       root     17506:        
1.1.1.24  root     17507:        // init mouse
                   17508:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     17509:        mouse.enabled = true;   // from DOSBox
                   17510:        mouse.hidden = 1;       // hidden in default ???
                   17511:        mouse.old_hidden = 1;   // from DOSBox
                   17512:        mouse.max_position.x = 8 * (scr_width  - 1);
                   17513:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     17514:        mouse.mickey.x = 8;
                   17515:        mouse.mickey.y = 16;
                   17516:        
1.1.1.26  root     17517: #ifdef SUPPORT_XMS
                   17518:        // init xms
                   17519:        msdos_xms_init();
                   17520: #endif
                   17521:        
1.1       root     17522:        // init process
                   17523:        memset(process, 0, sizeof(process));
                   17524:        
1.1.1.13  root     17525:        // init dtainfo
                   17526:        msdos_dta_info_init();
                   17527:        
1.1       root     17528:        // init memory
                   17529:        memset(mem, 0, sizeof(mem));
                   17530:        
                   17531:        // bios data area
1.1.1.23  root     17532:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     17533:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   17534:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     17535:        CONSOLE_FONT_INFO cfi;
                   17536:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   17537:        
                   17538:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   17539:        text_vram_top_address = TEXT_VRAM_TOP;
                   17540:        text_vram_end_address = text_vram_top_address + regen;
                   17541:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   17542:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     17543:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1.1.14  root     17544:        
                   17545:        if(regen > 0x4000) {
                   17546:                regen = 0x8000;
                   17547:                vram_pages = 1;
                   17548:        } else if(regen > 0x2000) {
                   17549:                regen = 0x4000;
                   17550:                vram_pages = 2;
                   17551:        } else if(regen > 0x1000) {
                   17552:                regen = 0x2000;
                   17553:                vram_pages = 4;
                   17554:        } else {
                   17555:                regen = 0x1000;
                   17556:                vram_pages = 8;
                   17557:        }
1.1       root     17558:        
1.1.1.25  root     17559:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   17560:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     17561:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   17562:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     17563:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     17564:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   17565:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     17566: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17567:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     17568: #endif
1.1.1.26  root     17569:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     17570:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     17571:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   17572:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     17573:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     17574:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   17575:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     17576:        *(UINT16 *)(mem + 0x44e) = 0;
                   17577:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     17578:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     17579:        *(UINT8  *)(mem + 0x460) = 7;
                   17580:        *(UINT8  *)(mem + 0x461) = 7;
                   17581:        *(UINT8  *)(mem + 0x462) = 0;
                   17582:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     17583:        *(UINT8  *)(mem + 0x465) = 0x09;
                   17584:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     17585:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     17586:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   17587:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     17588:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   17589:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   17590:        *(UINT8  *)(mem + 0x487) = 0x60;
                   17591:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     17592: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17593:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     17594: #endif
1.1.1.14  root     17595:        
                   17596:        // initial screen
                   17597:        SMALL_RECT rect;
                   17598:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   17599:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   17600:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   17601:                for(int x = 0; x < scr_width; x++) {
                   17602:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   17603:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   17604:                }
                   17605:        }
1.1       root     17606:        
1.1.1.19  root     17607:        // init mcb
1.1       root     17608:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     17609:        
                   17610:        // iret table
                   17611:        // note: int 2eh vector should address the routine in command.com,
                   17612:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   17613:        // so move iret table into allocated memory block
                   17614:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33  root     17615:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19  root     17616:        IRET_TOP = seg << 4;
                   17617:        seg += IRET_SIZE >> 4;
1.1.1.25  root     17618:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     17619:        
                   17620:        // dummy xms/ems device
1.1.1.33  root     17621:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     17622:        XMS_TOP = seg << 4;
                   17623:        seg += XMS_SIZE >> 4;
                   17624:        
                   17625:        // environment
1.1.1.33  root     17626:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     17627:        int env_seg = seg;
                   17628:        int ofs = 0;
1.1.1.32  root     17629:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   17630:        char comspec_added = 0;
1.1.1.33  root     17631:        char lastdrive_added = 0;
1.1.1.32  root     17632:        char env_msdos_path[ENV_SIZE] = {0};
                   17633:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     17634:        char prompt_added = 0;
1.1.1.32  root     17635:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     17636:        char tz_added = 0;
1.1.1.45  root     17637:        const char *path, *short_path;
1.1.1.32  root     17638:        
                   17639:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   17640:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17641:                        strcpy(env_append, short_path);
                   17642:                }
                   17643:        }
                   17644:        if((path = getenv("APPEND")) != NULL) {
                   17645:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17646:                        if(env_append[0] != '\0') {
                   17647:                                strcat(env_append, ";");
                   17648:                        }
                   17649:                        strcat(env_append, short_path);
                   17650:                }
                   17651:        }
                   17652:        
                   17653:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   17654:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17655:                        strcpy(comspec_path, short_path);
                   17656:                }
                   17657:        }
                   17658:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   17659:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17660:                        strcpy(comspec_path, short_path);
                   17661:                }
                   17662:        }
1.1       root     17663:        
1.1.1.28  root     17664:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     17665:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17666:                        strcpy(env_msdos_path, short_path);
                   17667:                        strcpy(env_path, short_path);
1.1.1.14  root     17668:                }
                   17669:        }
1.1.1.28  root     17670:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     17671:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17672:                        if(env_path[0] != '\0') {
                   17673:                                strcat(env_path, ";");
                   17674:                        }
                   17675:                        strcat(env_path, short_path);
1.1.1.9   root     17676:                }
                   17677:        }
1.1.1.32  root     17678:        
                   17679:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   17680:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     17681:        }
1.1.1.32  root     17682:        for(int i = 0; i < 4; i++) {
                   17683:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   17684:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   17685:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17686:                                strcpy(env_temp, short_path);
                   17687:                                break;
                   17688:                        }
                   17689:                }
1.1.1.24  root     17690:        }
1.1.1.32  root     17691:        
1.1.1.9   root     17692:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     17693:                // lower to upper
1.1.1.28  root     17694:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     17695:                strcpy(tmp, *p);
                   17696:                for(int i = 0;; i++) {
                   17697:                        if(tmp[i] == '=') {
                   17698:                                tmp[i] = '\0';
                   17699:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     17700:                                my_strupr(name);
1.1       root     17701:                                tmp[i] = '=';
                   17702:                                break;
                   17703:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     17704:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     17705:                        }
                   17706:                }
1.1.1.33  root     17707:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   17708:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   17709:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     17710:                        // ignore non standard environments
                   17711:                } else {
1.1.1.33  root     17712:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     17713:                                if(env_append[0] != '\0') {
                   17714:                                        sprintf(tmp, "APPEND=%s", env_append);
                   17715:                                } else {
                   17716:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   17717:                                }
                   17718:                                append_added = 1;
                   17719:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     17720:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     17721:                                comspec_added = 1;
1.1.1.33  root     17722:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   17723:                                char *env = getenv("MSDOS_LASTDRIVE");
                   17724:                                if(env != NULL) {
                   17725:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   17726:                                }
                   17727:                                lastdrive_added = 1;
                   17728:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     17729:                                if(env_msdos_path[0] != '\0') {
                   17730:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   17731:                                } else {
                   17732:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   17733:                                }
1.1.1.33  root     17734:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     17735:                                if(env_path[0] != '\0') {
                   17736:                                        sprintf(tmp, "PATH=%s", env_path);
                   17737:                                } else {
                   17738:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   17739:                                }
1.1.1.32  root     17740:                                path_added = 1;
1.1.1.33  root     17741:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   17742:                                prompt_added = 1;
1.1.1.28  root     17743:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   17744:                                if(env_temp[0] != '\0') {
                   17745:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   17746:                                } else {
                   17747:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   17748:                                }
1.1.1.32  root     17749:                                temp_added = 1;
1.1.1.33  root     17750:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     17751:                                if(env_temp[0] != '\0') {
                   17752:                                        sprintf(tmp, "TMP=%s", env_temp);
                   17753:                                } else {
                   17754:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     17755:                                }
1.1.1.32  root     17756:                                tmp_added = 1;
1.1.1.33  root     17757:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   17758:                                char *env = getenv("MSDOS_TZ");
                   17759:                                if(env != NULL) {
                   17760:                                        sprintf(tmp, "TZ=%s", env);
                   17761:                                }
                   17762:                                tz_added = 1;
1.1       root     17763:                        }
                   17764:                        int len = strlen(tmp);
1.1.1.14  root     17765:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     17766:                                fatalerror("too many environments\n");
                   17767:                        }
                   17768:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   17769:                        ofs += len + 1;
                   17770:                }
                   17771:        }
1.1.1.32  root     17772:        if(!append_added && env_append[0] != '\0') {
                   17773:                #define SET_ENV(name, value) { \
                   17774:                        char tmp[ENV_SIZE]; \
                   17775:                        sprintf(tmp, "%s=%s", name, value); \
                   17776:                        int len = strlen(tmp); \
                   17777:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   17778:                                fatalerror("too many environments\n"); \
                   17779:                        } \
                   17780:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   17781:                        ofs += len + 1; \
                   17782:                }
                   17783:                SET_ENV("APPEND", env_append);
                   17784:        }
                   17785:        if(!comspec_added) {
                   17786:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   17787:        }
1.1.1.33  root     17788:        if(!lastdrive_added) {
                   17789:                SET_ENV("LASTDRIVE", "Z");
                   17790:        }
1.1.1.32  root     17791:        if(!path_added) {
                   17792:                SET_ENV("PATH", env_path);
                   17793:        }
1.1.1.33  root     17794:        if(!prompt_added) {
                   17795:                SET_ENV("PROMPT", "$P$G");
                   17796:        }
1.1.1.32  root     17797:        if(!temp_added) {
                   17798:                SET_ENV("TEMP", env_temp);
                   17799:        }
                   17800:        if(!tmp_added) {
                   17801:                SET_ENV("TMP", env_temp);
                   17802:        }
1.1.1.33  root     17803:        if(!tz_added) {
                   17804:                TIME_ZONE_INFORMATION tzi;
                   17805:                HKEY hKey, hSubKey;
                   17806:                char tzi_std_name[64];
                   17807:                char tz_std[8] = "GMT";
                   17808:                char tz_dlt[8] = "GST";
                   17809:                char tz_value[32];
                   17810:                
                   17811:                // timezone name from GetTimeZoneInformation may not be english
                   17812:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   17813:                setlocale(LC_CTYPE, "");
                   17814:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   17815:                
                   17816:                // get english timezone name from registry
                   17817:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   17818:                        for(DWORD i = 0; !tz_added; i++) {
                   17819:                                char reg_name[256], sub_key[1024], std_name[256];
                   17820:                                DWORD size;
                   17821:                                FILETIME ftTime;
                   17822:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   17823:                                
                   17824:                                if(result == ERROR_SUCCESS) {
                   17825:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   17826:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   17827:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   17828:                                                        // search english timezone name from table
1.1.1.37  root     17829:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     17830:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   17831:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   17832:                                                                                if(tz_table[j].std != NULL) {
                   17833:                                                                                        strcpy(tz_std, tz_table[j].std);
                   17834:                                                                                }
                   17835:                                                                                if(tz_table[j].dlt != NULL) {
                   17836:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   17837:                                                                                }
                   17838:                                                                                tz_added = 1;
                   17839:                                                                                break;
                   17840:                                                                        }
                   17841:                                                                }
                   17842:                                                        }
                   17843:                                                }
                   17844:                                                RegCloseKey(hSubKey);
                   17845:                                        }
                   17846:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   17847:                                        break;
                   17848:                                }
                   17849:                        }
                   17850:                        RegCloseKey(hKey);
                   17851:                }
                   17852:                if((tzi.Bias % 60) != 0) {
                   17853:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   17854:                } else {
                   17855:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   17856:                }
                   17857:                if(daylight) {
                   17858:                        strcat(tz_value, tz_dlt);
                   17859:                }
                   17860:                SET_ENV("TZ", tz_value);
                   17861:        }
1.1       root     17862:        seg += (ENV_SIZE >> 4);
                   17863:        
                   17864:        // psp
1.1.1.33  root     17865:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     17866:        current_psp = seg;
1.1.1.35  root     17867:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   17868:        psp->parent_psp = current_psp;
1.1       root     17869:        seg += (PSP_SIZE >> 4);
                   17870:        
1.1.1.19  root     17871:        // first free mcb in conventional memory
1.1.1.33  root     17872:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     17873:        first_mcb = seg;
                   17874:        
1.1.1.19  root     17875:        // dummy mcb to link to umb
1.1.1.33  root     17876: #if 0
1.1.1.39  root     17877:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     17878: #else
1.1.1.39  root     17879:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     17880: #endif
1.1.1.19  root     17881:        
                   17882:        // first free mcb in upper memory block
1.1.1.8   root     17883:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     17884:        
1.1.1.29  root     17885: #ifdef SUPPORT_HMA
                   17886:        // first free mcb in high memory area
                   17887:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   17888: #endif
                   17889:        
1.1.1.26  root     17890:        // interrupt vector
                   17891:        for(int i = 0; i < 0x80; i++) {
                   17892:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   17893:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   17894:        }
1.1.1.49  root     17895:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffc:0018 irq0 (system timer)
                   17896:        *(UINT16 *)(mem + 4 * 0x08 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     17897:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   17898:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   17899:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   17900:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
1.1.1.49  root     17901:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffc:0000 irq12 (mouse)
                   17902:        *(UINT16 *)(mem + 4 * 0x74 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     17903:        
1.1.1.29  root     17904:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     17905:        static const struct {
                   17906:                UINT16 attributes;
                   17907:                char *dev_name;
                   17908:        } dummy_devices[] = {
                   17909:                {0x8013, "CON     "},
                   17910:                {0x8000, "AUX     "},
                   17911:                {0xa0c0, "PRN     "},
                   17912:                {0x8008, "CLOCK$  "},
                   17913:                {0x8000, "COM1    "},
                   17914:                {0xa0c0, "LPT1    "},
                   17915:                {0xa0c0, "LPT2    "},
                   17916:                {0xa0c0, "LPT3    "},
                   17917:                {0x8000, "COM2    "},
                   17918:                {0x8000, "COM3    "},
                   17919:                {0x8000, "COM4    "},
1.1.1.30  root     17920: //             {0xc000, "CONFIG$ "},
                   17921:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     17922:        };
                   17923:        static const UINT8 dummy_device_routine[] = {
                   17924:                // from NUL device of Windows 98 SE
                   17925:                // or word ptr ES:[BX+03],0100
                   17926:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   17927:                // retf
                   17928:                0xcb,
                   17929:        };
1.1.1.29  root     17930:        device_t *last = NULL;
1.1.1.32  root     17931:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     17932:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     17933:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   17934:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     17935:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     17936:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   17937:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     17938:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     17939:                last = device;
                   17940:        }
                   17941:        if(last != NULL) {
                   17942:                last->next_driver.w.l = 0;
                   17943:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     17944:        }
1.1.1.29  root     17945:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     17946:        
1.1.1.25  root     17947:        // dos info
                   17948:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   17949:        dos_info->magic_word = 1;
                   17950:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   17951:        dos_info->first_dpb.w.l = 0;
                   17952:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   17953:        dos_info->first_sft.w.l = 0;
                   17954:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     17955:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     17956:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     17957:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     17958:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   17959:        dos_info->max_sector_len = 512;
                   17960:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   17961:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   17962:        dos_info->cds.w.l = 0;
                   17963:        dos_info->cds.w.h = CDS_TOP >> 4;
                   17964:        dos_info->fcb_table.w.l = 0;
                   17965:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   17966:        dos_info->last_drive = 'Z' - 'A' + 1;
                   17967:        dos_info->buffers_x = 20;
                   17968:        dos_info->buffers_y = 0;
                   17969:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     17970:        dos_info->nul_device.next_driver.w.l = 22;
                   17971:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     17972:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     17973:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   17974:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     17975:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   17976:        dos_info->disk_buf_heads.w.l = 0;
                   17977:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     17978:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     17979:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   17980:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     17981:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     17982:        
                   17983:        char *env;
                   17984:        if((env = getenv("LASTDRIVE")) != NULL) {
                   17985:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   17986:                        dos_info->last_drive = env[0] - 'A' + 1;
                   17987:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   17988:                        dos_info->last_drive = env[0] - 'a' + 1;
                   17989:                }
                   17990:        }
                   17991:        if((env = getenv("windir")) != NULL) {
                   17992:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   17993:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   17994:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   17995:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   17996:                }
                   17997:        }
                   17998: #if defined(HAS_I386)
                   17999:        dos_info->i386_or_later = 1;
                   18000: #else
                   18001:        dos_info->i386_or_later = 0;
                   18002: #endif
                   18003:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   18004:        
1.1.1.27  root     18005:        // ems (int 67h) and xms
1.1.1.25  root     18006:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   18007:        xms_device->next_driver.w.l = 0xffff;
                   18008:        xms_device->next_driver.w.h = 0xffff;
                   18009:        xms_device->attributes = 0xc000;
1.1.1.29  root     18010:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   18011:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18012:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   18013:        
1.1.1.26  root     18014:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   18015:        mem[XMS_TOP + 0x13] = 0x68;
                   18016:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     18017: #ifdef SUPPORT_XMS
                   18018:        if(support_xms) {
1.1.1.26  root     18019:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   18020:                mem[XMS_TOP + 0x16] = 0x69;
                   18021:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     18022:        } else
                   18023: #endif
1.1.1.26  root     18024:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     18025:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     18026:        
1.1.1.26  root     18027:        // irq12 routine (mouse)
1.1.1.49  root     18028:        mem[DUMMY_TOP + 0x00] = 0xcd;   // int 6ah (dummy)
                   18029:        mem[DUMMY_TOP + 0x01] = 0x6a;
                   18030:        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far mouse
                   18031:        mem[DUMMY_TOP + 0x03] = 0xff;
                   18032:        mem[DUMMY_TOP + 0x04] = 0xff;
                   18033:        mem[DUMMY_TOP + 0x05] = 0xff;
                   18034:        mem[DUMMY_TOP + 0x06] = 0xff;
                   18035:        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   18036:        mem[DUMMY_TOP + 0x08] = 0x6b;
                   18037:        mem[DUMMY_TOP + 0x09] = 0xcf;   // iret
1.1.1.24  root     18038:        
1.1.1.27  root     18039:        // case map routine
1.1.1.49  root     18040:        mem[DUMMY_TOP + 0x0a] = 0xcd;   // int 6ch (dummy)
                   18041:        mem[DUMMY_TOP + 0x0b] = 0x6c;
                   18042:        mem[DUMMY_TOP + 0x0c] = 0xcb;   // retf
1.1.1.27  root     18043:        
                   18044:        // font read routine
1.1.1.49  root     18045:        mem[DUMMY_TOP + 0x0d] = 0xcd;   // int 6dh (dummy)
                   18046:        mem[DUMMY_TOP + 0x0e] = 0x6d;
                   18047:        mem[DUMMY_TOP + 0x0f] = 0xcb;   // retf
1.1.1.19  root     18048:        
1.1.1.32  root     18049:        // error message read routine
1.1.1.49  root     18050:        mem[DUMMY_TOP + 0x10] = 0xcd;   // int 6eh (dummy)
                   18051:        mem[DUMMY_TOP + 0x11] = 0x6e;
                   18052:        mem[DUMMY_TOP + 0x12] = 0xcb;   // retf
1.1.1.32  root     18053:        
1.1.1.35  root     18054:        // dummy loop to wait BIOS/DOS service is done
1.1.1.49  root     18055:        mem[DUMMY_TOP + 0x13] = 0xe6;   // out f7h, al
                   18056:        mem[DUMMY_TOP + 0x14] = 0xf7;
                   18057:        mem[DUMMY_TOP + 0x15] = 0x78;   // js/jns -4
                   18058:        mem[DUMMY_TOP + 0x16] = 0xfc;
                   18059:        mem[DUMMY_TOP + 0x17] = 0xcb;   // retf
1.1.1.35  root     18060:        
1.1.1.50  root     18061:        // irq0 routine (system timer)
1.1.1.49  root     18062:        mem[DUMMY_TOP + 0x18] = 0xcd;   // int 1ch
                   18063:        mem[DUMMY_TOP + 0x19] = 0x1c;
                   18064:        mem[DUMMY_TOP + 0x1a] = 0xea;   // jmp far (IRET_TOP >> 4):0008
                   18065:        mem[DUMMY_TOP + 0x1b] = 0x08;
                   18066:        mem[DUMMY_TOP + 0x1c] = 0x00;
                   18067:        mem[DUMMY_TOP + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   18068:        mem[DUMMY_TOP + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
                   18069:        
                   18070:        // alter page map and call routine
                   18071:        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   18072:        mem[DUMMY_TOP + 0x20] = 0xff;
                   18073:        mem[DUMMY_TOP + 0x21] = 0xff;
                   18074:        mem[DUMMY_TOP + 0x22] = 0xff;
                   18075:        mem[DUMMY_TOP + 0x23] = 0xff;
                   18076:        mem[DUMMY_TOP + 0x24] = 0xcd;   // int 6fh (dummy)
                   18077:        mem[DUMMY_TOP + 0x25] = 0x6f;
                   18078:        mem[DUMMY_TOP + 0x26] = 0xcb;   // retf
1.1.1.14  root     18079:        
1.1.1.50  root     18080:        // call int 29h routine
                   18081:        mem[DUMMY_TOP + 0x27] = 0xcd;   // int 29h
                   18082:        mem[DUMMY_TOP + 0x28] = 0x29;
                   18083:        mem[DUMMY_TOP + 0x29] = 0xcb;   // retf
                   18084:        
1.1.1.26  root     18085:        // boot routine
1.1.1.49  root     18086:        mem[0xffff0 + 0x00] = 0xf4;     // halt
                   18087:        mem[0xffff0 + 0x05] = '0';      // rom date
                   18088:        mem[0xffff0 + 0x06] = '2';
                   18089:        mem[0xffff0 + 0x07] = '/';
                   18090:        mem[0xffff0 + 0x08] = '2';
                   18091:        mem[0xffff0 + 0x09] = '2';
                   18092:        mem[0xffff0 + 0x0a] = '/';
                   18093:        mem[0xffff0 + 0x0b] = '0';
                   18094:        mem[0xffff0 + 0x0c] = '6';
                   18095:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id
                   18096:        mem[0xffff0 + 0x0f] = 0x00;
1.1.1.24  root     18097:        
1.1       root     18098:        // param block
                   18099:        // + 0: param block (22bytes)
                   18100:        // +24: fcb1/2 (20bytes)
                   18101:        // +44: command tail (128bytes)
                   18102:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   18103:        param->env_seg = 0;
                   18104:        param->cmd_line.w.l = 44;
                   18105:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   18106:        param->fcb1.w.l = 24;
                   18107:        param->fcb1.w.h = (WORK_TOP >> 4);
                   18108:        param->fcb2.w.l = 24;
                   18109:        param->fcb2.w.h = (WORK_TOP >> 4);
                   18110:        
                   18111:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   18112:        
                   18113:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   18114:        if(argc > 1) {
                   18115:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   18116:                for(int i = 2; i < argc; i++) {
                   18117:                        char tmp[128];
                   18118:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   18119:                        strcpy(cmd_line->cmd, tmp);
                   18120:                }
                   18121:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   18122:        } else {
                   18123:                cmd_line->len = 0;
                   18124:        }
                   18125:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   18126:        
                   18127:        // system file table
1.1.1.21  root     18128:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   18129:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     18130:        
1.1.1.19  root     18131:        // disk buffer header (from DOSBox)
                   18132:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   18133:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   18134:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   18135:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   18136:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   18137:        
1.1       root     18138:        // fcb table
                   18139:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     18140:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     18141:        
1.1.1.41  root     18142:        // drive parameter block
1.1.1.42  root     18143:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     18144:                // may be a floppy drive
1.1.1.44  root     18145:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   18146:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   18147:                cds->drive_attrib = 0x4000;     // physical drive
                   18148:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   18149:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   18150:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   18151:                cds->bs_offset = 2;
                   18152:                
1.1.1.41  root     18153:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   18154:                dpb->drive_num = i;
                   18155:                dpb->unit_num = i;
1.1.1.43  root     18156:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   18157:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     18158:        }
                   18159:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     18160:                msdos_cds_update(i);
1.1.1.42  root     18161:                UINT16 seg, ofs;
                   18162:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     18163:        }
                   18164:        
1.1.1.17  root     18165:        // nls stuff
                   18166:        msdos_nls_tables_init();
1.1       root     18167:        
                   18168:        // execute command
1.1.1.28  root     18169:        try {
1.1.1.52  root     18170:                if(msdos_process_exec(argv[0], param, 0, true)) {
1.1.1.28  root     18171:                        fatalerror("'%s' not found\n", argv[0]);
                   18172:                }
                   18173:        } catch(...) {
                   18174:                // we should not reach here :-(
                   18175:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     18176:        }
                   18177:        retval = 0;
                   18178:        return(0);
                   18179: }
                   18180: 
                   18181: #define remove_std_file(path) { \
                   18182:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   18183:        if(fd != -1) { \
                   18184:                _lseek(fd, 0, SEEK_END); \
                   18185:                int size = _tell(fd); \
                   18186:                _close(fd); \
                   18187:                if(size == 0) { \
                   18188:                        remove(path); \
                   18189:                } \
                   18190:        } \
                   18191: }
                   18192: 
                   18193: void msdos_finish()
                   18194: {
                   18195:        for(int i = 0; i < MAX_FILES; i++) {
                   18196:                if(file_handler[i].valid) {
                   18197:                        _close(i);
                   18198:                }
                   18199:        }
1.1.1.21  root     18200: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     18201:        remove_std_file("stdaux.txt");
1.1.1.21  root     18202: #endif
1.1.1.30  root     18203: #ifdef SUPPORT_XMS
                   18204:        msdos_xms_finish();
                   18205: #endif
1.1       root     18206:        msdos_dbcs_table_finish();
                   18207: }
                   18208: 
                   18209: /* ----------------------------------------------------------------------------
                   18210:        PC/AT hardware emulation
                   18211: ---------------------------------------------------------------------------- */
                   18212: 
                   18213: void hardware_init()
                   18214: {
1.1.1.3   root     18215:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     18216:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     18217:        m_IF = 1;
1.1.1.3   root     18218: #if defined(HAS_I386)
1.1       root     18219:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   18220:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     18221: #endif
                   18222:        i386_set_a20_line(0);
1.1.1.14  root     18223:        
1.1.1.19  root     18224:        ems_init();
1.1.1.25  root     18225:        dma_init();
1.1       root     18226:        pic_init();
1.1.1.25  root     18227:        pio_init();
1.1.1.8   root     18228: #ifdef PIT_ALWAYS_RUNNING
                   18229:        pit_init();
                   18230: #else
1.1       root     18231:        pit_active = 0;
                   18232: #endif
1.1.1.25  root     18233:        sio_init();
1.1.1.8   root     18234:        cmos_init();
                   18235:        kbd_init();
1.1       root     18236: }
                   18237: 
1.1.1.10  root     18238: void hardware_finish()
                   18239: {
                   18240: #if defined(HAS_I386)
                   18241:        vtlb_free(m_vtlb);
                   18242: #endif
1.1.1.19  root     18243:        ems_finish();
1.1.1.37  root     18244:        pio_finish();
1.1.1.25  root     18245:        sio_finish();
1.1.1.10  root     18246: }
                   18247: 
1.1.1.28  root     18248: void hardware_release()
                   18249: {
                   18250:        // release hardware resources when this program will be terminated abnormally
                   18251: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18252:        if(fp_debug_log != NULL) {
                   18253:                fclose(fp_debug_log);
                   18254:                fp_debug_log = NULL;
1.1.1.28  root     18255:        }
                   18256: #endif
                   18257: #if defined(HAS_I386)
                   18258:        vtlb_free(m_vtlb);
                   18259: #endif
                   18260:        ems_release();
1.1.1.37  root     18261:        pio_release();
1.1.1.28  root     18262:        sio_release();
                   18263: }
                   18264: 
1.1       root     18265: void hardware_run()
                   18266: {
1.1.1.22  root     18267: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     18268:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     18269:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     18270: #endif
1.1.1.51  root     18271: #ifdef USE_DEBUGGER
                   18272:        m_int_num = -1;
                   18273: #endif
1.1.1.54  root     18274:        while(!m_exit) {
1.1.1.50  root     18275:                hardware_run_cpu();
1.1       root     18276:        }
1.1.1.22  root     18277: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18278:        if(fp_debug_log != NULL) {
                   18279:                fclose(fp_debug_log);
                   18280:                fp_debug_log = NULL;
1.1.1.28  root     18281:        }
1.1.1.22  root     18282: #endif
1.1       root     18283: }
                   18284: 
1.1.1.50  root     18285: inline void hardware_run_cpu()
                   18286: {
                   18287: #if defined(HAS_I386)
                   18288:        CPU_EXECUTE_CALL(i386);
                   18289:        if(m_eip != m_prev_eip) {
                   18290:                idle_ops++;
                   18291:        }
                   18292: #else
                   18293:        CPU_EXECUTE_CALL(CPU_MODEL);
                   18294:        if(m_pc != m_prevpc) {
                   18295:                idle_ops++;
                   18296:        }
                   18297: #endif
                   18298: #ifdef USE_DEBUGGER
                   18299:        // Disallow reentering CPU_EXECUTE() in msdos_syscall()
                   18300:        if(m_int_num >= 0) {
                   18301:                unsigned num = (unsigned)m_int_num;
                   18302:                m_int_num = -1;
                   18303:                msdos_syscall(num);
                   18304:        }
                   18305: #endif
                   18306:        if(++update_ops == UPDATE_OPS) {
                   18307:                update_ops = 0;
                   18308:                hardware_update();
                   18309:        }
                   18310: }
                   18311: 
1.1       root     18312: void hardware_update()
                   18313: {
1.1.1.8   root     18314:        static UINT32 prev_time = 0;
                   18315:        UINT32 cur_time = timeGetTime();
                   18316:        
                   18317:        if(prev_time != cur_time) {
                   18318:                // update pit and raise irq0
                   18319: #ifndef PIT_ALWAYS_RUNNING
                   18320:                if(pit_active)
                   18321: #endif
                   18322:                {
                   18323:                        if(pit_run(0, cur_time)) {
                   18324:                                pic_req(0, 0, 1);
                   18325:                        }
                   18326:                        pit_run(1, cur_time);
                   18327:                        pit_run(2, cur_time);
                   18328:                }
1.1.1.24  root     18329:                
1.1.1.25  root     18330:                // update sio and raise irq4/3
1.1.1.29  root     18331:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     18332:                        sio_update(c);
                   18333:                }
                   18334:                
1.1.1.24  root     18335:                // update keyboard and mouse
1.1.1.14  root     18336:                static UINT32 prev_tick = 0;
                   18337:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     18338:                
1.1.1.14  root     18339:                if(prev_tick != cur_tick) {
                   18340:                        // update keyboard flags
                   18341:                        UINT8 state;
1.1.1.24  root     18342:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   18343:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   18344:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   18345:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   18346:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   18347:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   18348:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   18349:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     18350:                        mem[0x417] = state;
                   18351:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   18352:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   18353:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   18354:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     18355: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   18356: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     18357:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   18358:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   18359:                        mem[0x418] = state;
                   18360:                        
1.1.1.24  root     18361:                        // update console input if needed
1.1.1.34  root     18362:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     18363:                                update_console_input();
                   18364:                        }
                   18365:                        
1.1.1.55  root     18366:                        // raise irq1 if key buffer is not empty
1.1.1.56! root     18367:                        if(!key_changed) {
1.1.1.55  root     18368: #ifdef USE_SERVICE_THREAD
1.1.1.56! root     18369:                                EnterCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     18370: #endif
1.1.1.56! root     18371:                                key_changed = !pcbios_is_key_buffer_empty();
1.1.1.55  root     18372: #ifdef USE_SERVICE_THREAD
1.1.1.56! root     18373:                                LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     18374: #endif
1.1.1.56! root     18375:                        }
        !          18376:                        if(key_changed) {
1.1.1.8   root     18377:                                pic_req(0, 1, 1);
1.1.1.56! root     18378:                                key_changed = false;
1.1.1.24  root     18379:                        }
                   18380:                        
                   18381:                        // raise irq12 if mouse status is changed
1.1.1.54  root     18382:                        if((mouse.status & 0x1f) && mouse.call_addr_ps2.dw) {
                   18383:                                mouse.status_irq = 0; // ???
                   18384:                                mouse.status_irq_alt = 0; // ???
                   18385:                                mouse.status_irq_ps2 = mouse.status & 0x1f;
                   18386:                                mouse.status = 0;
                   18387:                                pic_req(1, 4, 1);
                   18388:                        } else if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
1.1.1.43  root     18389:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   18390:                                mouse.status_irq_alt = 0; // ???
1.1.1.54  root     18391:                                mouse.status_irq_ps2 = 0; // ???
1.1.1.24  root     18392:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     18393:                                pic_req(1, 4, 1);
                   18394:                        } else {
                   18395:                                for(int i = 0; i < 8; i++) {
                   18396:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   18397:                                                mouse.status_irq = 0; // ???
                   18398:                                                mouse.status_irq_alt = 0;
1.1.1.54  root     18399:                                                mouse.status_irq_ps2 = 0; // ???
1.1.1.43  root     18400:                                                for(int j = 0; j < 8; j++) {
                   18401:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   18402:                                                                mouse.status_irq_alt |=  (1 << j);
                   18403:                                                                mouse.status_alt     &= ~(1 << j);
                   18404:                                                        }
                   18405:                                                }
                   18406:                                                pic_req(1, 4, 1);
                   18407:                                                break;
                   18408:                                        }
                   18409:                                }
1.1.1.8   root     18410:                        }
1.1.1.24  root     18411:                        
1.1.1.14  root     18412:                        prev_tick = cur_tick;
1.1.1.8   root     18413:                }
1.1.1.24  root     18414:                
1.1.1.19  root     18415:                // update daily timer counter
                   18416:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     18417:                
1.1.1.8   root     18418:                prev_time = cur_time;
1.1       root     18419:        }
                   18420: }
                   18421: 
1.1.1.19  root     18422: // ems
                   18423: 
                   18424: void ems_init()
                   18425: {
                   18426:        memset(ems_handles, 0, sizeof(ems_handles));
                   18427:        memset(ems_pages, 0, sizeof(ems_pages));
                   18428:        free_ems_pages = MAX_EMS_PAGES;
                   18429: }
                   18430: 
                   18431: void ems_finish()
                   18432: {
1.1.1.28  root     18433:        ems_release();
                   18434: }
                   18435: 
                   18436: void ems_release()
                   18437: {
1.1.1.31  root     18438:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     18439:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     18440:                        free(ems_handles[i].buffer);
                   18441:                        ems_handles[i].buffer = NULL;
                   18442:                }
                   18443:        }
                   18444: }
                   18445: 
                   18446: void ems_allocate_pages(int handle, int pages)
                   18447: {
                   18448:        if(pages > 0) {
                   18449:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18450:        } else {
                   18451:                ems_handles[handle].buffer = NULL;
                   18452:        }
                   18453:        ems_handles[handle].pages = pages;
                   18454:        ems_handles[handle].allocated = true;
                   18455:        free_ems_pages -= pages;
                   18456: }
                   18457: 
                   18458: void ems_reallocate_pages(int handle, int pages)
                   18459: {
                   18460:        if(ems_handles[handle].allocated) {
                   18461:                if(ems_handles[handle].pages != pages) {
                   18462:                        UINT8 *new_buffer = NULL;
                   18463:                        
                   18464:                        if(pages > 0) {
                   18465:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18466:                        }
1.1.1.32  root     18467:                        if(ems_handles[handle].buffer != NULL) {
                   18468:                                if(new_buffer != NULL) {
1.1.1.19  root     18469:                                        if(pages > ems_handles[handle].pages) {
                   18470:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   18471:                                        } else {
                   18472:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   18473:                                        }
                   18474:                                }
                   18475:                                free(ems_handles[handle].buffer);
                   18476:                                ems_handles[handle].buffer = NULL;
                   18477:                        }
                   18478:                        free_ems_pages += ems_handles[handle].pages;
                   18479:                        
                   18480:                        ems_handles[handle].buffer = new_buffer;
                   18481:                        ems_handles[handle].pages = pages;
                   18482:                        free_ems_pages -= pages;
                   18483:                }
                   18484:        } else {
                   18485:                ems_allocate_pages(handle, pages);
                   18486:        }
                   18487: }
                   18488: 
                   18489: void ems_release_pages(int handle)
                   18490: {
                   18491:        if(ems_handles[handle].allocated) {
1.1.1.32  root     18492:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     18493:                        free(ems_handles[handle].buffer);
                   18494:                        ems_handles[handle].buffer = NULL;
                   18495:                }
                   18496:                free_ems_pages += ems_handles[handle].pages;
                   18497:                ems_handles[handle].allocated = false;
                   18498:        }
                   18499: }
                   18500: 
                   18501: void ems_map_page(int physical, int handle, int logical)
                   18502: {
                   18503:        if(ems_pages[physical].mapped) {
                   18504:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   18505:                        return;
                   18506:                }
                   18507:                ems_unmap_page(physical);
                   18508:        }
1.1.1.32  root     18509:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18510:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   18511:        }
                   18512:        ems_pages[physical].handle = handle;
                   18513:        ems_pages[physical].page = logical;
                   18514:        ems_pages[physical].mapped = true;
                   18515: }
                   18516: 
                   18517: void ems_unmap_page(int physical)
                   18518: {
                   18519:        if(ems_pages[physical].mapped) {
                   18520:                int handle = ems_pages[physical].handle;
                   18521:                int logical = ems_pages[physical].page;
                   18522:                
1.1.1.32  root     18523:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18524:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   18525:                }
                   18526:                ems_pages[physical].mapped = false;
                   18527:        }
                   18528: }
                   18529: 
1.1.1.25  root     18530: // dma
1.1       root     18531: 
1.1.1.25  root     18532: void dma_init()
1.1       root     18533: {
1.1.1.26  root     18534:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     18535:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     18536: //             for(int ch = 0; ch < 4; ch++) {
                   18537: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   18538: //             }
1.1.1.25  root     18539:                dma_reset(c);
                   18540:        }
1.1       root     18541: }
                   18542: 
1.1.1.25  root     18543: void dma_reset(int c)
1.1       root     18544: {
1.1.1.25  root     18545:        dma[c].low_high = false;
                   18546:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   18547:        dma[c].mask = 0xff;
                   18548: }
                   18549: 
                   18550: void dma_write(int c, UINT32 addr, UINT8 data)
                   18551: {
                   18552:        int ch = (addr >> 1) & 3;
                   18553:        UINT8 bit = 1 << (data & 3);
                   18554:        
                   18555:        switch(addr & 0x0f) {
                   18556:        case 0x00: case 0x02: case 0x04: case 0x06:
                   18557:                if(dma[c].low_high) {
                   18558:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     18559:                } else {
1.1.1.25  root     18560:                        dma[c].ch[ch].bareg.b.l = data;
                   18561:                }
                   18562:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   18563:                dma[c].low_high = !dma[c].low_high;
                   18564:                break;
                   18565:        case 0x01: case 0x03: case 0x05: case 0x07:
                   18566:                if(dma[c].low_high) {
                   18567:                        dma[c].ch[ch].bcreg.b.h = data;
                   18568:                } else {
                   18569:                        dma[c].ch[ch].bcreg.b.l = data;
                   18570:                }
                   18571:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   18572:                dma[c].low_high = !dma[c].low_high;
                   18573:                break;
                   18574:        case 0x08:
                   18575:                // command register
                   18576:                dma[c].cmd = data;
                   18577:                break;
                   18578:        case 0x09:
                   18579:                // dma[c].request register
                   18580:                if(data & 4) {
                   18581:                        if(!(dma[c].req & bit)) {
                   18582:                                dma[c].req |= bit;
                   18583: //                             dma_run(c, ch);
                   18584:                        }
                   18585:                } else {
                   18586:                        dma[c].req &= ~bit;
                   18587:                }
                   18588:                break;
                   18589:        case 0x0a:
                   18590:                // single mask register
                   18591:                if(data & 4) {
                   18592:                        dma[c].mask |= bit;
                   18593:                } else {
                   18594:                        dma[c].mask &= ~bit;
                   18595:                }
                   18596:                break;
                   18597:        case 0x0b:
                   18598:                // mode register
                   18599:                dma[c].ch[data & 3].mode = data;
                   18600:                break;
                   18601:        case 0x0c:
                   18602:                dma[c].low_high = false;
                   18603:                break;
                   18604:        case 0x0d:
                   18605:                // clear master
                   18606:                dma_reset(c);
                   18607:                break;
                   18608:        case 0x0e:
                   18609:                // clear mask register
                   18610:                dma[c].mask = 0;
                   18611:                break;
                   18612:        case 0x0f:
                   18613:                // all mask register
                   18614:                dma[c].mask = data & 0x0f;
                   18615:                break;
                   18616:        }
                   18617: }
                   18618: 
                   18619: UINT8 dma_read(int c, UINT32 addr)
                   18620: {
                   18621:        int ch = (addr >> 1) & 3;
                   18622:        UINT8 val = 0xff;
                   18623:        
                   18624:        switch(addr & 0x0f) {
                   18625:        case 0x00: case 0x02: case 0x04: case 0x06:
                   18626:                if(dma[c].low_high) {
                   18627:                        val = dma[c].ch[ch].areg.b.h;
                   18628:                } else {
                   18629:                        val = dma[c].ch[ch].areg.b.l;
                   18630:                }
                   18631:                dma[c].low_high = !dma[c].low_high;
                   18632:                return(val);
                   18633:        case 0x01: case 0x03: case 0x05: case 0x07:
                   18634:                if(dma[c].low_high) {
                   18635:                        val = dma[c].ch[ch].creg.b.h;
                   18636:                } else {
                   18637:                        val = dma[c].ch[ch].creg.b.l;
                   18638:                }
                   18639:                dma[c].low_high = !dma[c].low_high;
                   18640:                return(val);
                   18641:        case 0x08:
                   18642:                // status register
                   18643:                val = (dma[c].req << 4) | dma[c].tc;
                   18644:                dma[c].tc = 0;
                   18645:                return(val);
                   18646:        case 0x0d:
1.1.1.26  root     18647:                // temporary register (intel 82374 does not support)
1.1.1.25  root     18648:                return(dma[c].tmp & 0xff);
1.1.1.26  root     18649:        case 0x0f:
                   18650:                // mask register (intel 82374 does support)
                   18651:                return(dma[c].mask);
1.1.1.25  root     18652:        }
                   18653:        return(0xff);
                   18654: }
                   18655: 
                   18656: void dma_page_write(int c, int ch, UINT8 data)
                   18657: {
                   18658:        dma[c].ch[ch].pagereg = data;
                   18659: }
                   18660: 
                   18661: UINT8 dma_page_read(int c, int ch)
                   18662: {
                   18663:        return(dma[c].ch[ch].pagereg);
                   18664: }
                   18665: 
                   18666: void dma_run(int c, int ch)
                   18667: {
                   18668:        UINT8 bit = 1 << ch;
                   18669:        
                   18670:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   18671:                // execute dma
                   18672:                while(dma[c].req & bit) {
                   18673:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   18674:                                // memory -> memory
                   18675:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   18676:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   18677:                                
                   18678:                                if(c == 0) {
                   18679:                                        dma[c].tmp = read_byte(saddr);
                   18680:                                        write_byte(daddr, dma[c].tmp);
                   18681:                                } else {
                   18682:                                        dma[c].tmp = read_word(saddr << 1);
                   18683:                                        write_word(daddr << 1, dma[c].tmp);
                   18684:                                }
                   18685:                                if(!(dma[c].cmd & 0x02)) {
                   18686:                                        if(dma[c].ch[0].mode & 0x20) {
                   18687:                                                dma[c].ch[0].areg.w--;
                   18688:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   18689:                                                        dma[c].ch[0].pagereg--;
                   18690:                                                }
                   18691:                                        } else {
                   18692:                                                dma[c].ch[0].areg.w++;
                   18693:                                                if(dma[c].ch[0].areg.w == 0) {
                   18694:                                                        dma[c].ch[0].pagereg++;
                   18695:                                                }
                   18696:                                        }
                   18697:                                }
                   18698:                                if(dma[c].ch[1].mode & 0x20) {
                   18699:                                        dma[c].ch[1].areg.w--;
                   18700:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   18701:                                                dma[c].ch[1].pagereg--;
                   18702:                                        }
                   18703:                                } else {
                   18704:                                        dma[c].ch[1].areg.w++;
                   18705:                                        if(dma[c].ch[1].areg.w == 0) {
                   18706:                                                dma[c].ch[1].pagereg++;
                   18707:                                        }
                   18708:                                }
                   18709:                                
                   18710:                                // check dma condition
                   18711:                                if(dma[c].ch[0].creg.w-- == 0) {
                   18712:                                        if(dma[c].ch[0].mode & 0x10) {
                   18713:                                                // self initialize
                   18714:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   18715:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   18716:                                        } else {
                   18717: //                                             dma[c].mask |= bit;
                   18718:                                        }
                   18719:                                }
                   18720:                                if(dma[c].ch[1].creg.w-- == 0) {
                   18721:                                        // terminal count
                   18722:                                        if(dma[c].ch[1].mode & 0x10) {
                   18723:                                                // self initialize
                   18724:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   18725:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   18726:                                        } else {
                   18727:                                                dma[c].mask |= bit;
                   18728:                                        }
                   18729:                                        dma[c].req &= ~bit;
                   18730:                                        dma[c].tc |= bit;
                   18731:                                }
                   18732:                        } else {
                   18733:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   18734:                                
                   18735:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   18736:                                        // verify
                   18737:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   18738:                                        // io -> memory
                   18739:                                        if(c == 0) {
                   18740:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   18741:                                                write_byte(addr, dma[c].tmp);
                   18742:                                        } else {
                   18743:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   18744:                                                write_word(addr << 1, dma[c].tmp);
                   18745:                                        }
                   18746:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   18747:                                        // memory -> io
                   18748:                                        if(c == 0) {
                   18749:                                                dma[c].tmp = read_byte(addr);
                   18750:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   18751:                                        } else {
                   18752:                                                dma[c].tmp = read_word(addr << 1);
                   18753:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   18754:                                        }
                   18755:                                }
                   18756:                                if(dma[c].ch[ch].mode & 0x20) {
                   18757:                                        dma[c].ch[ch].areg.w--;
                   18758:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   18759:                                                dma[c].ch[ch].pagereg--;
                   18760:                                        }
                   18761:                                } else {
                   18762:                                        dma[c].ch[ch].areg.w++;
                   18763:                                        if(dma[c].ch[ch].areg.w == 0) {
                   18764:                                                dma[c].ch[ch].pagereg++;
                   18765:                                        }
                   18766:                                }
                   18767:                                
                   18768:                                // check dma condition
                   18769:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   18770:                                        // terminal count
                   18771:                                        if(dma[c].ch[ch].mode & 0x10) {
                   18772:                                                // self initialize
                   18773:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   18774:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   18775:                                        } else {
                   18776:                                                dma[c].mask |= bit;
                   18777:                                        }
                   18778:                                        dma[c].req &= ~bit;
                   18779:                                        dma[c].tc |= bit;
                   18780:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   18781:                                        // single mode
                   18782:                                        break;
                   18783:                                }
                   18784:                        }
                   18785:                }
                   18786:        }
                   18787: }
                   18788: 
                   18789: // pic
                   18790: 
                   18791: void pic_init()
                   18792: {
                   18793:        memset(pic, 0, sizeof(pic));
                   18794:        pic[0].imr = pic[1].imr = 0xff;
                   18795:        
                   18796:        // from bochs bios
                   18797:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   18798:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   18799:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   18800:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   18801:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   18802:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   18803:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   18804:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   18805:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   18806: }
                   18807: 
                   18808: void pic_write(int c, UINT32 addr, UINT8 data)
                   18809: {
                   18810:        if(addr & 1) {
                   18811:                if(pic[c].icw2_r) {
                   18812:                        // icw2
                   18813:                        pic[c].icw2 = data;
                   18814:                        pic[c].icw2_r = 0;
                   18815:                } else if(pic[c].icw3_r) {
                   18816:                        // icw3
                   18817:                        pic[c].icw3 = data;
                   18818:                        pic[c].icw3_r = 0;
                   18819:                } else if(pic[c].icw4_r) {
                   18820:                        // icw4
                   18821:                        pic[c].icw4 = data;
                   18822:                        pic[c].icw4_r = 0;
                   18823:                } else {
                   18824:                        // ocw1
1.1       root     18825:                        pic[c].imr = data;
                   18826:                }
                   18827:        } else {
                   18828:                if(data & 0x10) {
                   18829:                        // icw1
                   18830:                        pic[c].icw1 = data;
                   18831:                        pic[c].icw2_r = 1;
                   18832:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   18833:                        pic[c].icw4_r = data & 1;
                   18834:                        pic[c].irr = 0;
                   18835:                        pic[c].isr = 0;
                   18836:                        pic[c].imr = 0;
                   18837:                        pic[c].prio = 0;
                   18838:                        if(!(pic[c].icw1 & 1)) {
                   18839:                                pic[c].icw4 = 0;
                   18840:                        }
                   18841:                        pic[c].ocw3 = 0;
                   18842:                } else if(data & 8) {
                   18843:                        // ocw3
                   18844:                        if(!(data & 2)) {
                   18845:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   18846:                        }
                   18847:                        if(!(data & 0x40)) {
                   18848:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   18849:                        }
                   18850:                        pic[c].ocw3 = data;
                   18851:                } else {
                   18852:                        // ocw2
                   18853:                        int level = 0;
                   18854:                        if(data & 0x40) {
                   18855:                                level = data & 7;
                   18856:                        } else {
                   18857:                                if(!pic[c].isr) {
                   18858:                                        return;
                   18859:                                }
                   18860:                                level = pic[c].prio;
                   18861:                                while(!(pic[c].isr & (1 << level))) {
                   18862:                                        level = (level + 1) & 7;
                   18863:                                }
                   18864:                        }
                   18865:                        if(data & 0x80) {
                   18866:                                pic[c].prio = (level + 1) & 7;
                   18867:                        }
                   18868:                        if(data & 0x20) {
                   18869:                                pic[c].isr &= ~(1 << level);
                   18870:                        }
                   18871:                }
                   18872:        }
                   18873:        pic_update();
                   18874: }
                   18875: 
                   18876: UINT8 pic_read(int c, UINT32 addr)
                   18877: {
                   18878:        if(addr & 1) {
                   18879:                return(pic[c].imr);
                   18880:        } else {
                   18881:                // polling mode is not supported...
                   18882:                //if(pic[c].ocw3 & 4) {
                   18883:                //      return ???;
                   18884:                //}
                   18885:                if(pic[c].ocw3 & 1) {
                   18886:                        return(pic[c].isr);
                   18887:                } else {
                   18888:                        return(pic[c].irr);
                   18889:                }
                   18890:        }
                   18891: }
                   18892: 
                   18893: void pic_req(int c, int level, int signal)
                   18894: {
                   18895:        if(signal) {
                   18896:                pic[c].irr |= (1 << level);
                   18897:        } else {
                   18898:                pic[c].irr &= ~(1 << level);
                   18899:        }
                   18900:        pic_update();
                   18901: }
                   18902: 
                   18903: int pic_ack()
                   18904: {
                   18905:        // ack (INTA=L)
                   18906:        pic[pic_req_chip].isr |= pic_req_bit;
                   18907:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   18908:        if(pic_req_chip > 0) {
                   18909:                // update isr and irr of master
                   18910:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   18911:                pic[pic_req_chip - 1].isr |= slave;
                   18912:                pic[pic_req_chip - 1].irr &= ~slave;
                   18913:        }
                   18914:        //if(pic[pic_req_chip].icw4 & 1) {
                   18915:                // 8086 mode
                   18916:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   18917:        //} else {
                   18918:        //      // 8080 mode
                   18919:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   18920:        //      if(pic[pic_req_chip].icw1 & 4) {
                   18921:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   18922:        //      } else {
                   18923:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   18924:        //      }
                   18925:        //      vector = 0xcd | (addr << 8);
                   18926:        //}
                   18927:        if(pic[pic_req_chip].icw4 & 2) {
                   18928:                // auto eoi
                   18929:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   18930:        }
                   18931:        return(vector);
                   18932: }
                   18933: 
                   18934: void pic_update()
                   18935: {
                   18936:        for(int c = 0; c < 2; c++) {
                   18937:                UINT8 irr = pic[c].irr;
                   18938:                if(c + 1 < 2) {
                   18939:                        // this is master
                   18940:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   18941:                                // request from slave
                   18942:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   18943:                        }
                   18944:                }
                   18945:                irr &= (~pic[c].imr);
                   18946:                if(!irr) {
                   18947:                        break;
                   18948:                }
                   18949:                if(!(pic[c].ocw3 & 0x20)) {
                   18950:                        irr |= pic[c].isr;
                   18951:                }
                   18952:                int level = pic[c].prio;
                   18953:                UINT8 bit = 1 << level;
                   18954:                while(!(irr & bit)) {
                   18955:                        level = (level + 1) & 7;
                   18956:                        bit = 1 << level;
                   18957:                }
                   18958:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   18959:                        // check slave
                   18960:                        continue;
                   18961:                }
                   18962:                if(pic[c].isr & bit) {
                   18963:                        break;
                   18964:                }
                   18965:                // interrupt request
                   18966:                pic_req_chip = c;
                   18967:                pic_req_level = level;
                   18968:                pic_req_bit = bit;
1.1.1.3   root     18969:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     18970:                return;
                   18971:        }
1.1.1.3   root     18972:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     18973: }
1.1       root     18974: 
1.1.1.25  root     18975: // pio
                   18976: 
                   18977: void pio_init()
                   18978: {
1.1.1.38  root     18979: //     bool conv_mode = (GetConsoleCP() == 932);
                   18980:        
1.1.1.26  root     18981:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     18982:        
1.1.1.25  root     18983:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     18984:                pio[c].stat = 0xdf;
1.1.1.25  root     18985:                pio[c].ctrl = 0x0c;
1.1.1.38  root     18986: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     18987:        }
                   18988: }
                   18989: 
1.1.1.37  root     18990: void pio_finish()
                   18991: {
                   18992:        pio_release();
                   18993: }
                   18994: 
                   18995: void pio_release()
                   18996: {
                   18997:        for(int c = 0; c < 2; c++) {
                   18998:                if(pio[c].fp != NULL) {
1.1.1.38  root     18999:                        if(pio[c].jis_mode) {
                   19000:                                fputc(0x1c, pio[c].fp);
                   19001:                                fputc(0x2e, pio[c].fp);
                   19002:                        }
1.1.1.37  root     19003:                        fclose(pio[c].fp);
                   19004:                        pio[c].fp = NULL;
                   19005:                }
                   19006:        }
                   19007: }
                   19008: 
1.1.1.25  root     19009: void pio_write(int c, UINT32 addr, UINT8 data)
                   19010: {
                   19011:        switch(addr & 3) {
                   19012:        case 0:
                   19013:                pio[c].data = data;
                   19014:                break;
                   19015:        case 2:
1.1.1.37  root     19016:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   19017:                        // strobe H -> L
                   19018:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   19019:                                // auto feed
                   19020:                                printer_out(c, 0x0d);
                   19021:                                printer_out(c, 0x0a);
                   19022:                        } else {
                   19023:                                printer_out(c, pio[c].data);
                   19024:                        }
                   19025:                        pio[c].stat &= ~0x40; // set ack
                   19026:                }
1.1.1.25  root     19027:                pio[c].ctrl = data;
                   19028:                break;
                   19029:        }
                   19030: }
                   19031: 
                   19032: UINT8 pio_read(int c, UINT32 addr)
                   19033: {
                   19034:        switch(addr & 3) {
                   19035:        case 0:
1.1.1.37  root     19036:                if(pio[c].ctrl & 0x20) {
                   19037:                        // input mode
                   19038:                        return(0xff);
                   19039:                }
1.1.1.25  root     19040:                return(pio[c].data);
                   19041:        case 1:
1.1.1.37  root     19042:                {
                   19043:                        UINT8 stat = pio[c].stat;
                   19044:                        pio[c].stat |= 0x40; // clear ack
                   19045:                        return(stat);
                   19046:                }
1.1.1.25  root     19047:        case 2:
                   19048:                return(pio[c].ctrl);
                   19049:        }
                   19050:        return(0xff);
                   19051: }
                   19052: 
1.1.1.37  root     19053: void printer_out(int c, UINT8 data)
                   19054: {
                   19055:        SYSTEMTIME time;
1.1.1.38  root     19056:        bool jis_mode = false;
1.1.1.37  root     19057:        
                   19058:        GetLocalTime(&time);
                   19059:        
                   19060:        if(pio[c].fp != NULL) {
                   19061:                // if at least 1000ms passed from last written, close the current file
                   19062:                FILETIME ftime1;
                   19063:                FILETIME ftime2;
                   19064:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   19065:                SystemTimeToFileTime(&time, &ftime2);
                   19066:                INT64 *time1 = (INT64 *)&ftime1;
                   19067:                INT64 *time2 = (INT64 *)&ftime2;
                   19068:                INT64 msec = (*time2 - *time1) / 10000;
                   19069:                
                   19070:                if(msec >= 1000) {
1.1.1.38  root     19071:                        if(pio[c].jis_mode) {
                   19072:                                fputc(0x1c, pio[c].fp);
                   19073:                                fputc(0x2e, pio[c].fp);
                   19074:                                jis_mode = true;
                   19075:                        }
1.1.1.37  root     19076:                        fclose(pio[c].fp);
                   19077:                        pio[c].fp = NULL;
                   19078:                }
                   19079:        }
                   19080:        if(pio[c].fp == NULL) {
                   19081:                // create a new file in the temp folder
                   19082:                char file_name[MAX_PATH];
                   19083:                
                   19084:                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);
                   19085:                if(GetTempPath(MAX_PATH, pio[c].path)) {
                   19086:                        strcat(pio[c].path, file_name);
                   19087:                } else {
                   19088:                        strcpy(pio[c].path, file_name);
                   19089:                }
1.1.1.38  root     19090:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     19091:        }
                   19092:        if(pio[c].fp != NULL) {
1.1.1.38  root     19093:                if(jis_mode) {
                   19094:                        fputc(0x1c, pio[c].fp);
                   19095:                        fputc(0x26, pio[c].fp);
                   19096:                }
1.1.1.37  root     19097:                fputc(data, pio[c].fp);
1.1.1.38  root     19098:                
                   19099:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   19100:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   19101:                        UINT8 buffer[4];
                   19102:                        fseek(pio[c].fp, 0, SEEK_SET);
                   19103:                        fread(buffer, 4, 1, pio[c].fp);
                   19104:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   19105:                                fclose(pio[c].fp);
                   19106:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   19107:                        }
                   19108:                }
1.1.1.37  root     19109:                pio[c].time = time;
                   19110:        }
                   19111: }
                   19112: 
1.1       root     19113: // pit
                   19114: 
1.1.1.22  root     19115: #define PIT_FREQ 1193182ULL
1.1       root     19116: #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)
                   19117: 
                   19118: void pit_init()
                   19119: {
1.1.1.8   root     19120:        memset(pit, 0, sizeof(pit));
1.1       root     19121:        for(int ch = 0; ch < 3; ch++) {
                   19122:                pit[ch].count = 0x10000;
                   19123:                pit[ch].ctrl_reg = 0x34;
                   19124:                pit[ch].mode = 3;
                   19125:        }
                   19126:        
                   19127:        // from bochs bios
                   19128:        pit_write(3, 0x34);
                   19129:        pit_write(0, 0x00);
                   19130:        pit_write(0, 0x00);
                   19131: }
                   19132: 
                   19133: void pit_write(int ch, UINT8 val)
                   19134: {
1.1.1.8   root     19135: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19136:        if(!pit_active) {
                   19137:                pit_active = 1;
                   19138:                pit_init();
                   19139:        }
1.1.1.8   root     19140: #endif
1.1       root     19141:        switch(ch) {
                   19142:        case 0:
                   19143:        case 1:
                   19144:        case 2:
                   19145:                // write count register
                   19146:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19147:                        if(pit[ch].ctrl_reg & 0x10) {
                   19148:                                pit[ch].low_write = 1;
                   19149:                        }
                   19150:                        if(pit[ch].ctrl_reg & 0x20) {
                   19151:                                pit[ch].high_write = 1;
                   19152:                        }
                   19153:                }
                   19154:                if(pit[ch].low_write) {
                   19155:                        pit[ch].count_reg = val;
                   19156:                        pit[ch].low_write = 0;
                   19157:                } else if(pit[ch].high_write) {
                   19158:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19159:                                pit[ch].count_reg = val << 8;
                   19160:                        } else {
                   19161:                                pit[ch].count_reg |= val << 8;
                   19162:                        }
                   19163:                        pit[ch].high_write = 0;
                   19164:                }
                   19165:                // start count
1.1.1.8   root     19166:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19167:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   19168:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   19169:                                pit[ch].prev_time = timeGetTime();
                   19170:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19171:                        }
                   19172:                }
                   19173:                break;
                   19174:        case 3: // ctrl reg
                   19175:                if((val & 0xc0) == 0xc0) {
                   19176:                        // i8254 read-back command
                   19177:                        for(ch = 0; ch < 3; ch++) {
                   19178:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   19179:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   19180:                                        pit[ch].status_latched = 1;
                   19181:                                }
                   19182:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   19183:                                        pit_latch_count(ch);
                   19184:                                }
                   19185:                        }
                   19186:                        break;
                   19187:                }
                   19188:                ch = (val >> 6) & 3;
                   19189:                if(val & 0x30) {
1.1.1.35  root     19190:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     19191:                        pit[ch].mode = modes[(val >> 1) & 7];
                   19192:                        pit[ch].count_latched = 0;
                   19193:                        pit[ch].low_read = pit[ch].high_read = 0;
                   19194:                        pit[ch].low_write = pit[ch].high_write = 0;
                   19195:                        pit[ch].ctrl_reg = val;
                   19196:                        // stop count
1.1.1.8   root     19197:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     19198:                        pit[ch].count_reg = 0;
                   19199:                } else if(!pit[ch].count_latched) {
                   19200:                        pit_latch_count(ch);
                   19201:                }
                   19202:                break;
                   19203:        }
                   19204: }
                   19205: 
                   19206: UINT8 pit_read(int ch)
                   19207: {
1.1.1.8   root     19208: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19209:        if(!pit_active) {
                   19210:                pit_active = 1;
                   19211:                pit_init();
                   19212:        }
1.1.1.8   root     19213: #endif
1.1       root     19214:        switch(ch) {
                   19215:        case 0:
                   19216:        case 1:
                   19217:        case 2:
                   19218:                if(pit[ch].status_latched) {
                   19219:                        pit[ch].status_latched = 0;
                   19220:                        return(pit[ch].status);
                   19221:                }
                   19222:                // if not latched, through current count
                   19223:                if(!pit[ch].count_latched) {
                   19224:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   19225:                                pit_latch_count(ch);
                   19226:                        }
                   19227:                }
                   19228:                // return latched count
                   19229:                if(pit[ch].low_read) {
                   19230:                        pit[ch].low_read = 0;
                   19231:                        if(!pit[ch].high_read) {
                   19232:                                pit[ch].count_latched = 0;
                   19233:                        }
                   19234:                        return(pit[ch].latch & 0xff);
                   19235:                } else if(pit[ch].high_read) {
                   19236:                        pit[ch].high_read = 0;
                   19237:                        pit[ch].count_latched = 0;
                   19238:                        return((pit[ch].latch >> 8) & 0xff);
                   19239:                }
                   19240:        }
                   19241:        return(0xff);
                   19242: }
                   19243: 
1.1.1.8   root     19244: int pit_run(int ch, UINT32 cur_time)
1.1       root     19245: {
1.1.1.8   root     19246:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     19247:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     19248:                pit[ch].prev_time = pit[ch].expired_time;
                   19249:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   19250:                if(cur_time >= pit[ch].expired_time) {
                   19251:                        pit[ch].prev_time = cur_time;
                   19252:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19253:                }
1.1.1.8   root     19254:                return(1);
1.1       root     19255:        }
1.1.1.8   root     19256:        return(0);
1.1       root     19257: }
                   19258: 
                   19259: void pit_latch_count(int ch)
                   19260: {
1.1.1.8   root     19261:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     19262:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     19263:                pit_run(ch, cur_time);
                   19264:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     19265:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19266:                
                   19267:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   19268:                        // decrement counter in 1msec period
                   19269:                        if(pit[ch].next_latch == 0) {
                   19270:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   19271:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19272:                        }
                   19273:                        if(pit[ch].latch > pit[ch].next_latch) {
                   19274:                                pit[ch].latch--;
                   19275:                        }
                   19276:                } else {
                   19277:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   19278:                        pit[ch].next_latch = 0;
                   19279:                }
1.1.1.8   root     19280:        } else {
                   19281:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     19282:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     19283:        }
                   19284:        pit[ch].count_latched = 1;
                   19285:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   19286:                // lower byte
                   19287:                pit[ch].low_read = 1;
                   19288:                pit[ch].high_read = 0;
                   19289:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19290:                // upper byte
                   19291:                pit[ch].low_read = 0;
                   19292:                pit[ch].high_read = 1;
                   19293:        } else {
                   19294:                // lower -> upper
1.1.1.14  root     19295:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     19296:        }
                   19297: }
                   19298: 
1.1.1.8   root     19299: int pit_get_expired_time(int ch)
1.1       root     19300: {
1.1.1.22  root     19301:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   19302:        UINT64 val = pit[ch].accum >> 10;
                   19303:        pit[ch].accum -= val << 10;
                   19304:        return((val != 0) ? val : 1);
1.1.1.8   root     19305: }
                   19306: 
1.1.1.25  root     19307: // sio
                   19308: 
                   19309: void sio_init()
                   19310: {
1.1.1.26  root     19311:        memset(sio, 0, sizeof(sio));
                   19312:        memset(sio_mt, 0, sizeof(sio_mt));
                   19313:        
1.1.1.29  root     19314:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19315:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19316:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19317:                
                   19318:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   19319:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     19320:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   19321:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     19322:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   19323:                sio[c].irq_identify = 0x01;     // no pending irq
                   19324:                
                   19325:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   19326:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   19327:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   19328:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   19329:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   19330:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   19331:                
1.1.1.26  root     19332:                if(sio_port_number[c] != 0) {
1.1.1.25  root     19333:                        sio[c].channel = c;
                   19334:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   19335:                }
                   19336:        }
                   19337: }
                   19338: 
                   19339: void sio_finish()
                   19340: {
1.1.1.29  root     19341:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19342:                if(sio_mt[c].hThread != NULL) {
                   19343:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   19344:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     19345:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     19346:                }
                   19347:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   19348:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   19349:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   19350:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   19351:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   19352:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     19353:        }
                   19354:        sio_release();
                   19355: }
                   19356: 
                   19357: void sio_release()
                   19358: {
1.1.1.29  root     19359:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     19360:                // sio_thread() may access the resources :-(
1.1.1.32  root     19361:                bool running = (sio_mt[c].hThread != NULL);
                   19362:                
                   19363:                if(running) {
                   19364:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   19365:                }
                   19366:                if(sio[c].send_buffer != NULL) {
                   19367:                        sio[c].send_buffer->release();
                   19368:                        delete sio[c].send_buffer;
                   19369:                        sio[c].send_buffer = NULL;
                   19370:                }
                   19371:                if(running) {
                   19372:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19373:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   19374:                }
                   19375:                if(sio[c].recv_buffer != NULL) {
                   19376:                        sio[c].recv_buffer->release();
                   19377:                        delete sio[c].recv_buffer;
                   19378:                        sio[c].recv_buffer = NULL;
                   19379:                }
                   19380:                if(running) {
                   19381:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     19382:                }
1.1.1.25  root     19383:        }
                   19384: }
                   19385: 
                   19386: void sio_write(int c, UINT32 addr, UINT8 data)
                   19387: {
                   19388:        switch(addr & 7) {
                   19389:        case 0:
                   19390:                if(sio[c].selector & 0x80) {
                   19391:                        if(sio[c].divisor.b.l != data) {
                   19392:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19393:                                sio[c].divisor.b.l = data;
                   19394:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19395:                        }
                   19396:                } else {
                   19397:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19398:                        if(sio[c].send_buffer != NULL) {
                   19399:                                sio[c].send_buffer->write(data);
                   19400:                        }
1.1.1.25  root     19401:                        // transmitter holding/shift registers are not empty
                   19402:                        sio[c].line_stat_buf &= ~0x60;
                   19403:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19404:                        
                   19405:                        if(sio[c].irq_enable & 0x02) {
                   19406:                                sio_update_irq(c);
                   19407:                        }
                   19408:                }
                   19409:                break;
                   19410:        case 1:
                   19411:                if(sio[c].selector & 0x80) {
                   19412:                        if(sio[c].divisor.b.h != data) {
                   19413:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19414:                                sio[c].divisor.b.h = data;
                   19415:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19416:                        }
                   19417:                } else {
                   19418:                        if(sio[c].irq_enable != data) {
                   19419:                                sio[c].irq_enable = data;
                   19420:                                sio_update_irq(c);
                   19421:                        }
                   19422:                }
                   19423:                break;
                   19424:        case 3:
                   19425:                {
                   19426:                        UINT8 line_ctrl = data & 0x3f;
                   19427:                        bool set_brk = ((data & 0x40) != 0);
                   19428:                        
                   19429:                        if(sio[c].line_ctrl != line_ctrl) {
                   19430:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19431:                                sio[c].line_ctrl = line_ctrl;
                   19432:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19433:                        }
                   19434:                        if(sio[c].set_brk != set_brk) {
                   19435:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   19436:                                sio[c].set_brk = set_brk;
                   19437:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19438:                        }
                   19439:                }
                   19440:                sio[c].selector = data;
                   19441:                break;
                   19442:        case 4:
                   19443:                {
                   19444:                        bool set_dtr = ((data & 0x01) != 0);
                   19445:                        bool set_rts = ((data & 0x02) != 0);
                   19446:                        
                   19447:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     19448: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     19449:                                sio[c].set_dtr = set_dtr;
                   19450:                                sio[c].set_rts = set_rts;
1.1.1.26  root     19451: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19452:                                
                   19453:                                bool state_changed = false;
                   19454:                                
                   19455:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19456:                                if(set_dtr) {
                   19457:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   19458:                                } else {
                   19459:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   19460:                                }
                   19461:                                if(set_rts) {
                   19462:                                        sio[c].modem_stat |= 0x10;      // cts on
                   19463:                                } else {
                   19464:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   19465:                                }
                   19466:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   19467:                                        if(!(sio[c].modem_stat & 0x02)) {
                   19468:                                                if(sio[c].irq_enable & 0x08) {
                   19469:                                                        state_changed = true;
                   19470:                                                }
                   19471:                                                sio[c].modem_stat |= 0x02;
                   19472:                                        }
                   19473:                                }
                   19474:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   19475:                                        if(!(sio[c].modem_stat & 0x01)) {
                   19476:                                                if(sio[c].irq_enable & 0x08) {
                   19477:                                                        state_changed = true;
                   19478:                                                }
                   19479:                                                sio[c].modem_stat |= 0x01;
                   19480:                                        }
                   19481:                                }
                   19482:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19483:                                
                   19484:                                if(state_changed) {
                   19485:                                        sio_update_irq(c);
                   19486:                                }
1.1.1.25  root     19487:                        }
                   19488:                }
                   19489:                sio[c].modem_ctrl = data;
                   19490:                break;
                   19491:        case 7:
                   19492:                sio[c].scratch = data;
                   19493:                break;
                   19494:        }
                   19495: }
                   19496: 
                   19497: UINT8 sio_read(int c, UINT32 addr)
                   19498: {
                   19499:        switch(addr & 7) {
                   19500:        case 0:
                   19501:                if(sio[c].selector & 0x80) {
                   19502:                        return(sio[c].divisor.b.l);
                   19503:                } else {
                   19504:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19505:                        UINT8 data = 0;
                   19506:                        if(sio[c].recv_buffer != NULL) {
                   19507:                                data = sio[c].recv_buffer->read();
                   19508:                        }
1.1.1.25  root     19509:                        // data is not ready
                   19510:                        sio[c].line_stat_buf &= ~0x01;
                   19511:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19512:                        
                   19513:                        if(sio[c].irq_enable & 0x01) {
                   19514:                                sio_update_irq(c);
                   19515:                        }
                   19516:                        return(data);
                   19517:                }
                   19518:        case 1:
                   19519:                if(sio[c].selector & 0x80) {
                   19520:                        return(sio[c].divisor.b.h);
                   19521:                } else {
                   19522:                        return(sio[c].irq_enable);
                   19523:                }
                   19524:        case 2:
                   19525:                return(sio[c].irq_identify);
                   19526:        case 3:
                   19527:                return(sio[c].selector);
                   19528:        case 4:
                   19529:                return(sio[c].modem_ctrl);
                   19530:        case 5:
                   19531:                {
                   19532:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   19533:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   19534:                        sio[c].line_stat_err = 0x00;
                   19535:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   19536:                        
                   19537:                        bool state_changed = false;
                   19538:                        
                   19539:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   19540:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19541:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     19542:                                        // transmitter holding register will be empty first
                   19543:                                        if(sio[c].irq_enable & 0x02) {
                   19544:                                                state_changed = true;
                   19545:                                        }
                   19546:                                        sio[c].line_stat_buf |= 0x20;
                   19547:                                }
                   19548:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   19549:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   19550:                                // transmitter shift register will be empty later
                   19551:                                sio[c].line_stat_buf |= 0x40;
                   19552:                        }
                   19553:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   19554:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19555:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     19556:                                        // data is ready
                   19557:                                        if(sio[c].irq_enable & 0x01) {
                   19558:                                                state_changed = true;
                   19559:                                        }
                   19560:                                        sio[c].line_stat_buf |= 0x01;
                   19561:                                }
                   19562:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19563:                        }
                   19564:                        if(state_changed) {
                   19565:                                sio_update_irq(c);
                   19566:                        }
                   19567:                        return(val);
                   19568:                }
                   19569:        case 6:
                   19570:                {
                   19571:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   19572:                        UINT8 val = sio[c].modem_stat;
                   19573:                        sio[c].modem_stat &= 0xf0;
                   19574:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   19575:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19576:                        
                   19577:                        if(sio[c].modem_ctrl & 0x10) {
                   19578:                                // loop-back
                   19579:                                val &= 0x0f;
                   19580:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   19581:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   19582:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   19583:                        }
                   19584:                        return(val);
                   19585:                }
                   19586:        case 7:
                   19587:                return(sio[c].scratch);
                   19588:        }
                   19589:        return(0xff);
                   19590: }
                   19591: 
                   19592: void sio_update(int c)
                   19593: {
                   19594:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   19595:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19596:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     19597:                        // transmitter holding/shift registers will be empty
                   19598:                        sio[c].line_stat_buf |= 0x60;
                   19599:                }
                   19600:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   19601:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   19602:                // transmitter shift register will be empty
                   19603:                sio[c].line_stat_buf |= 0x40;
                   19604:        }
                   19605:        if(!(sio[c].line_stat_buf & 0x01)) {
                   19606:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19607:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     19608:                        // data is ready
                   19609:                        sio[c].line_stat_buf |= 0x01;
                   19610:                }
                   19611:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19612:        }
                   19613:        sio_update_irq(c);
                   19614: }
                   19615: 
                   19616: void sio_update_irq(int c)
                   19617: {
                   19618:        int level = -1;
                   19619:        
                   19620:        if(sio[c].irq_enable & 0x08) {
                   19621:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19622:                if((sio[c].modem_stat & 0x0f) != 0) {
                   19623:                        level = 0;
                   19624:                }
                   19625:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19626:        }
                   19627:        if(sio[c].irq_enable & 0x02) {
                   19628:                if(sio[c].line_stat_buf & 0x20) {
                   19629:                        level = 1;
                   19630:                }
                   19631:        }
                   19632:        if(sio[c].irq_enable & 0x01) {
                   19633:                if(sio[c].line_stat_buf & 0x01) {
                   19634:                        level = 2;
                   19635:                }
                   19636:        }
                   19637:        if(sio[c].irq_enable & 0x04) {
                   19638:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   19639:                if(sio[c].line_stat_err != 0) {
                   19640:                        level = 3;
                   19641:                }
                   19642:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   19643:        }
1.1.1.29  root     19644:        
                   19645:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     19646:        if(level != -1) {
                   19647:                sio[c].irq_identify = level << 1;
1.1.1.29  root     19648:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     19649:        } else {
                   19650:                sio[c].irq_identify = 1;
1.1.1.29  root     19651:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     19652:        }
                   19653: }
                   19654: 
                   19655: DWORD WINAPI sio_thread(void *lpx)
                   19656: {
                   19657:        volatile sio_t *p = (sio_t *)lpx;
                   19658:        sio_mt_t *q = &sio_mt[p->channel];
                   19659:        
                   19660:        char name[] = "COM1";
1.1.1.26  root     19661:        name[3] = '0' + sio_port_number[p->channel];
                   19662:        HANDLE hComm = NULL;
                   19663:        COMMPROP commProp;
                   19664:        DCB dcb;
                   19665:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   19666:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   19667:        
                   19668:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   19669:                if(GetCommProperties(hComm, &commProp)) {
                   19670:                        dwSettableBaud = commProp.dwSettableBaud;
                   19671:                }
1.1.1.25  root     19672:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     19673: //             EscapeCommFunction(hComm, SETRTS);
                   19674: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     19675:                
1.1.1.54  root     19676:                while(!m_exit) {
1.1.1.25  root     19677:                        // setup comm port
                   19678:                        bool comm_state_changed = false;
                   19679:                        
                   19680:                        EnterCriticalSection(&q->csLineCtrl);
                   19681:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   19682:                                p->prev_divisor = p->divisor.w;
                   19683:                                p->prev_line_ctrl = p->line_ctrl;
                   19684:                                comm_state_changed = true;
                   19685:                        }
                   19686:                        LeaveCriticalSection(&q->csLineCtrl);
                   19687:                        
                   19688:                        if(comm_state_changed) {
1.1.1.26  root     19689:                                if(GetCommState(hComm, &dcb)) {
                   19690: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   19691:                                        DWORD baud = 115200 / p->prev_divisor;
                   19692:                                        dcb.BaudRate = 9600; // default
                   19693:                                        
                   19694:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   19695:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   19696:                                        // 134.5bps is not supported ???
                   19697: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   19698:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   19699:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   19700:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   19701:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   19702:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   19703:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   19704:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   19705:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   19706:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   19707: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   19708: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   19709: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   19710:                                        
                   19711:                                        switch(p->prev_line_ctrl & 0x03) {
                   19712:                                        case 0x00: dcb.ByteSize = 5; break;
                   19713:                                        case 0x01: dcb.ByteSize = 6; break;
                   19714:                                        case 0x02: dcb.ByteSize = 7; break;
                   19715:                                        case 0x03: dcb.ByteSize = 8; break;
                   19716:                                        }
                   19717:                                        switch(p->prev_line_ctrl & 0x04) {
                   19718:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   19719:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   19720:                                        }
                   19721:                                        switch(p->prev_line_ctrl & 0x38) {
                   19722:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   19723:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   19724:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   19725:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   19726:                                        default:   dcb.Parity = NOPARITY;    break;
                   19727:                                        }
                   19728:                                        dcb.fBinary = TRUE;
                   19729:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   19730:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   19731:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   19732:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   19733:                                        dcb.fTXContinueOnXoff = TRUE;
                   19734:                                        dcb.fOutX = dcb.fInX = FALSE;
                   19735:                                        dcb.fErrorChar = FALSE;
                   19736:                                        dcb.fNull = FALSE;
                   19737:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   19738:                                        dcb.fAbortOnError = FALSE;
                   19739:                                        
                   19740:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     19741:                                }
                   19742:                                
                   19743:                                // check again to apply all comm state changes
                   19744:                                Sleep(10);
                   19745:                                continue;
                   19746:                        }
                   19747:                        
                   19748:                        // set comm pins
                   19749:                        bool change_brk = false;
1.1.1.26  root     19750: //                     bool change_rts = false;
                   19751: //                     bool change_dtr = false;
1.1.1.25  root     19752:                        
                   19753:                        EnterCriticalSection(&q->csModemCtrl);
                   19754:                        if(p->prev_set_brk != p->set_brk) {
                   19755:                                p->prev_set_brk = p->set_brk;
                   19756:                                change_brk = true;
                   19757:                        }
1.1.1.26  root     19758: //                     if(p->prev_set_rts != p->set_rts) {
                   19759: //                             p->prev_set_rts = p->set_rts;
                   19760: //                             change_rts = true;
                   19761: //                     }
                   19762: //                     if(p->prev_set_dtr != p->set_dtr) {
                   19763: //                             p->prev_set_dtr = p->set_dtr;
                   19764: //                             change_dtr = true;
                   19765: //                     }
1.1.1.25  root     19766:                        LeaveCriticalSection(&q->csModemCtrl);
                   19767:                        
                   19768:                        if(change_brk) {
1.1.1.26  root     19769:                                static UINT32 clear_time = 0;
                   19770:                                if(p->prev_set_brk) {
                   19771:                                        EscapeCommFunction(hComm, SETBREAK);
                   19772:                                        clear_time = timeGetTime() + 200;
                   19773:                                } else {
                   19774:                                        // keep break for at least 200msec
                   19775:                                        UINT32 cur_time = timeGetTime();
                   19776:                                        if(clear_time > cur_time) {
                   19777:                                                Sleep(clear_time - cur_time);
                   19778:                                        }
                   19779:                                        EscapeCommFunction(hComm, CLRBREAK);
                   19780:                                }
1.1.1.25  root     19781:                        }
1.1.1.26  root     19782: //                     if(change_rts) {
                   19783: //                             if(p->prev_set_rts) {
                   19784: //                                     EscapeCommFunction(hComm, SETRTS);
                   19785: //                             } else {
                   19786: //                                     EscapeCommFunction(hComm, CLRRTS);
                   19787: //                             }
                   19788: //                     }
                   19789: //                     if(change_dtr) {
                   19790: //                             if(p->prev_set_dtr) {
                   19791: //                                     EscapeCommFunction(hComm, SETDTR);
                   19792: //                             } else {
                   19793: //                                     EscapeCommFunction(hComm, CLRDTR);
                   19794: //                             }
                   19795: //                     }
1.1.1.25  root     19796:                        
                   19797:                        // get comm pins
                   19798:                        DWORD dwModemStat = 0;
                   19799:                        
                   19800:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   19801:                                EnterCriticalSection(&q->csModemStat);
                   19802:                                if(dwModemStat & MS_RLSD_ON) {
                   19803:                                        p->modem_stat |= 0x80;
                   19804:                                } else {
                   19805:                                        p->modem_stat &= ~0x80;
                   19806:                                }
                   19807:                                if(dwModemStat & MS_RING_ON) {
                   19808:                                        p->modem_stat |= 0x40;
                   19809:                                } else {
                   19810:                                        p->modem_stat &= ~0x40;
                   19811:                                }
1.1.1.26  root     19812: //                             if(dwModemStat & MS_DSR_ON) {
                   19813: //                                     p->modem_stat |= 0x20;
                   19814: //                             } else {
                   19815: //                                     p->modem_stat &= ~0x20;
                   19816: //                             }
                   19817: //                             if(dwModemStat & MS_CTS_ON) {
                   19818: //                                     p->modem_stat |= 0x10;
                   19819: //                             } else {
                   19820: //                                     p->modem_stat &= ~0x10;
                   19821: //                             }
1.1.1.25  root     19822:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   19823:                                        p->modem_stat |= 0x08;
                   19824:                                }
                   19825:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   19826:                                        p->modem_stat |= 0x04;
                   19827:                                }
1.1.1.26  root     19828: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   19829: //                                     p->modem_stat |= 0x02;
                   19830: //                             }
                   19831: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   19832: //                                     p->modem_stat |= 0x01;
                   19833: //                             }
1.1.1.25  root     19834:                                LeaveCriticalSection(&q->csModemStat);
                   19835:                        }
                   19836:                        
                   19837:                        // send data
                   19838:                        DWORD dwSend = 0;
                   19839:                        
                   19840:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     19841:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     19842:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   19843:                        }
                   19844:                        LeaveCriticalSection(&q->csSendData);
                   19845:                        
                   19846:                        if(dwSend != 0) {
                   19847:                                DWORD dwWritten = 0;
                   19848:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   19849:                        }
                   19850:                        
                   19851:                        // get line status and recv data
                   19852:                        DWORD dwLineStat = 0;
                   19853:                        COMSTAT comStat;
                   19854:                        
                   19855:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   19856:                                EnterCriticalSection(&q->csLineStat);
                   19857:                                if(dwLineStat & CE_BREAK) {
                   19858:                                        p->line_stat_err |= 0x10;
                   19859:                                }
                   19860:                                if(dwLineStat & CE_FRAME) {
                   19861:                                        p->line_stat_err |= 0x08;
                   19862:                                }
                   19863:                                if(dwLineStat & CE_RXPARITY) {
                   19864:                                        p->line_stat_err |= 0x04;
                   19865:                                }
                   19866:                                if(dwLineStat & CE_OVERRUN) {
                   19867:                                        p->line_stat_err |= 0x02;
                   19868:                                }
                   19869:                                LeaveCriticalSection(&q->csLineStat);
                   19870:                                
                   19871:                                if(comStat.cbInQue != 0) {
                   19872:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     19873:                                        DWORD dwRecv = 0;
                   19874:                                        if(p->recv_buffer != NULL) {
                   19875:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   19876:                                        }
1.1.1.25  root     19877:                                        LeaveCriticalSection(&q->csRecvData);
                   19878:                                        
                   19879:                                        if(dwRecv != 0) {
                   19880:                                                DWORD dwRead = 0;
                   19881:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   19882:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     19883:                                                        if(p->recv_buffer != NULL) {
                   19884:                                                                for(int i = 0; i < dwRead; i++) {
                   19885:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   19886:                                                                }
1.1.1.25  root     19887:                                                        }
                   19888:                                                        LeaveCriticalSection(&q->csRecvData);
                   19889:                                                }
                   19890:                                        }
                   19891:                                }
                   19892:                        }
                   19893:                        Sleep(10);
                   19894:                }
                   19895:                CloseHandle(hComm);
                   19896:        }
                   19897:        return 0;
                   19898: }
                   19899: 
1.1.1.8   root     19900: // cmos
                   19901: 
                   19902: void cmos_init()
                   19903: {
                   19904:        memset(cmos, 0, sizeof(cmos));
                   19905:        cmos_addr = 0;
1.1       root     19906:        
1.1.1.8   root     19907:        // from DOSBox
                   19908:        cmos_write(0x0a, 0x26);
                   19909:        cmos_write(0x0b, 0x02);
                   19910:        cmos_write(0x0d, 0x80);
1.1       root     19911: }
                   19912: 
1.1.1.8   root     19913: void cmos_write(int addr, UINT8 val)
1.1       root     19914: {
1.1.1.8   root     19915:        cmos[addr & 0x7f] = val;
                   19916: }
                   19917: 
                   19918: #define CMOS_GET_TIME() { \
                   19919:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   19920:        if(prev_sec != cur_sec) { \
                   19921:                GetLocalTime(&time); \
                   19922:                prev_sec = cur_sec; \
                   19923:        } \
1.1       root     19924: }
1.1.1.8   root     19925: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     19926: 
1.1.1.8   root     19927: UINT8 cmos_read(int addr)
1.1       root     19928: {
1.1.1.8   root     19929:        static SYSTEMTIME time;
                   19930:        static UINT32 prev_sec = 0;
1.1       root     19931:        
1.1.1.8   root     19932:        switch(addr & 0x7f) {
                   19933:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   19934:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   19935:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   19936:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   19937:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   19938:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   19939:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   19940: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   19941:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   19942:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   19943:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   19944:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   19945:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   19946:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   19947:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   19948:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     19949:        }
1.1.1.8   root     19950:        return(cmos[addr & 0x7f]);
1.1       root     19951: }
                   19952: 
1.1.1.7   root     19953: // kbd (a20)
                   19954: 
                   19955: void kbd_init()
                   19956: {
1.1.1.8   root     19957:        kbd_data = kbd_command = 0;
1.1.1.7   root     19958:        kbd_status = 0x18;
                   19959: }
                   19960: 
                   19961: UINT8 kbd_read_data()
                   19962: {
1.1.1.8   root     19963:        kbd_status &= ~1;
1.1.1.7   root     19964:        return(kbd_data);
                   19965: }
                   19966: 
                   19967: void kbd_write_data(UINT8 val)
                   19968: {
                   19969:        switch(kbd_command) {
                   19970:        case 0xd1:
                   19971:                i386_set_a20_line((val >> 1) & 1);
                   19972:                break;
                   19973:        }
                   19974:        kbd_command = 0;
1.1.1.8   root     19975:        kbd_status &= ~8;
1.1.1.7   root     19976: }
                   19977: 
                   19978: UINT8 kbd_read_status()
                   19979: {
                   19980:        return(kbd_status);
                   19981: }
                   19982: 
                   19983: void kbd_write_command(UINT8 val)
                   19984: {
                   19985:        switch(val) {
                   19986:        case 0xd0:
                   19987:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     19988:                kbd_status |= 1;
1.1.1.7   root     19989:                break;
                   19990:        case 0xdd:
                   19991:                i386_set_a20_line(0);
                   19992:                break;
                   19993:        case 0xdf:
                   19994:                i386_set_a20_line(1);
                   19995:                break;
1.1.1.26  root     19996:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   19997:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     19998:                if(!(val & 1)) {
1.1.1.8   root     19999:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     20000:                                // reset pic
                   20001:                                pic_init();
                   20002:                                pic[0].irr = pic[1].irr = 0x00;
                   20003:                                pic[0].imr = pic[1].imr = 0xff;
                   20004:                        }
                   20005:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     20006:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   20007:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   20008:                        i386_jmp_far(selector, address);
1.1.1.7   root     20009:                }
                   20010:                i386_set_a20_line((val >> 1) & 1);
                   20011:                break;
                   20012:        }
                   20013:        kbd_command = val;
1.1.1.8   root     20014:        kbd_status |= 8;
1.1.1.7   root     20015: }
                   20016: 
1.1.1.9   root     20017: // vga
                   20018: 
                   20019: UINT8 vga_read_status()
                   20020: {
                   20021:        // 60hz
                   20022:        static const int period[3] = {16, 17, 17};
                   20023:        static int index = 0;
                   20024:        UINT32 time = timeGetTime() % period[index];
                   20025:        
                   20026:        index = (index + 1) % 3;
1.1.1.14  root     20027:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     20028: }
                   20029: 
1.1       root     20030: // i/o bus
                   20031: 
1.1.1.29  root     20032: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   20033: //#define SW1US_PATCH
                   20034: 
1.1.1.25  root     20035: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     20036: #ifdef USE_DEBUGGER
1.1.1.25  root     20037: {
1.1.1.33  root     20038:        if(now_debugging) {
                   20039:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20040:                        if(in_break_point.table[i].status == 1) {
                   20041:                                if(addr == in_break_point.table[i].addr) {
                   20042:                                        in_break_point.hit = i + 1;
                   20043:                                        now_suspended = true;
                   20044:                                        break;
                   20045:                                }
                   20046:                        }
                   20047:                }
1.1.1.25  root     20048:        }
1.1.1.33  root     20049:        return(debugger_read_io_byte(addr));
1.1.1.25  root     20050: }
1.1.1.33  root     20051: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     20052: #endif
1.1       root     20053: {
1.1.1.33  root     20054:        UINT8 val = 0xff;
                   20055:        
1.1       root     20056:        switch(addr) {
1.1.1.29  root     20057: #ifdef SW1US_PATCH
                   20058:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20059:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     20060:                val = sio_read(0, addr - 1);
                   20061:                break;
1.1.1.29  root     20062: #else
1.1.1.25  root     20063:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20064:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     20065:                val = dma_read(0, addr);
                   20066:                break;
1.1.1.29  root     20067: #endif
1.1.1.25  root     20068:        case 0x20: case 0x21:
1.1.1.33  root     20069:                val = pic_read(0, addr);
                   20070:                break;
1.1.1.25  root     20071:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     20072:                val = pit_read(addr & 0x03);
                   20073:                break;
1.1.1.7   root     20074:        case 0x60:
1.1.1.33  root     20075:                val = kbd_read_data();
                   20076:                break;
1.1.1.9   root     20077:        case 0x61:
1.1.1.33  root     20078:                val = system_port;
                   20079:                break;
1.1.1.7   root     20080:        case 0x64:
1.1.1.33  root     20081:                val = kbd_read_status();
                   20082:                break;
1.1       root     20083:        case 0x71:
1.1.1.33  root     20084:                val = cmos_read(cmos_addr);
                   20085:                break;
1.1.1.25  root     20086:        case 0x81:
1.1.1.33  root     20087:                val = dma_page_read(0, 2);
                   20088:                break;
1.1.1.25  root     20089:        case 0x82:
1.1.1.33  root     20090:                val = dma_page_read(0, 3);
                   20091:                break;
1.1.1.25  root     20092:        case 0x83:
1.1.1.33  root     20093:                val = dma_page_read(0, 1);
                   20094:                break;
1.1.1.25  root     20095:        case 0x87:
1.1.1.33  root     20096:                val = dma_page_read(0, 0);
                   20097:                break;
1.1.1.25  root     20098:        case 0x89:
1.1.1.33  root     20099:                val = dma_page_read(1, 2);
                   20100:                break;
1.1.1.25  root     20101:        case 0x8a:
1.1.1.33  root     20102:                val = dma_page_read(1, 3);
                   20103:                break;
1.1.1.25  root     20104:        case 0x8b:
1.1.1.33  root     20105:                val = dma_page_read(1, 1);
                   20106:                break;
1.1.1.25  root     20107:        case 0x8f:
1.1.1.33  root     20108:                val = dma_page_read(1, 0);
                   20109:                break;
1.1       root     20110:        case 0x92:
1.1.1.33  root     20111:                val = (m_a20_mask >> 19) & 2;
                   20112:                break;
1.1.1.25  root     20113:        case 0xa0: case 0xa1:
1.1.1.33  root     20114:                val = pic_read(1, addr);
                   20115:                break;
1.1.1.25  root     20116:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20117:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     20118:                val = dma_read(1, (addr - 0xc0) >> 1);
                   20119:                break;
1.1.1.37  root     20120:        case 0x278: case 0x279: case 0x27a:
                   20121:                val = pio_read(1, addr);
                   20122:                break;
1.1.1.29  root     20123:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     20124:                val = sio_read(3, addr);
                   20125:                break;
1.1.1.25  root     20126:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     20127:                val = sio_read(1, addr);
                   20128:                break;
1.1.1.25  root     20129:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     20130:                val = pio_read(0, addr);
                   20131:                break;
1.1.1.25  root     20132:        case 0x3ba: case 0x3da:
1.1.1.33  root     20133:                val = vga_read_status();
                   20134:                break;
1.1.1.37  root     20135:        case 0x3bc: case 0x3bd: case 0x3be:
                   20136:                val = pio_read(2, addr);
                   20137:                break;
1.1.1.29  root     20138:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     20139:                val = sio_read(2, addr);
                   20140:                break;
1.1.1.25  root     20141:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     20142:                val = sio_read(0, addr);
                   20143:                break;
1.1       root     20144:        default:
1.1.1.33  root     20145: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     20146:                break;
                   20147:        }
1.1.1.33  root     20148: #ifdef ENABLE_DEBUG_IOPORT
                   20149:        if(fp_debug_log != NULL) {
                   20150:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   20151:        }
                   20152: #endif
                   20153:        return(val);
1.1       root     20154: }
                   20155: 
                   20156: UINT16 read_io_word(offs_t addr)
                   20157: {
                   20158:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   20159: }
                   20160: 
1.1.1.33  root     20161: #ifdef USE_DEBUGGER
                   20162: UINT16 debugger_read_io_word(offs_t addr)
                   20163: {
                   20164:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   20165: }
                   20166: #endif
                   20167: 
1.1       root     20168: UINT32 read_io_dword(offs_t addr)
                   20169: {
                   20170:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   20171: }
                   20172: 
1.1.1.33  root     20173: #ifdef USE_DEBUGGER
                   20174: UINT32 debugger_read_io_dword(offs_t addr)
                   20175: {
                   20176:        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));
                   20177: }
                   20178: #endif
                   20179: 
1.1       root     20180: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     20181: #ifdef USE_DEBUGGER
                   20182: {
                   20183:        if(now_debugging) {
                   20184:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20185:                        if(out_break_point.table[i].status == 1) {
                   20186:                                if(addr == out_break_point.table[i].addr) {
                   20187:                                        out_break_point.hit = i + 1;
                   20188:                                        now_suspended = true;
                   20189:                                        break;
                   20190:                                }
                   20191:                        }
                   20192:                }
                   20193:        }
                   20194:        debugger_write_io_byte(addr, val);
                   20195: }
                   20196: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   20197: #endif
1.1       root     20198: {
1.1.1.25  root     20199: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     20200:        if(fp_debug_log != NULL) {
1.1.1.43  root     20201: #ifdef USE_SERVICE_THREAD
                   20202:                if(addr != 0xf7)
                   20203: #endif
1.1.1.33  root     20204:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     20205:        }
                   20206: #endif
1.1       root     20207:        switch(addr) {
1.1.1.29  root     20208: #ifdef SW1US_PATCH
                   20209:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20210:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   20211:                sio_write(0, addr - 1, val);
                   20212:                break;
                   20213: #else
1.1.1.25  root     20214:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20215:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   20216:                dma_write(0, addr, val);
                   20217:                break;
1.1.1.29  root     20218: #endif
1.1.1.25  root     20219:        case 0x20: case 0x21:
1.1       root     20220:                pic_write(0, addr, val);
                   20221:                break;
1.1.1.25  root     20222:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     20223:                pit_write(addr & 0x03, val);
                   20224:                break;
1.1.1.7   root     20225:        case 0x60:
                   20226:                kbd_write_data(val);
                   20227:                break;
1.1.1.9   root     20228:        case 0x61:
                   20229:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   20230:                        // beep on
                   20231: //                     MessageBeep(-1);
                   20232:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   20233:                        // beep off
                   20234:                }
                   20235:                system_port = val;
                   20236:                break;
1.1       root     20237:        case 0x64:
1.1.1.7   root     20238:                kbd_write_command(val);
1.1       root     20239:                break;
                   20240:        case 0x70:
                   20241:                cmos_addr = val;
                   20242:                break;
                   20243:        case 0x71:
1.1.1.8   root     20244:                cmos_write(cmos_addr, val);
1.1       root     20245:                break;
1.1.1.25  root     20246:        case 0x81:
                   20247:                dma_page_write(0, 2, val);
                   20248:        case 0x82:
                   20249:                dma_page_write(0, 3, val);
                   20250:        case 0x83:
                   20251:                dma_page_write(0, 1, val);
                   20252:        case 0x87:
                   20253:                dma_page_write(0, 0, val);
                   20254:        case 0x89:
                   20255:                dma_page_write(1, 2, val);
                   20256:        case 0x8a:
                   20257:                dma_page_write(1, 3, val);
                   20258:        case 0x8b:
                   20259:                dma_page_write(1, 1, val);
                   20260:        case 0x8f:
                   20261:                dma_page_write(1, 0, val);
1.1       root     20262:        case 0x92:
1.1.1.7   root     20263:                i386_set_a20_line((val >> 1) & 1);
1.1       root     20264:                break;
1.1.1.25  root     20265:        case 0xa0: case 0xa1:
1.1       root     20266:                pic_write(1, addr, val);
                   20267:                break;
1.1.1.25  root     20268:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20269:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     20270:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     20271:                break;
1.1.1.35  root     20272: #ifdef USE_SERVICE_THREAD
                   20273:        case 0xf7:
                   20274:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     20275:                if(in_service && cursor_moved) {
                   20276:                        // update cursor position before service is done
                   20277:                        pcbios_update_cursor_position();
                   20278:                        cursor_moved = false;
                   20279:                }
1.1.1.35  root     20280:                finish_service_loop();
                   20281:                break;
                   20282: #endif
1.1.1.37  root     20283:        case 0x278: case 0x279: case 0x27a:
                   20284:                pio_write(1, addr, val);
                   20285:                break;
1.1.1.29  root     20286:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   20287:                sio_write(3, addr, val);
                   20288:                break;
1.1.1.25  root     20289:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   20290:                sio_write(1, addr, val);
                   20291:                break;
                   20292:        case 0x378: case 0x379: case 0x37a:
                   20293:                pio_write(0, addr, val);
                   20294:                break;
1.1.1.37  root     20295:        case 0x3bc: case 0x3bd: case 0x3be:
                   20296:                pio_write(2, addr, val);
                   20297:                break;
1.1.1.29  root     20298:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   20299:                sio_write(2, addr, val);
                   20300:                break;
1.1.1.25  root     20301:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   20302:                sio_write(0, addr, val);
                   20303:                break;
1.1       root     20304:        default:
1.1.1.33  root     20305: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     20306:                break;
                   20307:        }
                   20308: }
                   20309: 
                   20310: void write_io_word(offs_t addr, UINT16 val)
                   20311: {
                   20312:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20313:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20314: }
                   20315: 
1.1.1.33  root     20316: #ifdef USE_DEBUGGER
                   20317: void debugger_write_io_word(offs_t addr, UINT16 val)
                   20318: {
                   20319:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20320:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20321: }
                   20322: #endif
                   20323: 
1.1       root     20324: void write_io_dword(offs_t addr, UINT32 val)
                   20325: {
                   20326:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20327:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20328:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20329:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20330: }
1.1.1.33  root     20331: 
                   20332: #ifdef USE_DEBUGGER
                   20333: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   20334: {
                   20335:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20336:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20337:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20338:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20339: }
                   20340: #endif

unix.superglobalmegacorp.com

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