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

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.57! root     2234:        if(key_buf_data != NULL) {
        !          2235:                key_buf_data->release();
        !          2236:                delete key_buf_data;
        !          2237:                key_buf_data = NULL;
        !          2238:        }
1.1.1.32  root     2239: #ifdef SUPPORT_XMS
                   2240:        msdos_xms_release();
                   2241: #endif
1.1.1.28  root     2242:        hardware_release();
                   2243: }
                   2244: 
1.1.1.35  root     2245: #ifdef USE_VRAM_THREAD
1.1.1.28  root     2246: DWORD WINAPI vram_thread(LPVOID)
                   2247: {
1.1.1.54  root     2248:        while(!m_exit) {
1.1.1.28  root     2249:                EnterCriticalSection(&vram_crit_sect);
                   2250:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                   2251:                        vram_flush_char();
                   2252:                }
                   2253:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                   2254:                        vram_flush_attr();
                   2255:                }
                   2256:                vram_last_length_char = vram_length_char;
                   2257:                vram_last_length_attr = vram_length_attr;
                   2258:                LeaveCriticalSection(&vram_crit_sect);
                   2259:                // this is about half the maximum keyboard repeat rate - any
                   2260:                // lower tends to be jerky, any higher misses updates
                   2261:                Sleep(15);
                   2262:        }
                   2263:        return 0;
                   2264: }
                   2265: #endif
                   2266: 
1.1.1.45  root     2267: long get_section_in_exec_file(FILE *fp, const char *name)
1.1.1.28  root     2268: {
                   2269:        UINT8 header[0x400];
                   2270:        
                   2271:        long position = ftell(fp);
                   2272:        fseek(fp, 0, SEEK_SET);
                   2273:        fread(header, sizeof(header), 1, fp);
                   2274:        fseek(fp, position, SEEK_SET);
                   2275:        
                   2276:        try {
                   2277:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2278:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2279:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2280:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2281:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2282:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2283:                
                   2284:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                   2285:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                   2286:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                   2287:                                return(sectionHeader->PointerToRawData);
                   2288:                        }
                   2289:                }
                   2290:        } catch(...) {
                   2291:        }
                   2292:        return(0);
                   2293: }
                   2294: 
1.1.1.10  root     2295: bool is_started_from_command_prompt()
                   2296: {
1.1.1.18  root     2297:        bool ret = false;
                   2298:        
                   2299:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                   2300:        if(hLibrary) {
                   2301:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                   2302:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                   2303:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
                   2304:                if(lpfnGetConsoleProcessList) {
                   2305:                        DWORD pl;
                   2306:                        ret = (lpfnGetConsoleProcessList(&pl, 1) > 1);
                   2307:                        FreeLibrary(hLibrary);
                   2308:                        return(ret);
                   2309:                }
                   2310:                FreeLibrary(hLibrary);
                   2311:        }
                   2312:        
                   2313:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   2314:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                   2315:                DWORD dwParentProcessID = 0;
                   2316:                PROCESSENTRY32 pe32;
                   2317:                pe32.dwSize = sizeof(PROCESSENTRY32);
                   2318:                if(Process32First(hSnapshot, &pe32)) {
                   2319:                        do {
                   2320:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                   2321:                                        dwParentProcessID = pe32.th32ParentProcessID;
                   2322:                                        break;
                   2323:                                }
                   2324:                        } while(Process32Next(hSnapshot, &pe32));
                   2325:                }
                   2326:                CloseHandle(hSnapshot);
                   2327:                if(dwParentProcessID != 0) {
                   2328:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                   2329:                        if(hProcess != NULL) {
                   2330:                                HMODULE hMod;
                   2331:                                DWORD cbNeeded;
                   2332:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                   2333:                                        char module_name[MAX_PATH];
                   2334:                                        if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
                   2335:                                                ret = (_strnicmp(module_name, "cmd.exe", 7) == 0);
                   2336:                                        }
                   2337:                                }
                   2338:                                CloseHandle(hProcess);
                   2339:                        }
                   2340:                }
                   2341:        }
                   2342:        return(ret);
1.1.1.14  root     2343: }
                   2344: 
                   2345: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                   2346: {
1.1.1.24  root     2347:        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
1.1.1.14  root     2348:        OSVERSIONINFOEX osvi;
                   2349:        DWORDLONG dwlConditionMask = 0;
                   2350:        int op = VER_GREATER_EQUAL;
                   2351:        
                   2352:        // Initialize the OSVERSIONINFOEX structure.
                   2353:        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
                   2354:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
                   2355:        osvi.dwMajorVersion = dwMajorVersion;
                   2356:        osvi.dwMinorVersion = dwMinorVersion;
                   2357:        osvi.wServicePackMajor = wServicePackMajor;
                   2358:        osvi.wServicePackMinor = wServicePackMinor;
                   2359:        
                   2360:         // Initialize the condition mask.
                   2361:        VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                   2362:        VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                   2363:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                   2364:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                   2365:        
                   2366:        // Perform the test.
                   2367:        return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                   2368: }
                   2369: 
1.1.1.56  root     2370: bool set_console_font_size(HANDLE hStdout, int width, int height)
                   2371: {
                   2372:        // http://d.hatena.ne.jp/aharisu/20090427/1240852598
                   2373:        bool result = false;
                   2374:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                   2375:        
                   2376:        if(hLibrary) {
                   2377:                typedef BOOL (WINAPI* GetConsoleFontInfoFunction)(HANDLE, BOOL, DWORD, PCONSOLE_FONT_INFO);
                   2378:                typedef DWORD (WINAPI* GetNumberOfConsoleFontsFunction)(VOID);
                   2379:                typedef BOOL (WINAPI* SetConsoleFontFunction)(HANDLE, DWORD);
                   2380:                typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
                   2381:                
                   2382:                GetConsoleFontInfoFunction lpfnGetConsoleFontInfo = reinterpret_cast<GetConsoleFontInfoFunction>(::GetProcAddress(hLibrary, "GetConsoleFontInfo"));
                   2383:                GetNumberOfConsoleFontsFunction lpfnGetNumberOfConsoleFonts = reinterpret_cast<GetNumberOfConsoleFontsFunction>(::GetProcAddress(hLibrary, "GetNumberOfConsoleFonts"));
                   2384:                SetConsoleFontFunction lpfnSetConsoleFont = reinterpret_cast<SetConsoleFontFunction>(::GetProcAddress(hLibrary, "SetConsoleFont"));
                   2385:                GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
                   2386:                
                   2387:                if(lpfnGetConsoleFontInfo && lpfnGetNumberOfConsoleFonts && lpfnSetConsoleFont) {
                   2388:                        DWORD dwFontNum = lpfnGetNumberOfConsoleFonts();
                   2389:                        CONSOLE_FONT_INFO* fonts = (CONSOLE_FONT_INFO*)malloc(sizeof(CONSOLE_FONT_INFO) * dwFontNum);
                   2390:                        lpfnGetConsoleFontInfo(hStdout, FALSE, dwFontNum, fonts);
                   2391:                        for(int i = 0; i < dwFontNum; i++) {
                   2392:                                fonts[i].dwFontSize = GetConsoleFontSize(hStdout, fonts[i].nFont);
                   2393:                                if(fonts[i].dwFontSize.X == width && fonts[i].dwFontSize.Y == height) {
                   2394:                                        lpfnSetConsoleFont(hStdout, fonts[i].nFont);
                   2395:                                        result = true;
                   2396:                                        break;
                   2397:                                }
                   2398:                        }
                   2399:                        free(fonts);
                   2400:                }
                   2401:                if(lpfnGetCurrentConsoleFont && result) {
                   2402:                        CONSOLE_FONT_INFO fi;
                   2403:                        if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi)) {
1.1.1.57! root     2404:                                if(fi.dwFontSize.X == width && fi.dwFontSize.Y == height) {
        !          2405:                                        *(UINT16 *)(mem + 0x485) = fi.dwFontSize.Y;
        !          2406:                                } else {
        !          2407:                                        result = false;
        !          2408:                                }
1.1.1.56  root     2409:                        }
                   2410:                }
                   2411:                FreeLibrary(hLibrary);
                   2412:        }
                   2413:        return(result);
                   2414: }
                   2415: 
1.1.1.27  root     2416: void get_sio_port_numbers()
                   2417: {
                   2418:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                   2419:        HDEVINFO hDevInfo = 0;
                   2420:        HKEY hKey = 0;
                   2421:        if((hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
                   2422:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                   2423:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                   2424:                                char chData[256];
                   2425:                                DWORD dwType = 0;
                   2426:                                DWORD dwSize = sizeof(chData);
                   2427:                                int port_number = 0;
                   2428:                                
                   2429:                                if(RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
                   2430:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                   2431:                                                port_number = atoi(chData + 3);
                   2432:                                        }
                   2433:                                }
                   2434:                                RegCloseKey(hKey);
                   2435:                                
1.1.1.29  root     2436:                                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     2437:                                        continue;
                   2438:                                }
                   2439:                                if(sio_port_number[0] == 0) {
                   2440:                                        sio_port_number[0] = port_number;
                   2441:                                } else if(sio_port_number[1] == 0) {
                   2442:                                        sio_port_number[1] = port_number;
1.1.1.29  root     2443:                                } else if(sio_port_number[2] == 0) {
                   2444:                                        sio_port_number[2] = port_number;
                   2445:                                } else if(sio_port_number[3] == 0) {
                   2446:                                        sio_port_number[3] = port_number;
1.1.1.27  root     2447:                                }
1.1.1.29  root     2448:                                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     2449:                                        break;
                   2450:                                }
                   2451:                        }
                   2452:                }
                   2453:        }
                   2454: }
                   2455: 
1.1.1.28  root     2456: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                   2457: 
1.1       root     2458: int main(int argc, char *argv[], char *envp[])
                   2459: {
1.1.1.9   root     2460:        int arg_offset = 0;
                   2461:        int standard_env = 0;
1.1.1.14  root     2462:        int buf_width = 0, buf_height = 0;
1.1.1.28  root     2463:        bool get_console_info_success = false;
1.1.1.56  root     2464:        bool get_console_font_success = false;
1.1.1.28  root     2465:        bool screen_size_changed = false;
                   2466:        
                   2467:        _TCHAR path[MAX_PATH], full[MAX_PATH], *name = NULL;
                   2468:        GetModuleFileName(NULL, path, MAX_PATH);
                   2469:        GetFullPathName(path, MAX_PATH, full, &name);
1.1       root     2470:        
1.1.1.27  root     2471:        char dummy_argv_0[] = "msdos.exe";
                   2472:        char dummy_argv_1[MAX_PATH];
                   2473:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                   2474:        char new_exec_file[MAX_PATH];
                   2475:        bool convert_cmd_file = false;
1.1.1.28  root     2476:        unsigned int code_page = 0;
1.1.1.27  root     2477:        
                   2478:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root     2479:                // check if command file is embedded to this execution file
                   2480:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root     2481:                FILE* fp = fopen(full, "rb");
1.1.1.28  root     2482:                long offset = get_section_in_exec_file(fp, ".msdos");
                   2483:                if(offset != 0) {
1.1.1.30  root     2484:                        UINT8 buffer[16];
1.1.1.28  root     2485:                        fseek(fp, offset, SEEK_SET);
                   2486:                        fread(buffer, sizeof(buffer), 1, fp);
                   2487:                        
                   2488:                        // restore flags
                   2489:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                   2490:                        no_windows          = ((buffer[0] & 0x02) != 0);
                   2491:                        standard_env        = ((buffer[0] & 0x04) != 0);
                   2492:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                   2493:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                   2494:                        if((buffer[0] & 0x20) != 0) {
                   2495:                                get_sio_port_numbers();
                   2496:                        }
                   2497:                        if((buffer[0] & 0x40) != 0) {
                   2498:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                   2499:                                support_ems = true;
1.1.1.30  root     2500:                        }
1.1.1.27  root     2501: #ifdef SUPPORT_XMS
1.1.1.30  root     2502:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root     2503:                                support_xms = true;
                   2504:                        }
1.1.1.30  root     2505: #endif
1.1.1.28  root     2506:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                   2507:                                buf_width  = buffer[1] | (buffer[2] << 8);
                   2508:                                buf_height = buffer[3] | (buffer[4] << 8);
                   2509:                        }
                   2510:                        if(buffer[5] != 0) {
1.1.1.30  root     2511:                                dos_major_version = buffer[5];
                   2512:                                dos_minor_version = buffer[6];
                   2513:                        }
                   2514:                        if(buffer[7] != 0) {
                   2515:                                win_major_version = buffer[7];
                   2516:                                win_minor_version = buffer[8];
1.1.1.28  root     2517:                        }
1.1.1.30  root     2518:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root     2519:                                SetConsoleCP(code_page);
                   2520:                                SetConsoleOutputCP(code_page);
                   2521:                        }
1.1.1.30  root     2522:                        int name_len = buffer[11];
                   2523:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root     2524:                        
                   2525:                        // restore command file name
                   2526:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                   2527:                        fread(dummy_argv_1, name_len, 1, fp);
                   2528:                        
                   2529:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                   2530:                                // if original command file exists, create a temporary file name
                   2531:                                if(GetTempFileName(_T("."), _T("DOS"), 0, dummy_argv_1) != 0) {
                   2532:                                        // create a temporary command file in the current director
                   2533:                                        DeleteFile(dummy_argv_1);
1.1.1.27  root     2534:                                } else {
1.1.1.28  root     2535:                                        // create a temporary command file in the temporary folder
                   2536:                                        GetTempPath(MAX_PATH, path);
                   2537:                                        if(GetTempFileName(path, _T("DOS"), 0, dummy_argv_1) != 0) {
                   2538:                                                DeleteFile(dummy_argv_1);
                   2539:                                        } else {
                   2540:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                   2541:                                        }
1.1.1.27  root     2542:                                }
1.1.1.28  root     2543:                                // check the command file type
                   2544:                                fread(buffer, 2, 1, fp);
                   2545:                                fseek(fp, -2, SEEK_CUR);
                   2546:                                if(memcmp(buffer, "MZ", 2) != 0) {
                   2547:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                   2548:                                } else {
                   2549:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root     2550:                                }
                   2551:                        }
1.1.1.28  root     2552:                        
                   2553:                        // restore command file
                   2554:                        FILE* fo = fopen(dummy_argv_1, "wb");
                   2555:                        for(int i = 0; i < file_len; i++) {
                   2556:                                fputc(fgetc(fp), fo);
                   2557:                        }
                   2558:                        fclose(fo);
                   2559:                        
                   2560:                        GetFullPathName(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
                   2561:                        temp_file_created = true;
                   2562:                        SetFileAttributes(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
                   2563:                        
                   2564:                        // adjust argc/argv
                   2565:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                   2566:                                dummy_argv[i + 1] = argv[i];
                   2567:                        }
                   2568:                        argc++;
                   2569:                        argv = dummy_argv;
1.1.1.27  root     2570:                }
                   2571:                fclose(fp);
                   2572:        }
1.1.1.9   root     2573:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     2574:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   2575:                        stay_busy = true;
                   2576:                        arg_offset++;
1.1.1.27  root     2577:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   2578:                        if(argv[i][2] != '\0') {
                   2579:                                strcpy(new_exec_file, &argv[i][2]);
                   2580:                        } else {
                   2581:                                strcpy(new_exec_file, "new_exec_file.exe");
                   2582:                        }
                   2583:                        convert_cmd_file = true;
                   2584:                        arg_offset++;
1.1.1.28  root     2585:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   2586:                        if(IS_NUMERIC(argv[i][2])) {
                   2587:                                code_page = atoi(&argv[i][2]);
                   2588:                        } else {
                   2589:                                code_page = GetConsoleCP();
                   2590:                        }
                   2591:                        arg_offset++;
1.1.1.25  root     2592:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   2593:                        no_windows = true;
                   2594:                        arg_offset++;
                   2595:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     2596:                        standard_env = 1;
                   2597:                        arg_offset++;
1.1.1.14  root     2598:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   2599:                        ignore_illegal_insn = true;
                   2600:                        arg_offset++;
                   2601:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   2602:                        limit_max_memory = true;
                   2603:                        arg_offset++;
                   2604:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.51  root     2605:                        int result = sscanf(argv[i] + 2, "%d,%d", &buf_height, &buf_width);
                   2606:                        if(result == 1) {
                   2607:                                buf_width = 0;
                   2608:                        } else if(result != 2) {
1.1.1.17  root     2609:                                buf_width = buf_height = 0;
                   2610:                        }
                   2611:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   2612:                                buf_width = 80;
                   2613:                        }
                   2614:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   2615:                                buf_height = 25;
                   2616:                        }
1.1.1.14  root     2617:                        arg_offset++;
1.1.1.25  root     2618:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     2619:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     2620:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   2621:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   2622:                                        sio_port_number[1] = atoi(p1 + 1);
                   2623:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   2624:                                                sio_port_number[2] = atoi(p2 + 1);
                   2625:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   2626:                                                        sio_port_number[3] = atoi(p3 + 1);
                   2627:                                                }
                   2628:                                        }
1.1.1.25  root     2629:                                }
1.1.1.29  root     2630:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     2631:                        }
1.1.1.29  root     2632:                        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     2633:                                get_sio_port_numbers();
1.1.1.25  root     2634:                        }
                   2635:                        arg_offset++;
1.1.1.9   root     2636:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     2637:                        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     2638:                                dos_major_version = argv[i][2] - '0';
                   2639:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   2640:                        }
                   2641:                        arg_offset++;
                   2642:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   2643:                        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]))) {
                   2644:                                win_major_version = argv[i][2] - '0';
                   2645:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     2646:                        }
                   2647:                        arg_offset++;
1.1.1.25  root     2648:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   2649:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   2650:                        support_ems = true;
                   2651: #ifdef SUPPORT_XMS
                   2652:                        support_xms = true;
                   2653: #endif
                   2654:                        arg_offset++;
1.1.1.9   root     2655:                } else {
                   2656:                        break;
                   2657:                }
                   2658:        }
                   2659:        if(argc < 2 + arg_offset) {
1.1       root     2660: #ifdef _WIN64
1.1.1.14  root     2661:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     2662: #else
1.1.1.14  root     2663:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     2664: #endif
1.1.1.25  root     2665:                fprintf(stderr,
1.1.1.28  root     2666:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     2667:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     2668:                        "\n"
                   2669:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     2670: #ifdef _WIN64
1.1.1.27  root     2671:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     2672: #else
1.1.1.27  root     2673:                        "\t-c\tconvert command file to 32bit execution file\n"
                   2674: #endif
1.1.1.28  root     2675:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     2676:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   2677:                        "\t-e\tuse a reduced environment block\n"
                   2678:                        "\t-i\tignore invalid instructions\n"
                   2679:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   2680:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   2681:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   2682:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     2683:                        "\t-w\tset the Windows version\n"
1.1.1.19  root     2684: #ifdef SUPPORT_XMS
1.1.1.28  root     2685:                        "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19  root     2686: #else
1.1.1.28  root     2687:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2688: #endif
                   2689:                );
1.1.1.10  root     2690:                
                   2691:                if(!is_started_from_command_prompt()) {
                   2692:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2693:                        while(!_kbhit()) {
                   2694:                                Sleep(10);
                   2695:                        }
                   2696:                }
1.1.1.20  root     2697: #ifdef _DEBUG
                   2698:                _CrtDumpMemoryLeaks();
                   2699: #endif
1.1       root     2700:                return(EXIT_FAILURE);
                   2701:        }
1.1.1.27  root     2702:        if(convert_cmd_file) {
                   2703:                retval = EXIT_FAILURE;
1.1.1.28  root     2704:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2705:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2706:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2707:                        
1.1.1.28  root     2708:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2709:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2710:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2711:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2712:                        } else {
1.1.1.28  root     2713:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2714:                                if(offset != 0) {
                   2715:                                        UINT8 buffer[14];
                   2716:                                        fseek(fp, offset, SEEK_SET);
                   2717:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2718:                                        memset(path, 0, sizeof(path));
                   2719:                                        fread(path, buffer[9], 1, fp);
                   2720:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2721:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2722:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2723:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2724:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2725:                                } else {
                   2726:                                        // read pe header of msdos.exe
                   2727:                                        UINT8 header[0x400];
                   2728:                                        fseek(fp, 0, SEEK_SET);
                   2729:                                        fread(header, sizeof(header), 1, fp);
                   2730:                                        
                   2731:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2732:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2733:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2734:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2735:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2736:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2737:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2738:                                        
                   2739:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2740:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2741:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2742:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2743:                                        if(dwExtraLastSectionBytes != 0) {
                   2744:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2745:                                                dwLastSectionSize += dwRemain;
                   2746:                                        }
                   2747:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2748:                                        
                   2749:                                        // store msdos.exe
                   2750:                                        fseek(fp, 0, SEEK_SET);
                   2751:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2752:                                                if((data = fgetc(fp)) != EOF) {
                   2753:                                                        fputc(data, fo);
                   2754:                                                } else {
                   2755:                                                        // we should not reach here :-(
                   2756:                                                        fputc(0, fo);
                   2757:                                                }
                   2758:                                        }
                   2759:                                        
                   2760:                                        // store options
                   2761:                                        UINT8 flags = 0;
                   2762:                                        if(stay_busy) {
                   2763:                                                flags |= 0x01;
                   2764:                                        }
                   2765:                                        if(no_windows) {
                   2766:                                                flags |= 0x02;
                   2767:                                        }
                   2768:                                        if(standard_env) {
                   2769:                                                flags |= 0x04;
                   2770:                                        }
                   2771:                                        if(ignore_illegal_insn) {
                   2772:                                                flags |= 0x08;
                   2773:                                        }
                   2774:                                        if(limit_max_memory) {
                   2775:                                                flags |= 0x10;
                   2776:                                        }
1.1.1.29  root     2777:                                        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     2778:                                                flags |= 0x20;
                   2779:                                        }
                   2780:                                        if(support_ems) {
                   2781:                                                flags |= 0x40;
                   2782:                                        }
1.1.1.30  root     2783: #ifdef SUPPORT_XMS
                   2784:                                        if(support_xms) {
                   2785:                                                flags |= 0x80;
                   2786:                                        }
                   2787: #endif
1.1.1.28  root     2788:                                        
                   2789:                                        fputc(flags, fo);
                   2790:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2791:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2792:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2793:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2794:                                        fputc(dos_major_version, fo);
                   2795:                                        fputc(dos_minor_version, fo);
                   2796:                                        fputc(win_major_version, fo);
                   2797:                                        fputc(win_minor_version, fo);
1.1.1.28  root     2798:                                        fputc((code_page >> 0) & 0xff, fo);
                   2799:                                        fputc((code_page >> 8) & 0xff, fo);
                   2800:                                        
                   2801:                                        // store command file info
                   2802:                                        GetFullPathName(argv[arg_offset + 1], MAX_PATH, full, &name);
                   2803:                                        int name_len = strlen(name);
                   2804:                                        fseek(fs, 0, SEEK_END);
                   2805:                                        long file_size = ftell(fs);
                   2806:                                        
                   2807:                                        fputc(name_len, fo);
                   2808:                                        fputc((file_size >>  0) & 0xff, fo);
                   2809:                                        fputc((file_size >>  8) & 0xff, fo);
                   2810:                                        fputc((file_size >> 16) & 0xff, fo);
                   2811:                                        fputc((file_size >> 24) & 0xff, fo);
                   2812:                                        fwrite(name, name_len, 1, fo);
                   2813:                                        
                   2814:                                        // store command file
                   2815:                                        fseek(fs, 0, SEEK_SET);
                   2816:                                        for(int i = 0; i < file_size; i++) {
                   2817:                                                if((data = fgetc(fs)) != EOF) {
                   2818:                                                        fputc(data, fo);
                   2819:                                                } else {
                   2820:                                                        // we should not reach here :-(
                   2821:                                                        fputc(0, fo);
                   2822:                                                }
                   2823:                                        }
                   2824:                                        
                   2825:                                        // store padding data and update pe header
1.1.1.29  root     2826:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   2827:                                        coffHeader->NumberOfSections++;
                   2828:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   2829:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   2830:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   2831:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   2832:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     2833:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   2834:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   2835:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     2836:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     2837:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   2838:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     2839:                                                        if(i < 2) {
                   2840:                                                                fputc(padding[i & 15], fo);
                   2841:                                                        } else {
                   2842:                                                                fputc(padding[(i - 2) & 15], fo);
                   2843:                                                        }
1.1.1.28  root     2844:                                                }
                   2845:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   2846:                                        }
                   2847:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   2848:                                        
                   2849:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   2850:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   2851:                                        if(dwExtraNewSectionBytes != 0) {
                   2852:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   2853:                                                dwNewSectionSize += dwRemain;
                   2854:                                        }
                   2855:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   2856:                                        
                   2857:                                        fseek(fo, 0, SEEK_SET);
                   2858:                                        fwrite(header, sizeof(header), 1, fo);
                   2859:                                        
                   2860:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   2861:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     2862:                                }
                   2863:                        }
                   2864:                        if(fp != NULL) {
                   2865:                                fclose(fp);
                   2866:                        }
                   2867:                        if(fs != NULL) {
                   2868:                                fclose(fs);
                   2869:                        }
                   2870:                        if(fo != NULL) {
                   2871:                                fclose(fo);
                   2872:                        }
                   2873:                }
                   2874: #ifdef _DEBUG
                   2875:                _CrtDumpMemoryLeaks();
                   2876: #endif
                   2877:                return(retval);
                   2878:        }
1.1       root     2879:        
1.1.1.54  root     2880:        is_xp_64_or_later = is_greater_windows_version(5, 2, 0, 0);
1.1.1.14  root     2881:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   2882:        
1.1.1.23  root     2883:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     2884:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     2885:        CONSOLE_CURSOR_INFO ci;
1.1.1.56  root     2886:        CONSOLE_FONT_INFO fi;
1.1.1.23  root     2887:        
1.1.1.28  root     2888:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     2889:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.24  root     2890:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1.1.56  root     2891: //     get_console_font_success = (GetCurrentConsoleFont(hStdout, FALSE, &fi) != 0);
                   2892:        
                   2893:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                   2894:        if(hLibrary) {
                   2895:                typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
                   2896:                GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
                   2897:                if(lpfnGetCurrentConsoleFont) {
                   2898:                        get_console_font_success = (lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi) != 0);
                   2899:                }
                   2900:                FreeLibrary(hLibrary);
                   2901:        }
1.1       root     2902:        
1.1.1.14  root     2903:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   2904:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   2905:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   2906:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     2907:                }
                   2908:        }
1.1.1.28  root     2909:        if(get_console_info_success) {
1.1.1.12  root     2910:                scr_width = csbi.dwSize.X;
1.1.1.14  root     2911:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2912:                
1.1.1.28  root     2913:                // v-text shadow buffer size must be lesser than 0x7fd0
                   2914:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     2915:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   2916:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   2917:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     2918:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     2919:                                scr_width = 80;
                   2920:                                scr_height = 25;
                   2921:                        }
1.1.1.28  root     2922:                        screen_size_changed = true;
1.1.1.14  root     2923:                }
1.1.1.12  root     2924:        } else {
                   2925:                // for a proof (not a console)
                   2926:                scr_width = 80;
                   2927:                scr_height = 25;
                   2928:        }
1.1.1.14  root     2929:        scr_buf_size.X = scr_width;
                   2930:        scr_buf_size.Y = scr_height;
                   2931:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   2932:        scr_top = csbi.srWindow.Top;
1.1       root     2933:        cursor_moved = false;
                   2934:        
1.1.1.54  root     2935:        SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
                   2936:        
1.1.1.35  root     2937: #ifdef USE_SERVICE_THREAD
                   2938:        InitializeCriticalSection(&input_crit_sect);
                   2939:        InitializeCriticalSection(&key_buf_crit_sect);
                   2940:        InitializeCriticalSection(&putch_crit_sect);
1.1.1.50  root     2941:        main_thread_id = GetCurrentThreadId();
1.1.1.35  root     2942: #endif
1.1.1.50  root     2943:        
1.1.1.25  root     2944:        key_buf_char = new FIFO(256);
                   2945:        key_buf_scan = new FIFO(256);
1.1.1.57! root     2946:        key_buf_data = new FIFO(256);
1.1       root     2947:        
                   2948:        hardware_init();
                   2949:        
1.1.1.33  root     2950: #ifdef USE_DEBUGGER
                   2951:        debugger_init();
                   2952: #endif
                   2953:        
1.1.1.9   root     2954:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     2955:                retval = EXIT_FAILURE;
                   2956:        } else {
1.1.1.27  root     2957: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     2958:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   2959: #endif
                   2960:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   2961:                
1.1.1.28  root     2962:                if(screen_size_changed) {
1.1.1.24  root     2963:                        change_console_size(scr_width, scr_height);
                   2964:                }
1.1.1.8   root     2965:                TIMECAPS caps;
                   2966:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   2967:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.35  root     2968: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2969:                InitializeCriticalSection(&vram_crit_sect);
                   2970:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   2971: #endif
1.1.1.33  root     2972: #ifdef USE_DEBUGGER
                   2973:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   2974:                // wait until telnet client starts and connects to me
                   2975:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   2976:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   2977:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   2978:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   2979:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   2980:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   2981:                                Sleep(100);
                   2982:                        }
                   2983:                }
                   2984: #endif
1.1       root     2985:                hardware_run();
1.1.1.35  root     2986: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2987:                vram_flush();
                   2988:                DeleteCriticalSection(&vram_crit_sect);
                   2989: #endif
1.1.1.24  root     2990:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     2991:                
1.1.1.24  root     2992:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.56  root     2993:                if(get_console_font_success) {
                   2994:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   2995:                        set_console_font_size(hStdout, fi.dwFontSize.X, fi.dwFontSize.Y);
                   2996:                }
1.1.1.28  root     2997:                if(get_console_info_success) {
1.1.1.23  root     2998:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2999:                        if(restore_console_on_exit) {
1.1.1.14  root     3000:                                // window can't be bigger than buffer,
                   3001:                                // buffer can't be smaller than window,
                   3002:                                // so make a tiny window,
                   3003:                                // set the required buffer,
                   3004:                                // then set the required window
                   3005:                                SMALL_RECT rect;
                   3006:                                SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
                   3007:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     3008:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     3009:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12  root     3010:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3011:                        }
1.1.1.14  root     3012:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   3013:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     3014:                }
1.1.1.24  root     3015:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   3016:                
1.1       root     3017:                msdos_finish();
1.1.1.14  root     3018:                
                   3019:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     3020:        }
1.1.1.35  root     3021:        if(temp_file_created) {
                   3022:                DeleteFile(temp_file_path);
                   3023:                temp_file_created = false;
                   3024:        }
1.1.1.10  root     3025:        hardware_finish();
                   3026:        
1.1.1.28  root     3027:        if(key_buf_char != NULL) {
                   3028:                key_buf_char->release();
                   3029:                delete key_buf_char;
                   3030:                key_buf_char = NULL;
                   3031:        }
                   3032:        if(key_buf_scan != NULL) {
                   3033:                key_buf_scan->release();
                   3034:                delete key_buf_scan;
                   3035:                key_buf_scan = NULL;
                   3036:        }
1.1.1.57! root     3037:        if(key_buf_data != NULL) {
        !          3038:                key_buf_data->release();
        !          3039:                delete key_buf_data;
        !          3040:                key_buf_data = NULL;
        !          3041:        }
1.1.1.35  root     3042: #ifdef USE_SERVICE_THREAD
                   3043:        DeleteCriticalSection(&input_crit_sect);
                   3044:        DeleteCriticalSection(&key_buf_crit_sect);
                   3045:        DeleteCriticalSection(&putch_crit_sect);
                   3046: #endif
1.1.1.20  root     3047: #ifdef _DEBUG
                   3048:        _CrtDumpMemoryLeaks();
                   3049: #endif
1.1       root     3050:        return(retval);
                   3051: }
                   3052: 
1.1.1.20  root     3053: /* ----------------------------------------------------------------------------
                   3054:        console
                   3055: ---------------------------------------------------------------------------- */
                   3056: 
1.1.1.14  root     3057: void change_console_size(int width, int height)
1.1.1.12  root     3058: {
1.1.1.23  root     3059:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     3060:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   3061:        SMALL_RECT rect;
                   3062:        COORD co;
                   3063:        
                   3064:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     3065:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   3066:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
                   3067:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
                   3068:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   3069:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   3070:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   3071:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
                   3072:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   3073:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   3074:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     3075:                }
                   3076:        }
1.1.1.14  root     3077:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     3078:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     3079:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     3080:                SetConsoleCursorPosition(hStdout, co);
                   3081:                cursor_moved = true;
                   3082:        }
1.1.1.14  root     3083:        
                   3084:        // window can't be bigger than buffer,
                   3085:        // buffer can't be smaller than window,
                   3086:        // so make a tiny window,
                   3087:        // set the required buffer,
                   3088:        // then set the required window
                   3089:        SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12  root     3090:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     3091:        co.X = width;
                   3092:        co.Y = height;
1.1.1.12  root     3093:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     3094:        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   3095:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3096:        
                   3097:        scr_width = scr_buf_size.X = width;
                   3098:        scr_height = scr_buf_size.Y = height;
                   3099:        scr_top = 0;
                   3100:        
                   3101:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   3102:        
                   3103:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     3104:        text_vram_end_address = text_vram_top_address + regen;
                   3105:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   3106:        
1.1.1.14  root     3107:        if(regen > 0x4000) {
                   3108:                regen = 0x8000;
                   3109:                vram_pages = 1;
                   3110:        } else if(regen > 0x2000) {
                   3111:                regen = 0x4000;
                   3112:                vram_pages = 2;
                   3113:        } else if(regen > 0x1000) {
                   3114:                regen = 0x2000;
                   3115:                vram_pages = 4;
                   3116:        } else {
                   3117:                regen = 0x1000;
                   3118:                vram_pages = 8;
                   3119:        }
1.1.1.15  root     3120:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   3121:        *(UINT16 *)(mem + 0x44c) = regen;
                   3122:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   3123:        
1.1.1.24  root     3124:        mouse.min_position.x = 0;
                   3125:        mouse.min_position.y = 0;
1.1.1.34  root     3126:        mouse.max_position.x = 8 * (scr_width  - 1);
                   3127:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     3128:        
1.1.1.15  root     3129:        restore_console_on_exit = true;
1.1.1.14  root     3130: }
                   3131: 
                   3132: void clear_scr_buffer(WORD attr)
                   3133: {
                   3134:        for(int y = 0; y < scr_height; y++) {
                   3135:                for(int x = 0; x < scr_width; x++) {
                   3136:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3137:                        SCR_BUF(y,x).Attributes = attr;
                   3138:                }
                   3139:        }
1.1.1.12  root     3140: }
                   3141: 
1.1.1.24  root     3142: bool update_console_input()
1.1       root     3143: {
1.1.1.35  root     3144: #ifdef USE_SERVICE_THREAD
                   3145:        EnterCriticalSection(&input_crit_sect);
                   3146: #endif
1.1.1.23  root     3147:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     3148:        DWORD dwNumberOfEvents = 0;
1.1       root     3149:        DWORD dwRead;
                   3150:        INPUT_RECORD ir[16];
1.1.1.24  root     3151:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   3152:        bool result = false;
1.1       root     3153:        
1.1.1.8   root     3154:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   3155:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   3156:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     3157:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.34  root     3158:                                        if(mouse.hidden == 0) {
                   3159:                                                // NOTE: if restore_console_on_exit, console is not scrolled
                   3160:                                                if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   3161:                                                        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   3162:                                                }
                   3163:                                                // FIXME: character size is always 8x8 ???
                   3164:                                                int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   3165:                                                int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
                   3166:                                                
                   3167:                                                if(mouse.position.x != x || mouse.position.y != y) {
                   3168:                                                        mouse.position.x = x;
                   3169:                                                        mouse.position.y = y;
                   3170:                                                        mouse.status |= 1;
1.1.1.43  root     3171:                                                        mouse.status_alt |= 1;
1.1.1.34  root     3172:                                                }
                   3173:                                        }
                   3174:                                        if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   3175:                                                for(int i = 0; i < MAX_MOUSE_BUTTONS; i++) {
                   3176:                                                        static const DWORD bits[] = {
                   3177:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   3178:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
                   3179:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
                   3180:                                                        };
                   3181:                                                        bool prev_status = mouse.buttons[i].status;
                   3182:                                                        mouse.buttons[i].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[i]) != 0);
                   3183:                                                        
                   3184:                                                        if(!prev_status && mouse.buttons[i].status) {
                   3185:                                                                mouse.buttons[i].pressed_times++;
                   3186:                                                                mouse.buttons[i].pressed_position.x = mouse.position.x;
                   3187:                                                                mouse.buttons[i].pressed_position.y = mouse.position.x;
1.1.1.43  root     3188:                                                                if(i < 2) {
                   3189:                                                                        mouse.status_alt |= 2 << (i * 2);
                   3190:                                                                }
1.1.1.34  root     3191:                                                                mouse.status |= 2 << (i * 2);
                   3192:                                                        } else if(prev_status && !mouse.buttons[i].status) {
                   3193:                                                                mouse.buttons[i].released_times++;
                   3194:                                                                mouse.buttons[i].released_position.x = mouse.position.x;
                   3195:                                                                mouse.buttons[i].released_position.y = mouse.position.x;
1.1.1.43  root     3196:                                                                if(i < 2) {
                   3197:                                                                        mouse.status_alt |= 4 << (i * 2);
                   3198:                                                                }
1.1.1.34  root     3199:                                                                mouse.status |= 4 << (i * 2);
1.1.1.14  root     3200:                                                        }
                   3201:                                                }
                   3202:                                        }
1.1.1.24  root     3203:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3204:                                        // update keyboard flags in bios data area
1.1.1.35  root     3205:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
                   3206:                                                mem[0x417] |= 0x40;
1.1.1.33  root     3207:                                        } else {
1.1.1.35  root     3208:                                                mem[0x417] &= ~0x40;
1.1.1.33  root     3209:                                        }
1.1.1.35  root     3210:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
                   3211:                                                mem[0x417] |= 0x20;
1.1.1.33  root     3212:                                        } else {
1.1.1.35  root     3213:                                                mem[0x417] &= ~0x20;
                   3214:                                        }
                   3215:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
                   3216:                                                mem[0x417] |= 0x10;
                   3217:                                        } else {
                   3218:                                                mem[0x417] &= ~0x10;
1.1.1.33  root     3219:                                        }
                   3220:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43  root     3221:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3222:                                                        mouse.status_alt |= 0x80;
                   3223:                                                }
1.1.1.33  root     3224:                                                mem[0x417] |= 0x08;
                   3225:                                        } else {
                   3226:                                                mem[0x417] &= ~0x08;
                   3227:                                        }
1.1.1.35  root     3228:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43  root     3229:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3230:                                                        mouse.status_alt |= 0x40;
                   3231:                                                }
1.1.1.35  root     3232:                                                mem[0x417] |= 0x04;
1.1.1.33  root     3233:                                        } else {
1.1.1.35  root     3234:                                                mem[0x417] &= ~0x04;
1.1.1.33  root     3235:                                        }
                   3236:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43  root     3237:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3238:                                                        mouse.status_alt |= 0x20;
                   3239:                                                }
1.1.1.33  root     3240:                                                if(!(mem[0x417] & 0x03)) {
                   3241:                                                        mem[0x417] |= 0x02; // left shift
                   3242:                                                }
                   3243:                                        } else {
                   3244:                                                mem[0x417] &= ~0x03;
                   3245:                                        }
1.1.1.35  root     3246:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3247:                                                mem[0x418] |= 0x02;
                   3248:                                        } else {
                   3249:                                                mem[0x418] &= ~0x02;
                   3250:                                        }
                   3251:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3252:                                                mem[0x418] |= 0x01;
                   3253:                                        } else {
                   3254:                                                mem[0x418] &= ~0x01;
                   3255:                                        }
1.1.1.33  root     3256:                                        
1.1.1.28  root     3257:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.57! root     3258: //                                     kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
        !          3259: //                                     kbd_status |= 1;
        !          3260:                                        UINT8 tmp_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3261:                                        
                   3262:                                        // update dos key buffer
                   3263:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3264:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
1.1.1.51  root     3265:                                        UINT8 scn_old = scn;
1.1.1.33  root     3266:                                        
                   3267:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3268:                                                // make
1.1.1.57! root     3269:                                                tmp_data &= 0x7f;
1.1.1.24  root     3270:                                                
1.1.1.33  root     3271:                                                if(chr == 0x00) {
1.1.1.24  root     3272:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3273:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3274:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3275:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3276:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3277:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3278:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3279:                                                                } else if(scn == 0x35) {
                   3280:                                                                        scn = 0xa4;             // keypad /
                   3281:                                                                }
                   3282:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3283:                                                                if(scn == 0x07) {
                   3284:                                                                        chr = 0x1e;     // Ctrl+^
                   3285:                                                                } else if(scn == 0x0c) {
                   3286:                                                                        chr = 0x1f;     // Ctrl+_
                   3287:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3288:                                                                        static const UINT8 ctrl_map[] = {
                   3289:                                                                                0x95,   // keypad /
                   3290:                                                                                0,
                   3291:                                                                                0x96,   // keypad *
                   3292:                                                                                0, 0, 0,
                   3293:                                                                                0x5e,   // F1
                   3294:                                                                                0x5f,   // F2
                   3295:                                                                                0x60,   // F3
                   3296:                                                                                0x61,   // F4
                   3297:                                                                                0x62,   // F5
                   3298:                                                                                0x63,   // F6
                   3299:                                                                                0x64,   // F7
                   3300:                                                                                0x65,   // F8
                   3301:                                                                                0x66,   // F9
                   3302:                                                                                0x67,   // F10
                   3303:                                                                                0,
                   3304:                                                                                0,
                   3305:                                                                                0x77,   // Home
                   3306:                                                                                0x8d,   // Up
                   3307:                                                                                0x84,   // PgUp
                   3308:                                                                                0x8e,   // keypad -
                   3309:                                                                                0x73,   // Left
                   3310:                                                                                0x8f,   // keypad center
                   3311:                                                                                0x74,   // Right
                   3312:                                                                                0x90,   // keyapd +
                   3313:                                                                                0x75,   // End
                   3314:                                                                                0x91,   // Down
                   3315:                                                                                0x76,   // PgDn
                   3316:                                                                                0x92,   // Insert
                   3317:                                                                                0x93,   // Delete
                   3318:                                                                                0, 0, 0,
                   3319:                                                                                0x89,   // F11
                   3320:                                                                                0x8a,   // F12
                   3321:                                                                        };
                   3322:                                                                        scn = ctrl_map[scn - 0x35];
                   3323:                                                                }
                   3324:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3325:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3326:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3327:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3328:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3329:                                                                }
                   3330:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3331:                                                                scn += 0x85 - 0x57;
                   3332:                                                        }
                   3333:                                                        // ignore shift, ctrl, alt, win and menu keys
1.1.1.51  root     3334:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && !(scn >= 0x5b && scn <= 0x5d && scn == scn_old)) {
1.1.1.32  root     3335:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3336: #ifdef USE_SERVICE_THREAD
                   3337:                                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3338: #endif
1.1.1.32  root     3339:                                                                        if(chr == 0) {
1.1.1.51  root     3340:                                                                                pcbios_set_key_buffer(0x00, ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
1.1.1.32  root     3341:                                                                        }
1.1.1.51  root     3342:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3343: #ifdef USE_SERVICE_THREAD
                   3344:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3345: #endif
1.1.1.24  root     3346:                                                                }
                   3347:                                                        }
                   3348:                                                } else {
                   3349:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3350:                                                                chr = 0;
                   3351:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3352:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3353:                                                                }
                   3354:                                                        }
1.1.1.32  root     3355:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3356: #ifdef USE_SERVICE_THREAD
                   3357:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3358: #endif
1.1.1.51  root     3359:                                                                pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3360: #ifdef USE_SERVICE_THREAD
                   3361:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3362: #endif
1.1.1.32  root     3363:                                                        }
1.1.1.24  root     3364:                                                }
1.1.1.57! root     3365:                                        } else {
        !          3366:                                                if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
        !          3367:                                                        // ctrl-break, ctrl-c
        !          3368:                                                        if(scn == 0x46) {
        !          3369:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3370: #ifdef USE_SERVICE_THREAD
1.1.1.57! root     3371:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3372: #endif
1.1.1.57! root     3373:                                                                        pcbios_set_key_buffer(0x00, 0x00);
1.1.1.35  root     3374: #ifdef USE_SERVICE_THREAD
1.1.1.57! root     3375:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3376: #endif
1.1.1.57! root     3377:                                                                }
        !          3378:                                                                ctrl_break_pressed = true;
        !          3379:                                                                mem[0x471] = 0x80;
        !          3380:                                                                raise_int_1bh = true;
        !          3381:                                                        } else {
        !          3382:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3383: #ifdef USE_SERVICE_THREAD
1.1.1.57! root     3384:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3385: #endif
1.1.1.57! root     3386:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3387: #ifdef USE_SERVICE_THREAD
1.1.1.57! root     3388:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3389: #endif
1.1.1.57! root     3390:                                                                }
        !          3391:                                                                ctrl_c_pressed = (scn == 0x2e);
1.1.1.33  root     3392:                                                        }
                   3393:                                                }
                   3394:                                                // break
1.1.1.57! root     3395:                                                tmp_data |= 0x80;
        !          3396:                                        }
        !          3397:                                        if(!(kbd_status & 1)) {
        !          3398:                                                kbd_data = tmp_data;
        !          3399:                                                kbd_status |= 1;
        !          3400:                                        } else {
        !          3401:                                                if(key_buf_data != NULL) {
        !          3402: #ifdef USE_SERVICE_THREAD
        !          3403:                                                        EnterCriticalSection(&key_buf_crit_sect);
        !          3404: #endif
        !          3405:                                                        key_buf_data->write(tmp_data);
        !          3406: #ifdef USE_SERVICE_THREAD
        !          3407:                                                        LeaveCriticalSection(&key_buf_crit_sect);
        !          3408: #endif
        !          3409:                                                }
1.1       root     3410:                                        }
1.1.1.24  root     3411:                                        result = key_changed = true;
1.1.1.36  root     3412:                                        // IME may be on and it may causes screen scroll up and cursor position change
                   3413:                                        cursor_moved = true;
1.1       root     3414:                                }
                   3415:                        }
                   3416:                }
                   3417:        }
1.1.1.35  root     3418: #ifdef USE_SERVICE_THREAD
                   3419:        LeaveCriticalSection(&input_crit_sect);
                   3420: #endif
1.1.1.24  root     3421:        return(result);
1.1.1.8   root     3422: }
                   3423: 
1.1.1.14  root     3424: bool update_key_buffer()
1.1.1.8   root     3425: {
1.1.1.35  root     3426:        if(update_console_input()) {
                   3427:                return(true);
                   3428:        }
                   3429:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3430: #ifdef USE_SERVICE_THREAD
                   3431:                EnterCriticalSection(&key_buf_crit_sect);
                   3432: #endif
1.1.1.55  root     3433:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     3434: #ifdef USE_SERVICE_THREAD
                   3435:                LeaveCriticalSection(&key_buf_crit_sect);
                   3436: #endif
                   3437:                if(!empty) return(true);
                   3438:        }
                   3439:        return(false);
1.1.1.8   root     3440: }
                   3441: 
1.1.1.20  root     3442: /* ----------------------------------------------------------------------------
                   3443:        MS-DOS virtual machine
                   3444: ---------------------------------------------------------------------------- */
                   3445: 
1.1.1.32  root     3446: static const struct {
1.1.1.33  root     3447:        char *name;
                   3448:        DWORD lcid;
                   3449:        char *std;
                   3450:        char *dlt;
                   3451: } tz_table[] = {
                   3452:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3453: //     0       GMT             Greenwich Mean Time             GMT0
                   3454:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3455:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3456:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3457:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3458: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3459:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3460:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3461: //     3       BST             Brazil Standard Time            BST3
                   3462:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3463:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3464:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3465: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3466:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3467: //     3       GST             Greenland Standard Time         GST3
                   3468:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3469: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3470:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3471: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3472:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3473: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3474:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3475:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3476: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3477:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3478:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3479:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3480: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3481:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3482: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3483:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3484: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3485:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3486: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3487:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3488:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3489:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3490: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3491:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3492: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3493:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3494:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3495:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3496: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3497:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3498:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3499: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3500: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3501:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3502: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3503:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3504:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3505: //     11      SST             Samoa Standard Time             SST11
                   3506:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3507: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3508:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3509: //     -10     GST             Guam Standard Time              GST-10
                   3510:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3511: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3512:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3513:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3514:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3515: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3516:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3517:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3518: //     -9      JST             Japan Standard Time             JST-9
                   3519:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3520: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3521:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3522:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3523: //     -8      HKT             Hong Kong Time                  HKT-8
                   3524:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3525: //     -8      CCT             China Coast Time                CCT-8
                   3526:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3527:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3528: //     -8      SST             Singapore Standard Time         SST-8
                   3529:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3530: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3531:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3532:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3533: //     -7:30   JT              Java Standard Time              JST-7:30
                   3534: //     -7      NST             North Sumatra Time              NST-7
                   3535:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3536: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3537:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3538: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3539:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3540: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3541:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3542:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3543: //     -2      EET             Eastern Europe Time             EET-2
                   3544:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3545:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3546:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3547:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3548: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3549:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3550: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3551: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3552: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3553: //     -1      CET     CES     Central European Time           CET-1CES
                   3554:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3555:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3556:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3557:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3558: //     -1      WAT             West African Time               WAT-1
                   3559:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3560:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3561: //     0       UTC             Universal Coordinated Time      UTC0
                   3562:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3563:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3564:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3565:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3566:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3567:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3568: };
                   3569: 
1.1.1.53  root     3570: // FIXME: consider to build on non-Japanese environment :-(
                   3571: // message_japanese string must be in shift-jis
                   3572: 
1.1.1.33  root     3573: static const struct {
1.1.1.32  root     3574:        UINT16 code;
                   3575:        char *message_english;
                   3576:        char *message_japanese;
                   3577: } standard_error_table[] = {
                   3578:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3579:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3580:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3581:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3582:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3583:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3584:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3585:        {0x08,  "Insufficient memory", "������������܂���."},
                   3586:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3587:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3588:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3589:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3590:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3591:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3592:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3593:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3594:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3595:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3596:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3597:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3598:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3599:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3600:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3601:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3602:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3603:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3604:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3605:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3606:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3607:        {0x1F,  "General failure", "�G���[�ł�."},
                   3608:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3609:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3610:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3611:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3612:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3613:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3614:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3615:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3616: /*
                   3617:        {0x32,  "Network request not supported", NULL},
                   3618:        {0x33,  "Remote computer not listening", NULL},
                   3619:        {0x34,  "Duplicate name on network", NULL},
                   3620:        {0x35,  "Network name not found", NULL},
                   3621:        {0x36,  "Network busy", NULL},
                   3622:        {0x37,  "Network device no longer exists", NULL},
                   3623:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3624:        {0x39,  "Network adapter hardware error", NULL},
                   3625:        {0x3A,  "Incorrect response from network", NULL},
                   3626:        {0x3B,  "Unexpected network error", NULL},
                   3627:        {0x3C,  "Incompatible remote adapter", NULL},
                   3628:        {0x3D,  "Print queue full", NULL},
                   3629:        {0x3E,  "Queue not full", NULL},
                   3630:        {0x3F,  "Not enough space to print file", NULL},
                   3631:        {0x40,  "Network name was deleted", NULL},
                   3632:        {0x41,  "Network: Access denied", NULL},
                   3633:        {0x42,  "Network device type incorrect", NULL},
                   3634:        {0x43,  "Network name not found", NULL},
                   3635:        {0x44,  "Network name limit exceeded", NULL},
                   3636:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3637:        {0x46,  "Temporarily paused", NULL},
                   3638:        {0x47,  "Network request not accepted", NULL},
                   3639:        {0x48,  "Network print/disk redirection paused", NULL},
                   3640:        {0x49,  "Network software not installed", NULL},
                   3641:        {0x4A,  "Unexpected adapter close", NULL},
                   3642: */
                   3643:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3644:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3645:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3646:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3647:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3648:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3649:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3650:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3651:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3652:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
1.1.1.53  root     3653: #ifdef SUPPORT_MSCDEX
1.1.1.32  root     3654:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3655:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3656:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3657:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3658:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
1.1.1.53  root     3659: #endif
1.1.1.32  root     3660:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3661:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3662:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3663:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3664:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3665:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3666: };
                   3667: 
                   3668: static const struct {
                   3669:        UINT16 code;
                   3670:        char *message_english;
                   3671:        char *message_japanese;
                   3672: } param_error_table[] = {
                   3673:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3674:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3675:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3676:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3677:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3678:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3679:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3680:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3681:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3682:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3683:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3684: };
                   3685: 
                   3686: static const struct {
                   3687:        UINT16 code;
                   3688:        char *message_english;
                   3689:        char *message_japanese;
                   3690: } critical_error_table[] = {
                   3691:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3692:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3693:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3694:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3695:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3696:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3697:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3698:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3699:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3700:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3701:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3702:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3703:        {0x0C,  "General failure", "�G���[�ł�."},
                   3704:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3705:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3706:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3707:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3708:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3709:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3710:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3711:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3712:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3713: };
                   3714: 
1.1.1.20  root     3715: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3716: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3717: void msdos_putch(UINT8 data);
1.1.1.50  root     3718: void msdos_putch_fast(UINT8 data);
1.1.1.35  root     3719: #ifdef USE_SERVICE_THREAD
                   3720: void msdos_putch_tmp(UINT8 data);
                   3721: #endif
1.1.1.45  root     3722: const char *msdos_short_path(const char *path);
1.1.1.44  root     3723: bool msdos_is_valid_drive(int drv);
                   3724: bool msdos_is_removable_drive(int drv);
                   3725: bool msdos_is_cdrom_drive(int drv);
                   3726: bool msdos_is_remote_drive(int drv);
                   3727: bool msdos_is_subst_drive(int drv);
1.1.1.20  root     3728: 
1.1       root     3729: // process info
                   3730: 
                   3731: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3732: {
                   3733:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3734:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3735:                        memset(&process[i], 0, sizeof(process_t));
                   3736:                        process[i].psp = psp_seg;
                   3737:                        return(&process[i]);
                   3738:                }
                   3739:        }
                   3740:        fatalerror("too many processes\n");
                   3741:        return(NULL);
                   3742: }
                   3743: 
1.1.1.52  root     3744: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error = true)
1.1       root     3745: {
                   3746:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3747:                if(process[i].psp == psp_seg) {
                   3748:                        return(&process[i]);
                   3749:                }
                   3750:        }
1.1.1.33  root     3751:        if(show_error) {
                   3752:                fatalerror("invalid psp address\n");
                   3753:        }
1.1       root     3754:        return(NULL);
                   3755: }
                   3756: 
1.1.1.23  root     3757: void msdos_sda_update(int psp_seg)
                   3758: {
                   3759:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3760:        
                   3761:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3762:                if(process[i].psp == psp_seg) {
                   3763:                        sda->switchar = process[i].switchar;
                   3764:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3765:                        sda->current_dta.w.h = process[i].dta.w.h;
                   3766:                        sda->current_psp = process[i].psp;
                   3767:                        break;
                   3768:                }
                   3769:        }
                   3770:        sda->malloc_strategy = malloc_strategy;
                   3771:        sda->return_code = retval;
                   3772:        sda->current_drive = _getdrive();
                   3773: }
                   3774: 
1.1.1.13  root     3775: // dta info
                   3776: 
                   3777: void msdos_dta_info_init()
                   3778: {
1.1.1.14  root     3779:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     3780:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3781:        }
                   3782: }
                   3783: 
                   3784: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   3785: {
                   3786:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     3787:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3788:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   3789:                        if(free_dta == NULL) {
1.1.1.13  root     3790:                                free_dta = &dtalist[i];
                   3791:                        }
1.1.1.14  root     3792:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     3793:                        return(&dtalist[i]);
                   3794:                }
                   3795:        }
1.1.1.14  root     3796:        if(free_dta) {
1.1.1.13  root     3797:                free_dta->psp = psp_seg;
                   3798:                free_dta->dta = dta_laddr;
                   3799:                return(free_dta);
                   3800:        }
                   3801:        fatalerror("too many dta\n");
                   3802:        return(NULL);
                   3803: }
                   3804: 
                   3805: void msdos_dta_info_free(UINT16 psp_seg)
                   3806: {
1.1.1.14  root     3807:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3808:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     3809:                        FindClose(dtalist[i].find_handle);
                   3810:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3811:                }
                   3812:        }
                   3813: }
                   3814: 
1.1       root     3815: void msdos_cds_update(int drv)
                   3816: {
1.1.1.44  root     3817:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1       root     3818:        
1.1.1.44  root     3819:        memset(cds, 0, 88);
                   3820:        
                   3821:        if(msdos_is_valid_drive(drv)) {
                   3822:                char path[MAX_PATH];
                   3823:                if(msdos_is_remote_drive(drv)) {
                   3824:                        cds->drive_attrib = 0xc000;     // network drive
                   3825:                } else if(msdos_is_subst_drive(drv)) {
                   3826:                        cds->drive_attrib = 0x5000;     // subst drive
                   3827:                } else {
                   3828:                        cds->drive_attrib = 0x4000;     // physical drive
                   3829:                }
                   3830:                if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
                   3831:                        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
                   3832:                }
                   3833:        }
                   3834:        if(cds->path_name[0] == '\0') {
                   3835:                sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   3836:        }
                   3837:        cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   3838:        cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
                   3839:        cds->word_1 = cds->word_2 = 0xffff;
                   3840:        cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
                   3841:        cds->bs_offset = 2;
                   3842: }
                   3843: 
1.1.1.45  root     3844: void msdos_cds_update(int drv, const char *path)
1.1.1.44  root     3845: {
                   3846:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
                   3847:        
                   3848:        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1       root     3849: }
                   3850: 
1.1.1.17  root     3851: // nls information tables
                   3852: 
                   3853: // uppercase table (func 6502h)
                   3854: void msdos_upper_table_update()
                   3855: {
                   3856:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3857:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3858:                UINT8 c[4];
1.1.1.33  root     3859:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3860:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     3861:                c[0] = 0x80 + i;
                   3862:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   3863:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3864:        }
                   3865: }
                   3866: 
1.1.1.23  root     3867: // lowercase table (func 6503h)
                   3868: void msdos_lower_table_update()
                   3869: {
                   3870:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   3871:        for(unsigned i = 0; i < 0x80; ++i) {
                   3872:                UINT8 c[4];
1.1.1.33  root     3873:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3874:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     3875:                c[0] = 0x80 + i;
                   3876:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   3877:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3878:        }
                   3879: }
                   3880: 
1.1.1.17  root     3881: // filename uppercase table (func 6504h)
                   3882: void msdos_filename_upper_table_init()
                   3883: {
                   3884:        // depended on (file)system, not on active codepage
                   3885:        // temporary solution: just filling data
                   3886:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3887:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3888:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   3889:        }
                   3890: }
                   3891: 
                   3892: // filaname terminator table (func 6505h)
                   3893: void msdos_filename_terminator_table_init()
                   3894: {
                   3895:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   3896:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   3897:        
                   3898:        data[2] = 1;            // marker? (permissible character value)
                   3899:        data[3] = 0x00;         // 00h...FFh
                   3900:        data[4] = 0xff;
                   3901:        data[5] = 0;            // marker? (excluded character)
                   3902:        data[6] = 0x00;         // 00h...20h
                   3903:        data[7] = 0x20;
                   3904:        data[8] = 2;            // marker? (illegal characters for filename)
                   3905:        data[9] = (UINT8)strlen(illegal_chars);
                   3906:        memcpy(data + 10, illegal_chars, data[9]);
                   3907:        
                   3908:        // total length
                   3909:        *(UINT16 *)data = (10 - 2) + data[9];
                   3910: }
                   3911: 
                   3912: // collating table (func 6506h)
                   3913: void msdos_collating_table_update()
                   3914: {
                   3915:        // temporary solution: just filling data
                   3916:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     3917:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     3918:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   3919:        }
                   3920: }
                   3921: 
1.1       root     3922: // dbcs
                   3923: 
                   3924: void msdos_dbcs_table_update()
                   3925: {
                   3926:        UINT8 dbcs_data[DBCS_SIZE];
                   3927:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   3928:        
                   3929:        CPINFO info;
                   3930:        GetCPInfo(active_code_page, &info);
                   3931:        
                   3932:        if(info.MaxCharSize != 1) {
                   3933:                for(int i = 0;; i += 2) {
                   3934:                        UINT8 lo = info.LeadByte[i + 0];
                   3935:                        UINT8 hi = info.LeadByte[i + 1];
                   3936:                        dbcs_data[2 + i + 0] = lo;
                   3937:                        dbcs_data[2 + i + 1] = hi;
                   3938:                        if(lo == 0 && hi == 0) {
                   3939:                                dbcs_data[0] = i + 2;
                   3940:                                break;
                   3941:                        }
                   3942:                }
                   3943:        } else {
                   3944:                dbcs_data[0] = 2;       // ???
                   3945:        }
                   3946:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   3947: }
                   3948: 
1.1.1.17  root     3949: void msdos_dbcs_table_finish()
                   3950: {
1.1.1.32  root     3951:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     3952:                _setmbcp(system_code_page);
                   3953:        }
1.1.1.32  root     3954:        if(console_code_page != GetConsoleCP()) {
                   3955:                SetConsoleCP(console_code_page);
                   3956:                SetConsoleOutputCP(console_code_page);
                   3957:        }
1.1.1.17  root     3958: }
                   3959: 
                   3960: void msdos_nls_tables_init()
1.1       root     3961: {
1.1.1.32  root     3962:        active_code_page = console_code_page = GetConsoleCP();
                   3963:        system_code_page = _getmbcp();
                   3964:        
                   3965:        if(active_code_page != system_code_page) {
                   3966:                if(_setmbcp(active_code_page) != 0) {
                   3967:                        active_code_page = system_code_page;
                   3968:                }
                   3969:        }
                   3970:        
1.1.1.17  root     3971:        msdos_upper_table_update();
1.1.1.23  root     3972:        msdos_lower_table_update();
1.1.1.17  root     3973:        msdos_filename_terminator_table_init();
                   3974:        msdos_filename_upper_table_init();
                   3975:        msdos_collating_table_update();
1.1       root     3976:        msdos_dbcs_table_update();
                   3977: }
                   3978: 
1.1.1.17  root     3979: void msdos_nls_tables_update()
1.1       root     3980: {
1.1.1.17  root     3981:        msdos_dbcs_table_update();
                   3982:        msdos_upper_table_update();
1.1.1.23  root     3983:        msdos_lower_table_update();
                   3984: //     msdos_collating_table_update();
1.1       root     3985: }
                   3986: 
                   3987: int msdos_lead_byte_check(UINT8 code)
                   3988: {
                   3989:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   3990:        
                   3991:        for(int i = 0;; i += 2) {
                   3992:                UINT8 lo = dbcs_table[i + 0];
                   3993:                UINT8 hi = dbcs_table[i + 1];
                   3994:                if(lo == 0 && hi == 0) {
                   3995:                        break;
                   3996:                }
                   3997:                if(lo <= code && code <= hi) {
                   3998:                        return(1);
                   3999:                }
                   4000:        }
                   4001:        return(0);
                   4002: }
                   4003: 
1.1.1.20  root     4004: int msdos_ctrl_code_check(UINT8 code)
                   4005: {
1.1.1.22  root     4006:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     4007: }
                   4008: 
1.1.1.36  root     4009: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
                   4010: {
                   4011:        int is_kanji_1st = 0;
                   4012:        int is_kanji_2nd = 0;
                   4013:        
                   4014:        for(int p = 0;; p++) {
                   4015:                if(is_kanji_1st) {
                   4016:                        is_kanji_1st = 0;
                   4017:                        is_kanji_2nd = 1;
                   4018:                } else if(msdos_lead_byte_check(buf[p])) {
                   4019:                        is_kanji_1st = 1;
                   4020:                }
                   4021:                if(p == n) {
                   4022:                        return(is_kanji_2nd);
                   4023:                }
                   4024:                is_kanji_2nd = 0;
                   4025:        }
                   4026: }
                   4027: 
1.1       root     4028: // file control
                   4029: 
1.1.1.45  root     4030: const char *msdos_remove_double_quote(const char *path)
1.1.1.14  root     4031: {
                   4032:        static char tmp[MAX_PATH];
                   4033:        
                   4034:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45  root     4035:                memset(tmp, 0, sizeof(tmp));
1.1.1.14  root     4036:                memcpy(tmp, path + 1, strlen(path) - 2);
                   4037:        } else {
                   4038:                strcpy(tmp, path);
                   4039:        }
                   4040:        return(tmp);
                   4041: }
                   4042: 
1.1.1.45  root     4043: const char *msdos_remove_end_separator(const char *path)
1.1.1.32  root     4044: {
                   4045:        static char tmp[MAX_PATH];
                   4046:        
                   4047:        strcpy(tmp, path);
1.1.1.45  root     4048:        
                   4049:        // for example "C:\" case, the end separator should not be removed
                   4050:        if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
                   4051:                tmp[strlen(tmp) - 1] = '\0';
1.1.1.32  root     4052:        }
                   4053:        return(tmp);
                   4054: }
                   4055: 
1.1.1.45  root     4056: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14  root     4057: {
                   4058:        static char tmp[MAX_PATH];
1.1.1.45  root     4059:        const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14  root     4060:        
                   4061:        if(strlen(tmp_dir) == 0) {
                   4062:                strcpy(tmp, file);
                   4063:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   4064:                sprintf(tmp, "%s%s", tmp_dir, file);
                   4065:        } else {
                   4066:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   4067:        }
                   4068:        return(tmp);
                   4069: }
                   4070: 
1.1.1.45  root     4071: const char *msdos_trimmed_path(const char *path, int lfn)
1.1       root     4072: {
                   4073:        static char tmp[MAX_PATH];
                   4074:        
                   4075:        if(lfn) {
                   4076:                strcpy(tmp, path);
                   4077:        } else {
                   4078:                // remove space in the path
1.1.1.45  root     4079:                const char *src = path;
                   4080:                char *dst = tmp;
1.1       root     4081:                
                   4082:                while(*src != '\0') {
                   4083:                        if(msdos_lead_byte_check(*src)) {
                   4084:                                *dst++ = *src++;
                   4085:                                *dst++ = *src++;
                   4086:                        } else if(*src != ' ') {
                   4087:                                *dst++ = *src++;
                   4088:                        } else {
                   4089:                                src++;  // skip space
                   4090:                        }
                   4091:                }
                   4092:                *dst = '\0';
                   4093:        }
1.1.1.14  root     4094:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   4095:                // redirect C:\COMMAND.COM to comspec_path
                   4096:                strcpy(tmp, comspec_path);
                   4097:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   4098:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   4099:                static int root_drive_protected = -1;
                   4100:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   4101:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   4102:                
                   4103:                if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
                   4104:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   4105:                        strcpy(name, name_temp);
                   4106:                        name_temp[0] = '\0';
                   4107:                        
                   4108:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   4109:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   4110:                                if(root_drive_protected == -1) {
                   4111:                                        FILE *fp = NULL;
                   4112:                                        
                   4113:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   4114:                                        root_drive_protected = 1;
                   4115:                                        try {
                   4116:                                                if((fp = fopen(temp, "w")) != NULL) {
                   4117:                                                        if(fprintf(fp, "TEST") == 4) {
                   4118:                                                                root_drive_protected = 0;
                   4119:                                                        }
                   4120:                                                }
                   4121:                                        } catch(...) {
                   4122:                                        }
                   4123:                                        if(fp != NULL) {
                   4124:                                                fclose(fp);
                   4125:                                        }
                   4126:                                        if(_access(temp, 0) == 0) {
                   4127:                                                remove(temp);
                   4128:                                        }
                   4129:                                }
                   4130:                                if(root_drive_protected == 1) {
                   4131:                                        if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
                   4132:                                           GetEnvironmentVariable("TMP",  temp, MAX_PATH) != 0) {
                   4133:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   4134:                                        }
                   4135:                                }
                   4136:                        }
                   4137:                }
                   4138:        }
1.1       root     4139:        return(tmp);
                   4140: }
                   4141: 
1.1.1.45  root     4142: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28  root     4143: {
1.1.1.32  root     4144:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     4145:        static char env_path[ENV_SIZE];
                   4146:        char tmp[ENV_SIZE], *token;
                   4147:        
                   4148:        memset(env_path, 0, sizeof(env_path));
                   4149:        strcpy(tmp, src);
                   4150:        token = my_strtok(tmp, ";");
                   4151:        
                   4152:        while(token != NULL) {
                   4153:                if(token[0] != '\0') {
1.1.1.45  root     4154:                        const char *path = msdos_remove_double_quote(token);
                   4155:                        char short_path[MAX_PATH];
1.1.1.32  root     4156:                        if(path != NULL && strlen(path) != 0) {
                   4157:                                if(env_path[0] != '\0') {
                   4158:                                        strcat(env_path, ";");
                   4159:                                }
1.1.1.28  root     4160:                                if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     4161:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     4162:                                } else {
                   4163:                                        my_strupr(short_path);
1.1.1.32  root     4164:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     4165:                                }
                   4166:                        }
                   4167:                }
                   4168:                token = my_strtok(NULL, ";");
                   4169:        }
                   4170:        return(env_path);
                   4171: }
                   4172: 
1.1.1.45  root     4173: bool match(const char *text, const char *pattern)
1.1       root     4174: {
1.1.1.24  root     4175:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     4176:        switch(*pattern) {
1.1       root     4177:        case '\0':
                   4178:                return !*text;
                   4179:        case '*':
1.1.1.14  root     4180:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     4181:        case '?':
                   4182:                return *text && match(text + 1, pattern + 1);
                   4183:        default:
                   4184:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   4185:        }
                   4186: }
                   4187: 
1.1.1.45  root     4188: bool msdos_match_volume_label(const char *path, const char *volume)
1.1       root     4189: {
1.1.1.45  root     4190:        const char *p = NULL;
1.1       root     4191:        
1.1.1.14  root     4192:        if(!*volume) {
                   4193:                return false;
                   4194:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     4195:                return msdos_match_volume_label(p + 1, volume);
                   4196:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   4197:                return msdos_match_volume_label(p + 1, volume);
                   4198:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     4199:                char tmp[MAX_PATH];
                   4200:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   4201:                return match(volume, tmp);
1.1       root     4202:        } else {
                   4203:                return match(volume, path);
                   4204:        }
                   4205: }
                   4206: 
1.1.1.45  root     4207: const char *msdos_fcb_path(fcb_t *fcb)
1.1       root     4208: {
                   4209:        static char tmp[MAX_PATH];
                   4210:        char name[9], ext[4];
                   4211:        
                   4212:        memset(name, 0, sizeof(name));
                   4213:        memcpy(name, fcb->file_name, 8);
                   4214:        strcpy(name, msdos_trimmed_path(name, 0));
                   4215:        
                   4216:        memset(ext, 0, sizeof(ext));
                   4217:        memcpy(ext, fcb->file_name + 8, 3);
                   4218:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4219:        
                   4220:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4221:                strcpy(name, "*");
                   4222:        }
                   4223:        if(ext[0] == '\0') {
                   4224:                strcpy(tmp, name);
                   4225:        } else {
                   4226:                if(strcmp(ext, "???") == 0) {
                   4227:                        strcpy(ext, "*");
                   4228:                }
                   4229:                sprintf(tmp, "%s.%s", name, ext);
                   4230:        }
                   4231:        return(tmp);
                   4232: }
                   4233: 
1.1.1.45  root     4234: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1       root     4235: {
                   4236:        char *ext = my_strchr(path, '.');
                   4237:        
                   4238:        memset(fcb->file_name, 0x20, 8 + 3);
                   4239:        if(ext != NULL && path[0] != '.') {
                   4240:                *ext = '\0';
                   4241:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4242:        }
                   4243:        memcpy(fcb->file_name, path, strlen(path));
                   4244: }
                   4245: 
1.1.1.45  root     4246: const char *msdos_short_path(const char *path)
1.1       root     4247: {
                   4248:        static char tmp[MAX_PATH];
                   4249:        
1.1.1.24  root     4250:        if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
                   4251:                strcpy(tmp, path);
                   4252:        }
1.1       root     4253:        my_strupr(tmp);
                   4254:        return(tmp);
                   4255: }
                   4256: 
1.1.1.45  root     4257: const char *msdos_short_name(WIN32_FIND_DATA *fd)
1.1.1.13  root     4258: {
                   4259:        static char tmp[MAX_PATH];
1.1.1.45  root     4260:        
1.1.1.14  root     4261:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4262:                strcpy(tmp, fd->cAlternateFileName);
                   4263:        } else {
                   4264:                strcpy(tmp, fd->cFileName);
                   4265:        }
                   4266:        my_strupr(tmp);
                   4267:        return(tmp);
                   4268: }
                   4269: 
1.1.1.45  root     4270: const char *msdos_short_full_path(const char *path)
1.1       root     4271: {
                   4272:        static char tmp[MAX_PATH];
                   4273:        char full[MAX_PATH], *name;
                   4274:        
1.1.1.14  root     4275:        // Full works with non-existent files, but Short does not
1.1       root     4276:        GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14  root     4277:        *tmp = '\0';
                   4278:        if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
                   4279:                name[-1] = '\0';
                   4280:                DWORD len = GetShortPathName(full, tmp, MAX_PATH);
                   4281:                if(len == 0) {
                   4282:                        strcpy(tmp, full);
                   4283:                } else {
                   4284:                        tmp[len++] = '\\';
                   4285:                        strcpy(tmp + len, name);
                   4286:                }
                   4287:        }
1.1       root     4288:        my_strupr(tmp);
                   4289:        return(tmp);
                   4290: }
                   4291: 
1.1.1.45  root     4292: const char *msdos_short_full_dir(const char *path)
1.1       root     4293: {
                   4294:        static char tmp[MAX_PATH];
                   4295:        char full[MAX_PATH], *name;
                   4296:        
                   4297:        GetFullPathName(path, MAX_PATH, full, &name);
                   4298:        name[-1] = '\0';
1.1.1.24  root     4299:        if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
                   4300:                strcpy(tmp, full);
                   4301:        }
1.1       root     4302:        my_strupr(tmp);
                   4303:        return(tmp);
                   4304: }
                   4305: 
1.1.1.45  root     4306: const char *msdos_local_file_path(const char *path, int lfn)
1.1       root     4307: {
1.1.1.45  root     4308:        static char trimmed[MAX_PATH];
                   4309:        
                   4310:        strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14  root     4311: #if 0
                   4312:        // I have forgotten the reason of this routine... :-(
1.1       root     4313:        if(_access(trimmed, 0) != 0) {
                   4314:                process_t *process = msdos_process_info_get(current_psp);
                   4315:                static char tmp[MAX_PATH];
                   4316:                
                   4317:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4318:                if(_access(tmp, 0) == 0) {
                   4319:                        return(tmp);
                   4320:                }
                   4321:        }
1.1.1.14  root     4322: #endif
1.1       root     4323:        return(trimmed);
                   4324: }
                   4325: 
1.1.1.45  root     4326: bool msdos_is_device_path(const char *path)
1.1.1.11  root     4327: {
                   4328:        char full[MAX_PATH], *name;
                   4329:        
1.1.1.24  root     4330:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4331:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4332:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4333:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4334:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4335:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4336:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4337:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4338:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4339:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4340:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4341:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4342:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4343:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4344:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4345:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4346:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4347:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4348:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4349:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4350:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4351:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4352:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4353:                        return(true);
                   4354:                } else if(name != NULL) {
                   4355:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4356:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4357:                           _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43  root     4358: //                        _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30  root     4359:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4360:                                return(true);
                   4361:                        }
                   4362:                }
1.1.1.24  root     4363:        }
                   4364:        return(false);
1.1.1.11  root     4365: }
                   4366: 
1.1.1.45  root     4367: bool msdos_is_con_path(const char *path)
1.1.1.8   root     4368: {
1.1.1.14  root     4369:        char full[MAX_PATH], *name;
1.1.1.8   root     4370:        
1.1.1.24  root     4371:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4372:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4373:        }
                   4374:        return(false);
                   4375: }
                   4376: 
1.1.1.45  root     4377: int msdos_is_comm_path(const char *path)
1.1.1.24  root     4378: {
                   4379:        char full[MAX_PATH], *name;
                   4380:        
                   4381:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4382:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4383:                        return(1);
                   4384:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4385:                        return(2);
                   4386:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4387:                        return(3);
                   4388:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4389:                        return(4);
1.1.1.24  root     4390:                }
                   4391:        }
1.1.1.29  root     4392:        return(0);
                   4393: }
                   4394: 
1.1.1.45  root     4395: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37  root     4396: {
                   4397:        // 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     4398:        const char *p = NULL;
1.1.1.37  root     4399:        
                   4400:        if((p = strstr(path, ":")) != NULL) {
                   4401:                UINT8 selector = sio_read(sio_port - 1, 3);
                   4402:                
                   4403:                // baud rate
                   4404:                int baud = max(110, min(9600, atoi(p + 1)));
                   4405:                UINT16 divisor = 115200 / baud;
                   4406:                
                   4407:                if((p = strstr(p + 1, ",")) != NULL) {
                   4408:                        // parity
                   4409:                        if(p[1] == 'N' || p[1] == 'n') {
                   4410:                                selector = (selector & ~0x38) | 0x00;
                   4411:                        } else if(p[1] == 'O' || p[1] == 'o') {
                   4412:                                selector = (selector & ~0x38) | 0x08;
                   4413:                        } else if(p[1] == 'E' || p[1] == 'e') {
                   4414:                                selector = (selector & ~0x38) | 0x18;
                   4415:                        } else if(p[1] == 'M' || p[1] == 'm') {
                   4416:                                selector = (selector & ~0x38) | 0x28;
                   4417:                        } else if(p[1] == 'S' || p[1] == 's') {
                   4418:                                selector = (selector & ~0x38) | 0x38;
                   4419:                        }
                   4420:                        if((p = strstr(p + 1, ",")) != NULL) {
                   4421:                                // word length
                   4422:                                if(p[1] == '8') {
                   4423:                                        selector = (selector & ~0x03) | 0x03;
                   4424:                                } else if(p[1] == '7') {
                   4425:                                        selector = (selector & ~0x03) | 0x02;
                   4426:                                } else if(p[1] == '6') {
                   4427:                                        selector = (selector & ~0x03) | 0x01;
                   4428:                                } else if(p[1] == '5') {
                   4429:                                        selector = (selector & ~0x03) | 0x00;
                   4430:                                }
                   4431:                                if((p = strstr(p + 1, ",")) != NULL) {
                   4432:                                        // stop bits
                   4433:                                        float bits = atof(p + 1);
                   4434:                                        if(bits > 1.0F) {
                   4435:                                                selector |= 0x04;
                   4436:                                        } else {
                   4437:                                                selector &= ~0x04;
                   4438:                                        }
                   4439:                                }
                   4440:                        }
                   4441:                }
                   4442:                sio_write(sio_port - 1, 3, selector | 0x80);
                   4443:                sio_write(sio_port - 1, 0, divisor & 0xff);
                   4444:                sio_write(sio_port - 1, 1, divisor >> 8);
                   4445:                sio_write(sio_port - 1, 3, selector);
                   4446:        }
                   4447: }
                   4448: 
1.1.1.45  root     4449: int msdos_is_prn_path(const char *path)
1.1.1.30  root     4450: {
                   4451:        char full[MAX_PATH], *name;
                   4452:        
                   4453:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4454:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4455:                        return(1);
                   4456:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4457:                        return(1);
                   4458:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4459:                        return(2);
                   4460:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4461:                        return(3);
                   4462:                }
                   4463:        }
                   4464:        return(0);
                   4465: }
                   4466: 
1.1.1.44  root     4467: bool msdos_is_valid_drive(int drv)
                   4468: {
                   4469:        return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
                   4470: }
                   4471: 
                   4472: bool msdos_is_removable_drive(int drv)
                   4473: {
                   4474:        char volume[] = "A:\\";
                   4475:        
                   4476:        volume[0] = 'A' + drv;
                   4477:        
                   4478:        return(GetDriveType(volume) == DRIVE_REMOVABLE);
                   4479: }
                   4480: 
                   4481: bool msdos_is_cdrom_drive(int drv)
                   4482: {
                   4483:        char volume[] = "A:\\";
                   4484:        
                   4485:        volume[0] = 'A' + drv;
                   4486:        
                   4487:        return(GetDriveType(volume) == DRIVE_CDROM);
                   4488: }
                   4489: 
                   4490: bool msdos_is_remote_drive(int drv)
                   4491: {
                   4492:        char volume[] = "A:\\";
                   4493:        
                   4494:        volume[0] = 'A' + drv;
                   4495:        
                   4496:        return(GetDriveType(volume) == DRIVE_REMOTE);
                   4497: }
                   4498: 
                   4499: bool msdos_is_subst_drive(int drv)
                   4500: {
                   4501:        char device[] = "A:", path[MAX_PATH];
                   4502:        
                   4503:        device[0] = 'A' + drv;
                   4504:        
                   4505:        if(QueryDosDevice(device, path, MAX_PATH)) {
                   4506:                if(strncmp(path, "\\??\\", 4) == 0) {
                   4507:                        return(true);
                   4508:                }
                   4509:        }
                   4510:        return(false);
                   4511: }
                   4512: 
1.1.1.45  root     4513: bool msdos_is_existing_file(const char *path)
1.1.1.24  root     4514: {
                   4515:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   4516:        WIN32_FIND_DATA FindData;
                   4517:        HANDLE hFind;
                   4518:        
                   4519:        if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
                   4520:                FindClose(hFind);
                   4521:                return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
                   4522:        }
                   4523:        return(false);
1.1.1.8   root     4524: }
                   4525: 
1.1.1.45  root     4526: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9   root     4527: {
                   4528:        static char tmp[MAX_PATH];
1.1.1.28  root     4529:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4530:        
1.1.1.28  root     4531:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.9   root     4532:        if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
                   4533:                sprintf(file_name, "COMMAND.COM");
                   4534:                if(_access(tmp, 0) == 0) {
                   4535:                        return(tmp);
                   4536:                }
                   4537:        }
1.1.1.28  root     4538:        
                   4539:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.9   root     4540:        if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
                   4541:                sprintf(file_name, "COMMAND.COM");
                   4542:                if(_access(tmp, 0) == 0) {
                   4543:                        return(tmp);
                   4544:                }
                   4545:        }
1.1.1.28  root     4546:        
                   4547:        // check if COMMAND.COM is in the current directory
1.1.1.9   root     4548:        if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
                   4549:                if(_access(tmp, 0) == 0) {
                   4550:                        return(tmp);
                   4551:                }
                   4552:        }
1.1.1.28  root     4553:        
                   4554:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4555:        strcpy(path, env_path);
                   4556:        char *token = my_strtok(path, ";");
1.1.1.9   root     4557:        while(token != NULL) {
1.1.1.14  root     4558:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4559:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4560:                        if(_access(tmp, 0) == 0) {
                   4561:                                return(tmp);
                   4562:                        }
                   4563:                }
                   4564:                token = my_strtok(NULL, ";");
                   4565:        }
                   4566:        return(NULL);
                   4567: }
                   4568: 
1.1.1.14  root     4569: int msdos_drive_number(const char *path)
1.1       root     4570: {
                   4571:        char tmp[MAX_PATH], *name;
                   4572:        
1.1.1.45  root     4573:        if(GetFullPathName(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
                   4574:                if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4575:                        return(tmp[0] - 'a');
                   4576:                } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
                   4577:                        return(tmp[0] - 'A');
                   4578:                }
1.1       root     4579:        }
1.1.1.45  root     4580: //     return(msdos_drive_number("."));
                   4581:        return(_getdrive() - 1);
1.1       root     4582: }
                   4583: 
1.1.1.45  root     4584: const char *msdos_volume_label(const char *path)
1.1       root     4585: {
                   4586:        static char tmp[MAX_PATH];
                   4587:        char volume[] = "A:\\";
                   4588:        
                   4589:        if(path[1] == ':') {
                   4590:                volume[0] = path[0];
                   4591:        } else {
                   4592:                volume[0] = 'A' + _getdrive() - 1;
                   4593:        }
                   4594:        if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
                   4595:                memset(tmp, 0, sizeof(tmp));
                   4596:        }
                   4597:        return(tmp);
                   4598: }
                   4599: 
1.1.1.45  root     4600: const char *msdos_short_volume_label(const char *label)
1.1       root     4601: {
                   4602:        static char tmp[(8 + 1 + 3) + 1];
1.1.1.45  root     4603:        const char *src = label;
1.1       root     4604:        int remain = strlen(label);
                   4605:        char *dst_n = tmp;
                   4606:        char *dst_e = tmp + 9;
                   4607:        
                   4608:        strcpy(tmp, "        .   ");
                   4609:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4610:                if(msdos_lead_byte_check(*src)) {
                   4611:                        if(++i == 8) {
                   4612:                                break;
                   4613:                        }
                   4614:                        *dst_n++ = *src++;
                   4615:                        remain--;
                   4616:                }
                   4617:                *dst_n++ = *src++;
                   4618:                remain--;
                   4619:        }
                   4620:        if(remain > 0) {
                   4621:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4622:                        if(msdos_lead_byte_check(*src)) {
                   4623:                                if(++i == 3) {
                   4624:                                        break;
                   4625:                                }
                   4626:                                *dst_e++ = *src++;
                   4627:                                remain--;
                   4628:                        }
                   4629:                        *dst_e++ = *src++;
                   4630:                        remain--;
                   4631:                }
                   4632:                *dst_e = '\0';
                   4633:        } else {
                   4634:                *dst_n = '\0';
                   4635:        }
                   4636:        my_strupr(tmp);
                   4637:        return(tmp);
                   4638: }
                   4639: 
1.1.1.13  root     4640: errno_t msdos_maperr(unsigned long oserrno)
                   4641: {
                   4642:        _doserrno = oserrno;
1.1.1.14  root     4643:        switch(oserrno) {
1.1.1.13  root     4644:        case ERROR_FILE_NOT_FOUND:         // 2
                   4645:        case ERROR_PATH_NOT_FOUND:         // 3
                   4646:        case ERROR_INVALID_DRIVE:          // 15
                   4647:        case ERROR_NO_MORE_FILES:          // 18
                   4648:        case ERROR_BAD_NETPATH:            // 53
                   4649:        case ERROR_BAD_NET_NAME:           // 67
                   4650:        case ERROR_BAD_PATHNAME:           // 161
                   4651:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4652:                return ENOENT;
                   4653:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4654:                return EMFILE;
                   4655:        case ERROR_ACCESS_DENIED:          // 5
                   4656:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4657:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4658:        case ERROR_CANNOT_MAKE:            // 82
                   4659:        case ERROR_FAIL_I24:               // 83
                   4660:        case ERROR_DRIVE_LOCKED:           // 108
                   4661:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4662:        case ERROR_NOT_LOCKED:             // 158
                   4663:        case ERROR_LOCK_FAILED:            // 167
                   4664:                return EACCES;
                   4665:        case ERROR_INVALID_HANDLE:         // 6
                   4666:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4667:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4668:                return EBADF;
                   4669:        case ERROR_ARENA_TRASHED:          // 7
                   4670:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4671:        case ERROR_INVALID_BLOCK:          // 9
                   4672:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4673:                return ENOMEM;
                   4674:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4675:                return E2BIG;
                   4676:        case ERROR_BAD_FORMAT:             // 11
                   4677:                return ENOEXEC;
                   4678:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4679:                return EXDEV;
                   4680:        case ERROR_FILE_EXISTS:            // 80
                   4681:        case ERROR_ALREADY_EXISTS:         // 183
                   4682:                return EEXIST;
                   4683:        case ERROR_NO_PROC_SLOTS:          // 89
                   4684:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4685:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4686:                return EAGAIN;
                   4687:        case ERROR_BROKEN_PIPE:            // 109
                   4688:                return EPIPE;
                   4689:        case ERROR_DISK_FULL:              // 112
                   4690:                return ENOSPC;
                   4691:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4692:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4693:                return ECHILD;
                   4694:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4695:                return ENOTEMPTY;
                   4696:        }
1.1.1.14  root     4697:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4698:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4699:                return EACCES;
                   4700:        }
1.1.1.14  root     4701:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4702:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4703:                return ENOEXEC;
                   4704:        }
                   4705:        return EINVAL;
                   4706: }
                   4707: 
1.1.1.45  root     4708: int msdos_open(const char *path, int oflag)
1.1.1.13  root     4709: {
1.1.1.14  root     4710:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45  root     4711:                return(_open(path, oflag));
1.1.1.13  root     4712:        }
1.1.1.14  root     4713:        
                   4714:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4715:        DWORD disposition;
1.1.1.14  root     4716:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4717:        default:
1.1.1.13  root     4718:        case _O_EXCL:
                   4719:                disposition = OPEN_EXISTING;
                   4720:                break;
                   4721:        case _O_CREAT:
                   4722:                disposition = OPEN_ALWAYS;
                   4723:                break;
                   4724:        case _O_CREAT | _O_EXCL:
                   4725:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4726:                disposition = CREATE_NEW;
                   4727:                break;
                   4728:        case _O_TRUNC:
                   4729:        case _O_TRUNC | _O_EXCL:
                   4730:                disposition = TRUNCATE_EXISTING;
                   4731:                break;
                   4732:        case _O_CREAT | _O_TRUNC:
                   4733:                disposition = CREATE_ALWAYS;
                   4734:                break;
                   4735:        }
1.1.1.14  root     4736:        
1.1.1.45  root     4737:        HANDLE h = CreateFile(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
1.1.1.13  root     4738:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4739:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4740:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4741:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4742:                // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.45  root     4743:                h = CreateFile(path, GENERIC_READ,
1.1.1.13  root     4744:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4745:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4746:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4747:                        errno = msdos_maperr(GetLastError());
1.1.1.45  root     4748:                        return(-1);
1.1.1.13  root     4749:                }
                   4750:        }
1.1.1.14  root     4751:        
1.1.1.13  root     4752:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     4753:        if(fd == -1) {
1.1.1.13  root     4754:                CloseHandle(h);
                   4755:        }
1.1.1.45  root     4756:        return(fd);
                   4757: }
                   4758: 
                   4759: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
                   4760: {
                   4761:        int fd = -1;
                   4762:        
                   4763:        *sio_port = *lpt_port = 0;
                   4764:        
                   4765:        if(msdos_is_con_path(path)) {
                   4766:                // MODE.COM opens CON device with read/write mode :-(
                   4767:                if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
                   4768:                        oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
                   4769:                        oflag |= _O_RDONLY;
                   4770:                }
                   4771:                if((fd = msdos_open("CON", oflag)) == -1) {
                   4772: //                     fd = msdos_open("NUL", oflag);
                   4773:                }
                   4774:        } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
                   4775:                fd = msdos_open("NUL", oflag);
                   4776:                msdos_set_comm_params(*sio_port, path);
                   4777:        } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
                   4778:                fd = msdos_open("NUL", oflag);
                   4779:        } else if(msdos_is_device_path(path)) {
                   4780:                fd = msdos_open("NUL", oflag);
                   4781: //     } else if(oflag & _O_CREAT) {
                   4782: //             fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
                   4783: //     } else {
                   4784: //             fd = _open(path, oflag);
                   4785:        }
                   4786:        return(fd);
                   4787: }
                   4788: 
                   4789: UINT16 msdos_device_info(const char *path)
                   4790: {
                   4791:        if(msdos_is_con_path(path)) {
                   4792:                return(0x80d3);
                   4793:        } else if(msdos_is_comm_path(path)) {
                   4794:                return(0x80a0);
                   4795:        } else if(msdos_is_prn_path(path)) {
                   4796: //             return(0xa8c0);
                   4797:                return(0x80a0);
                   4798:        } else if(msdos_is_device_path(path)) {
                   4799:                if(strstr(path, "EMMXXXX0") != NULL) {
                   4800:                        return(0xc0c0);
                   4801:                } else if(strstr(path, "MSCD001") != NULL) {
                   4802:                        return(0xc880);
                   4803:                } else {
                   4804:                        return(0x8084);
                   4805:                }
                   4806:        } else {
                   4807:                return(msdos_drive_number(path));
                   4808:        }
1.1.1.13  root     4809: }
                   4810: 
1.1.1.52  root     4811: 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     4812: {
                   4813:        static int id = 0;
                   4814:        char full[MAX_PATH], *name;
                   4815:        
                   4816:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4817:                strcpy(file_handler[fd].path, full);
                   4818:        } else {
                   4819:                strcpy(file_handler[fd].path, path);
                   4820:        }
1.1.1.14  root     4821:        // isatty makes no distinction between CON & NUL
                   4822:        // GetFileSize fails on CON, succeeds on NUL
                   4823:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45  root     4824:                if(info == 0x80d3) {
                   4825:                        info = 0x8084;
                   4826:                }
1.1.1.14  root     4827:                atty = 0;
                   4828:        } else if(!atty && info == 0x80d3) {
1.1.1.45  root     4829: //             info = msdos_drive_number(".");
                   4830:                info = msdos_drive_number(path);
1.1.1.14  root     4831:        }
1.1       root     4832:        file_handler[fd].valid = 1;
                   4833:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37  root     4834:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     4835:        file_handler[fd].mode = mode;
                   4836:        file_handler[fd].info = info;
                   4837:        file_handler[fd].psp = psp_seg;
1.1.1.37  root     4838:        file_handler[fd].sio_port = sio_port;
                   4839:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     4840:        
                   4841:        // init system file table
                   4842:        if(fd < 20) {
                   4843:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   4844:                
                   4845:                memset(sft, 0, 0x3b);
                   4846:                
                   4847:                *(UINT16 *)(sft + 0x00) = 1;
                   4848:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
                   4849:                *(UINT8  *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
                   4850:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   4851:                
                   4852:                if(!(file_handler[fd].info & 0x80)) {
                   4853:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   4854:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   4855:                        
                   4856:                        FILETIME time, local;
                   4857:                        HANDLE hHandle;
                   4858:                        WORD dos_date = 0, dos_time = 0;
                   4859:                        DWORD file_size = 0;
                   4860:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   4861:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   4862:                                        FileTimeToLocalFileTime(&time, &local);
                   4863:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   4864:                                }
                   4865:                                file_size = GetFileSize(hHandle, NULL);
                   4866:                        }
                   4867:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   4868:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   4869:                        *(UINT32 *)(sft + 0x11) = file_size;
                   4870:                }
                   4871:                
                   4872:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   4873:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   4874:                my_strupr(fname);
                   4875:                my_strupr(ext);
                   4876:                memset(sft + 0x20, 0x20, 11);
                   4877:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   4878:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   4879:                
                   4880:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   4881:        }
1.1       root     4882: }
                   4883: 
                   4884: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   4885: {
                   4886:        strcpy(file_handler[dst].path, file_handler[src].path);
                   4887:        file_handler[dst].valid = 1;
                   4888:        file_handler[dst].id = file_handler[src].id;
                   4889:        file_handler[dst].atty = file_handler[src].atty;
                   4890:        file_handler[dst].mode = file_handler[src].mode;
                   4891:        file_handler[dst].info = file_handler[src].info;
                   4892:        file_handler[dst].psp = psp_seg;
1.1.1.37  root     4893:        file_handler[dst].sio_port = file_handler[src].sio_port;
                   4894:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     4895: }
                   4896: 
1.1.1.20  root     4897: void msdos_file_handler_close(int fd)
1.1       root     4898: {
                   4899:        file_handler[fd].valid = 0;
1.1.1.21  root     4900:        
                   4901:        if(fd < 20) {
                   4902:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   4903:        }
1.1       root     4904: }
                   4905: 
1.1.1.14  root     4906: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     4907: {
1.1.1.14  root     4908:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   4909:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   4910:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     4911: }
                   4912: 
                   4913: // find file
                   4914: 
                   4915: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   4916: {
                   4917:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4918:                return(0);      // search directory only !!!
                   4919:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   4920:                return(0);
                   4921:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   4922:                return(0);
                   4923:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4924:                return(0);
                   4925:        } else if((attribute & required_mask) != required_mask) {
                   4926:                return(0);
                   4927:        } else {
                   4928:                return(1);
                   4929:        }
                   4930: }
                   4931: 
1.1.1.13  root     4932: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
                   4933: {
1.1.1.14  root     4934:        if(fd->cAlternateFileName[0]) {
1.1.1.42  root     4935:                return(1);
1.1.1.13  root     4936:        }
                   4937:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     4938:        if(len > 12) {
1.1.1.42  root     4939:                return(0);
1.1.1.13  root     4940:        }
                   4941:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     4942:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42  root     4943:                return(0);
1.1.1.13  root     4944:        }
1.1.1.42  root     4945:        return(1);
1.1.1.13  root     4946: }
                   4947: 
1.1       root     4948: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
                   4949: {
                   4950:        FILETIME local;
                   4951:        
                   4952:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   4953:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   4954:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   4955:        
                   4956:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   4957:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   4958:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   4959:        
                   4960:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   4961:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   4962:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   4963: }
                   4964: 
                   4965: // i/o
                   4966: 
                   4967: void msdos_stdio_reopen()
                   4968: {
                   4969:        if(!file_handler[0].valid) {
                   4970:                _dup2(DUP_STDIN, 0);
                   4971:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   4972:        }
                   4973:        if(!file_handler[1].valid) {
                   4974:                _dup2(DUP_STDOUT, 1);
                   4975:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   4976:        }
                   4977:        if(!file_handler[2].valid) {
                   4978:                _dup2(DUP_STDERR, 2);
                   4979:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   4980:        }
1.1.1.21  root     4981:        if(!file_handler[3].valid) {
                   4982:                _dup2(DUP_STDAUX, 3);
                   4983:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   4984:        }
                   4985:        if(!file_handler[4].valid) {
                   4986:                _dup2(DUP_STDPRN, 4);
1.1.1.45  root     4987: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   4988:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     4989:        }
                   4990:        for(int i = 0; i < 5; i++) {
                   4991:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   4992:                        msdos_psp_set_file_table(i, i, current_psp);
                   4993:                }
                   4994:        }
1.1       root     4995: }
                   4996: 
1.1.1.37  root     4997: int msdos_read(int fd, void *buffer, unsigned int count)
                   4998: {
                   4999:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5000:                // read from serial port
                   5001:                int read = 0;
                   5002:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5003:                        UINT8 *buf = (UINT8 *)buffer;
                   5004:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5005:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5006:                        DWORD timeout = timeGetTime() + 1000;
                   5007:                        while(read < count) {
                   5008:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
                   5009:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
                   5010:                                        timeout = timeGetTime() + 1000;
                   5011:                                } else {
                   5012:                                        if(timeGetTime() > timeout) {
                   5013:                                                break;
                   5014:                                        }
                   5015:                                        Sleep(10);
1.1.1.37  root     5016:                                }
                   5017:                        }
                   5018:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5019:                }
                   5020:                return(read);
                   5021:        }
                   5022:        return(_read(fd, buffer, count));
                   5023: }
                   5024: 
1.1       root     5025: int msdos_kbhit()
                   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
1.1.1.20  root     5034:                return(eof(fd) == 0);
1.1       root     5035:        }
                   5036:        
                   5037:        // check keyboard status
1.1.1.35  root     5038:        if(key_recv != 0) {
1.1       root     5039:                return(1);
                   5040:        }
1.1.1.35  root     5041:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5042: #ifdef USE_SERVICE_THREAD
                   5043:                EnterCriticalSection(&key_buf_crit_sect);
                   5044: #endif
1.1.1.55  root     5045:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5046: #ifdef USE_SERVICE_THREAD
                   5047:                LeaveCriticalSection(&key_buf_crit_sect);
                   5048: #endif
                   5049:                if(!empty) return(1);
                   5050:        }
                   5051:        return(_kbhit());
1.1       root     5052: }
                   5053: 
                   5054: int msdos_getch_ex(int echo)
                   5055: {
                   5056:        static char prev = 0;
                   5057:        
                   5058:        msdos_stdio_reopen();
                   5059:        
1.1.1.20  root     5060:        process_t *process = msdos_process_info_get(current_psp);
                   5061:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5062:        
                   5063:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5064:                // stdin is redirected to file
                   5065: retry:
                   5066:                char data;
1.1.1.37  root     5067:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     5068:                        char tmp = data;
                   5069:                        if(data == 0x0a) {
                   5070:                                if(prev == 0x0d) {
                   5071:                                        goto retry; // CRLF -> skip LF
                   5072:                                } else {
                   5073:                                        data = 0x0d; // LF only -> CR
                   5074:                                }
                   5075:                        }
                   5076:                        prev = tmp;
                   5077:                        return(data);
                   5078:                }
                   5079:                return(EOF);
                   5080:        }
                   5081:        
                   5082:        // input from console
1.1.1.5   root     5083:        int key_char, key_scan;
1.1.1.33  root     5084:        if(key_recv != 0) {
1.1.1.5   root     5085:                key_char = (key_code >> 0) & 0xff;
                   5086:                key_scan = (key_code >> 8) & 0xff;
                   5087:                key_code >>= 16;
1.1.1.33  root     5088:                key_recv >>= 16;
1.1.1.5   root     5089:        } else {
1.1.1.54  root     5090:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_exit) {
1.1.1.35  root     5091:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5092: #ifdef USE_SERVICE_THREAD
                   5093:                                EnterCriticalSection(&key_buf_crit_sect);
                   5094: #endif
1.1.1.55  root     5095:                                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5096: #ifdef USE_SERVICE_THREAD
                   5097:                                LeaveCriticalSection(&key_buf_crit_sect);
                   5098: #endif
                   5099:                                if(!empty) break;
                   5100:                        }
1.1.1.23  root     5101:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   5102:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   5103:                                if(_kbhit()) {
1.1.1.32  root     5104:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5105: #ifdef USE_SERVICE_THREAD
                   5106:                                                EnterCriticalSection(&key_buf_crit_sect);
                   5107: #endif
1.1.1.51  root     5108:                                                pcbios_set_key_buffer(_getch(), 0x00);
1.1.1.35  root     5109: #ifdef USE_SERVICE_THREAD
                   5110:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   5111: #endif
1.1.1.32  root     5112:                                        }
1.1.1.23  root     5113:                                } else {
                   5114:                                        Sleep(10);
                   5115:                                }
                   5116:                        } else {
                   5117:                                if(!update_key_buffer()) {
                   5118:                                        Sleep(10);
                   5119:                                }
1.1.1.14  root     5120:                        }
                   5121:                }
1.1.1.54  root     5122:                if(m_exit) {
1.1.1.33  root     5123:                        // insert CR to terminate input loops
1.1.1.14  root     5124:                        key_char = 0x0d;
                   5125:                        key_scan = 0;
1.1.1.32  root     5126:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5127: #ifdef USE_SERVICE_THREAD
                   5128:                        EnterCriticalSection(&key_buf_crit_sect);
                   5129: #endif
1.1.1.51  root     5130:                        pcbios_get_key_buffer(&key_char, &key_scan);
1.1.1.35  root     5131: #ifdef USE_SERVICE_THREAD
                   5132:                        LeaveCriticalSection(&key_buf_crit_sect);
                   5133: #endif
1.1.1.5   root     5134:                }
1.1       root     5135:        }
                   5136:        if(echo && key_char) {
                   5137:                msdos_putch(key_char);
                   5138:        }
                   5139:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   5140: }
                   5141: 
                   5142: inline int msdos_getch()
                   5143: {
                   5144:        return(msdos_getch_ex(0));
                   5145: }
                   5146: 
                   5147: inline int msdos_getche()
                   5148: {
                   5149:        return(msdos_getch_ex(1));
                   5150: }
                   5151: 
                   5152: int msdos_write(int fd, const void *buffer, unsigned int count)
                   5153: {
1.1.1.37  root     5154:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5155:                // write to serial port
1.1.1.38  root     5156:                int written = 0;
1.1.1.37  root     5157:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5158:                        UINT8 *buf = (UINT8 *)buffer;
                   5159:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5160:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5161:                        DWORD timeout = timeGetTime() + 1000;
                   5162:                        while(written < count) {
                   5163:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
                   5164:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
                   5165:                                        timeout = timeGetTime() + 1000;
                   5166:                                } else {
                   5167:                                        if(timeGetTime() > timeout) {
                   5168:                                                break;
                   5169:                                        }
                   5170:                                        Sleep(10);
                   5171:                                }
1.1.1.37  root     5172:                        }
                   5173:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5174:                }
1.1.1.38  root     5175:                return(written);
1.1.1.37  root     5176:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   5177:                // write to printer port
                   5178:                UINT8 *buf = (UINT8 *)buffer;
                   5179:                for(unsigned int i = 0; i < count; i++) {
                   5180: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5181:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5182:                }
                   5183:                return(count);
                   5184:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     5185:                // CR+LF -> LF
1.1.1.37  root     5186:                static int is_cr = 0;
1.1       root     5187:                UINT8 *buf = (UINT8 *)buffer;
                   5188:                for(unsigned int i = 0; i < count; i++) {
                   5189:                        UINT8 data = buf[i];
                   5190:                        if(is_cr) {
                   5191:                                if(data != 0x0a) {
                   5192:                                        UINT8 tmp = 0x0d;
                   5193:                                        _write(1, &tmp, 1);
                   5194:                                }
                   5195:                                _write(1, &data, 1);
                   5196:                                is_cr = 0;
                   5197:                        } else if(data == 0x0d) {
                   5198:                                is_cr = 1;
                   5199:                        } else {
                   5200:                                _write(1, &data, 1);
                   5201:                        }
                   5202:                }
                   5203:                return(count);
                   5204:        }
1.1.1.14  root     5205:        vram_flush();
1.1       root     5206:        return(_write(fd, buffer, count));
                   5207: }
                   5208: 
                   5209: void msdos_putch(UINT8 data)
1.1.1.50  root     5210: {
                   5211:        msdos_stdio_reopen();
                   5212:        
                   5213:        process_t *process = msdos_process_info_get(current_psp);
                   5214:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5215:        
                   5216:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
                   5217:                // stdout is redirected to file
                   5218:                msdos_write(fd, &data, 1);
                   5219:                return;
                   5220:        }
                   5221:        
                   5222:        // call int 29h ?
                   5223:        if(*(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
                   5224:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   5225:                // int 29h is not hooked, no need to call int 29h
                   5226:                msdos_putch_fast(data);
                   5227: #ifdef USE_SERVICE_THREAD
                   5228:        } else if(in_service && main_thread_id != GetCurrentThreadId()) {
                   5229:                // XXX: in usually we should not reach here
                   5230:                // this is called from service thread to echo the input
                   5231:                // we can not call int 29h because it causes a critial issue to control cpu running in main thread :-(
                   5232:                msdos_putch_fast(data);
                   5233: #endif
1.1.1.51  root     5234:        } else if(in_service_29h) {
1.1.1.50  root     5235:                // disallow reentering call int 29h routine to prevent an infinite loop :-(
                   5236:                msdos_putch_fast(data);
                   5237:        } else {
                   5238:                // this is called from main thread, so we can call int 29h :-)
1.1.1.51  root     5239:                in_service_29h = true;
1.1.1.50  root     5240:                try {
                   5241:                        UINT32 tmp_pc = m_pc;
                   5242:                        UINT16 tmp_ax = REG16(AX);
                   5243:                        UINT32 tmp_bx = REG16(BX); // BX may be destroyed by some versions of DOS 3.3
                   5244:                        
                   5245:                        // call int 29h routine is at fffc:0027
                   5246:                        i386_call_far(DUMMY_TOP >> 4, 0x0027);
                   5247:                        REG8(AL) = data;
                   5248:                        
                   5249:                        // run cpu until call int 29h routine is done
1.1.1.54  root     5250:                        while(!m_exit && tmp_pc != m_pc) {
1.1.1.50  root     5251:                                try {
                   5252:                                        hardware_run_cpu();
                   5253:                                } catch(...) {
                   5254:                                }
                   5255:                        }
                   5256:                        REG16(AX) = tmp_ax;
                   5257:                        REG16(BX) = tmp_bx;
                   5258:                } catch(...) {
                   5259:                }
1.1.1.51  root     5260:                in_service_29h = false;
1.1.1.50  root     5261:        }
                   5262: }
                   5263: 
                   5264: void msdos_putch_fast(UINT8 data)
1.1.1.35  root     5265: #ifdef USE_SERVICE_THREAD
                   5266: {
                   5267:        EnterCriticalSection(&putch_crit_sect);
                   5268:        msdos_putch_tmp(data);
                   5269:        LeaveCriticalSection(&putch_crit_sect);
                   5270: }
                   5271: void msdos_putch_tmp(UINT8 data)
                   5272: #endif
1.1       root     5273: {
1.1.1.34  root     5274:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5275:        SMALL_RECT rect;
                   5276:        COORD co;
1.1       root     5277:        static int p = 0;
                   5278:        static int is_kanji = 0;
                   5279:        static int is_esc = 0;
                   5280:        static int stored_x;
                   5281:        static int stored_y;
                   5282:        static WORD stored_a;
1.1.1.20  root     5283:        static char tmp[64], out[64];
1.1       root     5284:        
1.1.1.23  root     5285:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     5286:        
                   5287:        // output to console
                   5288:        tmp[p++] = data;
                   5289:        
1.1.1.14  root     5290:        vram_flush();
                   5291:        
1.1       root     5292:        if(is_kanji) {
                   5293:                // kanji character
                   5294:                is_kanji = 0;
                   5295:        } else if(is_esc) {
                   5296:                // escape sequense
                   5297:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   5298:                        p = is_esc = 0;
                   5299:                } else if(tmp[1] == '=' && p == 4) {
                   5300:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     5301:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     5302:                        SetConsoleCursorPosition(hStdout, co);
                   5303:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5304:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     5305:                        cursor_moved = false;
                   5306:                        p = is_esc = 0;
                   5307:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
                   5308:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5309:                        co.X = csbi.dwCursorPosition.X;
                   5310:                        co.Y = csbi.dwCursorPosition.Y;
                   5311:                        WORD wAttributes = csbi.wAttributes;
                   5312:                        
                   5313:                        if(tmp[1] == 'D') {
                   5314:                                co.Y++;
                   5315:                        } else if(tmp[1] == 'E') {
                   5316:                                co.X = 0;
                   5317:                                co.Y++;
                   5318:                        } else if(tmp[1] == 'M') {
                   5319:                                co.Y--;
                   5320:                        } else if(tmp[1] == '*') {
1.1.1.14  root     5321:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5322:                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5323:                                co.X = 0;
                   5324:                                co.Y = csbi.srWindow.Top;
1.1       root     5325:                        } else if(tmp[1] == '[') {
                   5326:                                int param[256], params = 0;
                   5327:                                memset(param, 0, sizeof(param));
                   5328:                                for(int i = 2; i < p; i++) {
                   5329:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   5330:                                                param[params] *= 10;
                   5331:                                                param[params] += tmp[i] - '0';
                   5332:                                        } else {
                   5333:                                                params++;
                   5334:                                        }
                   5335:                                }
                   5336:                                if(data == 'A') {
1.1.1.14  root     5337:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     5338:                                } else if(data == 'B') {
1.1.1.14  root     5339:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     5340:                                } else if(data == 'C') {
1.1.1.14  root     5341:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     5342:                                } else if(data == 'D') {
1.1.1.14  root     5343:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     5344:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     5345:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   5346:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     5347:                                } else if(data == 'J') {
1.1.1.14  root     5348:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5349:                                        if(param[0] == 0) {
                   5350:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5351:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5352:                                                if(co.Y < csbi.srWindow.Bottom) {
                   5353:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5354:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5355:                                                }
                   5356:                                        } else if(param[0] == 1) {
1.1.1.14  root     5357:                                                if(co.Y > csbi.srWindow.Top) {
                   5358:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
                   5359:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5360:                                                }
                   5361:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5362:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5363:                                        } else if(param[0] == 2) {
1.1.1.14  root     5364:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5365:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5366:                                                co.X = co.Y = 0;
                   5367:                                        }
                   5368:                                } else if(data == 'K') {
1.1.1.14  root     5369:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5370:                                        if(param[0] == 0) {
                   5371:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5372:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5373:                                        } else if(param[0] == 1) {
                   5374:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5375:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5376:                                        } else if(param[0] == 2) {
                   5377:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5378:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5379:                                        }
                   5380:                                } else if(data == 'L') {
1.1.1.14  root     5381:                                        if(params == 0) {
                   5382:                                                param[0] = 1;
1.1       root     5383:                                        }
1.1.1.14  root     5384:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5385:                                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5386:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5387:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5388:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5389:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14  root     5390:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5391:                                        co.X = 0;
                   5392:                                } else if(data == 'M') {
1.1.1.14  root     5393:                                        if(params == 0) {
                   5394:                                                param[0] = 1;
                   5395:                                        }
                   5396:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5397:                                                clear_scr_buffer(csbi.wAttributes);
                   5398:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5399:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5400:                                        } else {
1.1.1.14  root     5401:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5402:                                                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5403:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5404:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5405:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5406:                                        }
                   5407:                                        co.X = 0;
                   5408:                                } else if(data == 'h') {
                   5409:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5410:                                                CONSOLE_CURSOR_INFO cur;
                   5411:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5412:                                                if(cur.bVisible) {
                   5413:                                                        cur.bVisible = FALSE;
1.1.1.14  root     5414: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5415:                                                }
                   5416:                                        }
                   5417:                                } else if(data == 'l') {
                   5418:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5419:                                                CONSOLE_CURSOR_INFO cur;
                   5420:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5421:                                                if(!cur.bVisible) {
                   5422:                                                        cur.bVisible = TRUE;
1.1.1.14  root     5423: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5424:                                                }
                   5425:                                        }
                   5426:                                } else if(data == 'm') {
                   5427:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5428:                                        int reverse = 0, hidden = 0;
                   5429:                                        for(int i = 0; i < params; i++) {
                   5430:                                                if(param[i] == 1) {
                   5431:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5432:                                                } else if(param[i] == 4) {
                   5433:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5434:                                                } else if(param[i] == 7) {
                   5435:                                                        reverse = 1;
                   5436:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5437:                                                        hidden = 1;
                   5438:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5439:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5440:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5441:                                                                param[i] -= 16;
                   5442:                                                        } else {
                   5443:                                                                param[i] -= 30;
                   5444:                                                        }
                   5445:                                                        if(param[i] & 1) {
                   5446:                                                                wAttributes |= FOREGROUND_RED;
                   5447:                                                        }
                   5448:                                                        if(param[i] & 2) {
                   5449:                                                                wAttributes |= FOREGROUND_GREEN;
                   5450:                                                        }
                   5451:                                                        if(param[i] & 4) {
                   5452:                                                                wAttributes |= FOREGROUND_BLUE;
                   5453:                                                        }
                   5454:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5455:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5456:                                                        if((param[i] - 40) & 1) {
                   5457:                                                                wAttributes |= BACKGROUND_RED;
                   5458:                                                        }
                   5459:                                                        if((param[i] - 40) & 2) {
                   5460:                                                                wAttributes |= BACKGROUND_GREEN;
                   5461:                                                        }
                   5462:                                                        if((param[i] - 40) & 4) {
                   5463:                                                                wAttributes |= BACKGROUND_BLUE;
                   5464:                                                        }
                   5465:                                                }
                   5466:                                        }
                   5467:                                        if(reverse) {
                   5468:                                                wAttributes &= ~0xff;
                   5469:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5470:                                        }
                   5471:                                        if(hidden) {
                   5472:                                                wAttributes &= ~0x0f;
                   5473:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5474:                                        }
                   5475:                                } else if(data == 'n') {
                   5476:                                        if(param[0] == 6) {
                   5477:                                                char tmp[16];
                   5478:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5479:                                                int len = strlen(tmp);
1.1.1.32  root     5480:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5481: #ifdef USE_SERVICE_THREAD
                   5482:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5483: #endif
1.1.1.32  root     5484:                                                        for(int i = 0; i < len; i++) {
1.1.1.51  root     5485:                                                                pcbios_set_key_buffer(tmp[i], 0x00);
1.1.1.32  root     5486:                                                        }
1.1.1.35  root     5487: #ifdef USE_SERVICE_THREAD
                   5488:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5489: #endif
1.1       root     5490:                                                }
                   5491:                                        }
                   5492:                                } else if(data == 's') {
                   5493:                                        stored_x = co.X;
                   5494:                                        stored_y = co.Y;
                   5495:                                        stored_a = wAttributes;
                   5496:                                } else if(data == 'u') {
                   5497:                                        co.X = stored_x;
                   5498:                                        co.Y = stored_y;
                   5499:                                        wAttributes = stored_a;
                   5500:                                }
                   5501:                        }
                   5502:                        if(co.X < 0) {
                   5503:                                co.X = 0;
                   5504:                        } else if(co.X >= csbi.dwSize.X) {
                   5505:                                co.X = csbi.dwSize.X - 1;
                   5506:                        }
1.1.1.14  root     5507:                        if(co.Y < csbi.srWindow.Top) {
                   5508:                                co.Y = csbi.srWindow.Top;
                   5509:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5510:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5511:                        }
                   5512:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5513:                                SetConsoleCursorPosition(hStdout, co);
                   5514:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5515:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5516:                                cursor_moved = false;
                   5517:                        }
                   5518:                        if(wAttributes != csbi.wAttributes) {
                   5519:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5520:                        }
                   5521:                        p = is_esc = 0;
                   5522:                }
                   5523:                return;
                   5524:        } else {
                   5525:                if(msdos_lead_byte_check(data)) {
                   5526:                        is_kanji = 1;
                   5527:                        return;
                   5528:                } else if(data == 0x1b) {
                   5529:                        is_esc = 1;
                   5530:                        return;
                   5531:                }
                   5532:        }
1.1.1.20  root     5533:        
                   5534:        DWORD q = 0, num;
                   5535:        is_kanji = 0;
                   5536:        for(int i = 0; i < p; i++) {
                   5537:                UINT8 c = tmp[i];
                   5538:                if(is_kanji) {
                   5539:                        is_kanji = 0;
                   5540:                } else if(msdos_lead_byte_check(data)) {
                   5541:                        is_kanji = 1;
                   5542:                } else if(msdos_ctrl_code_check(data)) {
                   5543:                        out[q++] = '^';
                   5544:                        c += 'A' - 1;
                   5545:                }
                   5546:                out[q++] = c;
                   5547:        }
1.1.1.34  root     5548:        if(q == 1 && out[0] == 0x08) {
                   5549:                // back space
                   5550:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5551:                if(csbi.dwCursorPosition.X > 0) {
                   5552:                        co.X = csbi.dwCursorPosition.X - 1;
                   5553:                        co.Y = csbi.dwCursorPosition.Y;
                   5554:                        SetConsoleCursorPosition(hStdout, co);
                   5555:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5556:                        co.X = csbi.dwSize.X - 1;
                   5557:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5558:                        SetConsoleCursorPosition(hStdout, co);
                   5559:                } else {
                   5560:                        WriteConsole(hStdout, out, q, &num, NULL); // to make sure
                   5561:                }
                   5562:        } else {
                   5563:                WriteConsole(hStdout, out, q, &num, NULL);
                   5564:        }
1.1       root     5565:        p = 0;
1.1.1.14  root     5566:        
1.1.1.15  root     5567:        if(!restore_console_on_exit) {
                   5568:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5569:                scr_top = csbi.srWindow.Top;
                   5570:        }
1.1       root     5571:        cursor_moved = true;
                   5572: }
                   5573: 
                   5574: int msdos_aux_in()
                   5575: {
1.1.1.21  root     5576:        msdos_stdio_reopen();
                   5577:        
1.1.1.20  root     5578:        process_t *process = msdos_process_info_get(current_psp);
                   5579:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5580:        
                   5581:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5582:                char data = 0;
1.1.1.37  root     5583:                msdos_read(fd, &data, 1);
1.1       root     5584:                return(data);
                   5585:        } else {
                   5586:                return(EOF);
                   5587:        }
                   5588: }
                   5589: 
                   5590: void msdos_aux_out(char data)
                   5591: {
1.1.1.21  root     5592:        msdos_stdio_reopen();
                   5593:        
1.1.1.20  root     5594:        process_t *process = msdos_process_info_get(current_psp);
                   5595:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5596:        
                   5597:        if(fd < process->max_files && file_handler[fd].valid) {
                   5598:                msdos_write(fd, &data, 1);
1.1       root     5599:        }
                   5600: }
                   5601: 
                   5602: void msdos_prn_out(char data)
                   5603: {
1.1.1.21  root     5604:        msdos_stdio_reopen();
                   5605:        
1.1.1.20  root     5606:        process_t *process = msdos_process_info_get(current_psp);
                   5607:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5608:        
                   5609:        if(fd < process->max_files && file_handler[fd].valid) {
                   5610:                msdos_write(fd, &data, 1);
1.1       root     5611:        }
                   5612: }
                   5613: 
                   5614: // memory control
                   5615: 
1.1.1.52  root     5616: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name = NULL)
1.1       root     5617: {
                   5618:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5619:        
                   5620:        mcb->mz = mz;
                   5621:        mcb->psp = psp;
1.1.1.30  root     5622:        mcb->paragraphs = paragraphs;
1.1.1.39  root     5623:        
                   5624:        if(prog_name != NULL) {
                   5625:                memset(mcb->prog_name, 0, 8);
                   5626:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
                   5627:        }
1.1       root     5628:        return(mcb);
                   5629: }
                   5630: 
                   5631: void msdos_mcb_check(mcb_t *mcb)
                   5632: {
                   5633:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5634:                #if 0
                   5635:                        // shutdown now !!!
                   5636:                        fatalerror("broken memory control block\n");
                   5637:                #else
                   5638:                        // return error code and continue
                   5639:                        throw(0x07); // broken memory control block
                   5640:                #endif
1.1       root     5641:        }
                   5642: }
                   5643: 
1.1.1.39  root     5644: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5645: {
                   5646:        int mcb_seg = seg - 1;
                   5647:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5648:        msdos_mcb_check(mcb);
                   5649:        
1.1.1.30  root     5650:        if(mcb->paragraphs > paragraphs) {
1.1       root     5651:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5652:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5653:                
                   5654:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5655:                mcb->mz = 'M';
1.1.1.30  root     5656:                mcb->paragraphs = paragraphs;
1.1       root     5657:        }
                   5658: }
                   5659: 
                   5660: void msdos_mem_merge(int seg)
                   5661: {
                   5662:        int mcb_seg = seg - 1;
                   5663:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5664:        msdos_mcb_check(mcb);
                   5665:        
                   5666:        while(1) {
                   5667:                if(mcb->mz == 'Z') {
                   5668:                        break;
                   5669:                }
1.1.1.30  root     5670:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5671:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5672:                msdos_mcb_check(next_mcb);
                   5673:                
                   5674:                if(next_mcb->psp != 0) {
                   5675:                        break;
                   5676:                }
                   5677:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5678:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5679:        }
                   5680: }
                   5681: 
1.1.1.8   root     5682: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5683: {
                   5684:        while(1) {
                   5685:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5686:                bool last_block;
1.1       root     5687:                
1.1.1.14  root     5688:                if(mcb->psp == 0) {
                   5689:                        msdos_mem_merge(mcb_seg + 1);
                   5690:                } else {
                   5691:                        msdos_mcb_check(mcb);
                   5692:                }
1.1.1.33  root     5693:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5694:                        // check if the next is dummy mcb to link to umb
                   5695:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5696:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5697:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5698:                }
                   5699:                if(!(new_process && !last_block)) {
1.1.1.30  root     5700:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5701:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5702:                                mcb->psp = current_psp;
                   5703:                                return(mcb_seg + 1);
                   5704:                        }
                   5705:                }
                   5706:                if(mcb->mz == 'Z') {
                   5707:                        break;
                   5708:                }
1.1.1.30  root     5709:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5710:        }
                   5711:        return(-1);
                   5712: }
                   5713: 
                   5714: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5715: {
                   5716:        int mcb_seg = seg - 1;
                   5717:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5718:        msdos_mcb_check(mcb);
1.1.1.30  root     5719:        int current_paragraphs = mcb->paragraphs;
1.1       root     5720:        
                   5721:        msdos_mem_merge(seg);
1.1.1.30  root     5722:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5723:                if(max_paragraphs) {
1.1.1.30  root     5724:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5725:                }
1.1       root     5726:                msdos_mem_split(seg, current_paragraphs);
                   5727:                return(-1);
                   5728:        }
                   5729:        msdos_mem_split(seg, paragraphs);
                   5730:        return(0);
                   5731: }
                   5732: 
                   5733: void msdos_mem_free(int seg)
                   5734: {
                   5735:        int mcb_seg = seg - 1;
                   5736:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5737:        msdos_mcb_check(mcb);
                   5738:        
                   5739:        mcb->psp = 0;
                   5740:        msdos_mem_merge(seg);
                   5741: }
                   5742: 
1.1.1.8   root     5743: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5744: {
                   5745:        int max_paragraphs = 0;
                   5746:        
                   5747:        while(1) {
                   5748:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5749:                bool last_block;
                   5750:                
1.1       root     5751:                msdos_mcb_check(mcb);
                   5752:                
1.1.1.33  root     5753:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5754:                        // check if the next is dummy mcb to link to umb
                   5755:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5756:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5757:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5758:                }
                   5759:                if(!(new_process && !last_block)) {
1.1.1.30  root     5760:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   5761:                                max_paragraphs = mcb->paragraphs;
1.1       root     5762:                        }
                   5763:                }
                   5764:                if(mcb->mz == 'Z') {
                   5765:                        break;
                   5766:                }
1.1.1.30  root     5767:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5768:        }
1.1.1.14  root     5769:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     5770: }
                   5771: 
1.1.1.8   root     5772: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   5773: {
                   5774:        int last_seg = -1;
                   5775:        
                   5776:        while(1) {
                   5777:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5778:                msdos_mcb_check(mcb);
                   5779:                
1.1.1.14  root     5780:                if(mcb->psp == psp) {
1.1.1.8   root     5781:                        last_seg = mcb_seg;
                   5782:                }
1.1.1.14  root     5783:                if(mcb->mz == 'Z') {
                   5784:                        break;
                   5785:                }
1.1.1.30  root     5786:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     5787:        }
                   5788:        return(last_seg);
                   5789: }
                   5790: 
1.1.1.19  root     5791: int msdos_mem_get_umb_linked()
                   5792: {
1.1.1.33  root     5793:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5794:        msdos_mcb_check(mcb);
1.1.1.19  root     5795:        
1.1.1.33  root     5796:        if(mcb->mz == 'M') {
                   5797:                return(-1);
1.1.1.19  root     5798:        }
                   5799:        return(0);
                   5800: }
                   5801: 
1.1.1.33  root     5802: void msdos_mem_link_umb()
1.1.1.19  root     5803: {
1.1.1.33  root     5804:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5805:        msdos_mcb_check(mcb);
1.1.1.19  root     5806:        
1.1.1.33  root     5807:        mcb->mz = 'M';
                   5808:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39  root     5809:        
                   5810:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     5811: }
                   5812: 
1.1.1.33  root     5813: void msdos_mem_unlink_umb()
1.1.1.19  root     5814: {
1.1.1.33  root     5815:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5816:        msdos_mcb_check(mcb);
1.1.1.19  root     5817:        
1.1.1.33  root     5818:        mcb->mz = 'Z';
                   5819:        mcb->paragraphs = 0;
1.1.1.39  root     5820:        
                   5821:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     5822: }
                   5823: 
1.1.1.29  root     5824: #ifdef SUPPORT_HMA
                   5825: 
                   5826: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   5827: {
                   5828:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5829:        
                   5830:        mcb->ms[0] = 'M';
                   5831:        mcb->ms[1] = 'S';
                   5832:        mcb->owner = owner;
                   5833:        mcb->size = size;
                   5834:        mcb->next = next;
                   5835:        return(mcb);
                   5836: }
                   5837: 
                   5838: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   5839: {
                   5840:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   5841: }
                   5842: 
                   5843: int msdos_hma_mem_split(int offset, int size)
                   5844: {
                   5845:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5846:        
                   5847:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5848:                return(-1);
                   5849:        }
                   5850:        if(mcb->size >= size + 0x10) {
                   5851:                int new_offset = offset + 0x10 + size;
                   5852:                int new_size = mcb->size - 0x10 - size;
                   5853:                
                   5854:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   5855:                mcb->size = size;
                   5856:                mcb->next = new_offset;
                   5857:                return(0);
                   5858:        }
                   5859:        return(-1);
                   5860: }
                   5861: 
                   5862: void msdos_hma_mem_merge(int offset)
                   5863: {
                   5864:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5865:        
                   5866:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5867:                return;
                   5868:        }
                   5869:        while(1) {
                   5870:                if(mcb->next == 0) {
                   5871:                        break;
                   5872:                }
                   5873:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   5874:                
                   5875:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   5876:                        return;
                   5877:                }
                   5878:                if(next_mcb->owner != 0) {
                   5879:                        break;
                   5880:                }
                   5881:                mcb->size += 0x10 + next_mcb->size;
                   5882:                mcb->next = next_mcb->next;
                   5883:        }
                   5884: }
                   5885: 
                   5886: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   5887: {
                   5888:        int offset = 0x10; // first mcb in HMA
                   5889:        
                   5890:        while(1) {
                   5891:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5892:                
                   5893:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5894:                        return(-1);
                   5895:                }
                   5896:                if(mcb->owner == 0) {
                   5897:                        msdos_hma_mem_merge(offset);
                   5898:                }
                   5899:                if(mcb->owner == 0 && mcb->size >= size) {
                   5900:                        msdos_hma_mem_split(offset, size);
                   5901:                        mcb->owner = owner;
                   5902:                        return(offset);
                   5903:                }
                   5904:                if(mcb->next == 0) {
                   5905:                        break;
                   5906:                }
                   5907:                offset = mcb->next;
                   5908:        }
                   5909:        return(-1);
                   5910: }
                   5911: 
                   5912: int msdos_hma_mem_realloc(int offset, int size)
                   5913: {
                   5914:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5915:        
                   5916:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5917:                return(-1);
                   5918:        }
                   5919:        if(mcb->size < size) {
                   5920:                return(-1);
                   5921:        }
                   5922:        msdos_hma_mem_split(offset, size);
                   5923:        return(0);
                   5924: }
                   5925: 
                   5926: void msdos_hma_mem_free(int offset)
                   5927: {
                   5928:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5929:        
                   5930:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5931:                return;
                   5932:        }
                   5933:        mcb->owner = 0;
                   5934:        msdos_hma_mem_merge(offset);
                   5935: }
                   5936: 
                   5937: int msdos_hma_mem_get_free(int *available_offset)
                   5938: {
                   5939:        int offset = 0x10; // first mcb in HMA
                   5940:        int size = 0;
                   5941:        
                   5942:        while(1) {
                   5943:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5944:                
                   5945:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5946:                        return(0);
                   5947:                }
                   5948:                if(mcb->owner == 0 && size < mcb->size) {
                   5949:                        if(available_offset != NULL) {
                   5950:                                *available_offset = offset;
                   5951:                        }
                   5952:                        size = mcb->size;
                   5953:                }
                   5954:                if(mcb->next == 0) {
                   5955:                        break;
                   5956:                }
                   5957:                offset = mcb->next;
                   5958:        }
                   5959:        return(size);
                   5960: }
                   5961: 
                   5962: #endif
                   5963: 
1.1       root     5964: // environment
                   5965: 
1.1.1.45  root     5966: void msdos_env_set_argv(int env_seg, const char *argv)
1.1       root     5967: {
                   5968:        char *dst = (char *)(mem + (env_seg << 4));
                   5969:        
                   5970:        while(1) {
                   5971:                if(dst[0] == 0) {
                   5972:                        break;
                   5973:                }
                   5974:                dst += strlen(dst) + 1;
                   5975:        }
                   5976:        *dst++ = 0; // end of environment
                   5977:        *dst++ = 1; // top of argv[0]
                   5978:        *dst++ = 0;
                   5979:        memcpy(dst, argv, strlen(argv));
                   5980:        dst += strlen(argv);
                   5981:        *dst++ = 0;
                   5982:        *dst++ = 0;
                   5983: }
                   5984: 
1.1.1.45  root     5985: const char *msdos_env_get_argv(int env_seg)
1.1       root     5986: {
                   5987:        static char env[ENV_SIZE];
                   5988:        char *src = env;
                   5989:        
                   5990:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5991:        while(1) {
                   5992:                if(src[0] == 0) {
                   5993:                        if(src[1] == 1) {
                   5994:                                return(src + 3);
                   5995:                        }
                   5996:                        break;
                   5997:                }
                   5998:                src += strlen(src) + 1;
                   5999:        }
                   6000:        return(NULL);
                   6001: }
                   6002: 
1.1.1.45  root     6003: const char *msdos_env_get(int env_seg, const char *name)
1.1       root     6004: {
                   6005:        static char env[ENV_SIZE];
                   6006:        char *src = env;
                   6007:        
                   6008:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   6009:        while(1) {
                   6010:                if(src[0] == 0) {
                   6011:                        break;
                   6012:                }
                   6013:                int len = strlen(src);
                   6014:                char *n = my_strtok(src, "=");
                   6015:                char *v = src + strlen(n) + 1;
                   6016:                
                   6017:                if(_stricmp(name, n) == 0) {
                   6018:                        return(v);
                   6019:                }
                   6020:                src += len + 1;
                   6021:        }
                   6022:        return(NULL);
                   6023: }
                   6024: 
1.1.1.45  root     6025: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1       root     6026: {
                   6027:        char env[ENV_SIZE];
                   6028:        char *src = env;
                   6029:        char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45  root     6030:        const char *argv = msdos_env_get_argv(env_seg);
1.1       root     6031:        int done = 0;
                   6032:        
                   6033:        memcpy(src, dst, ENV_SIZE);
                   6034:        memset(dst, 0, ENV_SIZE);
                   6035:        while(1) {
                   6036:                if(src[0] == 0) {
                   6037:                        break;
                   6038:                }
                   6039:                int len = strlen(src);
                   6040:                char *n = my_strtok(src, "=");
                   6041:                char *v = src + strlen(n) + 1;
                   6042:                char tmp[1024];
                   6043:                
                   6044:                if(_stricmp(name, n) == 0) {
                   6045:                        sprintf(tmp, "%s=%s", n, value);
                   6046:                        done = 1;
                   6047:                } else {
                   6048:                        sprintf(tmp, "%s=%s", n, v);
                   6049:                }
                   6050:                memcpy(dst, tmp, strlen(tmp));
                   6051:                dst += strlen(tmp) + 1;
                   6052:                src += len + 1;
                   6053:        }
                   6054:        if(!done) {
                   6055:                char tmp[1024];
                   6056:                
                   6057:                sprintf(tmp, "%s=%s", name, value);
                   6058:                memcpy(dst, tmp, strlen(tmp));
                   6059:                dst += strlen(tmp) + 1;
                   6060:        }
                   6061:        if(argv) {
                   6062:                *dst++ = 0; // end of environment
                   6063:                *dst++ = 1; // top of argv[0]
                   6064:                *dst++ = 0;
                   6065:                memcpy(dst, argv, strlen(argv));
                   6066:                dst += strlen(argv);
                   6067:                *dst++ = 0;
                   6068:                *dst++ = 0;
                   6069:        }
                   6070: }
                   6071: 
                   6072: // process
                   6073: 
1.1.1.8   root     6074: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     6075: {
                   6076:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6077:        
                   6078:        memset(psp, 0, PSP_SIZE);
                   6079:        psp->exit[0] = 0xcd;
                   6080:        psp->exit[1] = 0x20;
1.1.1.8   root     6081:        psp->first_mcb = mcb_seg;
1.1.1.46  root     6082: #if 1
1.1.1.49  root     6083:        psp->call5[0] = 0xcd;   // int 30h
                   6084:        psp->call5[1] = 0x30;
1.1.1.46  root     6085:        psp->call5[2] = 0xc3;   // ret
                   6086: #else
                   6087:        psp->call5[0] = 0x8a;   // mov ah, cl
                   6088:        psp->call5[1] = 0xe1;
                   6089:        psp->call5[2] = 0xcd;   // int 21h
                   6090:        psp->call5[3] = 0x21;
                   6091:        psp->call5[4] = 0xc3;   // ret
                   6092: #endif
1.1       root     6093:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   6094:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   6095:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   6096:        psp->parent_psp = parent_psp;
1.1.1.20  root     6097:        if(parent_psp == (UINT16)-1) {
                   6098:                for(int i = 0; i < 20; i++) {
                   6099:                        if(file_handler[i].valid) {
                   6100:                                psp->file_table[i] = i;
                   6101:                        } else {
                   6102:                                psp->file_table[i] = 0xff;
                   6103:                        }
1.1       root     6104:                }
1.1.1.20  root     6105:        } else {
                   6106:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     6107:        }
                   6108:        psp->env_seg = env_seg;
                   6109:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     6110:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     6111:        psp->file_table_size = 20;
                   6112:        psp->file_table_ptr.w.l = 0x18;
                   6113:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     6114:        psp->service[0] = 0xcd;
                   6115:        psp->service[1] = 0x21;
                   6116:        psp->service[2] = 0xcb;
                   6117:        return(psp);
                   6118: }
                   6119: 
1.1.1.20  root     6120: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   6121: {
                   6122:        if(psp_seg && fd < 20) {
                   6123:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6124:                psp->file_table[fd] = value;
                   6125:        }
                   6126: }
                   6127: 
                   6128: int msdos_psp_get_file_table(int fd, int psp_seg)
                   6129: {
                   6130:        if(psp_seg && fd < 20) {
                   6131:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6132:                fd = psp->file_table[fd];
                   6133:        }
                   6134:        return fd;
                   6135: }
                   6136: 
1.1.1.52  root     6137: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al, bool first_process = false)
1.1       root     6138: {
                   6139:        // load command file
                   6140:        int fd = -1;
1.1.1.45  root     6141:        int sio_port = 0;
                   6142:        int lpt_port = 0;
1.1       root     6143:        int dos_command = 0;
1.1.1.24  root     6144:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     6145:        char pipe_stdin_path[MAX_PATH] = {0};
                   6146:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39  root     6147:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     6148:        
                   6149:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6150:        int opt_len = mem[opt_ofs];
                   6151:        memset(opt, 0, sizeof(opt));
                   6152:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6153:        
1.1.1.14  root     6154:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   6155:                // this is a batch file, run command.com
                   6156:                char tmp[MAX_PATH];
                   6157:                if(opt_len != 0) {
                   6158:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   6159:                } else {
                   6160:                        sprintf(tmp, "/C %s", cmd);
                   6161:                }
                   6162:                strcpy(opt, tmp);
                   6163:                opt_len = strlen(opt);
                   6164:                mem[opt_ofs] = opt_len;
                   6165:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6166:                strcpy(command, comspec_path);
                   6167:                strcpy(name_tmp, "COMMAND.COM");
                   6168:        } else {
                   6169:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   6170:                        // redirect C:\COMMAND.COM to comspec_path
                   6171:                        strcpy(command, comspec_path);
                   6172:                } else {
                   6173:                        strcpy(command, cmd);
                   6174:                }
1.1.1.24  root     6175:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   6176:                        return(-1);
                   6177:                }
1.1.1.14  root     6178:                memset(name_tmp, 0, sizeof(name_tmp));
                   6179:                strcpy(name_tmp, name);
                   6180:                
                   6181:                // check command.com
1.1.1.38  root     6182:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   6183:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     6184:                        if(opt_len == 0) {
                   6185: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   6186:                                process_t *current_process = NULL;
                   6187:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   6188:                                        if(process[i].psp == current_psp) {
                   6189:                                                current_process = &process[i];
                   6190:                                                break;
                   6191:                                        }
                   6192:                                }
                   6193:                                if(current_process != NULL) {
                   6194:                                        param->cmd_line.dw = current_process->dta.dw;
                   6195:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6196:                                        opt_len = mem[opt_ofs];
                   6197:                                        memset(opt, 0, sizeof(opt));
                   6198:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6199:                                }
                   6200:                        }
                   6201:                        for(int i = 0; i < opt_len; i++) {
                   6202:                                if(opt[i] == ' ') {
                   6203:                                        continue;
                   6204:                                }
                   6205:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   6206:                                        for(int j = i + 3; j < opt_len; j++) {
                   6207:                                                if(opt[j] == ' ') {
                   6208:                                                        continue;
                   6209:                                                }
                   6210:                                                char *token = my_strtok(opt + j, " ");
                   6211:                                                
1.1.1.38  root     6212:                                                strcpy(command, token);
                   6213:                                                char tmp[MAX_PATH];
                   6214:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39  root     6215:                                                strcpy(opt, "");
                   6216:                                                for(int i = 0; i < strlen(tmp); i++) {
                   6217:                                                        if(tmp[i] != ' ') {
                   6218:                                                                strcpy(opt, tmp + i);
                   6219:                                                                break;
                   6220:                                                        }
                   6221:                                                }
                   6222:                                                strcpy(tmp, opt);
1.1.1.38  root     6223:                                                
                   6224:                                                if(al == 0x00) {
1.1.1.39  root     6225:                                                        #define GET_FILE_PATH() { \
                   6226:                                                                if(token[0] != '>' && token[0] != '<') { \
                   6227:                                                                        token++; \
                   6228:                                                                } \
                   6229:                                                                token++; \
                   6230:                                                                while(*token == ' ') { \
                   6231:                                                                        token++; \
                   6232:                                                                } \
                   6233:                                                                char *ptr = token; \
                   6234:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
                   6235:                                                                        ptr++; \
                   6236:                                                                } \
                   6237:                                                                *ptr = '\0'; \
                   6238:                                                        }
                   6239:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
                   6240:                                                                GET_FILE_PATH();
1.1.1.38  root     6241:                                                                strcpy(pipe_stdin_path, token);
                   6242:                                                                strcpy(opt, tmp);
                   6243:                                                        }
1.1.1.39  root     6244:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
                   6245:                                                                GET_FILE_PATH();
1.1.1.38  root     6246:                                                                strcpy(pipe_stdout_path, token);
                   6247:                                                                strcpy(opt, tmp);
                   6248:                                                        }
1.1.1.39  root     6249:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6250:                                                                GET_FILE_PATH();
                   6251:                                                                strcpy(pipe_stderr_path, token);
                   6252:                                                                strcpy(opt, tmp);
                   6253:                                                        }
                   6254:                                                        #undef GET_FILE_PATH
                   6255:                                                        
                   6256:                                                        if((token = strstr(opt, "0<")) != NULL) {
                   6257:                                                                *token = '\0';
                   6258:                                                        }
                   6259:                                                        if((token = strstr(opt, "1>")) != NULL) {
                   6260:                                                                *token = '\0';
                   6261:                                                        }
                   6262:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6263:                                                                *token = '\0';
                   6264:                                                        }
1.1.1.38  root     6265:                                                        if((token = strstr(opt, "<")) != NULL) {
                   6266:                                                                *token = '\0';
                   6267:                                                        }
                   6268:                                                        if((token = strstr(opt, ">")) != NULL) {
                   6269:                                                                *token = '\0';
                   6270:                                                        }
1.1.1.14  root     6271:                                                }
1.1.1.39  root     6272:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
                   6273:                                                        opt[i] = '\0';
                   6274:                                                }
1.1.1.38  root     6275:                                                opt_len = strlen(opt);
                   6276:                                                mem[opt_ofs] = opt_len;
                   6277:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6278:                                                dos_command = 1;
1.1.1.14  root     6279:                                                break;
1.1       root     6280:                                        }
                   6281:                                }
1.1.1.14  root     6282:                                break;
1.1       root     6283:                        }
                   6284:                }
                   6285:        }
                   6286:        
                   6287:        // load command file
                   6288:        strcpy(path, command);
                   6289:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6290:                sprintf(path, "%s.COM", command);
                   6291:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6292:                        sprintf(path, "%s.EXE", command);
                   6293:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     6294:                                sprintf(path, "%s.BAT", command);
                   6295:                                if(_access(path, 0) == 0) {
                   6296:                                        // this is a batch file, run command.com
                   6297:                                        char tmp[MAX_PATH];
                   6298:                                        if(opt_len != 0) {
                   6299:                                                sprintf(tmp, "/C %s %s", path, opt);
                   6300:                                        } else {
                   6301:                                                sprintf(tmp, "/C %s", path);
                   6302:                                        }
                   6303:                                        strcpy(opt, tmp);
                   6304:                                        opt_len = strlen(opt);
                   6305:                                        mem[opt_ofs] = opt_len;
                   6306:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6307:                                        strcpy(path, comspec_path);
                   6308:                                        strcpy(name_tmp, "COMMAND.COM");
                   6309:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6310:                                } else {
                   6311:                                        // search path in parent environments
                   6312:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45  root     6313:                                        const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14  root     6314:                                        if(env != NULL) {
                   6315:                                                char env_path[4096];
                   6316:                                                strcpy(env_path, env);
                   6317:                                                char *token = my_strtok(env_path, ";");
                   6318:                                                
                   6319:                                                while(token != NULL) {
                   6320:                                                        if(strlen(token) != 0) {
                   6321:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   6322:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6323:                                                                        break;
                   6324:                                                                }
                   6325:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   6326:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6327:                                                                        break;
                   6328:                                                                }
                   6329:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   6330:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6331:                                                                        break;
                   6332:                                                                }
                   6333:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   6334:                                                                if(_access(path, 0) == 0) {
                   6335:                                                                        // this is a batch file, run command.com
                   6336:                                                                        char tmp[MAX_PATH];
                   6337:                                                                        if(opt_len != 0) {
                   6338:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   6339:                                                                        } else {
                   6340:                                                                                sprintf(tmp, "/C %s", path);
                   6341:                                                                        }
                   6342:                                                                        strcpy(opt, tmp);
                   6343:                                                                        opt_len = strlen(opt);
                   6344:                                                                        mem[opt_ofs] = opt_len;
                   6345:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6346:                                                                        strcpy(path, comspec_path);
                   6347:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   6348:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6349:                                                                        break;
                   6350:                                                                }
1.1.1.8   root     6351:                                                        }
1.1.1.14  root     6352:                                                        token = my_strtok(NULL, ";");
1.1       root     6353:                                                }
                   6354:                                        }
                   6355:                                }
                   6356:                        }
                   6357:                }
                   6358:        }
                   6359:        if(fd == -1) {
1.1.1.38  root     6360:                // we can not find command.com in the path, so open comspec_path
                   6361:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6362:                        strcpy(command, comspec_path);
                   6363:                        strcpy(path, command);
                   6364:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6365:                }
                   6366:        }
                   6367:        if(fd == -1) {
1.1.1.52  root     6368:                if(!first_process && al == 0 && dos_command) {
1.1       root     6369:                        // may be dos command
                   6370:                        char tmp[MAX_PATH];
1.1.1.52  root     6371:                        if(opt_len != 0) {
                   6372:                                sprintf(tmp, "%s %s", command, opt);
                   6373:                        } else {
                   6374:                                sprintf(tmp, "%s", command);
                   6375:                        }
                   6376:                        retval = system(tmp);
1.1       root     6377:                        return(0);
                   6378:                } else {
                   6379:                        return(-1);
                   6380:                }
                   6381:        }
1.1.1.52  root     6382:        memset(file_buffer, 0, sizeof(file_buffer));
1.1       root     6383:        _read(fd, file_buffer, sizeof(file_buffer));
                   6384:        _close(fd);
                   6385:        
1.1.1.52  root     6386:        // check if this is win32 program
                   6387:        if(!first_process && al == 0) {
                   6388:                UINT16 sign_dos = *(UINT16 *)(file_buffer + 0x00);
                   6389:                UINT32 e_lfanew = *(UINT32 *)(file_buffer + 0x3c);
                   6390:                if(sign_dos == IMAGE_DOS_SIGNATURE && e_lfanew >= 0x40 && e_lfanew < 0x400) {
                   6391:                        UINT32 sign_nt = *(UINT32 *)(file_buffer + e_lfanew + 0x00);
                   6392:                        UINT16 machine = *(UINT16 *)(file_buffer + e_lfanew + 0x04);
                   6393:                        if(sign_nt == IMAGE_NT_SIGNATURE && (machine == IMAGE_FILE_MACHINE_I386 || machine == IMAGE_FILE_MACHINE_AMD64)) {
                   6394:                                char tmp[MAX_PATH];
                   6395:                                if(opt_len != 0) {
                   6396:                                        sprintf(tmp, "\"%s\" %s", path, opt);
                   6397:                                } else {
                   6398:                                        sprintf(tmp, "\"%s\"", path);
                   6399:                                }
                   6400:                                retval = system(tmp);
                   6401:                                return(0);
                   6402:                        }
                   6403:                }
                   6404:        }
                   6405:        
1.1       root     6406:        // copy environment
1.1.1.29  root     6407:        int umb_linked, env_seg, psp_seg;
1.1       root     6408:        
1.1.1.29  root     6409:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6410:                msdos_mem_unlink_umb();
                   6411:        }
1.1.1.8   root     6412:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6413:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6414:                        if(umb_linked != 0) {
                   6415:                                msdos_mem_link_umb();
                   6416:                        }
                   6417:                        return(-1);
                   6418:                }
1.1       root     6419:        }
                   6420:        if(param->env_seg == 0) {
                   6421:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6422:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6423:        } else {
                   6424:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6425:        }
                   6426:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6427:        
                   6428:        // check exe header
                   6429:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6430:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6431:        UINT16 cs, ss, ip, sp;
                   6432:        
                   6433:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6434:                // memory allocation
                   6435:                int header_size = header->header_size * 16;
                   6436:                int load_size = header->pages * 512 - header_size;
                   6437:                if(header_size + load_size < 512) {
                   6438:                        load_size = 512 - header_size;
                   6439:                }
                   6440:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6441:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6442:                        msdos_mem_free(env_seg);
                   6443:                        return(-1);
                   6444:                }
                   6445:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6446:                if(paragraphs > free_paragraphs) {
                   6447:                        paragraphs = free_paragraphs;
                   6448:                }
1.1.1.8   root     6449:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6450:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6451:                                if(umb_linked != 0) {
                   6452:                                        msdos_mem_link_umb();
                   6453:                                }
                   6454:                                msdos_mem_free(env_seg);
                   6455:                                return(-1);
                   6456:                        }
1.1       root     6457:                }
                   6458:                // relocation
                   6459:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6460:                for(int i = 0; i < header->relocations; i++) {
                   6461:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6462:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6463:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6464:                }
                   6465:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6466:                // segments
                   6467:                cs = header->init_cs + start_seg;
                   6468:                ss = header->init_ss + start_seg;
                   6469:                ip = header->init_ip;
                   6470:                sp = header->init_sp - 2; // for symdeb
                   6471:        } else {
                   6472:                // memory allocation
                   6473:                paragraphs = free_paragraphs;
1.1.1.8   root     6474:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6475:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6476:                                if(umb_linked != 0) {
                   6477:                                        msdos_mem_link_umb();
                   6478:                                }
                   6479:                                msdos_mem_free(env_seg);
                   6480:                                return(-1);
                   6481:                        }
1.1       root     6482:                }
                   6483:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6484:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6485:                // segments
                   6486:                cs = ss = psp_seg;
                   6487:                ip = 0x100;
                   6488:                sp = 0xfffe;
                   6489:        }
1.1.1.29  root     6490:        if(umb_linked != 0) {
                   6491:                msdos_mem_link_umb();
                   6492:        }
1.1       root     6493:        
                   6494:        // create psp
1.1.1.3   root     6495:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6496:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6497:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6498:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6499:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6500:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6501:        
                   6502:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6503:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6504:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6505:        
1.1.1.4   root     6506:        for(int i = 0; i < 8; i++) {
                   6507:                if(name_tmp[i] == '.') {
                   6508:                        mcb_psp->prog_name[i] = '\0';
                   6509:                        break;
                   6510:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6511:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6512:                        i++;
                   6513:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6514:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6515:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6516:                } else {
                   6517:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6518:                }
                   6519:        }
                   6520:        
1.1       root     6521:        // process info
                   6522:        process_t *process = msdos_process_info_create(psp_seg);
                   6523:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6524: #ifdef USE_DEBUGGER
                   6525:        strcpy(process->module_path, path);
                   6526: #endif
1.1       root     6527:        process->dta.w.l = 0x80;
                   6528:        process->dta.w.h = psp_seg;
                   6529:        process->switchar = '/';
                   6530:        process->max_files = 20;
                   6531:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6532:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6533:        process->parent_ds = SREG(DS);
1.1.1.31  root     6534:        process->parent_es = SREG(ES);
1.1       root     6535:        
                   6536:        current_psp = psp_seg;
1.1.1.23  root     6537:        msdos_sda_update(current_psp);
1.1       root     6538:        
                   6539:        if(al == 0x00) {
                   6540:                int_10h_feh_called = int_10h_ffh_called = false;
                   6541:                
1.1.1.38  root     6542:                // pipe
                   6543:                if(pipe_stdin_path[0] != '\0') {
1.1.1.45  root     6544: //                     if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
                   6545:                        if(msdos_is_device_path(pipe_stdin_path)) {
                   6546:                                fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
                   6547:                        } else {
                   6548:                                fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
                   6549:                        }
                   6550:                        if(fd != -1) {
                   6551:                                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     6552:                                psp->file_table[0] = fd;
                   6553:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6554:                        }
                   6555:                }
                   6556:                if(pipe_stdout_path[0] != '\0') {
                   6557:                        if(_access(pipe_stdout_path, 0) == 0) {
                   6558:                                SetFileAttributes(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6559:                                DeleteFile(pipe_stdout_path);
                   6560:                        }
1.1.1.45  root     6561: //                     if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6562:                        if(msdos_is_device_path(pipe_stdout_path)) {
                   6563:                                fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6564:                        } else {
                   6565:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6566:                        }
                   6567:                        if(fd != -1) {
                   6568:                                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     6569:                                psp->file_table[1] = fd;
                   6570:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6571:                        }
                   6572:                }
1.1.1.39  root     6573:                if(pipe_stderr_path[0] != '\0') {
                   6574:                        if(_access(pipe_stderr_path, 0) == 0) {
                   6575:                                SetFileAttributes(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
                   6576:                                DeleteFile(pipe_stderr_path);
                   6577:                        }
1.1.1.45  root     6578: //                     if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6579:                        if(msdos_is_device_path(pipe_stderr_path)) {
                   6580:                                fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6581:                        } else {
                   6582:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6583:                        }
                   6584:                        if(fd != -1) {
                   6585:                                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     6586:                                psp->file_table[2] = fd;
                   6587:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6588:                        }
                   6589:                }
1.1.1.38  root     6590:                
1.1       root     6591:                // registers and segments
                   6592:                REG16(AX) = REG16(BX) = 0x00;
                   6593:                REG16(CX) = 0xff;
                   6594:                REG16(DX) = psp_seg;
                   6595:                REG16(SI) = ip;
                   6596:                REG16(DI) = sp;
                   6597:                REG16(SP) = sp;
1.1.1.3   root     6598:                SREG(DS) = SREG(ES) = psp_seg;
                   6599:                SREG(SS) = ss;
                   6600:                i386_load_segment_descriptor(DS);
                   6601:                i386_load_segment_descriptor(ES);
                   6602:                i386_load_segment_descriptor(SS);
1.1       root     6603:                
                   6604:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6605:                i386_jmp_far(cs, ip);
                   6606:        } else if(al == 0x01) {
                   6607:                // copy ss:sp and cs:ip to param block
                   6608:                param->sp = sp;
                   6609:                param->ss = ss;
                   6610:                param->ip = ip;
                   6611:                param->cs = cs;
1.1.1.31  root     6612:                
                   6613:                // the AX value to be passed to the child program is put on top of the child's stack
                   6614:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6615:        }
                   6616:        return(0);
                   6617: }
                   6618: 
                   6619: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6620: {
                   6621:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6622:        
                   6623:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6624:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6625:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6626:        
1.1.1.3   root     6627:        SREG(SS) = psp->stack.w.h;
                   6628:        i386_load_segment_descriptor(SS);
1.1       root     6629:        REG16(SP) = psp->stack.w.l;
                   6630:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6631:        
1.1.1.28  root     6632: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6633:        process_t *current_process = NULL;
                   6634:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6635:                if(process[i].psp == psp_seg) {
                   6636:                        current_process = &process[i];
                   6637:                        break;
                   6638:                }
                   6639:        }
                   6640:        if(current_process == NULL) {
                   6641:                throw(0x1f); // general failure
                   6642:        }
                   6643:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6644:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6645:        if(current_process->called_by_int2eh) {
                   6646:                REG16(AX) = ret;
                   6647:        }
                   6648:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6649:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6650:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6651:        i386_load_segment_descriptor(ES);
1.1       root     6652:        
                   6653:        if(mem_free) {
1.1.1.8   root     6654:                int mcb_seg;
                   6655:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6656:                        msdos_mem_free(mcb_seg + 1);
                   6657:                }
                   6658:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6659:                        msdos_mem_free(mcb_seg + 1);
                   6660:                }
1.1       root     6661:                
                   6662:                for(int i = 0; i < MAX_FILES; i++) {
                   6663:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6664:                                _close(i);
1.1.1.20  root     6665:                                msdos_file_handler_close(i);
                   6666:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6667:                        }
                   6668:                }
1.1.1.13  root     6669:                msdos_dta_info_free(psp_seg);
1.1       root     6670:        }
1.1.1.14  root     6671:        msdos_stdio_reopen();
1.1       root     6672:        
1.1.1.28  root     6673:        memset(current_process, 0, sizeof(process_t));
1.1       root     6674:        
                   6675:        current_psp = psp->parent_psp;
                   6676:        retval = ret;
1.1.1.23  root     6677:        msdos_sda_update(current_psp);
1.1       root     6678: }
                   6679: 
                   6680: // drive
                   6681: 
1.1.1.42  root     6682: int pcbios_update_drive_param(int drive_num, int force_update);
                   6683: 
1.1       root     6684: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6685: {
1.1.1.41  root     6686:        if(!(drive_num >= 0 && drive_num < 26)) {
                   6687:                return(0);
                   6688:        }
1.1.1.42  root     6689:        pcbios_update_drive_param(drive_num, force_update);
                   6690:        
                   6691:        drive_param_t *drive_param = &drive_params[drive_num];
1.1       root     6692:        *seg = DPB_TOP >> 4;
                   6693:        *ofs = sizeof(dpb_t) * drive_num;
                   6694:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6695:        
                   6696:        memset(dpb, 0, sizeof(dpb_t));
                   6697:        
1.1.1.41  root     6698:        dpb->drive_num = drive_num;
                   6699:        dpb->unit_num = drive_num;
1.1.1.42  root     6700:        
                   6701:        if(drive_param->valid) {
                   6702:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   6703:                
                   6704:                dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
                   6705:                dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
                   6706:                dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6707:                dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6708:                switch(geo->MediaType) {
                   6709:                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6710:                        dpb->media_type = 0xff;
                   6711:                        break;
                   6712:                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6713:                        dpb->media_type = 0xfe;
                   6714:                        break;
                   6715:                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6716:                        dpb->media_type = 0xfd;
                   6717:                        break;
                   6718:                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6719:                        dpb->media_type = 0xfc;
                   6720:                        break;
                   6721:                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6722:                case F3_1Pt2_512:
                   6723:                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6724:                case F5_720_512:
                   6725:                        dpb->media_type = 0xf9;
                   6726:                        break;
                   6727:                case FixedMedia:        // hard disk
                   6728:                case RemovableMedia:
                   6729:                case Unknown:
                   6730:                        dpb->media_type = 0xf8;
                   6731:                        break;
                   6732:                default:
                   6733:                        dpb->media_type = 0xf0;
                   6734:                        break;
                   6735:                }
                   6736:        }
1.1.1.41  root     6737:        dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
                   6738:        dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42  root     6739:        dpb->info_sector = 0xffff;
                   6740:        dpb->backup_boot_sector = 0xffff;
                   6741:        dpb->free_clusters = 0xffff;
                   6742:        dpb->free_search_cluster = 0xffffffff;
                   6743:        
                   6744:        return(drive_param->valid);
1.1       root     6745: }
                   6746: 
                   6747: // pc bios
                   6748: 
1.1.1.35  root     6749: #ifdef USE_SERVICE_THREAD
                   6750: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   6751: {
                   6752: #if defined(HAS_I386)
                   6753:        if(m_SF != 0) {
                   6754:                m_SF = 0;
1.1.1.49  root     6755:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6756:        } else {
                   6757:                m_SF = 1;
1.1.1.49  root     6758:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6759:        }
                   6760: #else
                   6761:        if(m_SignVal < 0) {
                   6762:                m_SignVal = 0;
1.1.1.49  root     6763:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6764:        } else {
                   6765:                m_SignVal = -1;
1.1.1.49  root     6766:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6767:        }
                   6768: #endif
1.1.1.49  root     6769:        i386_call_far(DUMMY_TOP >> 4, 0x0013);
1.1.1.35  root     6770:        in_service = true;
                   6771:        service_exit = false;
                   6772:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   6773: }
                   6774: 
                   6775: void finish_service_loop()
                   6776: {
                   6777:        if(in_service && service_exit) {
                   6778: #if defined(HAS_I386)
                   6779:                if(m_SF != 0) {
                   6780:                        m_SF = 0;
                   6781:                } else {
                   6782:                        m_SF = 1;
                   6783:                }
                   6784: #else
                   6785:                if(m_SignVal < 0) {
                   6786:                        m_SignVal = 0;
                   6787:                } else {
                   6788:                        m_SignVal = -1;
                   6789:                }
                   6790: #endif
                   6791:                in_service = false;
                   6792:        }
                   6793: }
                   6794: #endif
                   6795: 
1.1.1.19  root     6796: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   6797: {
                   6798:        static unsigned __int64 start_msec_since_midnight = 0;
                   6799:        static unsigned __int64 start_msec_since_hostboot = 0;
                   6800:        
                   6801:        if(start_msec_since_midnight == 0) {
                   6802:                SYSTEMTIME time;
                   6803:                GetLocalTime(&time);
                   6804:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   6805:                start_msec_since_hostboot = cur_msec;
                   6806:        }
                   6807:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   6808:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   6809:        return (UINT32)tick;
                   6810: }
                   6811: 
                   6812: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   6813: {
                   6814:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   6815:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   6816:        
                   6817:        if(prev_tick > next_tick) {
                   6818:                mem[0x470] = 1;
                   6819:        }
                   6820:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   6821: }
                   6822: 
1.1.1.14  root     6823: inline void pcbios_irq0()
                   6824: {
                   6825:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     6826:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     6827: }
                   6828: 
1.1.1.16  root     6829: int pcbios_get_text_vram_address(int page)
1.1       root     6830: {
                   6831:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6832:                return TEXT_VRAM_TOP;
1.1       root     6833:        } else {
1.1.1.14  root     6834:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     6835:        }
                   6836: }
                   6837: 
1.1.1.16  root     6838: int pcbios_get_shadow_buffer_address(int page)
1.1       root     6839: {
1.1.1.14  root     6840:        if(!int_10h_feh_called) {
1.1.1.16  root     6841:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     6842:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6843:                return SHADOW_BUF_TOP;
                   6844:        } else {
1.1.1.14  root     6845:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     6846:        }
                   6847: }
                   6848: 
1.1.1.16  root     6849: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     6850: {
1.1.1.16  root     6851:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     6852: }
                   6853: 
1.1.1.56  root     6854: bool pcbios_set_font_size(int width, int height)
                   6855: {
                   6856:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   6857:        return(set_console_font_size(hStdout, width, height));
                   6858: }
                   6859: 
1.1.1.16  root     6860: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     6861: {
1.1.1.14  root     6862:        // clear the existing screen, not just the new one
                   6863:        int clr_height = max(height, scr_height);
                   6864:        
1.1.1.16  root     6865:        if(scr_width != width || scr_height != height) {
                   6866:                change_console_size(width, height);
1.1.1.14  root     6867:        }
                   6868:        mem[0x462] = 0;
                   6869:        *(UINT16 *)(mem + 0x44e) = 0;
                   6870:        
1.1.1.16  root     6871:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   6872:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   6873:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   6874:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1.1.51  root     6875:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     6876:        
1.1.1.23  root     6877:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     6878:        if(clr_screen) {
1.1.1.14  root     6879:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   6880:                        mem[ofs++] = 0x20;
                   6881:                        mem[ofs++] = 0x07;
                   6882:                }
                   6883:                
1.1.1.35  root     6884: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6885:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6886: #endif
1.1.1.14  root     6887:                for(int y = 0; y < clr_height; y++) {
                   6888:                        for(int x = 0; x < scr_width; x++) {
                   6889:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6890:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     6891:                        }
                   6892:                }
                   6893:                SMALL_RECT rect;
1.1.1.14  root     6894:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   6895:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6896:                vram_length_char = vram_last_length_char = 0;
                   6897:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     6898: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6899:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6900: #endif
1.1       root     6901:        }
1.1.1.14  root     6902:        COORD co;
                   6903:        co.X = 0;
                   6904:        co.Y = scr_top;
                   6905:        SetConsoleCursorPosition(hStdout, co);
                   6906:        cursor_moved = true;
                   6907:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     6908: }
                   6909: 
1.1.1.36  root     6910: void pcbios_update_cursor_position()
                   6911: {
                   6912:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6913:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   6914:        if(!restore_console_on_exit) {
                   6915:                scr_top = csbi.srWindow.Top;
                   6916:        }
                   6917:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   6918:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   6919: }
                   6920: 
1.1.1.16  root     6921: inline void pcbios_int_10h_00h()
                   6922: {
                   6923:        switch(REG8(AL) & 0x7f) {
                   6924:        case 0x70: // v-text mode
                   6925:        case 0x71: // extended cga v-text mode
                   6926:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   6927:                break;
                   6928:        default:
                   6929:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   6930:                break;
                   6931:        }
                   6932:        if(REG8(AL) & 0x80) {
                   6933:                mem[0x487] |= 0x80;
                   6934:        } else {
                   6935:                mem[0x487] &= ~0x80;
                   6936:        }
                   6937:        mem[0x449] = REG8(AL) & 0x7f;
                   6938: }
                   6939: 
1.1       root     6940: inline void pcbios_int_10h_01h()
                   6941: {
1.1.1.13  root     6942:        mem[0x460] = REG8(CL);
                   6943:        mem[0x461] = REG8(CH);
1.1.1.14  root     6944:        
1.1.1.23  root     6945:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6946:        CONSOLE_CURSOR_INFO ci;
                   6947:        GetConsoleCursorInfo(hStdout, &ci);
                   6948: //     ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
                   6949: //     if(ci.bVisible) {
                   6950:                int lines = max(8, REG8(CL) + 1);
                   6951:                ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
                   6952: //     }
                   6953:        SetConsoleCursorInfo(hStdout, &ci);
1.1       root     6954: }
                   6955: 
                   6956: inline void pcbios_int_10h_02h()
                   6957: {
1.1.1.14  root     6958:        // continuously setting the cursor effectively stops it blinking
                   6959:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     6960:                COORD co;
                   6961:                co.X = REG8(DL);
1.1.1.14  root     6962:                co.Y = REG8(DH) + scr_top;
                   6963:                
                   6964:                // some programs hide the cursor by moving it off screen
                   6965:                static bool hidden = false;
1.1.1.23  root     6966:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6967:                CONSOLE_CURSOR_INFO ci;
                   6968:                GetConsoleCursorInfo(hStdout, &ci);
                   6969:                
                   6970:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   6971:                        if(ci.bVisible) {
                   6972:                                ci.bVisible = FALSE;
                   6973: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6974:                                hidden = true;
                   6975:                        }
                   6976:                } else if(hidden) {
                   6977:                        if(!ci.bVisible) {
                   6978:                                ci.bVisible = TRUE;
                   6979: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6980:                        }
                   6981:                        hidden = false;
                   6982:                }
1.1       root     6983:        }
1.1.1.14  root     6984:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   6985:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     6986: }
                   6987: 
                   6988: inline void pcbios_int_10h_03h()
                   6989: {
1.1.1.14  root     6990:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6991:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6992:        REG8(CL) = mem[0x460];
                   6993:        REG8(CH) = mem[0x461];
                   6994: }
                   6995: 
                   6996: inline void pcbios_int_10h_05h()
                   6997: {
1.1.1.14  root     6998:        if(REG8(AL) >= vram_pages) {
                   6999:                return;
                   7000:        }
                   7001:        if(mem[0x462] != REG8(AL)) {
                   7002:                vram_flush();
                   7003:                
1.1.1.23  root     7004:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7005:                SMALL_RECT rect;
1.1.1.14  root     7006:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   7007:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7008:                
1.1.1.16  root     7009:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     7010:                        for(int x = 0; x < scr_width; x++) {
                   7011:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7012:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7013:                        }
                   7014:                }
1.1.1.16  root     7015:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     7016:                        for(int x = 0; x < scr_width; x++) {
                   7017:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   7018:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     7019:                        }
                   7020:                }
1.1.1.14  root     7021:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7022:                
                   7023:                COORD co;
1.1.1.14  root     7024:                co.X = mem[0x450 + REG8(AL) * 2];
                   7025:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   7026:                if(co.Y < scr_top + scr_height) {
                   7027:                        SetConsoleCursorPosition(hStdout, co);
                   7028:                }
1.1       root     7029:        }
1.1.1.14  root     7030:        mem[0x462] = REG8(AL);
                   7031:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   7032:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     7033:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     7034:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     7035:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     7036:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     7037:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     7038: }
                   7039: 
                   7040: inline void pcbios_int_10h_06h()
                   7041: {
1.1.1.14  root     7042:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7043:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7044:                return;
                   7045:        }
                   7046:        vram_flush();
                   7047:        
1.1.1.23  root     7048:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7049:        SMALL_RECT rect;
1.1.1.14  root     7050:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   7051:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   7052:        
                   7053:        int right = min(REG8(DL), scr_width - 1);
                   7054:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7055:        
                   7056:        if(REG8(AL) == 0) {
1.1.1.14  root     7057:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7058:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7059:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7060:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7061:                        }
                   7062:                }
                   7063:        } else {
1.1.1.14  root     7064:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     7065:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7066:                                if(y2 <= bottom) {
                   7067:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7068:                                } else {
1.1.1.14  root     7069:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7070:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7071:                                }
1.1.1.14  root     7072:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7073:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7074:                        }
                   7075:                }
                   7076:        }
1.1.1.14  root     7077:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7078: }
                   7079: 
                   7080: inline void pcbios_int_10h_07h()
                   7081: {
1.1.1.14  root     7082:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7083:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7084:                return;
                   7085:        }
                   7086:        vram_flush();
                   7087:        
1.1.1.23  root     7088:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7089:        SMALL_RECT rect;
1.1.1.14  root     7090:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   7091:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   7092:        
                   7093:        int right = min(REG8(DL), scr_width - 1);
                   7094:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7095:        
                   7096:        if(REG8(AL) == 0) {
1.1.1.14  root     7097:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7098:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7099:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7100:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7101:                        }
                   7102:                }
                   7103:        } else {
1.1.1.14  root     7104:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     7105:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7106:                                if(y2 >= REG8(CH)) {
                   7107:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7108:                                } else {
1.1.1.14  root     7109:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7110:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7111:                                }
1.1.1.14  root     7112:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7113:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7114:                        }
                   7115:                }
                   7116:        }
1.1.1.14  root     7117:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7118: }
                   7119: 
                   7120: inline void pcbios_int_10h_08h()
                   7121: {
                   7122:        COORD co;
                   7123:        DWORD num;
                   7124:        
1.1.1.14  root     7125:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7126:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7127:        
                   7128:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7129:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7130:                co.Y += scr_top;
                   7131:                vram_flush();
1.1       root     7132:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   7133:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   7134:                REG8(AL) = scr_char[0];
                   7135:                REG8(AH) = scr_attr[0];
                   7136:        } else {
1.1.1.16  root     7137:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     7138:        }
                   7139: }
                   7140: 
                   7141: inline void pcbios_int_10h_09h()
                   7142: {
                   7143:        COORD co;
                   7144:        
1.1.1.14  root     7145:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7146:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7147:        
1.1.1.16  root     7148:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7149:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7150:        
                   7151:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7152: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7153:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7154: #endif
1.1.1.16  root     7155:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7156:                while(dest < end) {
                   7157:                        write_text_vram_char(dest - vram, REG8(AL));
                   7158:                        mem[dest++] = REG8(AL);
                   7159:                        write_text_vram_attr(dest - vram, REG8(BL));
                   7160:                        mem[dest++] = REG8(BL);
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:                        mem[dest++] = REG8(BL);
                   7169:                }
                   7170:        }
                   7171: }
                   7172: 
                   7173: inline void pcbios_int_10h_0ah()
                   7174: {
                   7175:        COORD co;
                   7176:        
1.1.1.14  root     7177:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7178:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7179:        
1.1.1.16  root     7180:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7181:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7182:        
                   7183:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7184: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7185:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7186: #endif
1.1.1.16  root     7187:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7188:                while(dest < end) {
                   7189:                        write_text_vram_char(dest - vram, REG8(AL));
                   7190:                        mem[dest++] = REG8(AL);
                   7191:                        dest++;
1.1       root     7192:                }
1.1.1.35  root     7193: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7194:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7195: #endif
1.1       root     7196:        } else {
1.1.1.14  root     7197:                while(dest < end) {
1.1       root     7198:                        mem[dest++] = REG8(AL);
                   7199:                        dest++;
                   7200:                }
                   7201:        }
                   7202: }
                   7203: 
1.1.1.40  root     7204: HDC get_console_window_device_context()
                   7205: {
                   7206:        static HWND hwndFound = 0;
                   7207:        
                   7208:        if(hwndFound == 0) {
                   7209:                // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
                   7210:                char pszNewWindowTitle[1024];
                   7211:                char pszOldWindowTitle[1024];
                   7212:                
                   7213:                GetConsoleTitle(pszOldWindowTitle, 1024);
                   7214:                wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
                   7215:                SetConsoleTitle(pszNewWindowTitle);
                   7216:                Sleep(100);
                   7217:                hwndFound = FindWindow(NULL, pszNewWindowTitle);
                   7218:                SetConsoleTitle(pszOldWindowTitle);
                   7219:        }
                   7220:        return GetDC(hwndFound);
                   7221: }
                   7222: 
                   7223: inline void pcbios_int_10h_0ch()
                   7224: {
                   7225:        HDC hdc = get_console_window_device_context();
                   7226:        
                   7227:        if(hdc != NULL) {
                   7228:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
                   7229:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
                   7230:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
                   7231:                
                   7232:                if(REG8(AL) & 0x80) {
                   7233:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7234:                        if(color != CLR_INVALID) {
                   7235:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7236:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7237:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
                   7238:                        }
                   7239:                }
                   7240:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
                   7241:        }
                   7242: }
                   7243: 
                   7244: inline void pcbios_int_10h_0dh()
                   7245: {
                   7246:        HDC hdc = get_console_window_device_context();
                   7247:        BYTE r = 0;
                   7248:        BYTE g = 0;
                   7249:        BYTE b = 0;
                   7250:        
                   7251:        if(hdc != NULL) {
                   7252:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7253:                if(color != CLR_INVALID) {
                   7254:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7255:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7256:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
                   7257:                }
                   7258:        }
                   7259:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
                   7260: }
                   7261: 
1.1       root     7262: inline void pcbios_int_10h_0eh()
                   7263: {
1.1.1.14  root     7264:        DWORD num;
                   7265:        COORD co;
                   7266:        
1.1.1.54  root     7267:        co.X = mem[0x450 + mem[0x462] * 2];
                   7268:        co.Y = mem[0x451 + mem[0x462] * 2];
1.1.1.14  root     7269:        
                   7270:        if(REG8(AL) == 7) {
                   7271:                //MessageBeep(-1);
                   7272:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   7273:                if(REG8(AL) == 10) {
                   7274:                        vram_flush();
                   7275:                }
1.1.1.23  root     7276:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     7277:                cursor_moved = true;
                   7278:        } else {
1.1.1.54  root     7279:                int dest = pcbios_get_shadow_buffer_address(mem[0x462], co.X, co.Y);
1.1.1.35  root     7280: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7281:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7282: #endif
1.1.1.54  root     7283:                int vram = pcbios_get_shadow_buffer_address(mem[0x462]);
                   7284:                write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     7285: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7286:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7287: #endif
1.1.1.54  root     7288:                
                   7289:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   7290:                if(++co.X == scr_width) {
                   7291:                        co.X = 0;
                   7292:                        if(++co.Y == scr_height) {
                   7293:                                vram_flush();
                   7294:                                WriteConsole(hStdout, "\n", 1, &num, NULL);
1.1.1.14  root     7295:                                cursor_moved = true;
                   7296:                        }
                   7297:                }
1.1.1.54  root     7298:                if(!cursor_moved) {
                   7299:                        co.Y += scr_top;
                   7300:                        SetConsoleCursorPosition(hStdout, co);
                   7301:                        cursor_moved = true;
                   7302:                }
1.1.1.14  root     7303:                mem[dest] = REG8(AL);
                   7304:        }
1.1       root     7305: }
                   7306: 
                   7307: inline void pcbios_int_10h_0fh()
                   7308: {
                   7309:        REG8(AL) = mem[0x449];
                   7310:        REG8(AH) = mem[0x44a];
                   7311:        REG8(BH) = mem[0x462];
                   7312: }
                   7313: 
1.1.1.14  root     7314: inline void pcbios_int_10h_11h()
                   7315: {
                   7316:        switch(REG8(AL)) {
1.1.1.16  root     7317:        case 0x01:
1.1.1.14  root     7318:        case 0x11:
1.1.1.56  root     7319:                if(!pcbios_set_font_size(8, 14)) {
                   7320:                        if(active_code_page == 932) {
                   7321:                                pcbios_set_font_size(6, 13);
                   7322:                        } else {
                   7323:                                pcbios_set_font_size(8, 12);
                   7324:                        }
                   7325:                }
                   7326:                pcbios_set_console_size(80, 28, true); // 28 = 25 * 16 / 14
1.1.1.14  root     7327:                break;
1.1.1.16  root     7328:        case 0x02:
1.1.1.14  root     7329:        case 0x12:
1.1.1.56  root     7330:                if(!pcbios_set_font_size(8, 8)) {
                   7331:                        if(active_code_page == 932) {
                   7332:                                pcbios_set_font_size(5, 8);
                   7333:                        } else {
                   7334:                                pcbios_set_font_size(6, 8);
                   7335:                        }
                   7336:                }
                   7337:                pcbios_set_console_size(80, 50, true); // 50 = 25 * 16 / 8
1.1.1.14  root     7338:                break;
1.1.1.16  root     7339:        case 0x04:
1.1.1.14  root     7340:        case 0x14:
1.1.1.56  root     7341:        case 0x18:
                   7342:                if(!pcbios_set_font_size(8, 16)) {
                   7343:                        if(active_code_page == 932) {
                   7344:                                pcbios_set_font_size(8, 18);
                   7345:                        } else {
                   7346:                                pcbios_set_font_size(10, 18);
                   7347:                        }
                   7348:                }
                   7349:                if(REG8(AL) == 0x18) {
                   7350:                        pcbios_set_console_size(80, 50, true);
                   7351:                } else {
                   7352:                        pcbios_set_console_size(80, 25, true);
                   7353:                }
1.1.1.14  root     7354:                break;
                   7355:        case 0x30:
                   7356:                SREG(ES) = 0;
                   7357:                i386_load_segment_descriptor(ES);
                   7358:                REG16(BP) = 0;
                   7359:                REG16(CX) = mem[0x485];
                   7360:                REG8(DL) = mem[0x484];
                   7361:                break;
1.1.1.54  root     7362:        default:
                   7363:                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));
                   7364:                m_CF = 1;
                   7365:                break;
1.1.1.14  root     7366:        }
                   7367: }
                   7368: 
                   7369: inline void pcbios_int_10h_12h()
                   7370: {
1.1.1.16  root     7371:        switch(REG8(BL)) {
                   7372:        case 0x10:
1.1.1.14  root     7373:                REG16(BX) = 0x0003;
                   7374:                REG16(CX) = 0x0009;
1.1.1.16  root     7375:                break;
1.1.1.14  root     7376:        }
                   7377: }
                   7378: 
1.1       root     7379: inline void pcbios_int_10h_13h()
                   7380: {
1.1.1.3   root     7381:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     7382:        COORD co;
                   7383:        DWORD num;
                   7384:        
                   7385:        co.X = REG8(DL);
1.1.1.14  root     7386:        co.Y = REG8(DH) + scr_top;
                   7387:        
                   7388:        vram_flush();
1.1       root     7389:        
                   7390:        switch(REG8(AL)) {
                   7391:        case 0x00:
                   7392:        case 0x01:
                   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:                        if(csbi.wAttributes != REG8(BL)) {
                   7400:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   7401:                        }
1.1.1.14  root     7402:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   7403:                        
1.1       root     7404:                        if(csbi.wAttributes != REG8(BL)) {
                   7405:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7406:                        }
                   7407:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     7408:                                if(!restore_console_on_exit) {
                   7409:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7410:                                        scr_top = csbi.srWindow.Top;
                   7411:                                }
1.1.1.14  root     7412:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7413:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7414:                                SetConsoleCursorPosition(hStdout, co);
                   7415:                        } else {
                   7416:                                cursor_moved = true;
                   7417:                        }
                   7418:                } else {
1.1.1.3   root     7419:                        m_CF = 1;
1.1       root     7420:                }
                   7421:                break;
                   7422:        case 0x02:
                   7423:        case 0x03:
                   7424:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7425:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7426:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7427:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7428:                        SetConsoleCursorPosition(hStdout, co);
                   7429:                        
                   7430:                        WORD wAttributes = csbi.wAttributes;
                   7431:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7432:                                if(wAttributes != mem[ofs + 1]) {
                   7433:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7434:                                        wAttributes = mem[ofs + 1];
                   7435:                                }
1.1.1.14  root     7436:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7437:                        }
                   7438:                        if(csbi.wAttributes != wAttributes) {
                   7439:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7440:                        }
                   7441:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7442:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7443:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7444:                                SetConsoleCursorPosition(hStdout, co);
                   7445:                        } else {
                   7446:                                cursor_moved = true;
                   7447:                        }
                   7448:                } else {
1.1.1.3   root     7449:                        m_CF = 1;
1.1       root     7450:                }
                   7451:                break;
                   7452:        case 0x10:
                   7453:        case 0x11:
                   7454:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7455:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7456:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   7457:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7458:                        for(int i = 0; i < num; i++) {
                   7459:                                mem[ofs++] = scr_char[i];
                   7460:                                mem[ofs++] = scr_attr[i];
1.1.1.45  root     7461:                                if(REG8(AL) & 0x01) {
1.1       root     7462:                                        mem[ofs++] = 0;
                   7463:                                        mem[ofs++] = 0;
                   7464:                                }
                   7465:                        }
                   7466:                } else {
1.1.1.16  root     7467:                        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     7468:                                mem[ofs++] = mem[src++];
                   7469:                                mem[ofs++] = mem[src++];
1.1.1.45  root     7470:                                if(REG8(AL) & 0x01) {
1.1       root     7471:                                        mem[ofs++] = 0;
                   7472:                                        mem[ofs++] = 0;
                   7473:                                }
1.1.1.14  root     7474:                                if(++co.X == scr_width) {
                   7475:                                        if(++co.Y == scr_height) {
1.1       root     7476:                                                break;
                   7477:                                        }
                   7478:                                        co.X = 0;
                   7479:                                }
                   7480:                        }
                   7481:                }
                   7482:                break;
1.1.1.45  root     7483:        case 0x12: // ???
                   7484:        case 0x13: // ???
1.1       root     7485:        case 0x20:
                   7486:        case 0x21:
                   7487:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7488:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7489:                        int len = min(REG16(CX), scr_width * scr_height);
                   7490:                        for(int i = 0; i < len; i++) {
1.1       root     7491:                                scr_char[i] = mem[ofs++];
                   7492:                                scr_attr[i] = mem[ofs++];
1.1.1.45  root     7493:                                if(REG8(AL) & 0x01) {
1.1       root     7494:                                        ofs += 2;
                   7495:                                }
                   7496:                        }
1.1.1.14  root     7497:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7498:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7499:                } else {
1.1.1.16  root     7500:                        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     7501:                                mem[dest++] = mem[ofs++];
                   7502:                                mem[dest++] = mem[ofs++];
1.1.1.45  root     7503:                                if(REG8(AL) & 0x01) {
1.1       root     7504:                                        ofs += 2;
                   7505:                                }
1.1.1.14  root     7506:                                if(++co.X == scr_width) {
                   7507:                                        if(++co.Y == scr_height) {
1.1       root     7508:                                                break;
                   7509:                                        }
                   7510:                                        co.X = 0;
                   7511:                                }
                   7512:                        }
                   7513:                }
                   7514:                break;
                   7515:        default:
1.1.1.22  root     7516:                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     7517:                m_CF = 1;
1.1       root     7518:                break;
                   7519:        }
                   7520: }
                   7521: 
1.1.1.30  root     7522: inline void pcbios_int_10h_18h()
                   7523: {
                   7524:        switch(REG8(AL)) {
                   7525:        case 0x00:
                   7526:        case 0x01:
                   7527: //             REG8(AL) = 0x86;
                   7528:                REG8(AL) = 0x00;
                   7529:                break;
                   7530:        default:
                   7531:                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));
                   7532:                m_CF = 1;
                   7533:                break;
                   7534:        }
                   7535: }
                   7536: 
1.1.1.14  root     7537: inline void pcbios_int_10h_1ah()
                   7538: {
                   7539:        switch(REG8(AL)) {
                   7540:        case 0x00:
                   7541:                REG8(AL) = 0x1a;
                   7542:                REG8(BL) = 0x08;
                   7543:                REG8(BH) = 0x00;
                   7544:                break;
                   7545:        default:
1.1.1.22  root     7546:                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     7547:                m_CF = 1;
                   7548:                break;
                   7549:        }
                   7550: }
                   7551: 
1.1       root     7552: inline void pcbios_int_10h_1dh()
                   7553: {
                   7554:        switch(REG8(AL)) {
1.1.1.43  root     7555:        case 0x00:
                   7556:                // DOS/V Shift Status Line Control is not supported
                   7557:                m_CF = 1;
                   7558:                break;
1.1       root     7559:        case 0x01:
                   7560:                break;
                   7561:        case 0x02:
                   7562:                REG16(BX) = 0;
                   7563:                break;
                   7564:        default:
1.1.1.22  root     7565:                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));
                   7566:                m_CF = 1;
                   7567:                break;
                   7568:        }
                   7569: }
                   7570: 
                   7571: inline void pcbios_int_10h_4fh()
                   7572: {
                   7573:        switch(REG8(AL)) {
                   7574:        case 0x00:
                   7575:                REG8(AH) = 0x02; // not supported
                   7576:                break;
                   7577:        case 0x01:
                   7578:        case 0x02:
                   7579:        case 0x03:
                   7580:        case 0x04:
                   7581:        case 0x05:
                   7582:        case 0x06:
                   7583:        case 0x07:
                   7584:        case 0x08:
                   7585:        case 0x09:
                   7586:        case 0x0a:
                   7587:        case 0x0b:
                   7588:        case 0x0c:
                   7589:                REG8(AH) = 0x01; // failed
                   7590:                break;
                   7591:        default:
                   7592:                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     7593:                m_CF = 1;
1.1       root     7594:                break;
                   7595:        }
                   7596: }
                   7597: 
                   7598: inline void pcbios_int_10h_82h()
                   7599: {
                   7600:        static UINT8 mode = 0;
                   7601:        
                   7602:        switch(REG8(AL)) {
1.1.1.22  root     7603:        case 0x00:
1.1       root     7604:                if(REG8(BL) != 0xff) {
                   7605:                        mode = REG8(BL);
                   7606:                }
                   7607:                REG8(AL) = mode;
                   7608:                break;
                   7609:        default:
1.1.1.22  root     7610:                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     7611:                m_CF = 1;
1.1       root     7612:                break;
                   7613:        }
                   7614: }
                   7615: 
1.1.1.22  root     7616: inline void pcbios_int_10h_83h()
                   7617: {
                   7618:        static UINT8 mode = 0;
                   7619:        
                   7620:        switch(REG8(AL)) {
                   7621:        case 0x00:
                   7622:                REG16(AX) = 0; // offset???
                   7623:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7624:                i386_load_segment_descriptor(ES);
                   7625:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7626:                break;
                   7627:        default:
                   7628:                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));
                   7629:                m_CF = 1;
                   7630:                break;
                   7631:        }
                   7632: }
                   7633: 
                   7634: inline void pcbios_int_10h_90h()
                   7635: {
                   7636:        REG8(AL) = mem[0x449];
                   7637: }
                   7638: 
                   7639: inline void pcbios_int_10h_91h()
                   7640: {
                   7641:        REG8(AL) = 0x04; // VGA
                   7642: }
                   7643: 
                   7644: inline void pcbios_int_10h_efh()
                   7645: {
                   7646:        REG16(DX) = 0xffff;
                   7647: }
                   7648: 
1.1       root     7649: inline void pcbios_int_10h_feh()
                   7650: {
                   7651:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7652:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7653:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7654:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7655:        }
                   7656:        int_10h_feh_called = true;
                   7657: }
                   7658: 
                   7659: inline void pcbios_int_10h_ffh()
                   7660: {
                   7661:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7662:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7663:                COORD co;
                   7664:                DWORD num;
                   7665:                
1.1.1.14  root     7666:                vram_flush();
                   7667:                
                   7668:                co.X = (REG16(DI) >> 1) % scr_width;
                   7669:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7670:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7671:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7672:                int len;
                   7673:                for(len = 0; ofs < end; len++) {
                   7674:                        scr_char[len] = mem[ofs++];
                   7675:                        scr_attr[len] = mem[ofs++];
                   7676:                }
                   7677:                co.Y += scr_top;
                   7678:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7679:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7680:        }
                   7681:        int_10h_ffh_called = true;
                   7682: }
                   7683: 
1.1.1.42  root     7684: int pcbios_update_drive_param(int drive_num, int force_update)
                   7685: {
                   7686:        if(drive_num >= 0 && drive_num < 26) {
                   7687:                drive_param_t *drive_param = &drive_params[drive_num];
                   7688:                
                   7689:                if(force_update || !drive_param->initialized) {
                   7690:                        drive_param->valid = 0;
                   7691:                        char dev[64];
                   7692:                        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7693:                        
                   7694:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   7695:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7696:                                DWORD dwSize;
                   7697:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
                   7698:                                        drive_param->valid = 1;
                   7699:                                }
                   7700:                                CloseHandle(hFile);
                   7701:                        }
                   7702:                        drive_param->initialized = 1;
                   7703:                }
                   7704:                return(drive_param->valid);
                   7705:        }
                   7706:        return(0);
                   7707: }
                   7708: 
                   7709: inline void pcbios_int_13h_00h()
                   7710: {
                   7711:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7712:        
                   7713:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7714:                REG8(AH) = 0x00; // successful completion
                   7715:        } else {
                   7716:                if(REG8(DL) & 0x80) {
                   7717:                        REG8(AH) = 0x05; // reset failed (hard disk)
                   7718:                } else {
                   7719:                        REG8(AH) = 0x80; // timeout (not ready)
                   7720:                }
                   7721:                m_CF = 1;
                   7722:        }
                   7723: }
                   7724: 
                   7725: inline void pcbios_int_13h_02h()
                   7726: {
                   7727:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7728:        
                   7729:        if(REG8(AL) == 0) {
                   7730:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7731:                m_CF = 1;
                   7732:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7733:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7734:                m_CF = 1;
                   7735:        } else {
                   7736:                drive_param_t *drive_param = &drive_params[drive_num];
                   7737:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7738:                char dev[64];
                   7739:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7740:                
                   7741:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7742:                if(hFile == INVALID_HANDLE_VALUE) {
                   7743:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7744:                        m_CF = 1;
                   7745:                } else {
                   7746:                        UINT32 sector_num = REG8(AL);
                   7747:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7748:                        UINT32 head = REG8(DH);
                   7749:                        UINT32 sector = REG8(CL) & 0x3f;
                   7750:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7751:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7752:                        DWORD dwSize;
                   7753:                        
                   7754: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7755: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7756: //                             m_CF = 1;
                   7757: //                     } else 
                   7758:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7759:                                REG8(AH) = 0x04; // sector not found/read error
                   7760:                                m_CF = 1;
                   7761:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7762:                                REG8(AH) = 0x04; // sector not found/read error
                   7763:                                m_CF = 1;
                   7764:                        } else {
                   7765:                                REG8(AH) = 0x00; // successful completion
                   7766:                        }
                   7767:                        CloseHandle(hFile);
                   7768:                }
                   7769:        }
                   7770: }
                   7771: 
                   7772: inline void pcbios_int_13h_03h()
                   7773: {
                   7774:        // this operation may cause serious damage for drives, so support only floppy disk...
                   7775:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7776:        
                   7777:        if(REG8(AL) == 0) {
                   7778:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7779:                m_CF = 1;
                   7780:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7781:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7782:                m_CF = 1;
                   7783:        } else if(!drive_params[drive_num].is_fdd()) {
                   7784:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7785:                m_CF = 1;
                   7786:        } else {
                   7787:                drive_param_t *drive_param = &drive_params[drive_num];
                   7788:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7789:                char dev[64];
                   7790:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7791:                
                   7792:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7793:                if(hFile == INVALID_HANDLE_VALUE) {
                   7794:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7795:                        m_CF = 1;
                   7796:                } else {
                   7797:                        UINT32 sector_num = REG8(AL);
                   7798:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7799:                        UINT32 head = REG8(DH);
                   7800:                        UINT32 sector = REG8(CL) & 0x3f;
                   7801:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7802:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7803:                        DWORD dwSize;
                   7804:                        
                   7805: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7806: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7807: //                             m_CF = 1;
                   7808: //                     } else 
                   7809:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7810:                                REG8(AH) = 0x04; // sector not found/read error
                   7811:                                m_CF = 1;
                   7812:                        } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7813:                                REG8(AH) = 0x04; // sector not found/read error
                   7814:                                m_CF = 1;
                   7815:                        } else {
                   7816:                                REG8(AH) = 0x00; // successful completion
                   7817:                        }
                   7818:                        CloseHandle(hFile);
                   7819:                }
                   7820:        }
                   7821: }
                   7822: 
                   7823: inline void pcbios_int_13h_04h()
                   7824: {
                   7825:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7826:        
                   7827:        if(REG8(AL) == 0) {
                   7828:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7829:                m_CF = 1;
                   7830:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7831:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7832:                m_CF = 1;
                   7833:        } else {
                   7834:                drive_param_t *drive_param = &drive_params[drive_num];
                   7835:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7836:                char dev[64];
                   7837:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7838:                
                   7839:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7840:                if(hFile == INVALID_HANDLE_VALUE) {
                   7841:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7842:                        m_CF = 1;
                   7843:                } else {
                   7844:                        UINT32 sector_num = REG8(AL);
                   7845:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7846:                        UINT32 head = REG8(DH);
                   7847:                        UINT32 sector = REG8(CL) & 0x3f;
                   7848:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7849:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7850:                        DWORD dwSize;
                   7851:                        UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
                   7852:                        
                   7853: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7854: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7855: //                             m_CF = 1;
                   7856: //                     } else 
                   7857:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7858:                                REG8(AH) = 0x04; // sector not found/read error
                   7859:                                m_CF = 1;
                   7860:                        } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7861:                                REG8(AH) = 0x04; // sector not found/read error
                   7862:                                m_CF = 1;
                   7863:                        } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
                   7864:                                REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
                   7865:                                m_CF = 1;
                   7866:                        } else {
                   7867:                                REG8(AH) = 0x00; // successful completion
                   7868:                        }
                   7869:                        free(tmp_buffer);
                   7870:                        CloseHandle(hFile);
                   7871:                }
                   7872:        }
                   7873: }
                   7874: 
                   7875: inline void pcbios_int_13h_08h()
                   7876: {
                   7877:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7878:        
                   7879:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7880:                drive_param_t *drive_param = &drive_params[drive_num];
                   7881:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7882:                
                   7883:                REG16(AX) = 0x0000;
                   7884:                switch(geo->MediaType) {
                   7885:                case F5_360_512:
                   7886:                case F5_320_512:
                   7887:                case F5_320_1024:
                   7888:                case F5_180_512:
                   7889:                case F5_160_512:
                   7890:                        REG8(BL) = 0x01; // 320K/360K disk
                   7891:                        break;
                   7892:                case F5_1Pt2_512:
                   7893:                case F3_1Pt2_512:
                   7894:                case F3_1Pt23_1024:
                   7895:                case F5_1Pt23_1024:
                   7896:                        REG8(BL) = 0x02; // 1.2M disk
                   7897:                        break;
                   7898:                case F3_720_512:
                   7899:                case F3_640_512:
                   7900:                case F5_640_512:
                   7901:                case F5_720_512:
                   7902:                        REG8(BL) = 0x03; // 720K disk
                   7903:                        break;
                   7904:                case F3_1Pt44_512:
                   7905:                        REG8(BL) = 0x04; // 1.44M disk
                   7906:                        break;
                   7907:                case F3_2Pt88_512:
                   7908:                        REG8(BL) = 0x06; // 2.88M disk
                   7909:                        break;
                   7910:                case RemovableMedia:
                   7911:                        REG8(BL) = 0x10; // ATAPI Removable Media Device
                   7912:                        break;
                   7913:                default:
                   7914:                        REG8(BL) = 0x00; // unknown
                   7915:                        break;
                   7916:                }
                   7917:                if(REG8(DL) & 0x80) {
                   7918:                        switch(GetLogicalDrives() & 0x0c) {
                   7919:                        case 0x00: REG8(DL) = 0x00; break;
                   7920:                        case 0x04:
                   7921:                        case 0x08: REG8(DL) = 0x01; break;
                   7922:                        case 0x0c: REG8(DL) = 0x02; break;
                   7923:                        }
                   7924:                } else {
                   7925:                        switch(GetLogicalDrives() & 0x03) {
                   7926:                        case 0x00: REG8(DL) = 0x00; break;
                   7927:                        case 0x01:
                   7928:                        case 0x02: REG8(DL) = 0x01; break;
                   7929:                        case 0x03: REG8(DL) = 0x02; break;
                   7930:                        }
                   7931:                }
                   7932:                REG8(DH) = drive_param->head_num();
                   7933:                int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
                   7934:                int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
                   7935:                REG8(CH) = cyl & 0xff;
                   7936:                REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
                   7937:        } else {
                   7938:                REG8(AH) = 0x07;
                   7939:                m_CF = 1;
                   7940:        }
                   7941: }
                   7942: 
                   7943: inline void pcbios_int_13h_10h()
                   7944: {
                   7945:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7946:        
                   7947:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7948:                REG8(AH) = 0x00; // successful completion
                   7949:        } else {
                   7950:                if(REG8(DL) & 0x80) {
                   7951:                        REG8(AH) = 0xaa; // drive not ready (hard disk)
                   7952:                } else {
                   7953:                        REG8(AH) = 0x80; // timeout (not ready)
                   7954:                }
                   7955:                m_CF = 1;
                   7956:        }
                   7957: }
                   7958: 
                   7959: inline void pcbios_int_13h_15h()
                   7960: {
                   7961:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7962:        
                   7963:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7964:                if(REG8(DL) & 0x80) {
                   7965:                        REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
                   7966:                } else {
                   7967:                        REG8(AH) = 0x03; // hard disk
                   7968:                }
                   7969:        } else {
                   7970:                REG8(AH) = 0x00; // no such drive
                   7971:        }
                   7972: }
                   7973: 
1.1.1.43  root     7974: inline void pcbios_int_13h_41h()
                   7975: {
                   7976:        if(REG16(BX) == 0x55aa) {
                   7977:                // IBM/MS INT 13 Extensions is not installed
                   7978:                REG8(AH) = 0x01;
                   7979:                m_CF = 1;
                   7980:        } else {
                   7981:                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));
                   7982:                REG8(AH) = 0x01;
                   7983:                m_CF = 1;
                   7984:        }
                   7985: }
                   7986: 
1.1.1.25  root     7987: inline void pcbios_int_14h_00h()
                   7988: {
1.1.1.29  root     7989:        if(REG16(DX) < 4) {
1.1.1.25  root     7990:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   7991:                UINT8 selector = sio_read(REG16(DX), 3);
                   7992:                selector &= ~0x3f;
                   7993:                selector |= REG8(AL) & 0x1f;
                   7994:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   7995:                sio_write(REG16(DX), 3, selector | 0x80);
                   7996:                sio_write(REG16(DX), 0, divisor & 0xff);
                   7997:                sio_write(REG16(DX), 1, divisor >> 8);
                   7998:                sio_write(REG16(DX), 3, selector);
                   7999:                REG8(AH) = sio_read(REG16(DX), 5);
                   8000:                REG8(AL) = sio_read(REG16(DX), 6);
                   8001:        } else {
                   8002:                REG8(AH) = 0x80;
                   8003:        }
                   8004: }
                   8005: 
                   8006: inline void pcbios_int_14h_01h()
                   8007: {
1.1.1.29  root     8008:        if(REG16(DX) < 4) {
1.1.1.25  root     8009:                UINT8 selector = sio_read(REG16(DX), 3);
                   8010:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8011:                sio_write(REG16(DX), 0, REG8(AL));
                   8012:                sio_write(REG16(DX), 3, selector);
                   8013:                REG8(AH) = sio_read(REG16(DX), 5);
                   8014:        } else {
                   8015:                REG8(AH) = 0x80;
                   8016:        }
                   8017: }
                   8018: 
                   8019: inline void pcbios_int_14h_02h()
                   8020: {
1.1.1.29  root     8021:        if(REG16(DX) < 4) {
1.1.1.25  root     8022:                UINT8 selector = sio_read(REG16(DX), 3);
                   8023:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8024:                REG8(AL) = sio_read(REG16(DX), 0);
                   8025:                sio_write(REG16(DX), 3, selector);
                   8026:                REG8(AH) = sio_read(REG16(DX), 5);
                   8027:        } else {
                   8028:                REG8(AH) = 0x80;
                   8029:        }
                   8030: }
                   8031: 
                   8032: inline void pcbios_int_14h_03h()
                   8033: {
1.1.1.29  root     8034:        if(REG16(DX) < 4) {
1.1.1.25  root     8035:                REG8(AH) = sio_read(REG16(DX), 5);
                   8036:                REG8(AL) = sio_read(REG16(DX), 6);
                   8037:        } else {
                   8038:                REG8(AH) = 0x80;
                   8039:        }
                   8040: }
                   8041: 
                   8042: inline void pcbios_int_14h_04h()
                   8043: {
1.1.1.29  root     8044:        if(REG16(DX) < 4) {
1.1.1.25  root     8045:                UINT8 selector = sio_read(REG16(DX), 3);
                   8046:                if(REG8(CH) <= 0x03) {
                   8047:                        selector = (selector & ~0x03) | REG8(CH);
                   8048:                }
                   8049:                if(REG8(BL) == 0x00) {
                   8050:                        selector &= ~0x04;
                   8051:                } else if(REG8(BL) == 0x01) {
                   8052:                        selector |= 0x04;
                   8053:                }
                   8054:                if(REG8(BH) == 0x00) {
                   8055:                        selector = (selector & ~0x38) | 0x00;
                   8056:                } else if(REG8(BH) == 0x01) {
                   8057:                        selector = (selector & ~0x38) | 0x08;
                   8058:                } else if(REG8(BH) == 0x02) {
                   8059:                        selector = (selector & ~0x38) | 0x18;
                   8060:                } else if(REG8(BH) == 0x03) {
                   8061:                        selector = (selector & ~0x38) | 0x28;
                   8062:                } else if(REG8(BH) == 0x04) {
                   8063:                        selector = (selector & ~0x38) | 0x38;
                   8064:                }
                   8065:                if(REG8(AL) == 0x00) {
                   8066:                        selector |= 0x40;
                   8067:                } else if(REG8(AL) == 0x01) {
                   8068:                        selector &= ~0x40;
                   8069:                }
                   8070:                if(REG8(CL) <= 0x0b) {
                   8071:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   8072:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   8073:                        sio_write(REG16(DX), 3, selector | 0x80);
                   8074:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   8075:                        sio_write(REG16(DX), 1, divisor >> 8);
                   8076:                }
                   8077:                sio_write(REG16(DX), 3, selector);
                   8078:                REG8(AH) = sio_read(REG16(DX), 5);
                   8079:                REG8(AL) = sio_read(REG16(DX), 6);
                   8080:        } else {
                   8081:                REG8(AH) = 0x80;
                   8082:        }
                   8083: }
                   8084: 
                   8085: inline void pcbios_int_14h_05h()
                   8086: {
1.1.1.29  root     8087:        if(REG16(DX) < 4) {
1.1.1.25  root     8088:                if(REG8(AL) == 0x00) {
                   8089:                        REG8(BL) = sio_read(REG16(DX), 4);
                   8090:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8091:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8092:                } else if(REG8(AL) == 0x01) {
                   8093:                        sio_write(REG16(DX), 4, REG8(BL));
                   8094:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8095:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8096:                } else {
                   8097:                        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));
                   8098:                }
                   8099:        } else {
                   8100:                REG8(AH) = 0x80;
                   8101:        }
                   8102: }
                   8103: 
1.1.1.14  root     8104: inline void pcbios_int_15h_10h()
                   8105: {
1.1.1.22  root     8106:        switch(REG8(AL)) {
                   8107:        case 0x00:
1.1.1.14  root     8108:                Sleep(10);
1.1.1.35  root     8109:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     8110:                break;
                   8111:        default:
                   8112:                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     8113:                REG8(AH) = 0x86;
                   8114:                m_CF = 1;
                   8115:        }
                   8116: }
                   8117: 
1.1       root     8118: inline void pcbios_int_15h_23h()
                   8119: {
                   8120:        switch(REG8(AL)) {
1.1.1.22  root     8121:        case 0x00:
1.1.1.8   root     8122:                REG8(CL) = cmos_read(0x2d);
                   8123:                REG8(CH) = cmos_read(0x2e);
1.1       root     8124:                break;
1.1.1.22  root     8125:        case 0x01:
1.1.1.8   root     8126:                cmos_write(0x2d, REG8(CL));
                   8127:                cmos_write(0x2e, REG8(CH));
1.1       root     8128:                break;
                   8129:        default:
1.1.1.22  root     8130:                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     8131:                REG8(AH) = 0x86;
1.1.1.3   root     8132:                m_CF = 1;
1.1       root     8133:                break;
                   8134:        }
                   8135: }
                   8136: 
                   8137: inline void pcbios_int_15h_24h()
                   8138: {
                   8139:        switch(REG8(AL)) {
1.1.1.22  root     8140:        case 0x00:
1.1.1.3   root     8141:                i386_set_a20_line(0);
1.1       root     8142:                REG8(AH) = 0;
                   8143:                break;
1.1.1.22  root     8144:        case 0x01:
1.1.1.3   root     8145:                i386_set_a20_line(1);
1.1       root     8146:                REG8(AH) = 0;
                   8147:                break;
1.1.1.22  root     8148:        case 0x02:
1.1       root     8149:                REG8(AH) = 0;
1.1.1.3   root     8150:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     8151:                REG16(CX) = 0;
                   8152:                break;
1.1.1.22  root     8153:        case 0x03:
1.1       root     8154:                REG16(AX) = 0;
                   8155:                REG16(BX) = 0;
                   8156:                break;
1.1.1.22  root     8157:        default:
                   8158:                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));
                   8159:                REG8(AH) = 0x86;
                   8160:                m_CF = 1;
                   8161:                break;
1.1       root     8162:        }
                   8163: }
                   8164: 
                   8165: inline void pcbios_int_15h_49h()
                   8166: {
1.1.1.27  root     8167:        REG8(AH) = 0x00;
                   8168:        REG8(BL) = 0x00; // DOS/V
1.1       root     8169: }
                   8170: 
1.1.1.22  root     8171: inline void pcbios_int_15h_50h()
                   8172: {
                   8173:        switch(REG8(AL)) {
                   8174:        case 0x00:
                   8175:        case 0x01:
                   8176:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   8177:                        REG8(AH) = 0x01; // invalid font type in bh
                   8178:                        m_CF = 1;
1.1.1.27  root     8179:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     8180:                        REG8(AH) = 0x02; // bl not zero
                   8181:                        m_CF = 1;
                   8182:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   8183:                        REG8(AH) = 0x04; // invalid code page
                   8184:                        m_CF = 1;
1.1.1.27  root     8185:                } else if(REG8(AL) == 0x01) {
                   8186:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     8187:                        m_CF = 1;
1.1.1.27  root     8188:                } else {
1.1.1.49  root     8189:                        // dummy font read routine is at fffc:000d
                   8190:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.27  root     8191:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     8192:                        REG16(BX) = 0x000d;
1.1.1.27  root     8193:                        REG8(AH) = 0x00; // success
1.1.1.22  root     8194:                }
                   8195:                break;
                   8196:        default:
                   8197:                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));
                   8198:                REG8(AH) = 0x86;
                   8199:                m_CF = 1;
                   8200:                break;
                   8201:        }
                   8202: }
                   8203: 
1.1.1.30  root     8204: inline void pcbios_int_15h_53h()
                   8205: {
                   8206:        switch(REG8(AL)) {
                   8207:        case 0x00:
                   8208:                // APM is not installed
                   8209:                REG8(AH) = 0x86;
                   8210:                m_CF = 1;
                   8211:                break;
                   8212:        default:
                   8213:                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));
                   8214:                REG8(AH) = 0x86;
                   8215:                m_CF = 1;
                   8216:                break;
                   8217:        }
                   8218: }
                   8219: 
1.1.1.43  root     8220: inline void pcbios_int_15h_84h()
                   8221: {
                   8222:        // joystick support (from DOSBox)
                   8223:        switch(REG16(DX)) {
                   8224:        case 0x00:
                   8225:                REG16(AX) = 0x00f0;
                   8226:                REG16(DX) = 0x0201;
                   8227:                m_CF = 1;
                   8228:                break;
                   8229:        case 0x01:
                   8230:                REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   8231:                m_CF = 1;
                   8232:                break;
                   8233:        default:
                   8234:                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));
                   8235:                REG8(AH) = 0x86;
                   8236:                m_CF = 1;
                   8237:                break;
                   8238:        }
                   8239: }
1.1.1.35  root     8240: 
                   8241: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     8242: {
                   8243:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     8244:        UINT32 msec = usec / 1000;
                   8245:        
1.1.1.54  root     8246:        while(msec && !m_exit) {
1.1.1.14  root     8247:                UINT32 tmp = min(msec, 100);
                   8248:                if(msec - tmp < 10) {
                   8249:                        tmp = msec;
                   8250:                }
                   8251:                Sleep(tmp);
                   8252:                msec -= tmp;
                   8253:        }
1.1.1.35  root     8254:        
                   8255: #ifdef USE_SERVICE_THREAD
                   8256:        service_exit = true;
                   8257: #endif
                   8258:        return(0);
                   8259: }
                   8260: 
                   8261: inline void pcbios_int_15h_86h()
                   8262: {
                   8263:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   8264: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8265:                if(!in_service && !in_service_29h) {
                   8266:                        start_service_loop(pcbios_int_15h_86h_thread);
                   8267:                } else {
                   8268: #endif
                   8269:                        pcbios_int_15h_86h_thread(NULL);
                   8270:                        REQUEST_HARDWRE_UPDATE();
                   8271: #ifdef USE_SERVICE_THREAD
                   8272:                }
1.1.1.35  root     8273: #endif
                   8274:        }
1.1       root     8275: }
                   8276: 
                   8277: inline void pcbios_int_15h_87h()
                   8278: {
                   8279:        // copy extended memory (from DOSBox)
                   8280:        int len = REG16(CX) * 2;
1.1.1.3   root     8281:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     8282:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   8283:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   8284:        memcpy(mem + dst, mem + src, len);
                   8285:        REG16(AX) = 0x00;
                   8286: }
                   8287: 
                   8288: inline void pcbios_int_15h_88h()
                   8289: {
1.1.1.17  root     8290:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     8291: }
                   8292: 
                   8293: inline void pcbios_int_15h_89h()
                   8294: {
1.1.1.21  root     8295: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     8296:        // switch to protected mode (from DOSBox)
                   8297:        write_io_byte(0x20, 0x10);
                   8298:        write_io_byte(0x21, REG8(BH));
                   8299:        write_io_byte(0x21, 0x00);
                   8300:        write_io_byte(0xa0, 0x10);
                   8301:        write_io_byte(0xa1, REG8(BL));
                   8302:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     8303:        i386_set_a20_line(1);
                   8304:        int ofs = SREG_BASE(ES) + REG16(SI);
                   8305:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   8306:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   8307:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   8308:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   8309: #if defined(HAS_I386)
                   8310:        m_cr[0] |= 1;
                   8311: #else
                   8312:        m_msw |= 1;
                   8313: #endif
                   8314:        SREG(DS) = 0x18;
                   8315:        SREG(ES) = 0x20;
                   8316:        SREG(SS) = 0x28;
                   8317:        i386_load_segment_descriptor(DS);
                   8318:        i386_load_segment_descriptor(ES);
                   8319:        i386_load_segment_descriptor(SS);
1.1.1.21  root     8320:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     8321:        REG16(SP) += 6;
1.1.1.3   root     8322: #if defined(HAS_I386)
1.1.1.21  root     8323:        UINT32 flags = get_flags();
                   8324:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8325:        set_flags(flags);
1.1.1.3   root     8326: #else
1.1.1.21  root     8327:        UINT32 flags = CompressFlags();
                   8328:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8329:        ExpandFlags(flags);
1.1.1.3   root     8330: #endif
1.1       root     8331:        REG16(AX) = 0x00;
1.1.1.21  root     8332:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     8333: #else
1.1.1.21  root     8334:        // i86/i186/v30: protected mode is not supported
1.1       root     8335:        REG8(AH) = 0x86;
1.1.1.3   root     8336:        m_CF = 1;
1.1       root     8337: #endif
                   8338: }
                   8339: 
1.1.1.21  root     8340: inline void pcbios_int_15h_8ah()
                   8341: {
                   8342:        UINT32 size = MAX_MEM - 0x100000;
                   8343:        REG16(AX) = size & 0xffff;
                   8344:        REG16(DX) = size >> 16;
                   8345: }
                   8346: 
1.1.1.54  root     8347: #ifdef EXT_BIOS_TOP
                   8348: inline void pcbios_int_15h_c1h()
                   8349: {
                   8350:        SREG(ES) = EXT_BIOS_TOP >> 4;
                   8351:        i386_load_segment_descriptor(ES);
                   8352: }
                   8353: #endif
                   8354: 
                   8355: void pcbios_read_from_ps2_mouse(UINT16 *data_1st, UINT16 *data_2nd, UINT16 *data_3rd)
                   8356: {
                   8357:        // from DOSBox DoPS2Callback()
                   8358:        UINT16 mdat = 0x08;
                   8359:        INT16 xdiff = mouse.position.x - mouse.prev_position.x;
                   8360:        INT16 ydiff = mouse.prev_position.y - mouse.position.y;
                   8361:        
                   8362:        if(mouse.buttons[0].status) {
                   8363:                mdat |= 0x01;
                   8364:        }
                   8365:        if(mouse.buttons[1].status) {
                   8366:                mdat |= 0x02;
                   8367:        }
                   8368:        mouse.prev_position.x = mouse.position.x;
                   8369:        mouse.prev_position.y = mouse.position.y;
                   8370:        if((xdiff > 0xff) || (xdiff < -0xff)) {
                   8371:                mdat |= 0x40;   // x overflow
                   8372:        }
                   8373:        if((ydiff > 0xff) || (ydiff < -0xff)) {
                   8374:                mdat |= 0x80;   // y overflow
                   8375:        }
                   8376:        xdiff %= 256;
                   8377:        ydiff %= 256;
                   8378:        if(xdiff < 0) {
                   8379:                xdiff = (0x100 + xdiff);
                   8380:                mdat |= 0x10;
                   8381:        }
                   8382:        if(ydiff < 0) {
                   8383:                ydiff = (0x100 + ydiff);
                   8384:                mdat |= 0x20;
                   8385:        }
                   8386:        *data_1st = (UINT16)mdat;
                   8387:        *data_2nd = (UINT16)(xdiff % 256);
                   8388:        *data_3rd = (UINT16)(ydiff % 256);
                   8389: }
                   8390: 
                   8391: inline void pcbios_int_15h_c2h()
                   8392: {
                   8393:        static UINT8 enabled = 0;
                   8394:        static UINT8 sampling_rate = 5;
                   8395:        static UINT8 resolution = 2;
                   8396:        static UINT8 scaling = 1;
                   8397:        
                   8398:        switch(REG8(AL)) {
                   8399:        case 0x00:
                   8400:                if(REG8(BH) == 0x00 || REG8(BH) == 0x01) {
                   8401:                        enabled = REG8(BH);
                   8402:                        REG8(AH) = 0x00; // successful
                   8403:                } else {
                   8404:                        REG8(AH) = 0x01; // invalid function
                   8405:                        m_CF = 1;
                   8406:                }
                   8407:                break;
                   8408:        case 0x01:
                   8409:                REG8(BH) = 0x00; // device id
                   8410:                REG8(BL) = 0xaa; // mouse
                   8411:        case 0x05:
                   8412:                enabled = 0;
                   8413:                sampling_rate = 5;
                   8414:                resolution = 2;
                   8415:                scaling = 1;
                   8416:                REG8(AH) = 0x00; // successful
                   8417:                break;
                   8418:        case 0x02:
                   8419:                sampling_rate = REG8(BH);
                   8420:                REG8(AH) = 0x00; // successful
                   8421:                break;
                   8422:        case 0x03:
                   8423:                resolution = REG8(BH);
                   8424:                REG8(AH) = 0x00; // successful
                   8425:                break;
                   8426:        case 0x04:
                   8427:                REG8(BH) = 0x00; // device id
                   8428:                REG8(AH) = 0x00; // successful
                   8429:                break;
                   8430:        case 0x06:
                   8431:                switch(REG8(BH)) {
                   8432:                case 0x00:
                   8433:                        REG8(BL) = 0x00;
                   8434:                        if(mouse.buttons[1].status) {
                   8435:                                REG8(BL) |= 0x01;
                   8436:                        }
                   8437:                        if(mouse.buttons[0].status) {
                   8438:                                REG8(BL) |= 0x04;
                   8439:                        }
                   8440:                        if(scaling == 2) {
                   8441:                                REG8(BL) |= 0x10;
                   8442:                        }
                   8443:                        REG8(CL) = resolution;
                   8444:                        switch(sampling_rate) {
                   8445:                        case 0:  REG8(DL) =  10; break;
                   8446:                        case 1:  REG8(DL) =  20; break;
                   8447:                        case 2:  REG8(DL) =  40; break;
                   8448:                        case 3:  REG8(DL) =  60; break;
                   8449:                        case 4:  REG8(DL) =  80; break;
                   8450: //                     case 5:  REG8(DL) = 100; break;
                   8451:                        case 6:  REG8(DL) = 200; break;
                   8452:                        default: REG8(DL) = 100; break;
                   8453:                        }
                   8454:                        REG8(AH) = 0x00; // successful
                   8455:                        break;
                   8456:                case 0x01:
                   8457:                case 0x02:
                   8458:                        scaling = REG8(BH);
                   8459:                        REG8(AH) = 0x00; // successful
                   8460:                        break;
                   8461:                default:
                   8462:                        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));
                   8463:                        REG8(AH) = 0x01; // invalid function
                   8464:                        m_CF = 1;
                   8465:                        break;
                   8466:                }
                   8467:                break;
                   8468:        case 0x07: // set device handler addr
                   8469:                mouse.call_addr_ps2.w.l = REG16(BX);
                   8470:                mouse.call_addr_ps2.w.h = SREG(ES);
                   8471:                REG8(AH) = 0x00; // successful
                   8472:                break;
                   8473:        case 0x08:
                   8474:                REG8(AH) = 0x00; // successful
                   8475:                break;
                   8476:        case 0x09:
                   8477:                {
                   8478:                        UINT16 data_1st, data_2nd, data_3rd;
                   8479:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   8480:                        REG8(BL) = (UINT8)(data_1st & 0xff);
                   8481:                        REG8(CL) = (UINT8)(data_2nd & 0xff);
                   8482:                        REG8(DL) = (UINT8)(data_3rd & 0xff);
                   8483:                }
                   8484:                REG8(AH) = 0x00; // successful
                   8485:                break;
                   8486:        default:
                   8487:                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));
                   8488: //             REG8(AH) = 0x86;
                   8489:                REG8(AH) = 0x01; // invalid function
                   8490:                m_CF = 1;
                   8491:                break;
                   8492:        }
                   8493: }
                   8494: 
1.1.1.3   root     8495: #if defined(HAS_I386)
1.1       root     8496: inline void pcbios_int_15h_c9h()
                   8497: {
                   8498:        REG8(AH) = 0x00;
                   8499:        REG8(CH) = cpu_type;
                   8500:        REG8(CL) = cpu_step;
                   8501: }
1.1.1.3   root     8502: #endif
1.1       root     8503: 
                   8504: inline void pcbios_int_15h_cah()
                   8505: {
                   8506:        switch(REG8(AL)) {
1.1.1.22  root     8507:        case 0x00:
1.1       root     8508:                if(REG8(BL) > 0x3f) {
                   8509:                        REG8(AH) = 0x03;
1.1.1.3   root     8510:                        m_CF = 1;
1.1       root     8511:                } else if(REG8(BL) < 0x0e) {
                   8512:                        REG8(AH) = 0x04;
1.1.1.3   root     8513:                        m_CF = 1;
1.1       root     8514:                } else {
1.1.1.8   root     8515:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     8516:                }
                   8517:                break;
1.1.1.22  root     8518:        case 0x01:
1.1       root     8519:                if(REG8(BL) > 0x3f) {
                   8520:                        REG8(AH) = 0x03;
1.1.1.3   root     8521:                        m_CF = 1;
1.1       root     8522:                } else if(REG8(BL) < 0x0e) {
                   8523:                        REG8(AH) = 0x04;
1.1.1.3   root     8524:                        m_CF = 1;
1.1       root     8525:                } else {
1.1.1.8   root     8526:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     8527:                }
                   8528:                break;
                   8529:        default:
1.1.1.22  root     8530:                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     8531:                REG8(AH) = 0x86;
1.1.1.3   root     8532:                m_CF = 1;
1.1       root     8533:                break;
                   8534:        }
                   8535: }
                   8536: 
1.1.1.22  root     8537: inline void pcbios_int_15h_e8h()
1.1.1.17  root     8538: {
1.1.1.22  root     8539:        switch(REG8(AL)) {
                   8540: #if defined(HAS_I386)
                   8541:        case 0x01:
                   8542:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   8543:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8544:                break;
1.1.1.17  root     8545: #endif
1.1.1.22  root     8546:        default:
                   8547:                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));
                   8548:                REG8(AH) = 0x86;
                   8549:                m_CF = 1;
                   8550:                break;
                   8551:        }
                   8552: }
1.1.1.17  root     8553: 
1.1.1.55  root     8554: bool pcbios_is_key_buffer_empty()
                   8555: {
                   8556:        return(*(UINT16 *)(mem + 0x41a) == *(UINT16 *)(mem + 0x41c));
                   8557: }
                   8558: 
1.1.1.51  root     8559: void pcbios_clear_key_buffer()
                   8560: {
                   8561:        key_buf_char->clear();
                   8562:        key_buf_scan->clear();
                   8563:        
                   8564:        // update key buffer
                   8565:        *(UINT16 *)(mem + 0x41a) = *(UINT16 *)(mem + 0x41c); // head = tail
                   8566: }
                   8567: 
                   8568: void pcbios_set_key_buffer(int key_char, int key_scan)
                   8569: {
                   8570:        // update key buffer
                   8571:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8572:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8573:        UINT16 next = tail + 2;
                   8574:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8575:                next = *(UINT16 *)(mem + 0x480);
                   8576:        }
                   8577:        if(next != head) {
                   8578:                *(UINT16 *)(mem + 0x41c) = next;
                   8579:                mem[0x400 + (tail++)] = key_char;
                   8580:                mem[0x400 + (tail++)] = key_scan;
1.1.1.55  root     8581:        } else {
                   8582:                // store to extra key buffer
                   8583:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8584:                        key_buf_char->write(key_char);
                   8585:                        key_buf_scan->write(key_scan);
                   8586:                }
1.1.1.51  root     8587:        }
                   8588: }
                   8589: 
                   8590: bool pcbios_get_key_buffer(int *key_char, int *key_scan)
                   8591: {
                   8592:        // update key buffer
                   8593:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8594:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8595:        UINT16 next = head + 2;
                   8596:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8597:                next = *(UINT16 *)(mem + 0x480);
                   8598:        }
                   8599:        if(head != tail) {
                   8600:                *(UINT16 *)(mem + 0x41a) = next;
1.1.1.55  root     8601:                *key_char = mem[0x400 + (head++)];
                   8602:                *key_scan = mem[0x400 + (head++)];
                   8603:                
                   8604:                // restore from extra key buffer
                   8605:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8606:                        if(!key_buf_char->empty()) {
                   8607:                                pcbios_set_key_buffer(key_buf_char->read(), key_buf_scan->read());
                   8608:                        }
                   8609:                }
                   8610:                return(true);
                   8611:        } else {
                   8612:                *key_char = 0x00;
                   8613:                *key_scan = 0x00;
                   8614:                return(false);
1.1.1.51  root     8615:        }
                   8616: }
                   8617: 
1.1.1.33  root     8618: void pcbios_update_key_code(bool wait)
1.1       root     8619: {
1.1.1.32  root     8620:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8621: #ifdef USE_SERVICE_THREAD
                   8622:                EnterCriticalSection(&key_buf_crit_sect);
                   8623: #endif
1.1.1.55  root     8624:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     8625: #ifdef USE_SERVICE_THREAD
                   8626:                LeaveCriticalSection(&key_buf_crit_sect);
                   8627: #endif
                   8628:                if(empty) {
1.1.1.32  root     8629:                        if(!update_key_buffer()) {
1.1.1.33  root     8630:                                if(wait) {
1.1.1.32  root     8631:                                        Sleep(10);
                   8632:                                } else {
                   8633:                                        maybe_idle();
                   8634:                                }
1.1.1.14  root     8635:                        }
                   8636:                }
1.1.1.34  root     8637:        }
                   8638:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8639: #ifdef USE_SERVICE_THREAD
                   8640:                EnterCriticalSection(&key_buf_crit_sect);
                   8641: #endif
1.1.1.51  root     8642:                int key_char, key_scan;
                   8643:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8644:                        key_code  = key_char << 0;
                   8645:                        key_code |= key_scan << 8;
1.1.1.35  root     8646:                        key_recv  = 0x0000ffff;
1.1.1.51  root     8647:                }
                   8648:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8649:                        key_code |= key_char << 16;
                   8650:                        key_code |= key_scan << 24;
1.1.1.33  root     8651:                        key_recv |= 0xffff0000;
1.1.1.32  root     8652:                }
1.1.1.35  root     8653: #ifdef USE_SERVICE_THREAD
                   8654:                LeaveCriticalSection(&key_buf_crit_sect);
                   8655: #endif
1.1       root     8656:        }
                   8657: }
                   8658: 
1.1.1.35  root     8659: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     8660: {
1.1.1.54  root     8661:        while(key_recv == 0 && !m_exit) {
1.1.1.33  root     8662:                pcbios_update_key_code(true);
1.1       root     8663:        }
1.1.1.33  root     8664:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8665:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   8666:                        if(REG8(AH) == 0x10) {
                   8667:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   8668:                        } else {
                   8669:                                key_code = ((key_code >> 16) & 0xff00);
                   8670:                        }
                   8671:                        key_recv >>= 16;
1.1       root     8672:                }
                   8673:        }
                   8674:        REG16(AX) = key_code & 0xffff;
                   8675:        key_code >>= 16;
1.1.1.33  root     8676:        key_recv >>= 16;
1.1.1.35  root     8677:        
                   8678: #ifdef USE_SERVICE_THREAD
                   8679:        service_exit = true;
                   8680: #endif
                   8681:        return(0);
                   8682: }
                   8683: 
                   8684: inline void pcbios_int_16h_00h()
                   8685: {
                   8686: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8687:        if(!in_service && !in_service_29h) {
                   8688:                start_service_loop(pcbios_int_16h_00h_thread);
                   8689:        } else {
                   8690: #endif
                   8691:                pcbios_int_16h_00h_thread(NULL);
                   8692:                REQUEST_HARDWRE_UPDATE();
                   8693: #ifdef USE_SERVICE_THREAD
                   8694:        }
1.1.1.35  root     8695: #endif
1.1       root     8696: }
                   8697: 
                   8698: inline void pcbios_int_16h_01h()
                   8699: {
1.1.1.33  root     8700:        if(key_recv == 0) {
                   8701:                pcbios_update_key_code(false);
1.1.1.5   root     8702:        }
1.1.1.33  root     8703:        if(key_recv != 0) {
                   8704:                UINT32 key_code_tmp = key_code;
                   8705:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8706:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   8707:                                if(REG8(AH) == 0x11) {
                   8708:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   8709:                                } else {
                   8710:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   8711:                                }
                   8712:                        }
1.1       root     8713:                }
1.1.1.5   root     8714:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     8715: #if defined(HAS_I386)
1.1.1.33  root     8716:                m_ZF = 0;
                   8717: #else
                   8718:                m_ZeroVal = 1;
                   8719: #endif
                   8720:        } else {
                   8721: #if defined(HAS_I386)
                   8722:                m_ZF = 1;
1.1.1.3   root     8723: #else
1.1.1.33  root     8724:                m_ZeroVal = 0;
1.1.1.3   root     8725: #endif
1.1.1.33  root     8726:        }
1.1       root     8727: }
                   8728: 
                   8729: inline void pcbios_int_16h_02h()
                   8730: {
                   8731:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   8732:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   8733:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   8734:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   8735:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   8736:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   8737:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   8738:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   8739: }
                   8740: 
                   8741: inline void pcbios_int_16h_03h()
                   8742: {
                   8743:        static UINT16 status = 0;
                   8744:        
                   8745:        switch(REG8(AL)) {
                   8746:        case 0x05:
                   8747:                status = REG16(BX);
                   8748:                break;
                   8749:        case 0x06:
                   8750:                REG16(BX) = status;
                   8751:                break;
                   8752:        default:
1.1.1.3   root     8753:                m_CF = 1;
1.1       root     8754:                break;
                   8755:        }
                   8756: }
                   8757: 
                   8758: inline void pcbios_int_16h_05h()
                   8759: {
1.1.1.32  root     8760:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8761: #ifdef USE_SERVICE_THREAD
                   8762:                EnterCriticalSection(&key_buf_crit_sect);
                   8763: #endif
1.1.1.51  root     8764:                pcbios_set_key_buffer(REG8(CL), REG8(CH));
1.1.1.35  root     8765: #ifdef USE_SERVICE_THREAD
                   8766:                LeaveCriticalSection(&key_buf_crit_sect);
                   8767: #endif
1.1.1.32  root     8768:        }
1.1       root     8769:        REG8(AL) = 0x00;
                   8770: }
                   8771: 
                   8772: inline void pcbios_int_16h_12h()
                   8773: {
                   8774:        pcbios_int_16h_02h();
                   8775:        
                   8776:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   8777:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   8778:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   8779:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   8780:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   8781:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   8782:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   8783:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   8784: }
                   8785: 
                   8786: inline void pcbios_int_16h_13h()
                   8787: {
                   8788:        static UINT16 status = 0;
                   8789:        
                   8790:        switch(REG8(AL)) {
                   8791:        case 0x00:
                   8792:                status = REG16(DX);
                   8793:                break;
                   8794:        case 0x01:
                   8795:                REG16(DX) = status;
                   8796:                break;
                   8797:        default:
1.1.1.22  root     8798:                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     8799:                m_CF = 1;
1.1       root     8800:                break;
                   8801:        }
                   8802: }
                   8803: 
                   8804: inline void pcbios_int_16h_14h()
                   8805: {
                   8806:        static UINT8 status = 0;
                   8807:        
                   8808:        switch(REG8(AL)) {
                   8809:        case 0x00:
                   8810:        case 0x01:
                   8811:                status = REG8(AL);
                   8812:                break;
                   8813:        case 0x02:
                   8814:                REG8(AL) = status;
                   8815:                break;
                   8816:        default:
1.1.1.22  root     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));
1.1.1.3   root     8818:                m_CF = 1;
1.1       root     8819:                break;
                   8820:        }
                   8821: }
                   8822: 
1.1.1.24  root     8823: inline void pcbios_int_16h_55h()
                   8824: {
                   8825:        switch(REG8(AL)) {
                   8826:        case 0x00:
                   8827:                // keyboard tsr is not present
                   8828:                break;
                   8829:        case 0xfe:
                   8830:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   8831:                break;
                   8832:        case 0xff:
                   8833:                break;
                   8834:        default:
                   8835:                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));
                   8836:                m_CF = 1;
                   8837:                break;
                   8838:        }
                   8839: }
                   8840: 
1.1.1.30  root     8841: inline void pcbios_int_16h_6fh()
                   8842: {
                   8843:        switch(REG8(AL)) {
                   8844:        case 0x00:
                   8845:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   8846:                break;
                   8847:        default:
                   8848:                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));
                   8849:                m_CF = 1;
                   8850:                break;
                   8851:        }
                   8852: }
                   8853: 
1.1.1.37  root     8854: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   8855: {
                   8856:        UINT8 hi = jis >> 8;
                   8857:        UINT8 lo = jis & 0xff;
                   8858:        
                   8859:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   8860:        hi = (hi - 0x21) / 2 + 0x81;
                   8861:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   8862:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   8863:        
                   8864:        return((hi << 8) + lo);
                   8865: }
                   8866: 
                   8867: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   8868: {
                   8869:        UINT8 hi = sjis >> 8;
                   8870:        UINT8 lo = sjis & 0xff;
                   8871:        
                   8872:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   8873:                return(0x2121);
                   8874:        }
                   8875:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   8876:                return(0x2121);
                   8877:        }
                   8878:        if(hi >= 0xf0 && hi <= 0xf3) {
                   8879:                // gaiji
                   8880:                if(lo >= 0x40 && lo <= 0x7e) {
                   8881:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   8882:                }
                   8883:                if(lo >= 0x80 && lo <= 0x9e) {
                   8884:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   8885:                }
                   8886:                if(lo >= 0x9f && lo <= 0xfc) {
                   8887:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   8888:                }
                   8889:        }
                   8890:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   8891:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   8892:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   8893:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   8894:        
                   8895:        return((hi << 8) + lo);
                   8896: }
                   8897: 
1.1.1.38  root     8898: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   8899: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   8900: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     8901: 
                   8902: void pcbios_printer_out(int c, UINT8 data)
                   8903: {
                   8904:        if(pio[c].conv_mode) {
                   8905:                if(pio[c].sjis_hi != 0) {
                   8906:                        if(!pio[c].jis_mode) {
                   8907:                                printer_out(c, 0x1c);
                   8908:                                printer_out(c, 0x26);
                   8909:                                pio[c].jis_mode = true;
                   8910:                        }
                   8911:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   8912:                        printer_out(c, jis >> 8);
                   8913:                        printer_out(c, jis & 0xff);
                   8914:                        pio[c].sjis_hi = 0;
                   8915:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   8916:                        printer_out(c, data);
                   8917:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8918:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8919:                        }
                   8920:                        pio[c].esc_len++;
                   8921:                        
                   8922:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8923:                        case 0x33: // 1Bh 33h XX
                   8924:                        case 0x4a: // 1Bh 4Ah XX
                   8925:                        case 0x4e: // 1Bh 4Eh XX
                   8926:                        case 0x51: // 1Bh 51h XX
                   8927:                        case 0x55: // 1Bh 55h XX
                   8928:                        case 0x6c: // 1Bh 6Ch XX
                   8929:                        case 0x71: // 1Bh 71h XX
                   8930:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     8931:                                if(pio[c].esc_len == 3) {
                   8932:                                        pio[c].esc_buf[0] = 0x00;
                   8933:                                }
                   8934:                                break;
1.1.1.38  root     8935:                        case 0x24: // 1Bh 24h XX XX
                   8936:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     8937:                                if(pio[c].esc_len == 4) {
                   8938:                                        pio[c].esc_buf[0] = 0x00;
                   8939:                                }
                   8940:                                break;
1.1.1.38  root     8941:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     8942:                                if(pio[c].esc_len >= 3) {
                   8943:                                        switch(pio[c].esc_buf[2]) {
                   8944:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   8945:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   8946:                                                        pio[c].esc_buf[0] = 0x00;
                   8947:                                                }
                   8948:                                                break;
                   8949:                                        case 32: case 33: case 38: case 39: case 40:
                   8950:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   8951:                                                        pio[c].esc_buf[0] = 0x00;
                   8952:                                                }
                   8953:                                                break;
1.1.1.38  root     8954:                                        case 71: case 72: case 73:
                   8955:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   8956:                                                        pio[c].esc_buf[0] = 0x00;
                   8957:                                                }
                   8958:                                                break;
1.1.1.37  root     8959:                                        default:
                   8960:                                                pio[c].esc_buf[0] = 0x00;
                   8961:                                                break;
                   8962:                                        }
                   8963:                                }
                   8964:                                break;
1.1.1.38  root     8965:                        case 0x40: // 1Bh 40h
1.1.1.37  root     8966:                                if(pio[c].jis_mode) {
                   8967:                                        printer_out(c, 0x1c);
                   8968:                                        printer_out(c, 0x2e);
                   8969:                                        pio[c].jis_mode = false;
                   8970:                                }
                   8971:                                pio[c].esc_buf[0] = 0x00;
                   8972:                                break;
1.1.1.38  root     8973:                        case 0x42: // 1Bh 42h data 00h
                   8974:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8975:                                if(pio[c].esc_len >= 3 && data == 0) {
                   8976:                                        pio[c].esc_buf[0] = 0x00;
                   8977:                                }
                   8978:                                break;
1.1.1.38  root     8979:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     8980:                                if(pio[c].esc_len >= 3 && data != 0) {
                   8981:                                        pio[c].esc_buf[0] = 0x00;
                   8982:                                }
                   8983:                                break;
1.1.1.38  root     8984:                        default: // 1Bh XX
1.1.1.37  root     8985:                                pio[c].esc_buf[0] = 0x00;
                   8986:                                break;
                   8987:                        }
                   8988:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   8989:                        printer_out(c, data);
                   8990:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8991:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8992:                        }
                   8993:                        pio[c].esc_len++;
                   8994:                        
                   8995:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8996:                        case 0x21: // 1Ch 21h XX
                   8997:                        case 0x2d: // 1Ch 2Dh XX
                   8998:                        case 0x57: // 1Ch 57h XX
                   8999:                        case 0x6b: // 1Ch 6Bh XX
                   9000:                        case 0x72: // 1Ch 72h XX
                   9001:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     9002:                                if(pio[c].esc_len == 3) {
                   9003:                                        pio[c].esc_buf[0] = 0x00;
                   9004:                                }
                   9005:                                break;
1.1.1.38  root     9006:                        case 0x26: // 1Ch 26h
1.1.1.37  root     9007:                                pio[c].jis_mode = true;
                   9008:                                pio[c].esc_buf[0] = 0x00;
                   9009:                                break;
1.1.1.38  root     9010:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     9011:                                pio[c].jis_mode = false;
                   9012:                                pio[c].esc_buf[0] = 0x00;
                   9013:                                break;
1.1.1.38  root     9014:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     9015:                                if(pio[c].esc_len == 76) {
                   9016:                                        pio[c].esc_buf[0] = 0x00;
                   9017:                                }
                   9018:                                break;
1.1.1.38  root     9019:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     9020:                                if(pio[c].esc_len == 6) {
                   9021:                                        pio[c].esc_buf[0] = 0x00;
                   9022:                                }
                   9023:                                break;
1.1.1.38  root     9024:                        case 0x53: // 1Ch 53h XX XX
                   9025:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     9026:                                if(pio[c].esc_len == 4) {
                   9027:                                        pio[c].esc_buf[0] = 0x00;
                   9028:                                }
                   9029:                                break;
1.1.1.38  root     9030:                        default: // 1Ch XX
1.1.1.37  root     9031:                                pio[c].esc_buf[0] = 0x00;
                   9032:                                break;
                   9033:                        }
                   9034:                } else if(data == 0x1b || data == 0x1c) {
                   9035:                        printer_out(c, data);
                   9036:                        pio[c].esc_buf[0] = data;
                   9037:                        pio[c].esc_len = 1;
                   9038:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   9039:                        pio[c].sjis_hi = data;
                   9040:                } else {
                   9041:                        if(pio[c].jis_mode) {
                   9042:                                printer_out(c, 0x1c);
                   9043:                                printer_out(c, 0x2e);
                   9044:                                pio[c].jis_mode = false;
                   9045:                        }
                   9046:                        printer_out(c, data);
                   9047:                }
                   9048:        } else {
                   9049:                if(pio[c].jis_mode) {
                   9050:                        printer_out(c, 0x1c);
                   9051:                        printer_out(c, 0x2e);
                   9052:                        pio[c].jis_mode = false;
                   9053:                }
                   9054:                printer_out(c, data);
                   9055:        }
                   9056: }
                   9057: 
                   9058: inline void pcbios_int_17h_00h()
                   9059: {
                   9060:        if(REG16(DX) < 3) {
                   9061:                pcbios_printer_out(REG16(DX), REG8(AL));
                   9062:                REG8(AH) = 0xd0;
                   9063:        }
                   9064: }
                   9065: 
                   9066: inline void pcbios_int_17h_01h()
                   9067: {
                   9068:        if(REG16(DX) < 3) {
                   9069:                REG8(AH) = 0xd0;
                   9070:        }
                   9071: }
                   9072: 
                   9073: inline void pcbios_int_17h_02h()
                   9074: {
                   9075:        if(REG16(DX) < 3) {
                   9076:                REG8(AH) = 0xd0;
                   9077:        }
                   9078: }
                   9079: 
                   9080: inline void pcbios_int_17h_03h()
                   9081: {
                   9082:        switch(REG8(AL)) {
                   9083:        case 0x00:
                   9084:                if(REG16(DX) < 3) {
                   9085:                        if(pio[REG16(DX)].jis_mode) {
                   9086:                                printer_out(REG16(DX), 0x1c);
                   9087:                                printer_out(REG16(DX), 0x2e);
                   9088:                                pio[REG16(DX)].jis_mode = false;
                   9089:                        }
                   9090:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   9091:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   9092:                        }
                   9093:                        REG16(CX) = 0x0000;
                   9094:                        REG8(AH) = 0xd0;
                   9095:                }
                   9096:                break;
                   9097:        default:
                   9098:                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));
                   9099:                break;
                   9100:        }
                   9101: }
                   9102: 
                   9103: inline void pcbios_int_17h_50h()
                   9104: {
                   9105:        switch(REG8(AL)) {
                   9106:        case 0x00:
                   9107:                if(REG16(DX) < 3) {
                   9108:                        if(REG16(BX) = 0x0001) {
                   9109:                                pio[REG16(DX)].conv_mode = false;
                   9110:                                REG8(AL) = 0x00;
                   9111:                        } else if(REG16(BX) = 0x0051) {
                   9112:                                pio[REG16(DX)].conv_mode = true;
                   9113:                                REG8(AL) = 0x00;
                   9114:                        } else {
                   9115:                                REG8(AL) = 0x01;
                   9116:                        }
                   9117:                } else {
                   9118:                        REG8(AL) = 0x02;
                   9119:                }
                   9120:                break;
                   9121:        case 0x01:
                   9122:                if(REG16(DX) < 3) {
                   9123:                        if(pio[REG16(DX)].conv_mode) {
                   9124:                                REG16(BX) = 0x0051;
                   9125:                        } else {
                   9126:                                REG16(BX) = 0x0001;
                   9127:                        }
                   9128:                        REG8(AL) = 0x00;
                   9129:                } else {
                   9130:                        REG8(AL) = 0x02;
                   9131:                }
                   9132:                break;
                   9133:        default:
                   9134:                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));
                   9135:                break;
                   9136:        }
                   9137: }
                   9138: 
                   9139: inline void pcbios_int_17h_51h()
                   9140: {
                   9141:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   9142:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   9143:        } else {
                   9144:                REG16(DX) = 0x0000;
                   9145:        }
                   9146: }
                   9147: 
                   9148: inline void pcbios_int_17h_52h()
                   9149: {
                   9150:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   9151:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   9152:        } else {
                   9153:                REG16(DX) = 0x0000;
                   9154:        }
                   9155: }
                   9156: 
                   9157: inline void pcbios_int_17h_84h()
                   9158: {
                   9159:        if(REG16(DX) < 3) {
                   9160:                if(pio[REG16(DX)].jis_mode) {
                   9161:                        printer_out(REG16(DX), 0x1c);
                   9162:                        printer_out(REG16(DX), 0x2e);
                   9163:                        pio[REG16(DX)].jis_mode = false;
                   9164:                }
                   9165:                printer_out(REG16(DX), REG8(AL));
                   9166:                REG8(AH) = 0xd0;
                   9167:        }
                   9168: }
                   9169: 
                   9170: inline void pcbios_int_17h_85h()
                   9171: {
                   9172:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   9173: }
                   9174: 
1.1       root     9175: inline void pcbios_int_1ah_00h()
                   9176: {
1.1.1.19  root     9177:        pcbios_update_daily_timer_counter(timeGetTime());
                   9178:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   9179:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   9180:        REG8(AL) = mem[0x470];
                   9181:        mem[0x470] = 0;
1.1       root     9182: }
                   9183: 
                   9184: inline int to_bcd(int t)
                   9185: {
                   9186:        int u = (t % 100) / 10;
                   9187:        return (u << 4) | (t % 10);
                   9188: }
                   9189: 
                   9190: inline void pcbios_int_1ah_02h()
                   9191: {
                   9192:        SYSTEMTIME time;
                   9193:        
                   9194:        GetLocalTime(&time);
                   9195:        REG8(CH) = to_bcd(time.wHour);
                   9196:        REG8(CL) = to_bcd(time.wMinute);
                   9197:        REG8(DH) = to_bcd(time.wSecond);
                   9198:        REG8(DL) = 0x00;
                   9199: }
                   9200: 
                   9201: inline void pcbios_int_1ah_04h()
                   9202: {
                   9203:        SYSTEMTIME time;
                   9204:        
                   9205:        GetLocalTime(&time);
                   9206:        REG8(CH) = to_bcd(time.wYear / 100);
                   9207:        REG8(CL) = to_bcd(time.wYear);
                   9208:        REG8(DH) = to_bcd(time.wMonth);
                   9209:        REG8(DL) = to_bcd(time.wDay);
                   9210: }
                   9211: 
                   9212: inline void pcbios_int_1ah_0ah()
                   9213: {
                   9214:        SYSTEMTIME time;
                   9215:        FILETIME file_time;
                   9216:        WORD dos_date, dos_time;
                   9217:        
                   9218:        GetLocalTime(&time);
                   9219:        SystemTimeToFileTime(&time, &file_time);
                   9220:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   9221:        REG16(CX) = dos_date;
                   9222: }
                   9223: 
                   9224: // msdos system call
                   9225: 
1.1.1.43  root     9226: inline void msdos_int_21h_56h(int lfn);
                   9227: 
1.1       root     9228: inline void msdos_int_21h_00h()
                   9229: {
1.1.1.3   root     9230:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     9231: }
                   9232: 
1.1.1.35  root     9233: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     9234: {
                   9235:        REG8(AL) = msdos_getche();
1.1.1.33  root     9236:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9237:        
1.1.1.35  root     9238: #ifdef USE_SERVICE_THREAD
                   9239:        service_exit = true;
                   9240: #endif
                   9241:        return(0);
                   9242: }
                   9243: 
                   9244: inline void msdos_int_21h_01h()
                   9245: {
                   9246: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9247:        if(!in_service && !in_service_29h &&
                   9248:           *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50  root     9249:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9250:                // msdos_putch() will be used in this service
                   9251:                // if int 29h is hooked, run this service in main thread to call int 29h
                   9252:                start_service_loop(msdos_int_21h_01h_thread);
                   9253:        } else {
                   9254: #endif
                   9255:                msdos_int_21h_01h_thread(NULL);
                   9256:                REQUEST_HARDWRE_UPDATE();
                   9257: #ifdef USE_SERVICE_THREAD
                   9258:        }
1.1.1.35  root     9259: #endif
1.1       root     9260: }
                   9261: 
                   9262: inline void msdos_int_21h_02h()
                   9263: {
1.1.1.33  root     9264:        UINT8 data = REG8(DL);
                   9265:        msdos_putch(data);
                   9266:        REG8(AL) = data;
                   9267:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9268: }
                   9269: 
                   9270: inline void msdos_int_21h_03h()
                   9271: {
                   9272:        REG8(AL) = msdos_aux_in();
                   9273: }
                   9274: 
                   9275: inline void msdos_int_21h_04h()
                   9276: {
                   9277:        msdos_aux_out(REG8(DL));
                   9278: }
                   9279: 
                   9280: inline void msdos_int_21h_05h()
                   9281: {
                   9282:        msdos_prn_out(REG8(DL));
                   9283: }
                   9284: 
                   9285: inline void msdos_int_21h_06h()
                   9286: {
                   9287:        if(REG8(DL) == 0xff) {
                   9288:                if(msdos_kbhit()) {
                   9289:                        REG8(AL) = msdos_getch();
1.1.1.3   root     9290: #if defined(HAS_I386)
                   9291:                        m_ZF = 0;
                   9292: #else
                   9293:                        m_ZeroVal = 1;
                   9294: #endif
1.1       root     9295:                } else {
                   9296:                        REG8(AL) = 0;
1.1.1.3   root     9297: #if defined(HAS_I386)
                   9298:                        m_ZF = 1;
                   9299: #else
                   9300:                        m_ZeroVal = 0;
                   9301: #endif
1.1.1.14  root     9302:                        maybe_idle();
1.1       root     9303:                }
                   9304:        } else {
1.1.1.33  root     9305:                UINT8 data = REG8(DL);
                   9306:                msdos_putch(data);
                   9307:                REG8(AL) = data;
1.1       root     9308:        }
                   9309: }
                   9310: 
1.1.1.35  root     9311: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     9312: {
                   9313:        REG8(AL) = msdos_getch();
1.1.1.26  root     9314:        
1.1.1.35  root     9315: #ifdef USE_SERVICE_THREAD
                   9316:        service_exit = true;
                   9317: #endif
                   9318:        return(0);
1.1       root     9319: }
                   9320: 
1.1.1.35  root     9321: inline void msdos_int_21h_07h()
                   9322: {
                   9323: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9324:        if(!in_service && !in_service_29h) {
                   9325:                start_service_loop(msdos_int_21h_07h_thread);
                   9326:        } else {
                   9327: #endif
                   9328:                msdos_int_21h_07h_thread(NULL);
                   9329:                REQUEST_HARDWRE_UPDATE();
                   9330: #ifdef USE_SERVICE_THREAD
                   9331:        }
1.1.1.35  root     9332: #endif
                   9333: }
                   9334: 
                   9335: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     9336: {
                   9337:        REG8(AL) = msdos_getch();
1.1.1.33  root     9338:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9339:        
1.1.1.35  root     9340: #ifdef USE_SERVICE_THREAD
                   9341:        service_exit = true;
                   9342: #endif
                   9343:        return(0);
                   9344: }
                   9345: 
                   9346: inline void msdos_int_21h_08h()
                   9347: {
                   9348: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9349:        if(!in_service && !in_service_29h) {
                   9350:                start_service_loop(msdos_int_21h_08h_thread);
                   9351:        } else {
                   9352: #endif
                   9353:                msdos_int_21h_08h_thread(NULL);
                   9354:                REQUEST_HARDWRE_UPDATE();
                   9355: #ifdef USE_SERVICE_THREAD
                   9356:        }
1.1.1.35  root     9357: #endif
1.1       root     9358: }
                   9359: 
                   9360: inline void msdos_int_21h_09h()
                   9361: {
1.1.1.21  root     9362:        msdos_stdio_reopen();
                   9363:        
1.1.1.20  root     9364:        process_t *process = msdos_process_info_get(current_psp);
                   9365:        int fd = msdos_psp_get_file_table(1, current_psp);
                   9366:        
1.1.1.14  root     9367:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   9368:        int len = 0;
1.1       root     9369:        
1.1.1.14  root     9370:        while(str[len] != '$' && len < 0x10000) {
                   9371:                len++;
                   9372:        }
1.1.1.20  root     9373:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9374:                // stdout is redirected to file
1.1.1.20  root     9375:                msdos_write(fd, str, len);
1.1       root     9376:        } else {
                   9377:                for(int i = 0; i < len; i++) {
1.1.1.14  root     9378:                        msdos_putch(str[i]);
1.1       root     9379:                }
                   9380:        }
1.1.1.33  root     9381:        REG8(AL) = '$';
                   9382:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9383: }
                   9384: 
1.1.1.35  root     9385: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     9386: {
1.1.1.3   root     9387:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     9388:        int max = mem[ofs] - 1;
                   9389:        UINT8 *buf = mem + ofs + 2;
                   9390:        int chr, p = 0;
                   9391:        
                   9392:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     9393:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     9394:                        p = 0;
1.1.1.33  root     9395:                        msdos_putch(0x03);
                   9396:                        msdos_putch(0x0d);
                   9397:                        msdos_putch(0x0a);
1.1.1.26  root     9398:                        break;
1.1.1.33  root     9399:                } else if(ctrl_break_pressed) {
                   9400:                        // skip this byte
1.1.1.26  root     9401:                } else if(chr == 0x00) {
1.1       root     9402:                        // skip 2nd byte
                   9403:                        msdos_getch();
                   9404:                } else if(chr == 0x08) {
                   9405:                        // back space
                   9406:                        if(p > 0) {
                   9407:                                p--;
1.1.1.20  root     9408:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     9409:                                        msdos_putch(0x08);
                   9410:                                        msdos_putch(0x08);
                   9411:                                        msdos_putch(0x20);
                   9412:                                        msdos_putch(0x20);
                   9413:                                        msdos_putch(0x08);
                   9414:                                        msdos_putch(0x08);
1.1.1.36  root     9415:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9416:                                        p--;
                   9417:                                        msdos_putch(0x08);
                   9418:                                        msdos_putch(0x08);
                   9419:                                        msdos_putch(0x20);
                   9420:                                        msdos_putch(0x20);
                   9421:                                        msdos_putch(0x08);
                   9422:                                        msdos_putch(0x08);
1.1.1.34  root     9423:                                } else {
                   9424:                                        msdos_putch(0x08);
                   9425:                                        msdos_putch(0x20);
                   9426:                                        msdos_putch(0x08);
                   9427:                                }
                   9428:                        }
                   9429:                } else if(chr == 0x1b) {
                   9430:                        // escape
                   9431:                        while(p > 0) {
                   9432:                                p--;
                   9433:                                if(msdos_ctrl_code_check(buf[p])) {
                   9434:                                        msdos_putch(0x08);
                   9435:                                        msdos_putch(0x08);
                   9436:                                        msdos_putch(0x20);
                   9437:                                        msdos_putch(0x20);
                   9438:                                        msdos_putch(0x08);
                   9439:                                        msdos_putch(0x08);
1.1.1.20  root     9440:                                } else {
1.1.1.34  root     9441:                                        msdos_putch(0x08);
                   9442:                                        msdos_putch(0x20);
                   9443:                                        msdos_putch(0x08);
1.1.1.20  root     9444:                                }
1.1       root     9445:                        }
                   9446:                } else if(p < max) {
                   9447:                        buf[p++] = chr;
                   9448:                        msdos_putch(chr);
                   9449:                }
                   9450:        }
                   9451:        buf[p] = 0x0d;
                   9452:        mem[ofs + 1] = p;
1.1.1.33  root     9453:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9454:        
1.1.1.35  root     9455: #ifdef USE_SERVICE_THREAD
                   9456:        service_exit = true;
                   9457: #endif
                   9458:        return(0);
                   9459: }
                   9460: 
                   9461: inline void msdos_int_21h_0ah()
                   9462: {
                   9463:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   9464: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9465:                if(!in_service && !in_service_29h &&
                   9466:                   *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50  root     9467:                   *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9468:                        // msdos_putch() will be used in this service
                   9469:                        // if int 29h is hooked, run this service in main thread to call int 29h
                   9470:                        start_service_loop(msdos_int_21h_0ah_thread);
                   9471:                } else {
                   9472: #endif
                   9473:                        msdos_int_21h_0ah_thread(NULL);
                   9474:                        REQUEST_HARDWRE_UPDATE();
                   9475: #ifdef USE_SERVICE_THREAD
                   9476:                }
1.1.1.35  root     9477: #endif
                   9478:        }
1.1       root     9479: }
                   9480: 
                   9481: inline void msdos_int_21h_0bh()
                   9482: {
                   9483:        if(msdos_kbhit()) {
                   9484:                REG8(AL) = 0xff;
                   9485:        } else {
                   9486:                REG8(AL) = 0x00;
1.1.1.14  root     9487:                maybe_idle();
1.1       root     9488:        }
1.1.1.33  root     9489:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9490: }
                   9491: 
                   9492: inline void msdos_int_21h_0ch()
                   9493: {
                   9494:        // clear key buffer
1.1.1.21  root     9495:        msdos_stdio_reopen();
                   9496:        
1.1.1.20  root     9497:        process_t *process = msdos_process_info_get(current_psp);
                   9498:        int fd = msdos_psp_get_file_table(0, current_psp);
                   9499:        
                   9500:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9501:                // stdin is redirected to file
                   9502:        } else {
                   9503:                while(msdos_kbhit()) {
                   9504:                        msdos_getch();
                   9505:                }
                   9506:        }
                   9507:        
                   9508:        switch(REG8(AL)) {
                   9509:        case 0x01:
                   9510:                msdos_int_21h_01h();
                   9511:                break;
                   9512:        case 0x06:
                   9513:                msdos_int_21h_06h();
                   9514:                break;
                   9515:        case 0x07:
                   9516:                msdos_int_21h_07h();
                   9517:                break;
                   9518:        case 0x08:
                   9519:                msdos_int_21h_08h();
                   9520:                break;
                   9521:        case 0x0a:
                   9522:                msdos_int_21h_0ah();
                   9523:                break;
                   9524:        default:
1.1.1.48  root     9525:                // the buffer is flushed but no input is attempted
1.1       root     9526:                break;
                   9527:        }
                   9528: }
                   9529: 
                   9530: inline void msdos_int_21h_0dh()
                   9531: {
                   9532: }
                   9533: 
                   9534: inline void msdos_int_21h_0eh()
                   9535: {
                   9536:        if(REG8(DL) < 26) {
                   9537:                _chdrive(REG8(DL) + 1);
                   9538:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9539:                msdos_sda_update(current_psp);
1.1       root     9540:        }
                   9541:        REG8(AL) = 26; // zdrive
                   9542: }
                   9543: 
1.1.1.14  root     9544: inline void msdos_int_21h_0fh()
                   9545: {
                   9546:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9547:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9548:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9549:        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     9550:        
1.1.1.14  root     9551:        if(hFile == INVALID_HANDLE_VALUE) {
                   9552:                REG8(AL) = 0xff;
                   9553:        } else {
                   9554:                REG8(AL) = 0;
                   9555:                fcb->current_block = 0;
                   9556:                fcb->record_size = 128;
                   9557:                fcb->file_size = GetFileSize(hFile, NULL);
                   9558:                fcb->handle = hFile;
                   9559:                fcb->cur_record = 0;
                   9560:        }
                   9561: }
                   9562: 
                   9563: inline void msdos_int_21h_10h()
                   9564: {
                   9565:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9566:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9567:        
                   9568:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9569: }
                   9570: 
1.1       root     9571: inline void msdos_int_21h_11h()
                   9572: {
1.1.1.3   root     9573:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9574:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9575:        
                   9576:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9577:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9578:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9579:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45  root     9580:        const char *path = msdos_fcb_path(fcb);
1.1       root     9581:        WIN32_FIND_DATA fd;
                   9582:        
1.1.1.13  root     9583:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9584:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9585:                FindClose(dtainfo->find_handle);
                   9586:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9587:        }
                   9588:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9589:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9590:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9591:        
1.1.1.14  root     9592:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9593:                dtainfo->allowable_mask &= ~8;
1.1       root     9594:        }
1.1.1.14  root     9595:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   9596:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9597:                      !msdos_find_file_has_8dot3name(&fd)) {
                   9598:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9599:                                FindClose(dtainfo->find_handle);
                   9600:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9601:                                break;
                   9602:                        }
                   9603:                }
                   9604:        }
1.1.1.13  root     9605:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9606:                if(ext_fcb->flag == 0xff) {
                   9607:                        ext_find->flag = 0xff;
                   9608:                        memset(ext_find->reserved, 0, 5);
                   9609:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9610:                }
                   9611:                find->drive = _getdrive();
1.1.1.13  root     9612:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9613:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9614:                find->nt_res = 0;
                   9615:                msdos_find_file_conv_local_time(&fd);
                   9616:                find->create_time_ms = 0;
                   9617:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9618:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9619:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9620:                find->cluster_hi = find->cluster_lo = 0;
                   9621:                find->file_size = fd.nFileSizeLow;
                   9622:                REG8(AL) = 0x00;
1.1.1.14  root     9623:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9624:                if(ext_fcb->flag == 0xff) {
                   9625:                        ext_find->flag = 0xff;
                   9626:                        memset(ext_find->reserved, 0, 5);
                   9627:                        ext_find->attribute = 8;
                   9628:                }
                   9629:                find->drive = _getdrive();
                   9630:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9631:                find->attribute = 8;
                   9632:                find->nt_res = 0;
                   9633:                msdos_find_file_conv_local_time(&fd);
                   9634:                find->create_time_ms = 0;
                   9635:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9636:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9637:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9638:                find->cluster_hi = find->cluster_lo = 0;
                   9639:                find->file_size = 0;
1.1.1.14  root     9640:                dtainfo->allowable_mask &= ~8;
1.1       root     9641:                REG8(AL) = 0x00;
                   9642:        } else {
                   9643:                REG8(AL) = 0xff;
                   9644:        }
                   9645: }
                   9646: 
                   9647: inline void msdos_int_21h_12h()
                   9648: {
1.1.1.3   root     9649:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     9650: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9651:        
                   9652:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9653:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9654:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9655:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9656:        WIN32_FIND_DATA fd;
                   9657:        
1.1.1.13  root     9658:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9659:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9660:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     9661:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9662:                              !msdos_find_file_has_8dot3name(&fd)) {
                   9663:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9664:                                        FindClose(dtainfo->find_handle);
                   9665:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9666:                                        break;
                   9667:                                }
                   9668:                        }
                   9669:                } else {
1.1.1.13  root     9670:                        FindClose(dtainfo->find_handle);
                   9671:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9672:                }
                   9673:        }
1.1.1.13  root     9674:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9675:                if(ext_fcb->flag == 0xff) {
                   9676:                        ext_find->flag = 0xff;
                   9677:                        memset(ext_find->reserved, 0, 5);
                   9678:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9679:                }
                   9680:                find->drive = _getdrive();
1.1.1.13  root     9681:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9682:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9683:                find->nt_res = 0;
                   9684:                msdos_find_file_conv_local_time(&fd);
                   9685:                find->create_time_ms = 0;
                   9686:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9687:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9688:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9689:                find->cluster_hi = find->cluster_lo = 0;
                   9690:                find->file_size = fd.nFileSizeLow;
                   9691:                REG8(AL) = 0x00;
1.1.1.14  root     9692:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9693:                if(ext_fcb->flag == 0xff) {
                   9694:                        ext_find->flag = 0xff;
                   9695:                        memset(ext_find->reserved, 0, 5);
                   9696:                        ext_find->attribute = 8;
                   9697:                }
                   9698:                find->drive = _getdrive();
                   9699:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9700:                find->attribute = 8;
                   9701:                find->nt_res = 0;
                   9702:                msdos_find_file_conv_local_time(&fd);
                   9703:                find->create_time_ms = 0;
                   9704:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9705:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9706:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9707:                find->cluster_hi = find->cluster_lo = 0;
                   9708:                find->file_size = 0;
1.1.1.14  root     9709:                dtainfo->allowable_mask &= ~8;
1.1       root     9710:                REG8(AL) = 0x00;
                   9711:        } else {
                   9712:                REG8(AL) = 0xff;
                   9713:        }
                   9714: }
                   9715: 
                   9716: inline void msdos_int_21h_13h()
                   9717: {
1.1.1.3   root     9718:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     9719:                REG8(AL) = 0xff;
                   9720:        } else {
                   9721:                REG8(AL) = 0x00;
                   9722:        }
                   9723: }
                   9724: 
1.1.1.16  root     9725: inline void msdos_int_21h_14h()
                   9726: {
                   9727:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9728:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9729:        process_t *process = msdos_process_info_get(current_psp);
                   9730:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9731:        DWORD num = 0;
                   9732:        
                   9733:        memset(mem + dta_laddr, 0, fcb->record_size);
                   9734:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9735:                REG8(AL) = 1;
                   9736:        } else {
                   9737:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9738:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9739:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9740:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9741:        }
                   9742: }
                   9743: 
                   9744: inline void msdos_int_21h_15h()
1.1.1.14  root     9745: {
                   9746:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9747:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     9748:        process_t *process = msdos_process_info_get(current_psp);
                   9749:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9750:        DWORD num = 0;
1.1.1.14  root     9751:        
1.1.1.16  root     9752:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9753:                REG8(AL) = 1;
                   9754:        } else {
                   9755:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9756:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9757:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9758:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9759:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9760:        }
                   9761: }
                   9762: 
                   9763: inline void msdos_int_21h_16h()
                   9764: {
                   9765:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9766:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9767:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9768:        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     9769:        
1.1.1.14  root     9770:        if(hFile == INVALID_HANDLE_VALUE) {
                   9771:                REG8(AL) = 0xff;
                   9772:        } else {
                   9773:                REG8(AL) = 0;
                   9774:                fcb->current_block = 0;
                   9775:                fcb->record_size = 128;
                   9776:                fcb->file_size = 0;
                   9777:                fcb->handle = hFile;
                   9778:                fcb->cur_record = 0;
                   9779:        }
                   9780: }
                   9781: 
1.1.1.16  root     9782: inline void msdos_int_21h_17h()
                   9783: {
                   9784:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9785:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45  root     9786: //     const char *path_src = msdos_fcb_path(fcb_src);
                   9787:        char path_src[MAX_PATH];
                   9788:        strcpy(path_src, msdos_fcb_path(fcb_src));
                   9789:        
1.1.1.16  root     9790:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   9791:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45  root     9792: //     const char *path_dst = msdos_fcb_path(fcb_dst);
                   9793:        char path_dst[MAX_PATH];
                   9794:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     9795:        
                   9796:        if(rename(path_src, path_dst)) {
                   9797:                REG8(AL) = 0xff;
                   9798:        } else {
                   9799:                REG8(AL) = 0;
                   9800:        }
                   9801: }
                   9802: 
1.1       root     9803: inline void msdos_int_21h_18h()
                   9804: {
                   9805:        REG8(AL) = 0x00;
                   9806: }
                   9807: 
                   9808: inline void msdos_int_21h_19h()
                   9809: {
                   9810:        REG8(AL) = _getdrive() - 1;
                   9811: }
                   9812: 
                   9813: inline void msdos_int_21h_1ah()
                   9814: {
                   9815:        process_t *process = msdos_process_info_get(current_psp);
                   9816:        
                   9817:        process->dta.w.l = REG16(DX);
1.1.1.3   root     9818:        process->dta.w.h = SREG(DS);
1.1.1.23  root     9819:        msdos_sda_update(current_psp);
1.1       root     9820: }
                   9821: 
                   9822: inline void msdos_int_21h_1bh()
                   9823: {
                   9824:        int drive_num = _getdrive() - 1;
                   9825:        UINT16 seg, ofs;
                   9826:        
                   9827:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9828:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9829:                REG8(AL) = dpb->highest_sector_num + 1;
                   9830:                REG16(CX) = dpb->bytes_per_sector;
                   9831:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9832:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9833:        } else {
                   9834:                REG8(AL) = 0xff;
1.1.1.3   root     9835:                m_CF = 1;
1.1       root     9836:        }
                   9837: 
                   9838: }
                   9839: 
                   9840: inline void msdos_int_21h_1ch()
                   9841: {
1.1.1.41  root     9842:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     9843:        UINT16 seg, ofs;
                   9844:        
                   9845:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9846:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9847:                REG8(AL) = dpb->highest_sector_num + 1;
                   9848:                REG16(CX) = dpb->bytes_per_sector;
                   9849:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9850:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9851:        } else {
                   9852:                REG8(AL) = 0xff;
1.1.1.3   root     9853:                m_CF = 1;
1.1       root     9854:        }
                   9855: 
                   9856: }
                   9857: 
                   9858: inline void msdos_int_21h_1dh()
                   9859: {
                   9860:        REG8(AL) = 0;
                   9861: }
                   9862: 
                   9863: inline void msdos_int_21h_1eh()
                   9864: {
                   9865:        REG8(AL) = 0;
                   9866: }
                   9867: 
                   9868: inline void msdos_int_21h_1fh()
                   9869: {
                   9870:        int drive_num = _getdrive() - 1;
                   9871:        UINT16 seg, ofs;
                   9872:        
                   9873:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9874:                REG8(AL) = 0;
1.1.1.3   root     9875:                SREG(DS) = seg;
                   9876:                i386_load_segment_descriptor(DS);
1.1       root     9877:                REG16(BX) = ofs;
                   9878:        } else {
                   9879:                REG8(AL) = 0xff;
1.1.1.3   root     9880:                m_CF = 1;
1.1       root     9881:        }
                   9882: }
                   9883: 
                   9884: inline void msdos_int_21h_20h()
                   9885: {
                   9886:        REG8(AL) = 0;
                   9887: }
                   9888: 
1.1.1.14  root     9889: inline void msdos_int_21h_21h()
                   9890: {
                   9891:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9892:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9893:        
                   9894:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9895:                REG8(AL) = 1;
                   9896:        } else {
                   9897:                process_t *process = msdos_process_info_get(current_psp);
                   9898:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9899:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     9900:                DWORD num = 0;
1.1.1.14  root     9901:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9902:                        REG8(AL) = 1;
                   9903:                } else {
                   9904:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9905:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9906:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     9907:                }
                   9908:        }
                   9909: }
                   9910: 
                   9911: inline void msdos_int_21h_22h()
                   9912: {
                   9913:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9914:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9915:        
                   9916:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9917:                REG8(AL) = 0xff;
                   9918:        } else {
                   9919:                process_t *process = msdos_process_info_get(current_psp);
                   9920:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     9921:                DWORD num = 0;
1.1.1.14  root     9922:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   9923:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9924:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9925:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9926:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     9927:        }
                   9928: }
                   9929: 
1.1.1.16  root     9930: inline void msdos_int_21h_23h()
                   9931: {
                   9932:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9933:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9934:        const char *path = msdos_fcb_path(fcb);
1.1.1.16  root     9935:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9936:        
                   9937:        if(hFile == INVALID_HANDLE_VALUE) {
                   9938:                REG8(AL) = 0xff;
                   9939:        } else {
                   9940:                UINT32 size = GetFileSize(hFile, NULL);
                   9941:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   9942:                REG8(AL) = 0;
                   9943:        }
                   9944: }
                   9945: 
                   9946: inline void msdos_int_21h_24h()
                   9947: {
                   9948:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9949:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9950:        
                   9951:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   9952: }
                   9953: 
1.1       root     9954: inline void msdos_int_21h_25h()
                   9955: {
                   9956:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     9957:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     9958: }
                   9959: 
                   9960: inline void msdos_int_21h_26h()
                   9961: {
                   9962:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   9963:        
                   9964:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
1.1.1.48  root     9965:        psp->first_mcb = REG16(DX) + (PSP_SIZE >> 4);
1.1       root     9966:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   9967:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   9968:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   9969:        psp->parent_psp = 0;
                   9970: }
                   9971: 
1.1.1.16  root     9972: inline void msdos_int_21h_27h()
                   9973: {
                   9974:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9975:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9976:        
                   9977:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9978:                REG8(AL) = 1;
                   9979:        } else {
                   9980:                process_t *process = msdos_process_info_get(current_psp);
                   9981:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9982:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   9983:                DWORD num = 0;
                   9984:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   9985:                        REG8(AL) = 1;
                   9986:                } else {
                   9987:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9988:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9989:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9990:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9991:                }
                   9992:        }
                   9993: }
                   9994: 
                   9995: inline void msdos_int_21h_28h()
                   9996: {
                   9997:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9998:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9999:        
                   10000:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10001:                REG8(AL) = 0xff;
                   10002:        } else {
                   10003:                process_t *process = msdos_process_info_get(current_psp);
                   10004:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10005:                DWORD num = 0;
                   10006:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   10007:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10008:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10009:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   10010:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   10011:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   10012:        }
                   10013: }
                   10014: 
1.1       root     10015: inline void msdos_int_21h_29h()
                   10016: {
1.1.1.20  root     10017:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   10018:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     10019:        UINT8 drv = 0;
                   10020:        char sep_chars[] = ":.;,=+";
                   10021:        char end_chars[] = "\\<>|/\"[]";
                   10022:        char spc_chars[] = " \t";
                   10023:        
1.1.1.20  root     10024:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   10025:        buffer[1023] = 0;
                   10026:        memset(name, 0x20, sizeof(name));
                   10027:        memset(ext, 0x20, sizeof(ext));
                   10028:        
1.1       root     10029:        if(REG8(AL) & 1) {
1.1.1.20  root     10030:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   10031:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     10032:                        ofs++;
                   10033:                }
                   10034:        }
1.1.1.20  root     10035:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     10036:        
1.1.1.24  root     10037:        if(buffer[ofs + 1] == ':') {
                   10038:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   10039:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     10040:                        ofs += 2;
1.1.1.24  root     10041:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10042:                                ofs++;
                   10043:                        }
                   10044:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   10045:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     10046:                        ofs += 2;
1.1.1.24  root     10047:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10048:                                ofs++;
                   10049:                        }
1.1       root     10050:                }
                   10051:        }
1.1.1.20  root     10052:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10053:                UINT8 c = buffer[ofs];
                   10054:                if(is_kanji) {
                   10055:                        is_kanji = 0;
                   10056:                } else if(msdos_lead_byte_check(c)) {
                   10057:                        is_kanji = 1;
                   10058:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10059:                        break;
                   10060:                } else if(c >= 'a' && c <= 'z') {
                   10061:                        c -= 0x20;
                   10062:                }
                   10063:                ofs++;
                   10064:                name[i] = c;
                   10065:        }
1.1.1.20  root     10066:        if(buffer[ofs] == '.') {
1.1       root     10067:                ofs++;
1.1.1.20  root     10068:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10069:                        UINT8 c = buffer[ofs];
                   10070:                        if(is_kanji) {
                   10071:                                is_kanji = 0;
                   10072:                        } else if(msdos_lead_byte_check(c)) {
                   10073:                                is_kanji = 1;
                   10074:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10075:                                break;
                   10076:                        } else if(c >= 'a' && c <= 'z') {
                   10077:                                c -= 0x20;
                   10078:                        }
                   10079:                        ofs++;
                   10080:                        ext[i] = c;
                   10081:                }
                   10082:        }
1.1.1.20  root     10083:        int si = REG16(SI) + ofs;
1.1.1.3   root     10084:        int ds = SREG(DS);
1.1       root     10085:        while(si > 0xffff) {
                   10086:                si -= 0x10;
                   10087:                ds++;
                   10088:        }
                   10089:        REG16(SI) = si;
1.1.1.3   root     10090:        SREG(DS) = ds;
                   10091:        i386_load_segment_descriptor(DS);
1.1       root     10092:        
1.1.1.3   root     10093:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     10094:        if(!(REG8(AL) & 2) || drv != 0) {
                   10095:                fcb[0] = drv;
                   10096:        }
                   10097:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   10098:                memcpy(fcb + 1, name, 8);
                   10099:        }
                   10100:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   10101:                memcpy(fcb + 9, ext, 3);
                   10102:        }
                   10103:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     10104:                if(fcb[i] == '*') {
                   10105:                        found_star = 1;
                   10106:                }
                   10107:                if(found_star) {
                   10108:                        fcb[i] = '?';
                   10109:                }
                   10110:        }
1.1.1.20  root     10111:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     10112:                if(fcb[i] == '*') {
                   10113:                        found_star = 1;
                   10114:                }
                   10115:                if(found_star) {
                   10116:                        fcb[i] = '?';
                   10117:                }
                   10118:        }
                   10119:        
1.1.1.44  root     10120:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     10121:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   10122:                        REG8(AL) = 0x01;
1.1.1.20  root     10123:                } else {
                   10124:                        REG8(AL) = 0x00;
1.1       root     10125:                }
                   10126:        } else {
                   10127:                REG8(AL) = 0xff;
                   10128:        }
                   10129: }
                   10130: 
                   10131: inline void msdos_int_21h_2ah()
                   10132: {
                   10133:        SYSTEMTIME sTime;
                   10134:        
                   10135:        GetLocalTime(&sTime);
                   10136:        REG16(CX) = sTime.wYear;
                   10137:        REG8(DH) = (UINT8)sTime.wMonth;
                   10138:        REG8(DL) = (UINT8)sTime.wDay;
                   10139:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   10140: }
                   10141: 
                   10142: inline void msdos_int_21h_2bh()
                   10143: {
1.1.1.14  root     10144:        REG8(AL) = 0xff;
1.1       root     10145: }
                   10146: 
                   10147: inline void msdos_int_21h_2ch()
                   10148: {
                   10149:        SYSTEMTIME sTime;
                   10150:        
                   10151:        GetLocalTime(&sTime);
                   10152:        REG8(CH) = (UINT8)sTime.wHour;
                   10153:        REG8(CL) = (UINT8)sTime.wMinute;
                   10154:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     10155:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     10156: }
                   10157: 
                   10158: inline void msdos_int_21h_2dh()
                   10159: {
                   10160:        REG8(AL) = 0x00;
                   10161: }
                   10162: 
                   10163: inline void msdos_int_21h_2eh()
                   10164: {
                   10165:        process_t *process = msdos_process_info_get(current_psp);
                   10166:        
                   10167:        process->verify = REG8(AL);
                   10168: }
                   10169: 
                   10170: inline void msdos_int_21h_2fh()
                   10171: {
                   10172:        process_t *process = msdos_process_info_get(current_psp);
                   10173:        
                   10174:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     10175:        SREG(ES) = process->dta.w.h;
                   10176:        i386_load_segment_descriptor(ES);
1.1       root     10177: }
                   10178: 
                   10179: inline void msdos_int_21h_30h()
                   10180: {
                   10181:        // Version Flag / OEM
1.1.1.27  root     10182:        if(REG8(AL) == 0x01) {
1.1.1.29  root     10183: #ifdef SUPPORT_HMA
                   10184:                REG16(BX) = 0x0000;
                   10185: #else
                   10186:                REG16(BX) = 0x1000; // DOS is in HMA
                   10187: #endif
1.1       root     10188:        } else {
1.1.1.27  root     10189:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   10190:                // but this is not correct on Windows 98 SE
                   10191: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   10192:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     10193:        }
1.1.1.27  root     10194:        REG16(CX) = 0x0000;
1.1.1.30  root     10195:        REG8(AL) = dos_major_version;   // 7
                   10196:        REG8(AH) = dos_minor_version;   // 10
1.1       root     10197: }
                   10198: 
                   10199: inline void msdos_int_21h_31h()
                   10200: {
1.1.1.29  root     10201:        try {
                   10202:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10203:        } catch(...) {
                   10204:                // recover the broken mcb
                   10205:                int mcb_seg = current_psp - 1;
                   10206:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     10207:                
1.1.1.29  root     10208:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     10209:                        mcb->mz = 'M';
                   10210:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   10211:                        
1.1.1.29  root     10212:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     10213:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     10214:                        } else {
1.1.1.39  root     10215:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     10216:                        }
                   10217:                } else {
                   10218:                        mcb->mz = 'Z';
1.1.1.30  root     10219:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     10220:                }
                   10221:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10222:        }
1.1       root     10223:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   10224: }
                   10225: 
                   10226: inline void msdos_int_21h_32h()
                   10227: {
                   10228:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   10229:        UINT16 seg, ofs;
                   10230:        
                   10231:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10232:                REG8(AL) = 0;
1.1.1.3   root     10233:                SREG(DS) = seg;
                   10234:                i386_load_segment_descriptor(DS);
1.1       root     10235:                REG16(BX) = ofs;
                   10236:        } else {
                   10237:                REG8(AL) = 0xff;
1.1.1.3   root     10238:                m_CF = 1;
1.1       root     10239:        }
                   10240: }
                   10241: 
                   10242: inline void msdos_int_21h_33h()
                   10243: {
                   10244:        char path[MAX_PATH];
1.1.1.48  root     10245:        char drive = 3; // C:
1.1       root     10246:        
                   10247:        switch(REG8(AL)) {
                   10248:        case 0x00:
1.1.1.33  root     10249:                REG8(DL) = ctrl_break_checking;
1.1       root     10250:                break;
                   10251:        case 0x01:
1.1.1.33  root     10252:                ctrl_break_checking = REG8(DL);
                   10253:                break;
                   10254:        case 0x02:
                   10255:                {
                   10256:                        UINT8 old = ctrl_break_checking;
                   10257:                        ctrl_break_checking = REG8(DL);
                   10258:                        REG8(DL) = old;
                   10259:                }
                   10260:                break;
                   10261:        case 0x03:
                   10262:        case 0x04:
                   10263:                // DOS 4.0+ - Unused
1.1       root     10264:                break;
                   10265:        case 0x05:
1.1.1.48  root     10266:                if(GetSystemDirectory(path, MAX_PATH) != 0) {
                   10267:                        if(path[0] >= 'a' && path[0] <= 'z') {
                   10268:                                drive = path[0] - 'a' + 1;
                   10269:                        } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   10270:                                drive = path[0] - 'A' + 1;
                   10271:                        }
1.1       root     10272:                }
1.1.1.48  root     10273:                REG8(DL) = (UINT8)drive;
1.1       root     10274:                break;
                   10275:        case 0x06:
1.1.1.2   root     10276:                // MS-DOS version (7.10)
1.1       root     10277:                REG8(BL) = 7;
1.1.1.2   root     10278:                REG8(BH) = 10;
1.1       root     10279:                REG8(DL) = 0;
1.1.1.29  root     10280: #ifdef SUPPORT_HMA
                   10281:                REG8(DH) = 0x00;
                   10282: #else
                   10283:                REG8(DH) = 0x10; // DOS is in HMA
                   10284: #endif
1.1       root     10285:                break;
1.1.1.6   root     10286:        case 0x07:
                   10287:                if(REG8(DL) == 0) {
                   10288:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   10289:                } else if(REG8(DL) == 1) {
                   10290:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   10291:                }
                   10292:                break;
1.1       root     10293:        default:
1.1.1.22  root     10294:                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     10295: //             REG16(AX) = 0x01;
                   10296: //             m_CF = 1;
                   10297:                REG8(AL) = 0xff;
1.1       root     10298:                break;
                   10299:        }
                   10300: }
                   10301: 
1.1.1.23  root     10302: inline void msdos_int_21h_34h()
                   10303: {
                   10304:        SREG(ES) = SDA_TOP >> 4;
                   10305:        i386_load_segment_descriptor(ES);
                   10306:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   10307: }
                   10308: 
1.1       root     10309: inline void msdos_int_21h_35h()
                   10310: {
                   10311:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     10312:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   10313:        i386_load_segment_descriptor(ES);
1.1       root     10314: }
                   10315: 
                   10316: inline void msdos_int_21h_36h()
                   10317: {
                   10318:        struct _diskfree_t df = {0};
                   10319:        
                   10320:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   10321:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   10322:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     10323:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   10324:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     10325:        } else {
                   10326:                REG16(AX) = 0xffff;
                   10327:        }
                   10328: }
                   10329: 
                   10330: inline void msdos_int_21h_37h()
                   10331: {
1.1.1.22  root     10332:        static UINT8 dev_flag = 0xff;
1.1       root     10333:        
                   10334:        switch(REG8(AL)) {
                   10335:        case 0x00:
1.1.1.22  root     10336:                {
                   10337:                        process_t *process = msdos_process_info_get(current_psp);
                   10338:                        REG8(AL) = 0x00;
                   10339:                        REG8(DL) = process->switchar;
                   10340:                }
1.1       root     10341:                break;
                   10342:        case 0x01:
1.1.1.22  root     10343:                {
                   10344:                        process_t *process = msdos_process_info_get(current_psp);
                   10345:                        REG8(AL) = 0x00;
                   10346:                        process->switchar = REG8(DL);
1.1.1.23  root     10347:                        msdos_sda_update(current_psp);
1.1.1.22  root     10348:                }
                   10349:                break;
                   10350:        case 0x02:
                   10351:                REG8(DL) = dev_flag;
                   10352:                break;
                   10353:        case 0x03:
                   10354:                dev_flag = REG8(DL);
                   10355:                break;
                   10356:        case 0xd0:
                   10357:        case 0xd1:
                   10358:        case 0xd2:
                   10359:        case 0xd3:
                   10360:        case 0xd4:
                   10361:        case 0xd5:
                   10362:        case 0xd6:
                   10363:        case 0xd7:
                   10364:        case 0xdc:
                   10365:        case 0xdd:
                   10366:        case 0xde:
                   10367:        case 0xdf:
1.1.1.48  root     10368:                // DIET v1.43e
                   10369: //             REG16(AX) = 1;
                   10370:                REG8(AL) = 0xff;
1.1       root     10371:                break;
                   10372:        default:
1.1.1.22  root     10373:                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     10374: //             REG16(AX) = 1;
                   10375:                REG8(AL) = 0xff;
1.1       root     10376:                break;
                   10377:        }
                   10378: }
                   10379: 
1.1.1.52  root     10380: int get_country_info(country_info_t *ci, LCID locale = LOCALE_USER_DEFAULT)
1.1.1.17  root     10381: {
                   10382:        char LCdata[80];
                   10383:        
1.1.1.19  root     10384:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.42  root     10385:        GetLocaleInfo(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     10386:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.42  root     10387:        GetLocaleInfo(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10388:        ci->currency_format = *LCdata - '0';
1.1.1.42  root     10389:        GetLocaleInfo(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10390:        ci->date_format = *LCdata - '0';
1.1.1.42  root     10391:        GetLocaleInfo(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10392:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.42  root     10393:        GetLocaleInfo(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10394:        *ci->date_sep = *LCdata;
1.1.1.42  root     10395:        GetLocaleInfo(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     10396:        *ci->dec_sep = *LCdata;
1.1.1.42  root     10397:        GetLocaleInfo(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     10398:        *ci->list_sep = *LCdata;
1.1.1.42  root     10399:        GetLocaleInfo(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     10400:        *ci->thou_sep = *LCdata;
1.1.1.42  root     10401:        GetLocaleInfo(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     10402:        *ci->time_sep = *LCdata;
1.1.1.42  root     10403:        GetLocaleInfo(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     10404:        if(strchr(LCdata, 'H') != NULL) {
                   10405:                ci->time_format = 1;
                   10406:        }
1.1.1.49  root     10407:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffc:000a
                   10408:        ci->case_map.w.h = DUMMY_TOP >> 4;
1.1.1.42  root     10409:        GetLocaleInfo(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     10410:        return atoi(LCdata);
                   10411: }
                   10412: 
1.1.1.42  root     10413: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   10414: {
                   10415:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   10416: }
                   10417: 
1.1.1.43  root     10418: void set_country_info(country_info_t *ci, int size)
                   10419: {
                   10420:        char LCdata[80];
                   10421:        
                   10422:        if(size >= 0x00 + 2) {
                   10423:                memset(LCdata, 0, sizeof(LCdata));
                   10424:                *LCdata = '0' + ci->date_format;
                   10425:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
                   10426:        }
                   10427:        if(size >= 0x02 + 5) {
                   10428:                memset(LCdata, 0, sizeof(LCdata));
                   10429:                memcpy(LCdata, &ci->currency_symbol, 4);
                   10430:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
                   10431:        }
                   10432:        if(size >= 0x07 + 2) {
                   10433:                memset(LCdata, 0, sizeof(LCdata));
                   10434:                *LCdata = *ci->thou_sep;
                   10435:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
                   10436:        }
                   10437:        if(size >= 0x09 + 2) {
                   10438:                memset(LCdata, 0, sizeof(LCdata));
                   10439:                *LCdata = *ci->dec_sep;
                   10440:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
                   10441:        }
                   10442:        if(size >= 0x0b + 2) {
                   10443:                memset(LCdata, 0, sizeof(LCdata));
                   10444:                *LCdata = *ci->date_sep;
                   10445:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
                   10446:        }
                   10447:        if(size >= 0x0d + 2) {
                   10448:                memset(LCdata, 0, sizeof(LCdata));
                   10449:                *LCdata = *ci->time_sep;
                   10450:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
                   10451:        }
                   10452:        if(size >= 0x0f + 1) {
                   10453:                memset(LCdata, 0, sizeof(LCdata));
                   10454:                *LCdata = '0' + ci->currency_format;
                   10455:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
                   10456:        }
                   10457:        if(size >= 0x10 + 1) {
                   10458:                sprintf(LCdata, "%d", ci->currency_dec_digits);
                   10459:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
                   10460:        }
                   10461:        if(size >= 0x11 + 1) {
                   10462:                // FIXME: is time format always H/h:mm:ss ???
                   10463:                if(ci->time_format & 1) {
                   10464:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   10465:                } else {
                   10466:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   10467:                }
                   10468:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
                   10469:        }
                   10470:        if(size >= 0x12 + 4) {
                   10471:                // 12h  DWORD   address of case map routine
                   10472:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   10473:        }
                   10474:        if(size >= 0x16 + 2) {
                   10475:                memset(LCdata, 0, sizeof(LCdata));
                   10476:                *LCdata = *ci->list_sep;
                   10477:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
                   10478:        }
                   10479: }
                   10480: 
1.1.1.42  root     10481: #ifndef SUBLANG_SWAHILI
                   10482:        #define SUBLANG_SWAHILI 0x01
                   10483: #endif
                   10484: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10485:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10486: #endif
                   10487: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10488:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10489: #endif
                   10490: #ifndef LANG_BANGLA
                   10491:        #define LANG_BANGLA 0x45
                   10492: #endif
                   10493: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10494:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10495: #endif
                   10496: 
                   10497: static const struct {
                   10498:        int code;
                   10499:        USHORT usPrimaryLanguage;
                   10500:        USHORT usSubLanguage;
                   10501: } country_table[] = {
                   10502:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10503:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10504:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10505:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10506:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10507:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10508: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10509: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10510: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10511: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10512: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10513:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10514:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10515: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10516:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10517: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10518:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10519:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10520:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10521:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10522:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10523:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10524:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10525: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10526: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10527:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10528:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10529:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10530:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10531:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10532: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10533: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10534: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10535:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10536: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10537: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10538: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10539: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10540:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10541:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10542:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10543: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10544:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10545:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10546:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10547:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10548:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10549:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10550:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10551:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10552:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10553:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10554:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10555:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10556:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10557: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10558:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10559:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10560:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10561:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10562:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10563:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10564:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10565:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10566:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10567:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10568: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10569:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10570: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10571:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10572:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10573:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10574:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10575:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10576:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10577:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10578:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10579: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10580:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10581: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10582: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10583:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   10584: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   10585:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   10586:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   10587:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   10588:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   10589:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   10590:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   10591:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   10592: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   10593: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   10594:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   10595: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   10596:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   10597:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   10598:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   10599:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   10600: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   10601: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   10602: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   10603:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   10604:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   10605:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   10606:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   10607:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   10608:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   10609:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   10610:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   10611:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   10612:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   10613:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   10614:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   10615:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   10616:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   10617:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   10618:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   10619:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   10620:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   10621:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   10622:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   10623:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     10624: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     10625:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   10626: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   10627:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   10628:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   10629:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   10630:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   10631:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   10632:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   10633:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   10634:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   10635:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   10636:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   10637:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   10638:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   10639:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   10640:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   10641:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   10642:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   10643:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   10644:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   10645:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   10646:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   10647:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   10648:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   10649:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   10650:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   10651:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   10652: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   10653:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   10654:        {-1, 0, 0},
                   10655: };
                   10656: 
1.1.1.14  root     10657: inline void msdos_int_21h_38h()
                   10658: {
                   10659:        switch(REG8(AL)) {
                   10660:        case 0x00:
1.1.1.19  root     10661:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     10662:                break;
                   10663:        default:
1.1.1.42  root     10664:                for(int i = 0;; i++) {
                   10665:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   10666:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   10667:                                break;
                   10668:                        } else if(country_table[i].code == -1) {
                   10669: //                             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));
                   10670: //                             REG16(AX) = 2;
                   10671: //                             m_CF = 1;
1.1.1.48  root     10672:                                // get current coutry info
1.1.1.42  root     10673:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   10674:                                break;
                   10675:                        }
                   10676:                }
1.1.1.14  root     10677:                break;
                   10678:        }
                   10679: }
                   10680: 
1.1       root     10681: inline void msdos_int_21h_39h(int lfn)
                   10682: {
1.1.1.3   root     10683:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10684:                REG16(AX) = errno;
1.1.1.3   root     10685:                m_CF = 1;
1.1       root     10686:        }
                   10687: }
                   10688: 
                   10689: inline void msdos_int_21h_3ah(int lfn)
                   10690: {
1.1.1.3   root     10691:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10692:                REG16(AX) = errno;
1.1.1.3   root     10693:                m_CF = 1;
1.1       root     10694:        }
                   10695: }
                   10696: 
                   10697: inline void msdos_int_21h_3bh(int lfn)
                   10698: {
1.1.1.45  root     10699:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     10700:        
                   10701:        if(_chdir(path)) {
1.1.1.17  root     10702:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     10703:                m_CF = 1;
1.1.1.44  root     10704:        } else {
                   10705:                int drv = _getdrive() - 1;
                   10706:                if(path[1] == ':') {
                   10707:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   10708:                                drv = path[0] - 'A';
                   10709:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   10710:                                drv = path[0] - 'a';
                   10711:                        }
                   10712:                }
                   10713:                msdos_cds_update(drv, path);
1.1       root     10714:        }
                   10715: }
                   10716: 
                   10717: inline void msdos_int_21h_3ch()
                   10718: {
1.1.1.45  root     10719:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10720:        int attr = GetFileAttributes(path);
1.1.1.37  root     10721:        int fd = -1;
                   10722:        int sio_port = 0;
                   10723:        int lpt_port = 0;
1.1       root     10724:        
1.1.1.45  root     10725:        if(msdos_is_device_path(path)) {
                   10726:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     10727:        } else {
                   10728:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10729:        }
                   10730:        if(fd != -1) {
                   10731:                if(attr == -1) {
                   10732:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   10733:                }
                   10734:                SetFileAttributes(path, attr);
                   10735:                REG16(AX) = fd;
1.1.1.45  root     10736:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10737:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10738:        } else {
                   10739:                REG16(AX) = errno;
1.1.1.3   root     10740:                m_CF = 1;
1.1       root     10741:        }
                   10742: }
                   10743: 
                   10744: inline void msdos_int_21h_3dh()
                   10745: {
1.1.1.45  root     10746:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10747:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     10748:        int fd = -1;
                   10749:        int sio_port = 0;
                   10750:        int lpt_port = 0;
1.1       root     10751:        
                   10752:        if(mode < 0x03) {
1.1.1.45  root     10753:                if(msdos_is_device_path(path)) {
                   10754:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     10755:                } else {
1.1.1.13  root     10756:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     10757:                }
1.1       root     10758:                if(fd != -1) {
                   10759:                        REG16(AX) = fd;
1.1.1.45  root     10760:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10761:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10762:                } else {
                   10763:                        REG16(AX) = errno;
1.1.1.3   root     10764:                        m_CF = 1;
1.1       root     10765:                }
                   10766:        } else {
                   10767:                REG16(AX) = 0x0c;
1.1.1.3   root     10768:                m_CF = 1;
1.1       root     10769:        }
                   10770: }
                   10771: 
                   10772: inline void msdos_int_21h_3eh()
                   10773: {
                   10774:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10775:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10776:        
1.1.1.20  root     10777:        if(fd < process->max_files && file_handler[fd].valid) {
                   10778:                _close(fd);
                   10779:                msdos_file_handler_close(fd);
                   10780:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     10781:        } else {
                   10782:                REG16(AX) = 0x06;
1.1.1.3   root     10783:                m_CF = 1;
1.1       root     10784:        }
                   10785: }
                   10786: 
1.1.1.35  root     10787: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   10788: {
                   10789:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   10790:        int max = REG16(CX);
                   10791:        int p = 0;
                   10792:        
                   10793:        while(max > p) {
                   10794:                int chr = msdos_getch();
                   10795:                
                   10796:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   10797:                        p = 0;
                   10798:                        buf[p++] = 0x0d;
                   10799:                        if(max > p) {
                   10800:                                buf[p++] = 0x0a;
                   10801:                        }
                   10802:                        msdos_putch(0x03);
                   10803:                        msdos_putch(0x0d);
                   10804:                        msdos_putch(0x0a);
                   10805:                        break;
                   10806:                } else if(ctrl_break_pressed) {
                   10807:                        // skip this byte
                   10808:                } else if(chr == 0x00) {
                   10809:                        // skip 2nd byte
                   10810:                        msdos_getch();
                   10811:                } else if(chr == 0x0d) {
                   10812:                        // carriage return
                   10813:                        buf[p++] = 0x0d;
                   10814:                        if(max > p) {
                   10815:                                buf[p++] = 0x0a;
                   10816:                        }
                   10817:                        msdos_putch('\n');
                   10818:                        break;
                   10819:                } else if(chr == 0x08) {
                   10820:                        // back space
                   10821:                        if(p > 0) {
                   10822:                                p--;
                   10823:                                if(msdos_ctrl_code_check(buf[p])) {
                   10824:                                        msdos_putch(0x08);
                   10825:                                        msdos_putch(0x08);
                   10826:                                        msdos_putch(0x20);
                   10827:                                        msdos_putch(0x20);
                   10828:                                        msdos_putch(0x08);
                   10829:                                        msdos_putch(0x08);
1.1.1.36  root     10830:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   10831:                                        p--;
                   10832:                                        msdos_putch(0x08);
                   10833:                                        msdos_putch(0x08);
                   10834:                                        msdos_putch(0x20);
                   10835:                                        msdos_putch(0x20);
                   10836:                                        msdos_putch(0x08);
                   10837:                                        msdos_putch(0x08);
1.1.1.35  root     10838:                                } else {
                   10839:                                        msdos_putch(0x08);
                   10840:                                        msdos_putch(0x20);
                   10841:                                        msdos_putch(0x08);
                   10842:                                }
                   10843:                        }
                   10844:                } else if(chr == 0x1b) {
                   10845:                        // escape
                   10846:                        while(p > 0) {
                   10847:                                p--;
                   10848:                                if(msdos_ctrl_code_check(buf[p])) {
                   10849:                                        msdos_putch(0x08);
                   10850:                                        msdos_putch(0x08);
                   10851:                                        msdos_putch(0x20);
                   10852:                                        msdos_putch(0x20);
                   10853:                                        msdos_putch(0x08);
                   10854:                                        msdos_putch(0x08);
                   10855:                                } else {
                   10856:                                        msdos_putch(0x08);
                   10857:                                        msdos_putch(0x20);
                   10858:                                        msdos_putch(0x08);
                   10859:                                }
                   10860:                        }
                   10861:                } else {
                   10862:                        buf[p++] = chr;
                   10863:                        msdos_putch(chr);
                   10864:                }
                   10865:        }
                   10866:        REG16(AX) = p;
                   10867:        
                   10868: #ifdef USE_SERVICE_THREAD
                   10869:        service_exit = true;
                   10870: #endif
                   10871:        return(0);
                   10872: }
                   10873: 
1.1       root     10874: inline void msdos_int_21h_3fh()
                   10875: {
                   10876:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10877:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10878:        
1.1.1.20  root     10879:        if(fd < process->max_files && file_handler[fd].valid) {
                   10880:                if(file_mode[file_handler[fd].mode].in) {
                   10881:                        if(file_handler[fd].atty) {
1.1       root     10882:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     10883:                                if(REG16(CX) != 0) {
                   10884: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     10885:                                        if(!in_service && !in_service_29h &&
                   10886:                                           *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50  root     10887:                                           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   10888:                                                // msdos_putch() will be used in this service
                   10889:                                                // if int 29h is hooked, run this service in main thread to call int 29h
                   10890:                                                start_service_loop(msdos_int_21h_3fh_thread);
                   10891:                                        } else {
                   10892: #endif
                   10893:                                                msdos_int_21h_3fh_thread(NULL);
                   10894:                                                REQUEST_HARDWRE_UPDATE();
                   10895: #ifdef USE_SERVICE_THREAD
                   10896:                                        }
1.1.1.35  root     10897: #endif
                   10898:                                } else {
                   10899:                                        REG16(AX) = 0;
1.1       root     10900:                                }
                   10901:                        } else {
1.1.1.37  root     10902:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10903:                        }
                   10904:                } else {
                   10905:                        REG16(AX) = 0x05;
1.1.1.3   root     10906:                        m_CF = 1;
1.1       root     10907:                }
                   10908:        } else {
                   10909:                REG16(AX) = 0x06;
1.1.1.3   root     10910:                m_CF = 1;
1.1       root     10911:        }
                   10912: }
                   10913: 
                   10914: inline void msdos_int_21h_40h()
                   10915: {
                   10916:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10917:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10918:        
1.1.1.20  root     10919:        if(fd < process->max_files && file_handler[fd].valid) {
                   10920:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     10921:                        if(REG16(CX)) {
1.1.1.20  root     10922:                                if(file_handler[fd].atty) {
1.1       root     10923:                                        // BX is stdout/stderr or is redirected to stdout
                   10924:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     10925:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     10926:                                        }
                   10927:                                        REG16(AX) = REG16(CX);
                   10928:                                } else {
1.1.1.20  root     10929:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10930:                                }
                   10931:                        } else {
1.1.1.20  root     10932:                                UINT32 pos = _tell(fd);
                   10933:                                _lseek(fd, 0, SEEK_END);
                   10934:                                UINT32 size = _tell(fd);
1.1.1.12  root     10935:                                if(pos < size) {
1.1.1.20  root     10936:                                        _lseek(fd, pos, SEEK_SET);
                   10937:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     10938:                                } else {
                   10939:                                        for(UINT32 i = size; i < pos; i++) {
                   10940:                                                UINT8 tmp = 0;
1.1.1.23  root     10941:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     10942:                                        }
1.1.1.20  root     10943:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     10944:                                }
1.1.1.23  root     10945:                                REG16(AX) = 0;
1.1       root     10946:                        }
                   10947:                } else {
                   10948:                        REG16(AX) = 0x05;
1.1.1.3   root     10949:                        m_CF = 1;
1.1       root     10950:                }
                   10951:        } else {
                   10952:                REG16(AX) = 0x06;
1.1.1.3   root     10953:                m_CF = 1;
1.1       root     10954:        }
                   10955: }
                   10956: 
                   10957: inline void msdos_int_21h_41h(int lfn)
                   10958: {
1.1.1.3   root     10959:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10960:                REG16(AX) = errno;
1.1.1.3   root     10961:                m_CF = 1;
1.1       root     10962:        }
                   10963: }
                   10964: 
                   10965: inline void msdos_int_21h_42h()
                   10966: {
                   10967:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10968:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10969:        
1.1.1.20  root     10970:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     10971:                if(REG8(AL) < 0x03) {
1.1.1.35  root     10972:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     10973:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   10974:                        UINT32 pos = _tell(fd);
1.1       root     10975:                        REG16(AX) = pos & 0xffff;
                   10976:                        REG16(DX) = (pos >> 16);
                   10977:                } else {
                   10978:                        REG16(AX) = 0x01;
1.1.1.3   root     10979:                        m_CF = 1;
1.1       root     10980:                }
                   10981:        } else {
                   10982:                REG16(AX) = 0x06;
1.1.1.3   root     10983:                m_CF = 1;
1.1       root     10984:        }
                   10985: }
                   10986: 
                   10987: inline void msdos_int_21h_43h(int lfn)
                   10988: {
1.1.1.45  root     10989:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     10990:        int attr;
                   10991:        
1.1.1.14  root     10992:        if(!lfn && REG8(AL) > 2) {
                   10993:                REG16(AX) = 0x01;
                   10994:                m_CF = 1;
                   10995:                return;
                   10996:        }
                   10997:        switch(REG8(lfn ? BL : AL)) {
1.1       root     10998:        case 0x00:
                   10999:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     11000:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   11001:                } else {
                   11002:                        REG16(AX) = (UINT16)GetLastError();
                   11003:                        m_CF = 1;
                   11004:                }
                   11005:                break;
                   11006:        case 0x01:
                   11007:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   11008:                        REG16(AX) = (UINT16)GetLastError();
                   11009:                        m_CF = 1;
                   11010:                }
                   11011:                break;
                   11012:        case 0x02:
                   11013:                {
1.1.1.45  root     11014:                        DWORD compressed_size = GetCompressedFileSize(path, NULL), file_size = 0;
                   11015:                        if(compressed_size != INVALID_FILE_SIZE) {
                   11016:                                if(compressed_size != 0) {
                   11017:                                        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   11018:                                        if(hFile != INVALID_HANDLE_VALUE) {
                   11019:                                                file_size = GetFileSize(hFile, NULL);
                   11020:                                                CloseHandle(hFile);
                   11021:                                        }
                   11022:                                        if(compressed_size == file_size) {
                   11023:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   11024:                                                // this isn't correct if the file is in the NTFS MFT
                   11025:                                                if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   11026:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   11027:                                                }
1.1.1.14  root     11028:                                        }
                   11029:                                }
1.1.1.45  root     11030:                                REG16(AX) = LOWORD(compressed_size);
                   11031:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     11032:                        } else {
                   11033:                                REG16(AX) = (UINT16)GetLastError();
                   11034:                                m_CF = 1;
1.1       root     11035:                        }
1.1.1.14  root     11036:                }
                   11037:                break;
                   11038:        case 0x03:
                   11039:        case 0x05:
                   11040:        case 0x07:
1.1.1.48  root     11041:                if(lfn) {
1.1.1.14  root     11042:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   11043:                        if(hFile != INVALID_HANDLE_VALUE) {
                   11044:                                FILETIME local, time;
                   11045:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   11046:                                if(REG8(BL) == 7) {
                   11047:                                        ULARGE_INTEGER hund;
                   11048:                                        hund.LowPart = local.dwLowDateTime;
                   11049:                                        hund.HighPart = local.dwHighDateTime;
                   11050:                                        hund.QuadPart += REG16(SI) * 100000;
                   11051:                                        local.dwLowDateTime = hund.LowPart;
                   11052:                                        local.dwHighDateTime = hund.HighPart;
                   11053:                                }
                   11054:                                LocalFileTimeToFileTime(&local, &time);
                   11055:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   11056:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   11057:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   11058:                                        REG16(AX) = (UINT16)GetLastError();
                   11059:                                        m_CF = 1;
                   11060:                                }
                   11061:                                CloseHandle(hFile);
                   11062:                        } else {
                   11063:                                REG16(AX) = (UINT16)GetLastError();
                   11064:                                m_CF = 1;
1.1       root     11065:                        }
1.1.1.48  root     11066:                } else {
                   11067:                        // 214303 DR DOS 3.41+ internal - Set Access Rights And Password
                   11068:                        // 214305 DR DOS 5.0-6.0 internal - Set Extended File Attributes
                   11069:                        // 214307 DR DOS 6.0 - Set 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;
                   11075:        case 0x04:
                   11076:        case 0x06:
                   11077:        case 0x08:
1.1.1.48  root     11078:                if(lfn) {
1.1.1.14  root     11079:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   11080:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   11081:                                FILETIME *time, local;
                   11082:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   11083:                                                   0x06 ? &fad.ftLastAccessTime :
                   11084:                                                          &fad.ftCreationTime;
                   11085:                                FileTimeToLocalFileTime(time, &local);
                   11086:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   11087:                                if(REG8(BL) == 0x08) {
                   11088:                                        ULARGE_INTEGER hund;
                   11089:                                        hund.LowPart = local.dwLowDateTime;
                   11090:                                        hund.HighPart = local.dwHighDateTime;
                   11091:                                        hund.QuadPart /= 100000;
                   11092:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   11093:                                }
                   11094:                        } else {
                   11095:                                REG16(AX) = (UINT16)GetLastError();
                   11096:                                m_CF = 1;
1.1       root     11097:                        }
1.1.1.48  root     11098:                } else {
                   11099:                        // 214304 DR DOS 5.0-6.0 internal - Get Encrypted Password
                   11100:                        // 214306 DR DOS 6.0 - Get File Owner
                   11101: //                     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));
                   11102:                        REG16(AX) = 0x01;
                   11103:                        m_CF = 1;
1.1.1.14  root     11104:                }
                   11105:                break;
1.1.1.43  root     11106:        case 0xff:
1.1.1.48  root     11107:                if(!lfn && REG16(BP) == 0x5053) {
1.1.1.43  root     11108:                        if(REG8(CL) == 0x39) {
                   11109:                                msdos_int_21h_39h(1);
                   11110:                                break;
                   11111:                        } else if(REG8(CL) == 0x56) {
                   11112:                                msdos_int_21h_56h(1);
                   11113:                                break;
                   11114:                        }
                   11115:                }
1.1.1.14  root     11116:        default:
1.1.1.22  root     11117:                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     11118:                REG16(AX) = lfn ? 0x7100 : 0x01;
1.1.1.14  root     11119:                m_CF = 1;
                   11120:                break;
                   11121:        }
                   11122: }
                   11123: 
                   11124: inline void msdos_int_21h_44h()
                   11125: {
1.1.1.22  root     11126:        static UINT16 iteration_count = 0;
                   11127:        
1.1.1.44  root     11128:        process_t *process;
                   11129:        int fd, drv;
1.1.1.14  root     11130:        
                   11131:        switch(REG8(AL)) {
                   11132:        case 0x00:
                   11133:        case 0x01:
                   11134:        case 0x02:
                   11135:        case 0x03:
                   11136:        case 0x04:
                   11137:        case 0x05:
                   11138:        case 0x06:
                   11139:        case 0x07:
1.1.1.44  root     11140:                process = msdos_process_info_get(current_psp);
                   11141:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     11142:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   11143:                        REG16(AX) = 0x06;
                   11144:                        m_CF = 1;
                   11145:                        return;
1.1.1.14  root     11146:                }
                   11147:                break;
                   11148:        case 0x08:
                   11149:        case 0x09:
1.1.1.44  root     11150:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11151:                if(!msdos_is_valid_drive(drv)) {
                   11152:                        // invalid drive
1.1.1.14  root     11153:                        REG16(AX) = 0x0f;
                   11154:                        m_CF = 1;
                   11155:                        return;
1.1       root     11156:                }
                   11157:                break;
                   11158:        }
                   11159:        switch(REG8(AL)) {
1.1.1.48  root     11160:        case 0x00: // Get Device Information
1.1.1.20  root     11161:                REG16(DX) = file_handler[fd].info;
1.1       root     11162:                break;
1.1.1.48  root     11163:        case 0x01: // Set Device Information
1.1.1.45  root     11164:                if(REG8(DH) != 0) {
                   11165: //                     REG16(AX) = 0x0d; // data invalid
                   11166: //                     m_CF = 1;
                   11167:                        file_handler[fd].info = REG16(DX);
                   11168:                } else {
                   11169:                        file_handler[fd].info &= 0xff00;
                   11170:                        file_handler[fd].info |= REG8(DL);
                   11171:                }
1.1       root     11172:                break;
1.1.1.48  root     11173:        case 0x02: // Read From Character Device Control Channel
1.1.1.45  root     11174:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
                   11175:                        // from DOSBox
                   11176:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
                   11177:                        case 0x00:
                   11178:                                if(REG16(CX) >= 6) {
                   11179:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
                   11180:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
                   11181:                                        REG16(AX) = 6; // number of bytes actually read
                   11182:                                } else {
                   11183:                                        REG16(AX) = 0x0d; // data invalid
                   11184:                                        m_CF = 1;
                   11185:                                }
                   11186:                                break;
                   11187:                        case 0x01:
                   11188:                                if(REG16(CX) >= 6) {
                   11189:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
                   11190:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
                   11191:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
                   11192:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
                   11193:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
                   11194:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
                   11195:                                                        int page = (addr - EMS_TOP) / 0x4000;
                   11196:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
                   11197:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11198:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
                   11199:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
                   11200:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
                   11201:                                                } else {
                   11202:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
                   11203:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11204:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
                   11205:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
                   11206:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
                   11207:                                                }
                   11208:                                        }
                   11209:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
                   11210:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
                   11211:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
                   11212:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
                   11213:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
                   11214:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
                   11215:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
                   11216:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
                   11217:                                        
                   11218:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
                   11219:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
                   11220:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
                   11221:                                        REG16(AX) = 6; // number of bytes actually read
                   11222:                                } else {
                   11223:                                        REG16(AX) = 0x0d; // data invalid
                   11224:                                        m_CF = 1;
                   11225:                                }
                   11226:                                break;
                   11227:                        case 0x02:
                   11228:                                if(REG16(CX) >= 2) {
                   11229:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
                   11230:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
                   11231:                                        REG16(AX) = 2; // number of bytes actually read
                   11232:                                } else {
                   11233:                                        REG16(AX) = 0x0d; // data invalid
                   11234:                                        m_CF = 1;
                   11235:                                }
                   11236:                                break;
                   11237:                        case 0x03:
                   11238:                                if(REG16(CX) >= 4) {
                   11239:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
                   11240:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
                   11241:                                        REG16(AX) = 4; // number of bytes actually read
                   11242:                                } else {
                   11243:                                        REG16(AX) = 0x0d; // data invalid
                   11244:                                        m_CF = 1;
                   11245:                                }
                   11246:                                break;
                   11247:                        default:
                   11248:                                REG16(AX) = 0x01; // function number invalid
                   11249:                                m_CF = 1;
                   11250:                        }
                   11251:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
                   11252:                        if(REG16(CX) >= 5) {
                   11253:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
                   11254:                                REG16(AX) = 5; // number of bytes actually read
                   11255:                        } else {
                   11256:                                REG16(AX) = 0x0d; // data invalid
                   11257:                                m_CF = 1;
                   11258:                        }
                   11259:                } else {
                   11260: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
                   11261: //                     REG16(AX) = REG16(CX);
                   11262:                        REG16(AX) = 0x05; // access denied
                   11263:                        m_CF = 1;
                   11264:                }
                   11265:                break;
1.1.1.48  root     11266:        case 0x03: // Write To Character Device Control Channel
1.1.1.45  root     11267: //             REG16(AX) = 0x05;
                   11268: //             m_CF = 1;
                   11269:                REG16(AX) = 0x00; // success
                   11270:                break;
1.1.1.48  root     11271:        case 0x04: // Read From Block Device Control Channel
                   11272:        case 0x05: // Write To Block Device Control Channel
1.1       root     11273:                REG16(AX) = 0x05;
1.1.1.3   root     11274:                m_CF = 1;
1.1       root     11275:                break;
1.1.1.48  root     11276:        case 0x06: // Get Input Status
1.1.1.20  root     11277:                if(file_mode[file_handler[fd].mode].in) {
                   11278:                        if(file_handler[fd].atty) {
1.1.1.14  root     11279:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     11280:                        } else {
1.1.1.20  root     11281:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     11282:                        }
1.1.1.14  root     11283:                } else {
                   11284:                        REG8(AL) = 0x00;
1.1       root     11285:                }
                   11286:                break;
1.1.1.48  root     11287:        case 0x07: // Get Output Status
1.1.1.20  root     11288:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     11289:                        REG8(AL) = 0xff;
                   11290:                } else {
                   11291:                        REG8(AL) = 0x00;
1.1       root     11292:                }
                   11293:                break;
1.1.1.48  root     11294:        case 0x08: // Check If Block Device Removable
1.1.1.44  root     11295:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     11296:                        // removable drive
                   11297:                        REG16(AX) = 0x00;
1.1       root     11298:                } else {
1.1.1.14  root     11299:                        // fixed drive
                   11300:                        REG16(AX) = 0x01;
1.1       root     11301:                }
                   11302:                break;
1.1.1.48  root     11303:        case 0x09: // Check If Block Device Remote
1.1.1.44  root     11304:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     11305:                        // remote drive
                   11306:                        REG16(DX) = 0x1000;
1.1.1.44  root     11307:                } else if(msdos_is_subst_drive(drv)) {
                   11308:                        // subst drive
                   11309:                        REG16(DX) = 0x8000;
1.1       root     11310:                } else {
1.1.1.14  root     11311:                        // local drive
1.1.1.44  root     11312:                        REG16(DX) = 0x0000;
1.1       root     11313:                }
                   11314:                break;
1.1.1.48  root     11315:        case 0x0a: // Check If Handle Is Remote
1.1.1.45  root     11316:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
                   11317:                        REG16(DX) = 0x8000;
                   11318:                } else {
                   11319:                        REG16(DX) = 0x0000;
                   11320:                }
1.1.1.21  root     11321:                break;
1.1.1.48  root     11322:        case 0x0b: // Set Sharing Retry Count
1.1       root     11323:                break;
1.1.1.48  root     11324:        case 0x0c: // Generic Character Device Request
1.1.1.22  root     11325:                if(REG8(CL) == 0x45) {
                   11326:                        // set iteration (retry) count
                   11327:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   11328:                } else if(REG8(CL) == 0x4a) {
                   11329:                        // select code page
                   11330:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   11331:                        msdos_nls_tables_update();
1.1.1.44  root     11332:                } else if(REG8(CL) == 0x4c) {
                   11333:                        // start code-page preparation
                   11334:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11335:                        int count = 1, offset = 0;
                   11336:                        if(active_code_page != 437) {
                   11337:                                ids[count++] = active_code_page;
                   11338:                        }
                   11339:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11340:                                ids[count++] = system_code_page;
                   11341:                        }
                   11342:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   11343:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   11344:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11345:                        for(int i = 0; i < count; i++) {
                   11346:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11347:                        }
                   11348:                } else if(REG8(CL) == 0x4d) {
                   11349:                        // end code-page preparation
                   11350:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   11351:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.50  root     11352:                } else if(REG8(CL) == 0x5f) {
                   11353:                        // set display information
                   11354:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   11355:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   11356:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   11357:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   11358:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   11359:                                
                   11360:                                if(cur_width != new_width || cur_height != new_height) {
                   11361:                                        pcbios_set_console_size(new_width, new_height, true);
                   11362:                                }
                   11363:                        }
1.1.1.22  root     11364:                } else if(REG8(CL) == 0x65) {
                   11365:                        // get iteration (retry) count
                   11366:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   11367:                } else if(REG8(CL) == 0x6a) {
                   11368:                        // query selected code page
                   11369:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   11370:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   11371:                        
                   11372:                        CPINFO info;
                   11373:                        GetCPInfo(active_code_page, &info);
                   11374:                        
                   11375:                        if(info.MaxCharSize != 1) {
                   11376:                                for(int i = 0;; i++) {
                   11377:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   11378:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   11379:                                        
                   11380:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   11381:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   11382:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   11383:                                        
                   11384:                                        if(lo == 0 && hi == 0) {
                   11385:                                                break;
                   11386:                                        }
                   11387:                                }
                   11388:                        }
1.1.1.44  root     11389:                } else if(REG8(CL) == 0x6b) {
                   11390:                        // query prepare list
                   11391:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11392:                        int count = 1, offset = 0;
                   11393:                        if(active_code_page != 437) {
                   11394:                                ids[count++] = active_code_page;
                   11395:                        }
                   11396:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11397:                                ids[count++] = system_code_page;
                   11398:                        }
                   11399:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   11400:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11401:                        for(int i = 0; i < count; i++) {
                   11402:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11403:                        }
                   11404:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11405:                        for(int i = 0; i < count; i++) {
                   11406:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11407:                        }
1.1.1.22  root     11408:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     11409:                        // get display information
1.1.1.50  root     11410:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   11411:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   11412:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   11413:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   11414:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   11415:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   11416:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   11417:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   11418:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   11419:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   11420:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.22  root     11421:                } else {
                   11422:                        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));
                   11423:                        REG16(AX) = 0x01; // invalid function
                   11424:                        m_CF = 1;
                   11425:                }
                   11426:                break;
1.1.1.48  root     11427:        case 0x0d: // Generic Block Device Request
1.1.1.22  root     11428:                if(REG8(CL) == 0x40) {
                   11429:                        // set device parameters
1.1.1.48  root     11430: //             } else if(REG8(CL) == 0x41) {
                   11431: //                     // write logical device track
                   11432: //             } else if(REG8(CL) == 0x42) {
                   11433: //                     // format and verify logical device track
1.1.1.22  root     11434:                } else if(REG8(CL) == 0x46) {
                   11435:                        // set volume serial number
1.1.1.48  root     11436:                } else if(REG8(CL) == 0x47) {
                   11437:                        // set access flag
                   11438: //             } else if(REG8(CL) == 0x48) {
                   11439: //                     // set media lock state
                   11440: //             } else if(REG8(CL) == 0x49) {
                   11441: //                     // eject media in drive
1.1.1.22  root     11442:                } else if(REG8(CL) == 0x4a) {
                   11443:                        // lock logical volume
                   11444:                } else if(REG8(CL) == 0x4b) {
                   11445:                        // lock physical volume
                   11446:                } else if(REG8(CL) == 0x60) {
                   11447:                        // get device parameters
1.1.1.42  root     11448:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11449:                        
1.1.1.42  root     11450:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11451:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11452:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11453:                                
                   11454:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   11455:                                switch(geo->MediaType) {
                   11456:                                case F5_360_512:
                   11457:                                case F5_320_512:
                   11458:                                case F5_320_1024:
                   11459:                                case F5_180_512:
                   11460:                                case F5_160_512:
                   11461:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   11462:                                        break;
                   11463:                                case F5_1Pt2_512:
                   11464:                                case F3_1Pt2_512:
                   11465:                                case F3_1Pt23_1024:
                   11466:                                case F5_1Pt23_1024:
                   11467:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   11468:                                        break;
                   11469:                                case F3_720_512:
                   11470:                                case F3_640_512:
                   11471:                                case F5_640_512:
                   11472:                                case F5_720_512:
                   11473:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   11474:                                        break;
                   11475:                                case F8_256_128:
                   11476:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   11477:                                        break;
                   11478:                                case FixedMedia:
                   11479:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11480:                                        break;
                   11481:                                case F3_1Pt44_512:
                   11482:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11483:                                        break;
                   11484:                                case F3_2Pt88_512:
                   11485:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   11486:                                        break;
                   11487:                                default:
                   11488:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11489: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11490:                                        break;
1.1.1.22  root     11491:                                }
1.1.1.42  root     11492:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   11493:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   11494:                                switch(geo->MediaType) {
                   11495:                                case F5_360_512:
                   11496:                                case F5_320_512:
                   11497:                                case F5_320_1024:
                   11498:                                case F5_180_512:
                   11499:                                case F5_160_512:
                   11500:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   11501:                                        break;
                   11502:                                default:
                   11503:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   11504:                                        break;
                   11505:                                }
                   11506:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   11507:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   11508:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   11509:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   11510:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   11511:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   11512:                                switch(geo->MediaType) {
                   11513:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   11514:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   11515:                                        break;
                   11516:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   11517:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   11518:                                        break;
                   11519:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   11520:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11521:                                        break;
                   11522:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11523:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11524:                                        break;
                   11525:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11526:                                case F3_1Pt2_512:
                   11527:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11528:                                case F5_720_512:
                   11529:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11530:                                        break;
                   11531:                                case FixedMedia:        // hard disk
                   11532:                                case RemovableMedia:
                   11533:                                case Unknown:
                   11534:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11535:                                        break;
                   11536:                                default:
                   11537:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11538:                                        break;
                   11539:                                }
                   11540:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11541:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11542:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11543:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11544:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11545:                                // 21h  BYTE    device type
                   11546:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11547:                        } else {
                   11548:                                REG16(AX) = 0x0f; // invalid drive
                   11549:                                m_CF = 1;
                   11550:                        }
1.1.1.48  root     11551: //             } else if(REG8(CL) == 0x61) {
                   11552: //                     // read logical device track
                   11553: //             } else if(REG8(CL) == 0x62) {
                   11554: //                     // verify logical device track
1.1.1.22  root     11555:                } else if(REG8(CL) == 0x66) {
                   11556:                        // get volume serial number
                   11557:                        char path[] = "A:\\";
                   11558:                        char volume_label[MAX_PATH];
                   11559:                        DWORD serial_number = 0;
                   11560:                        char file_system[MAX_PATH];
                   11561:                        
                   11562:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11563:                        
                   11564:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   11565:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11566:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11567:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11568:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11569:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11570:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11571:                        } else {
                   11572:                                REG16(AX) = 0x0f; // invalid drive
                   11573:                                m_CF = 1;
                   11574:                        }
                   11575:                } else if(REG8(CL) == 0x67) {
                   11576:                        // get access flag
                   11577:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11578:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11579:                } else if(REG8(CL) == 0x68) {
                   11580:                        // sense media type
1.1.1.42  root     11581:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11582:                        
1.1.1.42  root     11583:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11584:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11585:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11586:                                
                   11587:                                switch(geo->MediaType) {
                   11588:                                case F3_720_512:
                   11589:                                case F5_720_512:
                   11590:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11591:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   11592:                                        break;
                   11593:                                case F3_1Pt44_512:
                   11594:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11595:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   11596:                                        break;
                   11597:                                case F3_2Pt88_512:
                   11598:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11599:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   11600:                                        break;
                   11601:                                default:
                   11602:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   11603:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   11604:                                        break;
1.1.1.22  root     11605:                                }
                   11606:                        } else {
                   11607:                                REG16(AX) = 0x0f; // invalid drive
                   11608:                                m_CF = 1;
                   11609:                        }
                   11610:                } else if(REG8(CL) == 0x6a) {
                   11611:                        // unlock logical volume
                   11612:                } else if(REG8(CL) == 0x6b) {
                   11613:                        // unlock physical volume
1.1.1.48  root     11614: //             } else if(REG8(CL) == 0x6c) {
                   11615: //                     // get lock flag
                   11616: //             } else if(REG8(CL) == 0x6d) {
                   11617: //                     // enumerate open files
                   11618: //             } else if(REG8(CL) == 0x6e) {
                   11619: //                     // find swap file
                   11620: //             } else if(REG8(CL) == 0x6f) {
                   11621: //                     // get drive map information
                   11622: //             } else if(REG8(CL) == 0x70) {
                   11623: //                     // get current lock state
                   11624: //             } else if(REG8(CL) == 0x71) {
                   11625: //                     // get first cluster
1.1.1.22  root     11626:                } else {
                   11627:                        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));
                   11628:                        REG16(AX) = 0x01; // invalid function
                   11629:                        m_CF = 1;
                   11630:                }
                   11631:                break;
1.1.1.48  root     11632:        case 0x0e: // Get Lgical Drive Map
1.1.1.44  root     11633:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11634:                        REG16(AX) = 0x0f; // invalid drive
                   11635:                        m_CF = 1;
                   11636:                } else {
                   11637:                        REG8(AL) = 0;
1.1.1.22  root     11638:                }
                   11639:                break;
1.1.1.48  root     11640:        case 0x0f: // Set Logical Drive Map
1.1.1.44  root     11641:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11642:                        REG16(AX) = 0x0f; // invalid drive
                   11643:                        m_CF = 1;
1.1.1.22  root     11644:                }
                   11645:                break;
1.1.1.48  root     11646:        case 0x10: // Query Generic IOCTRL Capability (Handle)
1.1.1.22  root     11647:                switch(REG8(CL)) {
                   11648:                case 0x45:
                   11649:                case 0x4a:
1.1.1.48  root     11650:                case 0x4c:
                   11651:                case 0x4d:
1.1.1.22  root     11652:                case 0x65:
                   11653:                case 0x6a:
1.1.1.48  root     11654:                case 0x6b:
1.1.1.22  root     11655:                case 0x7f:
                   11656:                        REG16(AX) = 0x0000; // supported
                   11657:                        break;
                   11658:                default:
                   11659:                        REG8(AL) = 0x01; // ioctl capability not available
                   11660:                        m_CF = 1;
                   11661:                        break;
                   11662:                }
                   11663:                break;
1.1.1.48  root     11664:        case 0x11: // Query Generic IOCTRL Capability (Drive)
1.1.1.22  root     11665:                switch(REG8(CL)) {
                   11666:                case 0x40:
                   11667:                case 0x46:
                   11668:                case 0x4a:
                   11669:                case 0x4b:
                   11670:                case 0x60:
                   11671:                case 0x66:
                   11672:                case 0x67:
                   11673:                case 0x68:
                   11674:                case 0x6a:
                   11675:                case 0x6b:
1.1.1.48  root     11676:                        if(REG8(CH) == 0x00 || REG8(CH) == 0x01 || REG8(CH) == 0x03 || REG8(CH) == 0x05) {
                   11677:                                // CH = 00h     Unknown
                   11678:                                // CH = 01h     COMn:
                   11679:                                // CH = 03h     CON
                   11680:                                // CH = 05h     LPTn:
                   11681:                                REG16(AX) = 0x0000; // supported
                   11682:                                break;
                   11683:                        }
1.1.1.22  root     11684:                default:
                   11685:                        REG8(AL) = 0x01; // ioctl capability not available
                   11686:                        m_CF = 1;
                   11687:                        break;
                   11688:                }
                   11689:                break;
1.1.1.48  root     11690:        case 0x12: // DR DOS 5.0-6.0 - Determine DOS Type
                   11691:        case 0x14: // DR DOS 5.0-6.0 - Set Global Password
                   11692:        case 0x16: // DR DOS 5.0-6.0 - History Buffer, Share, And Hiload Control
                   11693:        case 0x51: // Concurrent DOS v3.2+ - Installation Check
                   11694:        case 0x52: // DR DOS 3.41+ - Determine DOS tTpe/Get DR DOS Version
                   11695:        case 0x54: // DR DOS 3.41+ - Set Global Password
                   11696:        case 0x56: // DR DOS 5.0+ - History Buffer Control
                   11697:        case 0x57: // DR DOS 5.0-6.0 - Share/Hiload Control
                   11698:        case 0x58: // DR DOS 5.0+ internal - Get Pointer To Internal Variable Table
                   11699:        case 0x59: // DR Multiuser DOS 5.0 - API
                   11700:                REG16(AX) = 0x01; // this is not DR-DOS
1.1.1.22  root     11701:                m_CF = 1;
                   11702:                break;
1.1       root     11703:        default:
1.1.1.22  root     11704:                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     11705:                REG16(AX) = 0x01;
1.1.1.3   root     11706:                m_CF = 1;
1.1       root     11707:                break;
                   11708:        }
                   11709: }
                   11710: 
                   11711: inline void msdos_int_21h_45h()
                   11712: {
                   11713:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11714:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11715:        
1.1.1.20  root     11716:        if(fd < process->max_files && file_handler[fd].valid) {
                   11717:                int dup_fd = _dup(fd);
                   11718:                if(dup_fd != -1) {
                   11719:                        REG16(AX) = dup_fd;
                   11720:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11721: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11722:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11723:                } else {
                   11724:                        REG16(AX) = errno;
1.1.1.3   root     11725:                        m_CF = 1;
1.1       root     11726:                }
                   11727:        } else {
                   11728:                REG16(AX) = 0x06;
1.1.1.3   root     11729:                m_CF = 1;
1.1       root     11730:        }
                   11731: }
                   11732: 
                   11733: inline void msdos_int_21h_46h()
                   11734: {
                   11735:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11736:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11737:        int dup_fd = REG16(CX);
                   11738:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     11739:        
1.1.1.20  root     11740:        if(REG16(BX) == REG16(CX)) {
                   11741:                REG16(AX) = 0x06;
                   11742:                m_CF = 1;
                   11743:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   11744:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   11745:                        _close(tmp_fd);
                   11746:                        msdos_file_handler_close(tmp_fd);
                   11747:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   11748:                }
                   11749:                if(_dup2(fd, dup_fd) != -1) {
                   11750:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11751: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11752:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11753:                } else {
                   11754:                        REG16(AX) = errno;
1.1.1.3   root     11755:                        m_CF = 1;
1.1       root     11756:                }
                   11757:        } else {
                   11758:                REG16(AX) = 0x06;
1.1.1.3   root     11759:                m_CF = 1;
1.1       root     11760:        }
                   11761: }
                   11762: 
                   11763: inline void msdos_int_21h_47h(int lfn)
                   11764: {
                   11765:        char path[MAX_PATH];
                   11766:        
                   11767:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45  root     11768:                if(!lfn) {
                   11769:                        strcpy(path, msdos_short_path(path));
                   11770:                }
1.1       root     11771:                if(path[1] == ':') {
                   11772:                        // the returned path does not include a drive or the initial backslash
1.1.1.45  root     11773:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     11774:                } else {
1.1.1.45  root     11775:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     11776:                }
                   11777:        } else {
                   11778:                REG16(AX) = errno;
1.1.1.3   root     11779:                m_CF = 1;
1.1       root     11780:        }
                   11781: }
                   11782: 
                   11783: inline void msdos_int_21h_48h()
                   11784: {
1.1.1.19  root     11785:        int seg, umb_linked;
1.1       root     11786:        
1.1.1.8   root     11787:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     11788:                // unlink umb not to allocate memory in umb
                   11789:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   11790:                        msdos_mem_unlink_umb();
                   11791:                }
1.1.1.8   root     11792:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11793:                        REG16(AX) = seg;
                   11794:                } else {
                   11795:                        REG16(AX) = 0x08;
                   11796:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   11797:                        m_CF = 1;
                   11798:                }
1.1.1.19  root     11799:                if(umb_linked != 0) {
                   11800:                        msdos_mem_link_umb();
                   11801:                }
1.1.1.8   root     11802:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   11803:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11804:                        REG16(AX) = seg;
                   11805:                } else {
                   11806:                        REG16(AX) = 0x08;
                   11807:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   11808:                        m_CF = 1;
                   11809:                }
                   11810:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   11811:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11812:                        REG16(AX) = seg;
                   11813:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11814:                        REG16(AX) = seg;
                   11815:                } else {
                   11816:                        REG16(AX) = 0x08;
                   11817:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   11818:                        m_CF = 1;
                   11819:                }
1.1       root     11820:        }
                   11821: }
                   11822: 
                   11823: inline void msdos_int_21h_49h()
                   11824: {
1.1.1.14  root     11825:        int mcb_seg = SREG(ES) - 1;
                   11826:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   11827:        
                   11828:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11829:                msdos_mem_free(SREG(ES));
                   11830:        } else {
1.1.1.33  root     11831:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     11832:                m_CF = 1;
                   11833:        }
1.1       root     11834: }
                   11835: 
                   11836: inline void msdos_int_21h_4ah()
                   11837: {
1.1.1.14  root     11838:        int mcb_seg = SREG(ES) - 1;
                   11839:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     11840:        int max_paragraphs;
                   11841:        
1.1.1.14  root     11842:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11843:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   11844:                        REG16(AX) = 0x08;
                   11845:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   11846:                        m_CF = 1;
                   11847:                }
                   11848:        } else {
1.1.1.33  root     11849:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     11850:                m_CF = 1;
1.1       root     11851:        }
                   11852: }
                   11853: 
                   11854: inline void msdos_int_21h_4bh()
                   11855: {
1.1.1.3   root     11856:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11857:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     11858:        
                   11859:        switch(REG8(AL)) {
                   11860:        case 0x00:
                   11861:        case 0x01:
                   11862:                if(msdos_process_exec(command, param, REG8(AL))) {
                   11863:                        REG16(AX) = 0x02;
1.1.1.3   root     11864:                        m_CF = 1;
1.1       root     11865:                }
                   11866:                break;
1.1.1.14  root     11867:        case 0x03:
                   11868:                {
                   11869:                        int fd;
                   11870:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   11871:                                REG16(AX) = 0x02;
                   11872:                                m_CF = 1;
                   11873:                                break;
                   11874:                        }
                   11875:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   11876:                        _close(fd);
                   11877:                        
                   11878:                        UINT16 *overlay = (UINT16 *)param;
                   11879:                        
                   11880:                        // check exe header
                   11881:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   11882:                        int header_size = 0;
                   11883:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   11884:                                header_size = header->header_size * 16;
                   11885:                                // relocation
                   11886:                                int start_seg = overlay[1];
                   11887:                                for(int i = 0; i < header->relocations; i++) {
                   11888:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   11889:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   11890:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   11891:                                }
                   11892:                        }
                   11893:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   11894:                }
                   11895:                break;
1.1.1.48  root     11896:        case 0x04:
                   11897:                // Load And Execute In Background (European MS-DOS 4.0 only)
                   11898: //     case 0x05:
                   11899: //             // DOS 5+ - Set Execution State
                   11900:        case 0x80:
                   11901:                // DR DOS v3.41 - Run Already-Loaded Kernel File
                   11902:        case 0xf0:
                   11903:        case 0xf1:
                   11904:                // DIET v1.10+
1.1.1.43  root     11905:        case 0xfd:
                   11906:        case 0xfe:
                   11907:                // unknown function called in FreeCOM
                   11908:                REG16(AX) = 0x01;
                   11909:                m_CF = 1;
                   11910:                break;
1.1       root     11911:        default:
1.1.1.22  root     11912:                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     11913:                REG16(AX) = 0x01;
1.1.1.3   root     11914:                m_CF = 1;
1.1       root     11915:                break;
                   11916:        }
                   11917: }
                   11918: 
                   11919: inline void msdos_int_21h_4ch()
                   11920: {
                   11921:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   11922: }
                   11923: 
                   11924: inline void msdos_int_21h_4dh()
                   11925: {
                   11926:        REG16(AX) = retval;
                   11927: }
                   11928: 
                   11929: inline void msdos_int_21h_4eh()
                   11930: {
                   11931:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11932:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11933:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45  root     11934:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11935:        WIN32_FIND_DATA fd;
                   11936:        
1.1.1.14  root     11937:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   11938:        find->find_magic = FIND_MAGIC;
                   11939:        find->dta_index = dtainfo - dtalist;
1.1       root     11940:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     11941:        dtainfo->allowable_mask = REG8(CL);
                   11942:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     11943:        
1.1.1.14  root     11944:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   11945:                dtainfo->allowable_mask &= ~8;
1.1       root     11946:        }
1.1.1.14  root     11947:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   11948:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11949:                      !msdos_find_file_has_8dot3name(&fd)) {
                   11950:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11951:                                FindClose(dtainfo->find_handle);
                   11952:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11953:                                break;
                   11954:                        }
                   11955:                }
                   11956:        }
1.1.1.13  root     11957:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11958:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11959:                msdos_find_file_conv_local_time(&fd);
                   11960:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11961:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11962:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11963:                REG16(AX) = 0;
1.1.1.14  root     11964:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11965:                find->attrib = 8;
                   11966:                find->size = 0;
                   11967:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11968:                dtainfo->allowable_mask &= ~8;
1.1       root     11969:                REG16(AX) = 0;
                   11970:        } else {
                   11971:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     11972:                m_CF = 1;
1.1       root     11973:        }
                   11974: }
                   11975: 
                   11976: inline void msdos_int_21h_4fh()
                   11977: {
                   11978:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11979:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11980:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     11981:        WIN32_FIND_DATA fd;
                   11982:        
1.1.1.14  root     11983:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   11984:                REG16(AX) = 0x12;
                   11985:                m_CF = 1;
                   11986:                return;
                   11987:        }
                   11988:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     11989:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11990:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     11991:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11992:                              !msdos_find_file_has_8dot3name(&fd)) {
                   11993:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11994:                                        FindClose(dtainfo->find_handle);
                   11995:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11996:                                        break;
                   11997:                                }
                   11998:                        }
                   11999:                } else {
1.1.1.13  root     12000:                        FindClose(dtainfo->find_handle);
                   12001:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12002:                }
                   12003:        }
1.1.1.13  root     12004:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12005:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   12006:                msdos_find_file_conv_local_time(&fd);
                   12007:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   12008:                find->size = fd.nFileSizeLow;
1.1.1.13  root     12009:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     12010:                REG16(AX) = 0;
1.1.1.14  root     12011:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12012:                find->attrib = 8;
                   12013:                find->size = 0;
                   12014:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12015:                dtainfo->allowable_mask &= ~8;
1.1       root     12016:                REG16(AX) = 0;
                   12017:        } else {
                   12018:                REG16(AX) = 0x12;
1.1.1.3   root     12019:                m_CF = 1;
1.1       root     12020:        }
                   12021: }
                   12022: 
                   12023: inline void msdos_int_21h_50h()
                   12024: {
1.1.1.8   root     12025:        if(current_psp != REG16(BX)) {
                   12026:                process_t *process = msdos_process_info_get(current_psp);
                   12027:                if(process != NULL) {
                   12028:                        process->psp = REG16(BX);
                   12029:                }
                   12030:                current_psp = REG16(BX);
1.1.1.23  root     12031:                msdos_sda_update(current_psp);
1.1.1.8   root     12032:        }
1.1       root     12033: }
                   12034: 
                   12035: inline void msdos_int_21h_51h()
                   12036: {
                   12037:        REG16(BX) = current_psp;
                   12038: }
                   12039: 
                   12040: inline void msdos_int_21h_52h()
                   12041: {
1.1.1.25  root     12042:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     12043:        i386_load_segment_descriptor(ES);
1.1.1.25  root     12044:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     12045: }
                   12046: 
1.1.1.43  root     12047: inline void msdos_int_21h_53h()
                   12048: {
                   12049:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   12050:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   12051:        
                   12052:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   12053:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   12054:        dpb->shift_count = 0;
                   12055:        dpb->reserved_sectors = 0;
                   12056:        dpb->fat_num = bpb->fat_num;
                   12057:        dpb->root_entries = bpb->root_entries;
                   12058:        dpb->first_data_sector = 0;
                   12059:        if(bpb->sectors_per_cluster != 0) {
                   12060:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   12061:        } else {
                   12062:                dpb->highest_cluster_num = 0;
                   12063:        }
                   12064:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   12065:        dpb->first_dir_sector = 0;
                   12066:        dpb->device_driver_header = 0;
                   12067:        dpb->media_type = bpb->media_type;
                   12068:        dpb->drive_accessed = 0;
                   12069:        dpb->next_dpb_ofs = 0xffff;
                   12070:        dpb->next_dpb_seg = 0xffff;
                   12071:        dpb->first_free_cluster = 0;
                   12072:        dpb->free_clusters = 0xffff;
                   12073: }
                   12074: 
1.1       root     12075: inline void msdos_int_21h_54h()
                   12076: {
                   12077:        process_t *process = msdos_process_info_get(current_psp);
                   12078:        
                   12079:        REG8(AL) = process->verify;
                   12080: }
                   12081: 
                   12082: inline void msdos_int_21h_55h()
                   12083: {
                   12084:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   12085:        
                   12086:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   12087:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   12088:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   12089:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   12090:        psp->parent_psp = current_psp;
                   12091: }
                   12092: 
                   12093: inline void msdos_int_21h_56h(int lfn)
                   12094: {
                   12095:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     12096:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   12097:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     12098:        
                   12099:        if(rename(src, dst)) {
                   12100:                REG16(AX) = errno;
1.1.1.3   root     12101:                m_CF = 1;
1.1       root     12102:        }
                   12103: }
                   12104: 
                   12105: inline void msdos_int_21h_57h()
                   12106: {
                   12107:        FILETIME time, local;
1.1.1.14  root     12108:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     12109:        HANDLE hHandle;
1.1       root     12110:        
1.1.1.21  root     12111:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     12112:                REG16(AX) = (UINT16)GetLastError();
                   12113:                m_CF = 1;
                   12114:                return;
                   12115:        }
                   12116:        ctime = atime = mtime = NULL;
                   12117:        
1.1       root     12118:        switch(REG8(AL)) {
                   12119:        case 0x00:
1.1.1.6   root     12120:        case 0x01:
1.1.1.14  root     12121:                mtime = &time;
1.1.1.6   root     12122:                break;
                   12123:        case 0x04:
                   12124:        case 0x05:
1.1.1.14  root     12125:                atime = &time;
1.1       root     12126:                break;
1.1.1.6   root     12127:        case 0x06:
                   12128:        case 0x07:
1.1.1.14  root     12129:                ctime = &time;
                   12130:                break;
                   12131:        default:
1.1.1.22  root     12132:                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     12133:                REG16(AX) = 0x01;
                   12134:                m_CF = 1;
                   12135:                return;
                   12136:        }
                   12137:        if(REG8(AL) & 1) {
1.1       root     12138:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   12139:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     12140:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     12141:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12142:                        m_CF = 1;
1.1       root     12143:                }
1.1.1.14  root     12144:        } else {
1.1.1.21  root     12145:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     12146:                        // assume a device and use the current time
                   12147:                        GetSystemTimeAsFileTime(&time);
                   12148:                }
                   12149:                FileTimeToLocalFileTime(&time, &local);
                   12150:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     12151:        }
                   12152: }
                   12153: 
                   12154: inline void msdos_int_21h_58h()
                   12155: {
                   12156:        switch(REG8(AL)) {
                   12157:        case 0x00:
1.1.1.7   root     12158:                REG16(AX) = malloc_strategy;
                   12159:                break;
                   12160:        case 0x01:
1.1.1.24  root     12161: //             switch(REG16(BX)) {
                   12162:                switch(REG8(BL)) {
1.1.1.7   root     12163:                case 0x0000:
                   12164:                case 0x0001:
                   12165:                case 0x0002:
                   12166:                case 0x0040:
                   12167:                case 0x0041:
                   12168:                case 0x0042:
                   12169:                case 0x0080:
                   12170:                case 0x0081:
                   12171:                case 0x0082:
                   12172:                        malloc_strategy = REG16(BX);
1.1.1.23  root     12173:                        msdos_sda_update(current_psp);
1.1.1.7   root     12174:                        break;
                   12175:                default:
1.1.1.22  root     12176:                        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     12177:                        REG16(AX) = 0x01;
                   12178:                        m_CF = 1;
                   12179:                        break;
                   12180:                }
                   12181:                break;
                   12182:        case 0x02:
1.1.1.19  root     12183:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     12184:                break;
                   12185:        case 0x03:
1.1.1.24  root     12186: //             switch(REG16(BX)) {
                   12187:                switch(REG8(BL)) {
1.1.1.7   root     12188:                case 0x0000:
1.1.1.19  root     12189:                        msdos_mem_unlink_umb();
                   12190:                        break;
1.1.1.7   root     12191:                case 0x0001:
1.1.1.19  root     12192:                        msdos_mem_link_umb();
1.1.1.7   root     12193:                        break;
                   12194:                default:
1.1.1.22  root     12195:                        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     12196:                        REG16(AX) = 0x01;
                   12197:                        m_CF = 1;
                   12198:                        break;
                   12199:                }
1.1       root     12200:                break;
                   12201:        default:
1.1.1.22  root     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));
1.1       root     12203:                REG16(AX) = 0x01;
1.1.1.3   root     12204:                m_CF = 1;
1.1       root     12205:                break;
                   12206:        }
                   12207: }
                   12208: 
                   12209: inline void msdos_int_21h_59h()
                   12210: {
1.1.1.47  root     12211:        if(REG16(BX) == 0x0000) {
                   12212:                sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   12213:                
                   12214:                REG16(AX) = sda->extended_error_code;
                   12215:                REG8(BH)  = sda->error_class;
                   12216:                REG8(BL)  = sda->suggested_action;
                   12217:                REG8(CH)  = sda->locus_of_last_error;
                   12218:                // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
                   12219:                if(sda->int21h_5d0ah_called != 0) {
                   12220:                        REG8(CL)  = sda->int21h_5d0ah_cl;
                   12221:                        REG16(DX) = sda->int21h_5d0ah_dx;
                   12222: //                     REG16(SI) = sda->int21h_5d0ah_si;
                   12223:                        REG16(DI) = sda->last_error_pointer.w.l;
                   12224: //                     SREG(DS)  = sda->int21h_5d0ah_ds;
                   12225: //                     i386_load_segment_descriptor(DS);
                   12226:                        SREG(ES)  = sda->last_error_pointer.w.h;
                   12227:                        i386_load_segment_descriptor(ES);
                   12228:                }
                   12229:                sda->int21h_5d0ah_called = 0;
                   12230: //     } else if(REG16(BX) == 0x0001) {
                   12231: //             // European MS-DOS 4.0 - Get Hard Error Information
                   12232:        } else {
                   12233:                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));
                   12234:                REG16(AX) = 0x01;
                   12235:                m_CF = 1;
                   12236:        }
1.1       root     12237: }
                   12238: 
                   12239: inline void msdos_int_21h_5ah()
                   12240: {
1.1.1.3   root     12241:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12242:        int len = strlen(path);
                   12243:        char tmp[MAX_PATH];
                   12244:        
                   12245:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   12246:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12247:                
                   12248:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12249:                REG16(AX) = fd;
                   12250:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12251:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12252:                
                   12253:                strcpy(path, tmp);
                   12254:                int dx = REG16(DX) + len;
1.1.1.3   root     12255:                int ds = SREG(DS);
1.1       root     12256:                while(dx > 0xffff) {
                   12257:                        dx -= 0x10;
                   12258:                        ds++;
                   12259:                }
                   12260:                REG16(DX) = dx;
1.1.1.3   root     12261:                SREG(DS) = ds;
                   12262:                i386_load_segment_descriptor(DS);
1.1       root     12263:        } else {
                   12264:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12265:                m_CF = 1;
1.1       root     12266:        }
                   12267: }
                   12268: 
                   12269: inline void msdos_int_21h_5bh()
                   12270: {
1.1.1.45  root     12271:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     12272:        
1.1.1.45  root     12273: //     if(msdos_is_existing_file(path)) {
                   12274:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12275:                // already exists
                   12276:                REG16(AX) = 0x50;
1.1.1.3   root     12277:                m_CF = 1;
1.1       root     12278:        } else {
                   12279:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12280:                
                   12281:                if(fd != -1) {
                   12282:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12283:                        REG16(AX) = fd;
                   12284:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12285:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12286:                } else {
                   12287:                        REG16(AX) = errno;
1.1.1.3   root     12288:                        m_CF = 1;
1.1       root     12289:                }
                   12290:        }
                   12291: }
                   12292: 
                   12293: inline void msdos_int_21h_5ch()
                   12294: {
                   12295:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12296:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12297:        
1.1.1.20  root     12298:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     12299:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     12300:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     12301:                        UINT32 pos = _tell(fd);
                   12302:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   12303:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     12304:                                REG16(AX) = errno;
1.1.1.3   root     12305:                                m_CF = 1;
1.1       root     12306:                        }
1.1.1.20  root     12307:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     12308:                        
1.1       root     12309:                        // some seconds may be passed in _locking()
1.1.1.35  root     12310:                        REQUEST_HARDWRE_UPDATE();
1.1       root     12311:                } else {
                   12312:                        REG16(AX) = 0x01;
1.1.1.3   root     12313:                        m_CF = 1;
1.1       root     12314:                }
                   12315:        } else {
                   12316:                REG16(AX) = 0x06;
1.1.1.3   root     12317:                m_CF = 1;
1.1       root     12318:        }
                   12319: }
                   12320: 
1.1.1.22  root     12321: inline void msdos_int_21h_5dh()
                   12322: {
                   12323:        switch(REG8(AL)) {
1.1.1.45  root     12324:        case 0x00:
                   12325:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
                   12326:                        // current system
                   12327:                        static bool reenter = false;
                   12328:                        if(!reenter) {
                   12329:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
                   12330:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
                   12331:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
                   12332:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
                   12333:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
                   12334:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
                   12335:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
                   12336:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
                   12337:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
                   12338:                                i386_load_segment_descriptor(DS);
                   12339:                                i386_load_segment_descriptor(ES);
                   12340:                                reenter = true;
                   12341:                                try {
                   12342:                                        msdos_syscall(0x21);
                   12343:                                } catch(...) {
                   12344:                                }
                   12345:                                reenter = false;
                   12346:                        }
                   12347:                } else {
                   12348:                        REG16(AX) = 0x49; //  network software not installed
                   12349:                        m_CF = 1;
                   12350:                }
                   12351:                break;
1.1.1.22  root     12352:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     12353:                SREG(DS) = (SDA_TOP >> 4);
                   12354:                i386_load_segment_descriptor(DS);
                   12355:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   12356:                REG16(CX) = 0x80;
                   12357:                REG16(DX) = 0x1a;
                   12358:                break;
1.1.1.45  root     12359:        case 0x07: // get redirected printer mode
                   12360:        case 0x08: // set redirected printer mode
                   12361:        case 0x09: // flush redirected printer output
                   12362:                REG16(AX) = 0x49; //  network software not installed
                   12363:                m_CF = 1;
                   12364:                break;
1.1.1.43  root     12365:        case 0x0a: // set extended error information
                   12366:                {
                   12367:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     12368:                        sda->int21h_5d0ah_called    = 1;
1.1.1.43  root     12369:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47  root     12370:                        // XXX: which one is correct ???
                   12371: #if 1
                   12372:                        // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43  root     12373:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   12374:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47  root     12375:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43  root     12376:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47  root     12377: #else
                   12378:                        // PC DOS 7 Technical Update
                   12379:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
                   12380:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
                   12381:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
                   12382:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
                   12383: #endif
                   12384:                        sda->int21h_5d0ah_dx        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   12385: //                     sda->int21h_5d0ah_si        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43  root     12386:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47  root     12387: //                     sda->int21h_5d0ah_ds        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43  root     12388:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   12389:                }
                   12390:                break;
1.1.1.23  root     12391:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     12392:                REG16(AX) = 0x01;
                   12393:                m_CF = 1;
                   12394:                break;
                   12395:        default:
                   12396:                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));
                   12397:                REG16(AX) = 0x01;
                   12398:                m_CF = 1;
                   12399:                break;
                   12400:        }
                   12401: }
                   12402: 
1.1.1.42  root     12403: inline void msdos_int_21h_5eh()
                   12404: {
                   12405:        switch(REG8(AL)) {
                   12406:        case 0x00:
                   12407:                {
                   12408:                        char name[256] = {0};
                   12409:                        DWORD dwSize = 256;
                   12410:                        
                   12411:                        if(GetComputerName(name, &dwSize)) {
                   12412:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12413:                                for(int i = 0; i < 15; i++) {
                   12414:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   12415:                                }
                   12416:                                dest[15] = '\0';
                   12417:                                REG8(CH) = 0x01; // nonzero valid
                   12418:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   12419:                        } else {
                   12420:                                REG16(AX) = 0x01;
                   12421:                                m_CF = 1;
                   12422:                        }
                   12423:                }
                   12424:                break;
                   12425:        default:
1.1.1.45  root     12426: //             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));
                   12427: //             REG16(AX) = 0x01;
                   12428:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     12429:                m_CF = 1;
                   12430:                break;
                   12431:        }
                   12432: }
                   12433: 
1.1.1.30  root     12434: inline void msdos_int_21h_5fh()
                   12435: {
                   12436:        switch(REG8(AL)) {
1.1.1.42  root     12437:        case 0x05:
1.1.1.44  root     12438:                REG16(BP) = 0;
                   12439:                for(int i = 0; i < 26; i++) {
                   12440:                        if(msdos_is_remote_drive(i)) {
                   12441:                                REG16(BP)++;
1.1.1.42  root     12442:                        }
                   12443:                }
1.1.1.30  root     12444:        case 0x02:
1.1.1.44  root     12445:                for(int i = 0, index = 0; i < 26; i++) {
                   12446:                        if(msdos_is_remote_drive(i)) {
                   12447:                                if(index == REG16(BX)) {
                   12448:                                        char volume[] = "A:";
1.1.1.30  root     12449:                                        volume[0] = 'A' + i;
1.1.1.44  root     12450:                                        DWORD dwSize = 128;
                   12451:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   12452:                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   12453:                                        REG8(BH) = 0x00; // valid
                   12454:                                        REG8(BL) = 0x04; // disk drive
                   12455:                                        REG16(CX) = 0x00; // LANtastic
                   12456:                                        return;
1.1.1.30  root     12457:                                }
1.1.1.44  root     12458:                                index++;
1.1.1.30  root     12459:                        }
                   12460:                }
                   12461:                REG16(AX) = 0x12; // no more files
                   12462:                m_CF = 1;
                   12463:                break;
1.1.1.44  root     12464:        case 0x07:
                   12465:                if(msdos_is_valid_drive(REG8(DL))) {
                   12466:                        msdos_cds_update(REG8(DL));
                   12467:                } else {
                   12468:                        REG16(AX) = 0x0f; // invalid drive
                   12469:                        m_CF = 1;
                   12470:                }
                   12471:                break;
                   12472:        case 0x08:
                   12473:                if(msdos_is_valid_drive(REG8(DL))) {
                   12474:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   12475:                        cds->drive_attrib = 0x0000;
                   12476:                } else {
                   12477:                        REG16(AX) = 0x0f; // invalid drive
                   12478:                        m_CF = 1;
                   12479:                }
                   12480:                break;
1.1.1.30  root     12481:        default:
1.1.1.45  root     12482: //             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));
                   12483: //             REG16(AX) = 0x01;
                   12484:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     12485:                m_CF = 1;
                   12486:                break;
                   12487:        }
                   12488: }
                   12489: 
1.1       root     12490: inline void msdos_int_21h_60h(int lfn)
                   12491: {
1.1.1.45  root     12492:        char full[MAX_PATH];
                   12493:        const char *path = NULL;
1.1.1.14  root     12494:        
1.1       root     12495:        if(lfn) {
1.1.1.14  root     12496:                char *name;
                   12497:                *full = '\0';
1.1.1.3   root     12498:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     12499:                switch(REG8(CL)) {
                   12500:                case 1:
                   12501:                        GetShortPathName(full, full, MAX_PATH);
                   12502:                        my_strupr(full);
                   12503:                        break;
                   12504:                case 2:
                   12505:                        GetLongPathName(full, full, MAX_PATH);
                   12506:                        break;
                   12507:                }
                   12508:                path = full;
                   12509:        } else {
                   12510:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12511:        }
                   12512:        if(*path != '\0') {
                   12513:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     12514:        } else {
1.1.1.14  root     12515:                REG16(AX) = (UINT16)GetLastError();
                   12516:                m_CF = 1;
1.1       root     12517:        }
                   12518: }
                   12519: 
                   12520: inline void msdos_int_21h_61h()
                   12521: {
                   12522:        REG8(AL) = 0;
                   12523: }
                   12524: 
                   12525: inline void msdos_int_21h_62h()
                   12526: {
                   12527:        REG16(BX) = current_psp;
                   12528: }
                   12529: 
                   12530: inline void msdos_int_21h_63h()
                   12531: {
                   12532:        switch(REG8(AL)) {
                   12533:        case 0x00:
1.1.1.3   root     12534:                SREG(DS) = (DBCS_TABLE >> 4);
                   12535:                i386_load_segment_descriptor(DS);
1.1       root     12536:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   12537:                REG8(AL) = 0x00;
                   12538:                break;
1.1.1.22  root     12539:        case 0x01: // set korean input mode
                   12540:        case 0x02: // get korean input mode
                   12541:                REG8(AL) = 0xff; // not supported
                   12542:                break;
1.1       root     12543:        default:
1.1.1.22  root     12544:                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     12545:                REG16(AX) = 0x01;
1.1.1.3   root     12546:                m_CF = 1;
1.1       root     12547:                break;
                   12548:        }
                   12549: }
                   12550: 
1.1.1.25  root     12551: UINT16 get_extended_country_info(UINT8 func)
1.1       root     12552: {
1.1.1.25  root     12553:        switch(func) {
1.1.1.17  root     12554:        case 0x01:
                   12555:                if(REG16(CX) >= 5) {
1.1.1.19  root     12556:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     12557:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   12558:                                REG16(CX) = sizeof(data);
                   12559:                        ZeroMemory(data, sizeof(data));
                   12560:                        data[0] = 0x01;
                   12561:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     12562:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     12563:                        *(UINT16 *)(data + 5) = active_code_page;
                   12564:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     12565: //                     REG16(AX) = active_code_page;
1.1.1.17  root     12566:                } else {
1.1.1.25  root     12567:                        return(0x08); // insufficient memory
1.1.1.17  root     12568:                }
                   12569:                break;
                   12570:        case 0x02:
                   12571:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12572:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   12573:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     12574: //             REG16(AX) = active_code_page;
1.1.1.17  root     12575:                REG16(CX) = 0x05;
                   12576:                break;
1.1.1.23  root     12577:        case 0x03:
                   12578:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12579:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   12580:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     12581: //             REG16(AX) = active_code_page;
1.1.1.23  root     12582:                REG16(CX) = 0x05;
                   12583:                break;
1.1.1.17  root     12584:        case 0x04:
                   12585:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   12586:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   12587:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     12588: //             REG16(AX) = active_code_page;
1.1.1.17  root     12589:                REG16(CX) = 0x05;
                   12590:                break;
                   12591:        case 0x05:
                   12592:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   12593:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   12594:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     12595: //             REG16(AX) = active_code_page;
1.1.1.17  root     12596:                REG16(CX) = 0x05;
                   12597:                break;
                   12598:        case 0x06:
                   12599:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   12600:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   12601:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     12602: //             REG16(AX) = active_code_page;
1.1.1.17  root     12603:                REG16(CX) = 0x05;
                   12604:                break;
1.1       root     12605:        case 0x07:
1.1.1.3   root     12606:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   12607:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   12608:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     12609: //             REG16(AX) = active_code_page;
1.1       root     12610:                REG16(CX) = 0x05;
                   12611:                break;
1.1.1.25  root     12612:        default:
                   12613:                return(0x01); // function number invalid
                   12614:        }
                   12615:        return(0x00);
                   12616: }
                   12617: 
                   12618: inline void msdos_int_21h_65h()
                   12619: {
                   12620:        char tmp[0x10000];
                   12621:        
                   12622:        switch(REG8(AL)) {
1.1.1.43  root     12623:        case 0x00:
                   12624:                if(REG16(CX) >= 7) {
                   12625:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   12626:                        REG16(AX) = system_code_page;
                   12627:                } else {
                   12628:                        REG16(AX) = 0x0c;
                   12629:                        m_CF = 1;
                   12630:                }
                   12631:                break;
1.1.1.25  root     12632:        case 0x01:
                   12633:        case 0x02:
                   12634:        case 0x03:
                   12635:        case 0x04:
                   12636:        case 0x05:
                   12637:        case 0x06:
                   12638:        case 0x07:
                   12639:                {
                   12640:                        UINT16 result = get_extended_country_info(REG8(AL));
                   12641:                        if(result) {
                   12642:                                REG16(AX) = result;
                   12643:                                m_CF = 1;
                   12644:                        } else {
                   12645:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   12646:                        }
                   12647:                }
                   12648:                break;
1.1       root     12649:        case 0x20:
1.1.1.25  root     12650:        case 0xa0:
1.1.1.19  root     12651:                memset(tmp, 0, sizeof(tmp));
                   12652:                tmp[0] = REG8(DL);
1.1       root     12653:                my_strupr(tmp);
                   12654:                REG8(DL) = tmp[0];
                   12655:                break;
                   12656:        case 0x21:
1.1.1.25  root     12657:        case 0xa1:
1.1       root     12658:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12659:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     12660:                my_strupr(tmp);
1.1.1.3   root     12661:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     12662:                break;
                   12663:        case 0x22:
1.1.1.25  root     12664:        case 0xa2:
1.1.1.3   root     12665:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     12666:                break;
1.1.1.25  root     12667:        case 0x23:
                   12668:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45  root     12669:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     12670:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45  root     12671:                        REG16(AX) = 0x00;
                   12672:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   12673:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     12674:                        REG16(AX) = 0x01;
                   12675:                } else {
                   12676:                        REG16(AX) = 0x02;
                   12677:                }
                   12678:                break;
1.1       root     12679:        default:
1.1.1.22  root     12680:                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     12681:                REG16(AX) = 0x01;
1.1.1.3   root     12682:                m_CF = 1;
1.1       root     12683:                break;
                   12684:        }
                   12685: }
                   12686: 
                   12687: inline void msdos_int_21h_66h()
                   12688: {
                   12689:        switch(REG8(AL)) {
                   12690:        case 0x01:
                   12691:                REG16(BX) = active_code_page;
                   12692:                REG16(DX) = system_code_page;
                   12693:                break;
                   12694:        case 0x02:
                   12695:                if(active_code_page == REG16(BX)) {
                   12696:                        REG16(AX) = 0xeb41;
                   12697:                } else if(_setmbcp(REG16(BX)) == 0) {
                   12698:                        active_code_page = REG16(BX);
1.1.1.17  root     12699:                        msdos_nls_tables_update();
1.1       root     12700:                        REG16(AX) = 0xeb41;
1.1.1.32  root     12701:                        SetConsoleCP(active_code_page);
                   12702:                        SetConsoleOutputCP(active_code_page);
1.1       root     12703:                } else {
                   12704:                        REG16(AX) = 0x25;
1.1.1.3   root     12705:                        m_CF = 1;
1.1       root     12706:                }
                   12707:                break;
                   12708:        default:
1.1.1.22  root     12709:                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     12710:                REG16(AX) = 0x01;
1.1.1.3   root     12711:                m_CF = 1;
1.1       root     12712:                break;
                   12713:        }
                   12714: }
                   12715: 
                   12716: inline void msdos_int_21h_67h()
                   12717: {
                   12718:        process_t *process = msdos_process_info_get(current_psp);
                   12719:        
                   12720:        if(REG16(BX) <= MAX_FILES) {
                   12721:                process->max_files = max(REG16(BX), 20);
                   12722:        } else {
                   12723:                REG16(AX) = 0x08;
1.1.1.3   root     12724:                m_CF = 1;
1.1       root     12725:        }
                   12726: }
                   12727: 
                   12728: inline void msdos_int_21h_68h()
                   12729: {
                   12730:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12731:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12732:        
1.1.1.20  root     12733:        if(fd < process->max_files && file_handler[fd].valid) {
                   12734:                // fflush(_fdopen(fd, ""));
1.1       root     12735:        } else {
                   12736:                REG16(AX) = 0x06;
1.1.1.3   root     12737:                m_CF = 1;
1.1       root     12738:        }
                   12739: }
                   12740: 
                   12741: inline void msdos_int_21h_69h()
                   12742: {
1.1.1.3   root     12743:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12744:        char path[] = "A:\\";
                   12745:        char volume_label[MAX_PATH];
                   12746:        DWORD serial_number = 0;
                   12747:        char file_system[MAX_PATH];
                   12748:        
                   12749:        if(REG8(BL) == 0) {
                   12750:                path[0] = 'A' + _getdrive() - 1;
                   12751:        } else {
                   12752:                path[0] = 'A' + REG8(BL) - 1;
                   12753:        }
                   12754:        
                   12755:        switch(REG8(AL)) {
                   12756:        case 0x00:
                   12757:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   12758:                        info->info_level = 0;
                   12759:                        info->serial_number = serial_number;
                   12760:                        memset(info->volume_label, 0x20, 11);
                   12761:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   12762:                        memset(info->file_system, 0x20, 8);
                   12763:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   12764:                } else {
                   12765:                        REG16(AX) = errno;
1.1.1.3   root     12766:                        m_CF = 1;
1.1       root     12767:                }
                   12768:                break;
                   12769:        case 0x01:
                   12770:                REG16(AX) = 0x03;
1.1.1.3   root     12771:                m_CF = 1;
1.1.1.45  root     12772:                break;
                   12773:        default:
                   12774:                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));
                   12775:                REG16(AX) = 0x01;
                   12776:                m_CF = 1;
                   12777:                break;
1.1       root     12778:        }
                   12779: }
                   12780: 
                   12781: inline void msdos_int_21h_6ah()
                   12782: {
                   12783:        REG8(AH) = 0x68;
                   12784:        msdos_int_21h_68h();
                   12785: }
                   12786: 
                   12787: inline void msdos_int_21h_6bh()
                   12788: {
1.1.1.45  root     12789:        REG8(AL) = 0x00;
1.1       root     12790: }
                   12791: 
                   12792: inline void msdos_int_21h_6ch(int lfn)
                   12793: {
1.1.1.45  root     12794:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     12795:        int mode = REG8(BL) & 0x03;
                   12796:        
                   12797:        if(mode < 0x03) {
1.1.1.29  root     12798:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12799:                        // file exists
                   12800:                        if(REG8(DL) & 1) {
1.1.1.37  root     12801:                                int fd = -1;
                   12802:                                int sio_port = 0;
                   12803:                                int lpt_port = 0;
1.1       root     12804:                                
1.1.1.45  root     12805:                                if(msdos_is_device_path(path)) {
                   12806:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     12807:                                } else {
1.1.1.13  root     12808:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     12809:                                }
1.1       root     12810:                                if(fd != -1) {
                   12811:                                        REG16(AX) = fd;
                   12812:                                        REG16(CX) = 1;
1.1.1.45  root     12813:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12814:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12815:                                } else {
                   12816:                                        REG16(AX) = errno;
1.1.1.3   root     12817:                                        m_CF = 1;
1.1       root     12818:                                }
                   12819:                        } else if(REG8(DL) & 2) {
                   12820:                                int attr = GetFileAttributes(path);
1.1.1.37  root     12821:                                int fd = -1;
                   12822:                                int sio_port = 0;
                   12823:                                int lpt_port = 0;
1.1       root     12824:                                
1.1.1.45  root     12825:                                if(msdos_is_device_path(path)) {
                   12826:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     12827:                                } else {
                   12828:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12829:                                }
                   12830:                                if(fd != -1) {
                   12831:                                        if(attr == -1) {
                   12832:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   12833:                                        }
                   12834:                                        SetFileAttributes(path, attr);
                   12835:                                        REG16(AX) = fd;
                   12836:                                        REG16(CX) = 3;
1.1.1.45  root     12837:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12838:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12839:                                } else {
                   12840:                                        REG16(AX) = errno;
1.1.1.3   root     12841:                                        m_CF = 1;
1.1       root     12842:                                }
                   12843:                        } else {
                   12844:                                REG16(AX) = 0x50;
1.1.1.3   root     12845:                                m_CF = 1;
1.1       root     12846:                        }
                   12847:                } else {
                   12848:                        // file not exists
                   12849:                        if(REG8(DL) & 0x10) {
                   12850:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12851:                                
                   12852:                                if(fd != -1) {
                   12853:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12854:                                        REG16(AX) = fd;
                   12855:                                        REG16(CX) = 2;
                   12856:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12857:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12858:                                } else {
                   12859:                                        REG16(AX) = errno;
1.1.1.3   root     12860:                                        m_CF = 1;
1.1       root     12861:                                }
                   12862:                        } else {
                   12863:                                REG16(AX) = 0x02;
1.1.1.3   root     12864:                                m_CF = 1;
1.1       root     12865:                        }
                   12866:                }
                   12867:        } else {
                   12868:                REG16(AX) = 0x0c;
1.1.1.3   root     12869:                m_CF = 1;
1.1       root     12870:        }
                   12871: }
                   12872: 
1.1.1.43  root     12873: inline void msdos_int_21h_70h()
                   12874: {
                   12875:        switch(REG8(AL)) {
1.1.1.48  root     12876:        case 0x00: // get ??? info
                   12877:        case 0x01: // set above info
                   12878: //             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));
                   12879:                REG16(AX) = 0x7000;
                   12880:                m_CF = 1;
                   12881:                break;
                   12882:        case 0x02: // set general internationalization info
1.1.1.43  root     12883:                if(REG16(CX) >= 7) {
                   12884:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   12885:                        msdos_nls_tables_update();
                   12886:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   12887:                        REG16(AX) = system_code_page;
                   12888:                } else {
                   12889:                        REG16(AX) = 0x0c;
                   12890:                        m_CF = 1;
                   12891:                }
                   12892:                break;
                   12893:        default:
                   12894:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48  root     12895:                REG16(AX) = 0x7000;
1.1.1.43  root     12896:                m_CF = 1;
                   12897:                break;
                   12898:        }
                   12899: }
                   12900: 
1.1       root     12901: inline void msdos_int_21h_710dh()
                   12902: {
                   12903:        // reset drive
                   12904: }
                   12905: 
1.1.1.48  root     12906: inline void msdos_int_21h_7141h()
1.1.1.17  root     12907: {
                   12908:        if(REG16(SI) == 0) {
1.1.1.48  root     12909:                msdos_int_21h_41h(1);
1.1.1.17  root     12910:                return;
                   12911:        }
                   12912:        if(REG16(SI) != 1) {
                   12913:                REG16(AX) = 5;
                   12914:                m_CF = 1;
                   12915:        }
                   12916:        /* wild card and matching attributes... */
                   12917:        char tmp[MAX_PATH * 2];
                   12918:        // copy search pathname (and quick check overrun)
                   12919:        ZeroMemory(tmp, sizeof(tmp));
                   12920:        tmp[MAX_PATH - 1] = '\0';
                   12921:        tmp[MAX_PATH] = 1;
                   12922:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   12923:        
                   12924:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   12925:                REG16(AX) = 1;
                   12926:                m_CF = 1;
                   12927:                return;
                   12928:        }
                   12929:        for(char *s = tmp; *s; ++s) {
                   12930:                if(*s == '/') {
                   12931:                        *s = '\\';
                   12932:                }
                   12933:        }
                   12934:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   12935:        if(tmp_name) {
                   12936:                ++tmp_name;
                   12937:        } else {
                   12938:                tmp_name = strchr(tmp, ':');
                   12939:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   12940:        }
                   12941:        
                   12942:        WIN32_FIND_DATAA fd;
                   12943:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   12944:        if(fh == INVALID_HANDLE_VALUE) {
                   12945:                REG16(AX) = 2;
                   12946:                m_CF = 1;
                   12947:                return;
                   12948:        }
                   12949:        do {
                   12950:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   12951:                        strcpy(tmp_name, fd.cFileName);
1.1.1.48  root     12952:                        if(remove(msdos_trimmed_path(tmp, 1))) {
1.1.1.17  root     12953:                                REG16(AX) = 5;
                   12954:                                m_CF = 1;
                   12955:                                break;
                   12956:                        }
                   12957:                }
                   12958:        } while(FindNextFileA(fh, &fd));
                   12959:        if(!m_CF) {
                   12960:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   12961:                        m_CF = 1;
                   12962:                        REG16(AX) = 2;
                   12963:                }
                   12964:        }
                   12965:        FindClose(fh);
                   12966: }
                   12967: 
1.1       root     12968: inline void msdos_int_21h_714eh()
                   12969: {
                   12970:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12971:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   12972:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12973:        WIN32_FIND_DATA fd;
                   12974:        
1.1.1.13  root     12975:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12976:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12977:                FindClose(dtainfo->find_handle);
                   12978:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12979:        }
                   12980:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12981:        dtainfo->allowable_mask = REG8(CL);
                   12982:        dtainfo->required_mask = REG8(CH);
                   12983:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12984:        
1.1.1.14  root     12985:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12986:                dtainfo->allowable_mask &= ~8;
1.1       root     12987:        }
1.1.1.14  root     12988:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   12989:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12990:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12991:                                FindClose(dtainfo->find_handle);
                   12992:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12993:                                break;
                   12994:                        }
                   12995:                }
                   12996:        }
1.1.1.13  root     12997:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12998:                find->attrib = fd.dwFileAttributes;
                   12999:                msdos_find_file_conv_local_time(&fd);
                   13000:                if(REG16(SI) == 0) {
                   13001:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13002:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13003:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13004:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13005:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13006:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13007:                } else {
                   13008:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13009:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13010:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13011:                }
                   13012:                find->size_hi = fd.nFileSizeHigh;
                   13013:                find->size_lo = fd.nFileSizeLow;
                   13014:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13015:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13016:                REG16(AX) = dtainfo - dtalist + 1;
                   13017:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13018:                // volume label
                   13019:                find->attrib = 8;
                   13020:                find->size_hi = find->size_lo = 0;
                   13021:                strcpy(find->full_name, process->volume_label);
                   13022:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13023:                dtainfo->allowable_mask &= ~8;
                   13024:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     13025:        } else {
                   13026:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     13027:                m_CF = 1;
1.1       root     13028:        }
                   13029: }
                   13030: 
                   13031: inline void msdos_int_21h_714fh()
                   13032: {
                   13033:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13034:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     13035:        WIN32_FIND_DATA fd;
                   13036:        
1.1.1.14  root     13037:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13038:                REG16(AX) = 6;
1.1.1.13  root     13039:                m_CF = 1;
                   13040:                return;
                   13041:        }
1.1.1.14  root     13042:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13043:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13044:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     13045:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     13046:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   13047:                                        FindClose(dtainfo->find_handle);
                   13048:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13049:                                        break;
                   13050:                                }
                   13051:                        }
                   13052:                } else {
1.1.1.13  root     13053:                        FindClose(dtainfo->find_handle);
                   13054:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13055:                }
                   13056:        }
1.1.1.13  root     13057:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13058:                find->attrib = fd.dwFileAttributes;
                   13059:                msdos_find_file_conv_local_time(&fd);
                   13060:                if(REG16(SI) == 0) {
                   13061:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13062:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13063:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13064:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13065:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13066:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13067:                } else {
                   13068:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13069:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13070:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13071:                }
                   13072:                find->size_hi = fd.nFileSizeHigh;
                   13073:                find->size_lo = fd.nFileSizeLow;
                   13074:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13075:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13076:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13077:                // volume label
                   13078:                find->attrib = 8;
                   13079:                find->size_hi = find->size_lo = 0;
                   13080:                strcpy(find->full_name, process->volume_label);
                   13081:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13082:                dtainfo->allowable_mask &= ~8;
1.1       root     13083:        } else {
                   13084:                REG16(AX) = 0x12;
1.1.1.3   root     13085:                m_CF = 1;
1.1       root     13086:        }
                   13087: }
                   13088: 
                   13089: inline void msdos_int_21h_71a0h()
                   13090: {
                   13091:        DWORD max_component_len, file_sys_flag;
                   13092:        
1.1.1.14  root     13093:        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))) {
                   13094:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   13095:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     13096:                REG16(CX) = (UINT16)max_component_len;          // 255
                   13097:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   13098:        } else {
                   13099:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13100:                m_CF = 1;
1.1       root     13101:        }
                   13102: }
                   13103: 
                   13104: inline void msdos_int_21h_71a1h()
                   13105: {
1.1.1.14  root     13106:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13107:                REG16(AX) = 6;
1.1.1.13  root     13108:                m_CF = 1;
                   13109:                return;
                   13110:        }
1.1.1.14  root     13111:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13112:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13113:                FindClose(dtainfo->find_handle);
                   13114:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13115:        }
                   13116: }
                   13117: 
                   13118: inline void msdos_int_21h_71a6h()
                   13119: {
                   13120:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     13121:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13122:        
1.1.1.3   root     13123:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13124:        struct _stat64 status;
                   13125:        DWORD serial_number = 0;
                   13126:        
1.1.1.20  root     13127:        if(fd < process->max_files && file_handler[fd].valid) {
                   13128:                if(_fstat64(fd, &status) == 0) {
                   13129:                        if(file_handler[fd].path[1] == ':') {
1.1       root     13130:                                // NOTE: we need to consider the network file path "\\host\share\"
                   13131:                                char volume[] = "A:\\";
1.1.1.20  root     13132:                                volume[0] = file_handler[fd].path[1];
1.1       root     13133:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   13134:                        }
1.1.1.20  root     13135:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     13136:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   13137:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   13138:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   13139:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   13140:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   13141:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   13142:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   13143:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   13144:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   13145:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     13146:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     13147:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     13148:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     13149:                } else {
                   13150:                        REG16(AX) = errno;
1.1.1.3   root     13151:                        m_CF = 1;
1.1       root     13152:                }
                   13153:        } else {
                   13154:                REG16(AX) = 0x06;
1.1.1.3   root     13155:                m_CF = 1;
1.1       root     13156:        }
                   13157: }
                   13158: 
                   13159: inline void msdos_int_21h_71a7h()
                   13160: {
                   13161:        switch(REG8(BL)) {
                   13162:        case 0x00:
1.1.1.3   root     13163:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     13164:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13165:                        m_CF = 1;
1.1       root     13166:                }
                   13167:                break;
                   13168:        case 0x01:
                   13169:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     13170:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     13171:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13172:                        m_CF = 1;
1.1       root     13173:                }
                   13174:                break;
                   13175:        default:
1.1.1.22  root     13176:                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     13177:                REG16(AX) = 0x7100;
1.1.1.3   root     13178:                m_CF = 1;
1.1       root     13179:                break;
                   13180:        }
                   13181: }
                   13182: 
                   13183: inline void msdos_int_21h_71a8h()
                   13184: {
                   13185:        if(REG8(DH) == 0) {
                   13186:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     13187:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13188:                memset(fcb, 0x20, sizeof(fcb));
                   13189:                int len = strlen(tmp);
1.1.1.21  root     13190:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     13191:                        if(tmp[i] == '.') {
                   13192:                                pos = 8;
                   13193:                        } else {
                   13194:                                if(msdos_lead_byte_check(tmp[i])) {
                   13195:                                        fcb[pos++] = tmp[i++];
                   13196:                                }
                   13197:                                fcb[pos++] = tmp[i];
                   13198:                        }
                   13199:                }
1.1.1.3   root     13200:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     13201:        } else {
1.1.1.3   root     13202:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13203:        }
                   13204: }
                   13205: 
1.1.1.22  root     13206: inline void msdos_int_21h_71aah()
                   13207: {
                   13208:        char drv[] = "A:", path[MAX_PATH];
                   13209:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13210:        
                   13211:        if(REG8(BL) == 0) {
                   13212:                drv[0] = 'A' + _getdrive() - 1;
                   13213:        } else {
                   13214:                drv[0] = 'A' + REG8(BL) - 1;
                   13215:        }
                   13216:        switch(REG8(BH)) {
                   13217:        case 0x00:
1.1.1.44  root     13218:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13219:                        REG16(AX) = 0x0f; // invalid drive
                   13220:                        m_CF = 1;
                   13221:                } else if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   13222:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     13223:                        m_CF = 1;
                   13224:                }
                   13225:                break;
                   13226:        case 0x01:
1.1.1.44  root     13227:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13228:                        REG16(AX) = 0x0f; // invalid drive
                   13229:                        m_CF = 1;
                   13230:                } else if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     13231:                        REG16(AX) = 0x0f; // invalid drive
                   13232:                        m_CF = 1;
                   13233:                }
                   13234:                break;
                   13235:        case 0x02:
1.1.1.44  root     13236:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13237:                        REG16(AX) = 0x0f; // invalid drive
                   13238:                        m_CF = 1;
                   13239:                } else if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     13240:                        REG16(AX) = 0x0f; // invalid drive
                   13241:                        m_CF = 1;
                   13242:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   13243:                        REG16(AX) = 0x0f; // invalid drive
                   13244:                        m_CF = 1;
                   13245:                } else {
                   13246:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   13247:                }
                   13248:                break;
                   13249:        default:
                   13250:                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     13251:                REG16(AX) = 0x7100;
1.1.1.22  root     13252:                m_CF = 1;
                   13253:                break;
                   13254:        }
                   13255: }
                   13256: 
1.1.1.14  root     13257: inline void msdos_int_21h_7300h()
                   13258: {
1.1.1.44  root     13259:        REG8(AL) = REG8(CL);
                   13260:        REG8(AH) = 0;
1.1.1.14  root     13261: }
                   13262: 
                   13263: inline void msdos_int_21h_7302h()
                   13264: {
                   13265:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   13266:        UINT16 seg, ofs;
                   13267:        
                   13268:        if(REG16(CX) < 0x3f) {
                   13269:                REG8(AL) = 0x18;
                   13270:                m_CF = 1;
                   13271:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   13272:                REG8(AL) = 0xff;
                   13273:                m_CF = 1;
                   13274:        } else {
                   13275:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   13276:        }
                   13277: }
                   13278: 
1.1       root     13279: inline void msdos_int_21h_7303h()
                   13280: {
1.1.1.3   root     13281:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13282:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     13283:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   13284:        
                   13285:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   13286:                info->size_of_structure = sizeof(ext_space_info_t);
                   13287:                info->structure_version = 0;
                   13288:                info->sectors_per_cluster = sectors_per_cluster;
                   13289:                info->bytes_per_sector = bytes_per_sector;
                   13290:                info->available_clusters_on_drive = free_clusters;
                   13291:                info->total_clusters_on_drive = total_clusters;
                   13292:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   13293:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   13294:                info->available_allocation_units = free_clusters;       // ???
                   13295:                info->total_allocation_units = total_clusters;          // ???
                   13296:        } else {
                   13297:                REG16(AX) = errno;
1.1.1.3   root     13298:                m_CF = 1;
1.1       root     13299:        }
                   13300: }
                   13301: 
1.1.1.30  root     13302: inline void msdos_int_21h_dbh()
                   13303: {
                   13304:        // Novell NetWare - Workstation - Get Number of Local Drives
                   13305:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   13306:        REG8(AL) = dos_info->last_drive;
                   13307: }
                   13308: 
                   13309: inline void msdos_int_21h_dch()
                   13310: {
                   13311:        // Novell NetWare - Connection Services - Get Connection Number
                   13312:        REG8(AL) = 0x00;
                   13313: }
                   13314: 
1.1.1.32  root     13315: inline void msdos_int_24h()
                   13316: {
                   13317:        const char *message = NULL;
                   13318:        int key = 0;
                   13319:        
                   13320:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   13321:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   13322:                        if(active_code_page == 932) {
                   13323:                                message = critical_error_table[i].message_japanese;
                   13324:                        }
                   13325:                        if(message == NULL) {
                   13326:                                message = critical_error_table[i].message_english;
                   13327:                        }
                   13328:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   13329:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   13330:                        
                   13331:                        SREG(ES) = WORK_TOP >> 4;
                   13332:                        i386_load_segment_descriptor(ES);
                   13333:                        REG16(DI) = 0x0000;
                   13334:                        break;
                   13335:                }
                   13336:        }
                   13337:        fprintf(stderr, "\n%s", message);
                   13338:        if(!(REG8(AH) & 0x80)) {
                   13339:                if(REG8(AH) & 0x01) {
                   13340:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   13341:                } else {
                   13342:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   13343:                }
                   13344:        }
                   13345:        fprintf(stderr, "\n");
                   13346:        
1.1.1.33  root     13347:        {
1.1.1.32  root     13348:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     13349:        }
1.1.1.32  root     13350:        if(REG8(AH) & 0x10) {
                   13351:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   13352:        }
                   13353:        if(REG8(AH) & 0x20) {
                   13354:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   13355:        }
                   13356:        if(REG8(AH) & 0x08) {
                   13357:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   13358:        }
                   13359:        fprintf(stderr, "? ");
                   13360:        
                   13361:        while(1) {
                   13362:                while(!_kbhit()) {
                   13363:                        Sleep(10);
                   13364:                }
                   13365:                key = _getch();
                   13366:                
                   13367:                if(key == 'I' || key == 'i') {
                   13368:                        if(REG8(AH) & 0x20) {
                   13369:                                REG8(AL) = 0;
                   13370:                                break;
                   13371:                        }
                   13372:                } else if(key == 'R' || key == 'r') {
                   13373:                        if(REG8(AH) & 0x10) {
                   13374:                                REG8(AL) = 1;
                   13375:                                break;
                   13376:                        }
                   13377:                } else if(key == 'A' || key == 'a') {
                   13378:                        REG8(AL) = 2;
                   13379:                        break;
                   13380:                } else if(key == 'F' || key == 'f') {
                   13381:                        if(REG8(AH) & 0x08) {
                   13382:                                REG8(AL) = 3;
                   13383:                                break;
                   13384:                        }
                   13385:                }
                   13386:        }
                   13387:        fprintf(stderr, "%c\n", key);
                   13388: }
                   13389: 
1.1       root     13390: inline void msdos_int_25h()
                   13391: {
                   13392:        UINT16 seg, ofs;
                   13393:        DWORD dwSize;
                   13394:        
1.1.1.3   root     13395: #if defined(HAS_I386)
                   13396:        I386OP(pushf)();
                   13397: #else
                   13398:        PREFIX86(_pushf());
                   13399: #endif
1.1       root     13400:        
                   13401:        if(!(REG8(AL) < 26)) {
                   13402:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13403:                m_CF = 1;
1.1       root     13404:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13405:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13406:                m_CF = 1;
1.1       root     13407:        } else {
                   13408:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13409:                char dev[64];
                   13410:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13411:                
                   13412:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   13413:                if(hFile == INVALID_HANDLE_VALUE) {
                   13414:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13415:                        m_CF = 1;
1.1       root     13416:                } else {
1.1.1.19  root     13417:                        UINT32 top_sector  = REG16(DX);
                   13418:                        UINT16 sector_num  = REG16(CX);
                   13419:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13420:                        
                   13421:                        if(sector_num == 0xffff) {
                   13422:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13423:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13424:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13425:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13426:                                buffer_addr = (seg << 4) + ofs;
                   13427:                        }
                   13428: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13429: //                             REG8(AL) = 0x02; // drive not ready
                   13430: //                             m_CF = 1;
                   13431: //                     } else 
                   13432:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13433:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13434:                                m_CF = 1;
1.1.1.19  root     13435:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13436:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     13437:                                m_CF = 1;
1.1       root     13438:                        }
                   13439:                        CloseHandle(hFile);
                   13440:                }
                   13441:        }
                   13442: }
                   13443: 
                   13444: inline void msdos_int_26h()
                   13445: {
1.1.1.42  root     13446:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     13447:        UINT16 seg, ofs;
                   13448:        DWORD dwSize;
                   13449:        
1.1.1.3   root     13450: #if defined(HAS_I386)
                   13451:        I386OP(pushf)();
                   13452: #else
                   13453:        PREFIX86(_pushf());
                   13454: #endif
1.1       root     13455:        
                   13456:        if(!(REG8(AL) < 26)) {
                   13457:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13458:                m_CF = 1;
1.1       root     13459:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13460:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13461:                m_CF = 1;
1.1       root     13462:        } else {
                   13463:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13464:                char dev[64];
                   13465:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13466:                
                   13467:                if(dpb->media_type == 0xf8) {
                   13468:                        // this drive is not a floppy
1.1.1.6   root     13469: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   13470: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   13471: //                     }
1.1       root     13472:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13473:                        m_CF = 1;
1.1       root     13474:                } else {
                   13475:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   13476:                        if(hFile == INVALID_HANDLE_VALUE) {
                   13477:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13478:                                m_CF = 1;
1.1       root     13479:                        } else {
1.1.1.19  root     13480:                                UINT32 top_sector  = REG16(DX);
                   13481:                                UINT16 sector_num  = REG16(CX);
                   13482:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13483:                                
                   13484:                                if(sector_num == 0xffff) {
                   13485:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13486:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13487:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13488:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13489:                                        buffer_addr = (seg << 4) + ofs;
                   13490:                                }
1.1       root     13491:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13492:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13493:                                        m_CF = 1;
1.1.1.19  root     13494:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13495:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13496:                                        m_CF = 1;
1.1.1.19  root     13497:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13498:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     13499:                                        m_CF = 1;
1.1       root     13500:                                }
                   13501:                                CloseHandle(hFile);
                   13502:                        }
                   13503:                }
                   13504:        }
                   13505: }
                   13506: 
                   13507: inline void msdos_int_27h()
                   13508: {
1.1.1.29  root     13509:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   13510:        try {
                   13511:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13512:        } catch(...) {
                   13513:                // recover the broken mcb
                   13514:                int mcb_seg = SREG(CS) - 1;
                   13515:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     13516:                
1.1.1.29  root     13517:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     13518:                        mcb->mz = 'M';
                   13519:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   13520:                        
1.1.1.29  root     13521:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     13522:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     13523:                        } else {
1.1.1.39  root     13524:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     13525:                        }
                   13526:                } else {
                   13527:                        mcb->mz = 'Z';
1.1.1.30  root     13528:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     13529:                }
                   13530:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13531:        }
1.1.1.3   root     13532:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     13533: }
                   13534: 
                   13535: inline void msdos_int_29h()
                   13536: {
1.1.1.50  root     13537:        msdos_putch_fast(REG8(AL));
1.1       root     13538: }
                   13539: 
                   13540: inline void msdos_int_2eh()
                   13541: {
                   13542:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   13543:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13544:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     13545:        char *token = my_strtok(tmp, " ");
                   13546:        strcpy(command, token);
                   13547:        strcpy(opt, token + strlen(token) + 1);
                   13548:        
                   13549:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   13550:        param->env_seg = 0;
                   13551:        param->cmd_line.w.l = 44;
                   13552:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13553:        param->fcb1.w.l = 24;
                   13554:        param->fcb1.w.h = (WORK_TOP >> 4);
                   13555:        param->fcb2.w.l = 24;
                   13556:        param->fcb2.w.h = (WORK_TOP >> 4);
                   13557:        
                   13558:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   13559:        
                   13560:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   13561:        cmd_line->len = strlen(opt);
                   13562:        strcpy(cmd_line->cmd, opt);
                   13563:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   13564:        
1.1.1.28  root     13565:        try {
                   13566:                if(msdos_process_exec(command, param, 0)) {
                   13567:                        REG16(AX) = 0xffff; // error before processing command
                   13568:                } else {
                   13569:                        // set flag to set retval to ax when the started process is terminated
                   13570:                        process_t *process = msdos_process_info_get(current_psp);
                   13571:                        process->called_by_int2eh = true;
                   13572:                }
                   13573:        } catch(...) {
                   13574:                REG16(AX) = 0xffff; // error before processing command
                   13575:        }
1.1       root     13576: }
                   13577: 
1.1.1.29  root     13578: inline void msdos_int_2fh_05h()
                   13579: {
                   13580:        switch(REG8(AL)) {
                   13581:        case 0x00:
1.1.1.49  root     13582:                // critical error handler is installed
1.1.1.32  root     13583:                REG8(AL) = 0xff;
                   13584:                break;
                   13585:        case 0x01:
                   13586:        case 0x02:
                   13587:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   13588:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   13589:                                const char *message = NULL;
                   13590:                                if(active_code_page == 932) {
                   13591:                                        message = standard_error_table[i].message_japanese;
                   13592:                                }
                   13593:                                if(message == NULL) {
                   13594:                                        message = standard_error_table[i].message_english;
                   13595:                                }
                   13596:                                strcpy((char *)(mem + WORK_TOP), message);
                   13597:                                
                   13598:                                SREG(ES) = WORK_TOP >> 4;
                   13599:                                i386_load_segment_descriptor(ES);
                   13600:                                REG16(DI) = 0x0000;
                   13601:                                REG8(AL) = 0x01;
                   13602:                                break;
                   13603:                        }
                   13604:                }
1.1.1.29  root     13605:                break;
                   13606:        default:
                   13607:                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     13608:                REG16(AX) = 0x01;
1.1.1.29  root     13609:                m_CF = 1;
                   13610:        }
                   13611: }
                   13612: 
1.1.1.44  root     13613: inline void msdos_int_2fh_06h()
                   13614: {
                   13615:        switch(REG8(AL)) {
                   13616:        case 0x00:
                   13617:                // ASSIGN is not installed
1.1.1.49  root     13618: //             REG8(AL) = 0x00;
1.1.1.44  root     13619:                break;
                   13620:        case 0x01:
                   13621:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   13622:                REG16(AX) = 0x01;
                   13623:                m_CF = 1;
                   13624:                break;
                   13625:        default:
                   13626:                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));
                   13627:                REG16(AX) = 0x01;
                   13628:                m_CF = 1;
                   13629:                break;
                   13630:        }
                   13631: }
                   13632: 
1.1.1.22  root     13633: inline void msdos_int_2fh_11h()
                   13634: {
                   13635:        switch(REG8(AL)) {
                   13636:        case 0x00:
1.1.1.29  root     13637:                if(i386_read_stack() == 0xdada) {
1.1.1.53  root     13638: #ifdef SUPPORT_MSCDEX
                   13639:                        // MSCDEX is installed
                   13640:                        REG8(AL) = 0xff;
                   13641:                        i386_write_stack(0xadad);
                   13642: #else
1.1.1.29  root     13643:                        // MSCDEX is not installed
                   13644: //                     REG8(AL) = 0x00;
1.1.1.53  root     13645: #endif
1.1.1.29  root     13646:                } else {
                   13647:                        // Network Redirector is not installed
                   13648: //                     REG8(AL) = 0x00;
                   13649:                }
1.1.1.22  root     13650:                break;
                   13651:        default:
1.1.1.43  root     13652: //             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     13653:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     13654:                m_CF = 1;
                   13655:                break;
                   13656:        }
                   13657: }
                   13658: 
1.1.1.21  root     13659: inline void msdos_int_2fh_12h()
                   13660: {
                   13661:        switch(REG8(AL)) {
1.1.1.22  root     13662:        case 0x00:
1.1.1.29  root     13663:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     13664:                REG8(AL) = 0xff;
                   13665:                break;
1.1.1.29  root     13666: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   13667:        case 0x02:
                   13668:                {
                   13669:                        UINT16 stack = i386_read_stack();
                   13670:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   13671:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   13672:                        i386_load_segment_descriptor(ES);
                   13673:                }
                   13674:                break;
1.1.1.30  root     13675:        case 0x03:
                   13676:                SREG(DS) = (DEVICE_TOP >> 4);
                   13677:                i386_load_segment_descriptor(DS);
                   13678:                break;
1.1.1.29  root     13679:        case 0x04:
                   13680:                {
                   13681:                        UINT16 stack = i386_read_stack();
                   13682:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   13683: #if defined(HAS_I386)
                   13684:                        m_ZF = (REG8(AL) == '\\');
                   13685: #else
                   13686:                        m_ZeroVal = (REG8(AL) != '\\');
                   13687: #endif
                   13688:                }
                   13689:                break;
                   13690:        case 0x05:
1.1.1.49  root     13691:                {
                   13692:                        UINT16 c = i386_read_stack();
                   13693:                        if((c >> 0) & 0xff) {
                   13694:                                msdos_putch((c >> 0) & 0xff);
                   13695:                        }
                   13696:                        if((c >> 8) & 0xff) {
                   13697:                                msdos_putch((c >> 8) & 0xff);
                   13698:                        }
                   13699:                }
1.1.1.29  root     13700:                break;
1.1.1.49  root     13701: //     case 0x06: // DOS 3.0+ internal - Invoke Critical Error
1.1.1.29  root     13702: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   13703: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
1.1.1.49  root     13704: //     case 0x09: // DOS 3.0+ internal - Flush and Free Disk Buffer
1.1.1.29  root     13705: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   13706: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   13707: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   13708:        case 0x0d:
                   13709:                {
                   13710:                        SYSTEMTIME time;
                   13711:                        FILETIME file_time;
                   13712:                        WORD dos_date, dos_time;
                   13713:                        GetLocalTime(&time);
                   13714:                        SystemTimeToFileTime(&time, &file_time);
                   13715:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   13716:                        REG16(AX) = dos_date;
                   13717:                        REG16(DX) = dos_time;
                   13718:                }
                   13719:                break;
                   13720: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   13721: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   13722: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   13723:        case 0x11:
                   13724:                {
                   13725:                        char path[MAX_PATH], *p;
                   13726:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13727:                        my_strupr(path);
                   13728:                        while((p = my_strchr(path, '/')) != NULL) {
                   13729:                                *p = '\\';
                   13730:                        }
                   13731:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   13732:                }
                   13733:                break;
                   13734:        case 0x12:
                   13735:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13736:                break;
                   13737:        case 0x13:
                   13738:                {
                   13739:                        char tmp[2] = {0};
                   13740:                        tmp[0] = i386_read_stack();
                   13741:                        my_strupr(tmp);
                   13742:                        REG8(AL) = tmp[0];
                   13743:                }
                   13744:                break;
                   13745:        case 0x14:
                   13746: #if defined(HAS_I386)
                   13747:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   13748: #else
                   13749:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13750: #endif
                   13751:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13752:                break;
                   13753: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     13754:        case 0x16:
                   13755:                if(REG16(BX) < 20) {
                   13756:                        SREG(ES) = SFT_TOP >> 4;
                   13757:                        i386_load_segment_descriptor(ES);
                   13758:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   13759:                        
                   13760:                        // update system file table
                   13761:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   13762:                        if(file_handler[REG16(BX)].valid) {
                   13763:                                int count = 0;
                   13764:                                for(int i = 0; i < 20; i++) {
                   13765:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   13766:                                                count++;
                   13767:                                        }
                   13768:                                }
                   13769:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   13770:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   13771:                                _lseek(REG16(BX), 0, SEEK_END);
                   13772:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   13773:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   13774:                        } else {
                   13775:                                memset(sft, 0, 0x3b);
                   13776:                        }
                   13777:                } else {
                   13778:                        REG16(AX) = 0x06;
                   13779:                        m_CF = 1;
                   13780:                }
                   13781:                break;
1.1.1.49  root     13782:        case 0x17:
                   13783:                {
                   13784:                        UINT16 drive = i386_read_stack();
                   13785:                        if(msdos_is_valid_drive(drive)) {
                   13786:                                msdos_cds_update(drive);
                   13787:                        }
                   13788:                        REG16(SI) = 88 * drive;
                   13789:                        SREG(DS) = (CDS_TOP >> 4);
                   13790:                        i386_load_segment_descriptor(DS);
                   13791:                }
                   13792:                break;
1.1.1.29  root     13793: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   13794: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   13795:        case 0x1a:
                   13796:                {
                   13797:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   13798:                        if(path[1] == ':') {
                   13799:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   13800:                                        REG8(AL) = path[0] - 'a' + 1;
                   13801:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   13802:                                        REG8(AL) = path[0] - 'A' + 1;
                   13803:                                } else {
                   13804:                                        REG8(AL) = 0xff; // invalid
                   13805:                                }
                   13806:                                strcpy(full, path);
                   13807:                                strcpy(path, full + 2);
                   13808:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   13809:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   13810:                                        REG8(AL) = full[0] - 'a' + 1;
                   13811:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   13812:                                        REG8(AL) = full[0] - 'A' + 1;
                   13813:                                } else {
                   13814:                                        REG8(AL) = 0xff; // invalid
                   13815:                                }
                   13816:                        } else {
                   13817:                                REG8(AL) = 0x00; // default
                   13818:                        }
                   13819:                }
                   13820:                break;
                   13821:        case 0x1b:
                   13822:                {
                   13823:                        int year = REG16(CX) + 1980;
                   13824:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   13825:                }
                   13826:                break;
                   13827: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   13828: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   13829:        case 0x1e:
                   13830:                {
                   13831:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   13832:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   13833:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   13834: #if defined(HAS_I386)
                   13835:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   13836: #else
                   13837:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   13838: #endif
                   13839:                        } else {
                   13840: #if defined(HAS_I386)
                   13841:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   13842: #else
                   13843:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   13844: #endif
                   13845:                        }
                   13846:                }
                   13847:                break;
1.1.1.49  root     13848:        case 0x1f:
                   13849:                {
                   13850:                        UINT16 drive = i386_read_stack();
                   13851:                        if(msdos_is_valid_drive(drive)) {
                   13852:                                msdos_cds_update(drive);
                   13853:                        }
                   13854:                        REG16(SI) = 88 * drive;
                   13855:                        SREG(ES) = (CDS_TOP >> 4);
                   13856:                        i386_load_segment_descriptor(ES);
                   13857:                }
                   13858:                break;
1.1.1.21  root     13859:        case 0x20:
                   13860:                {
                   13861:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13862:                        
                   13863:                        if(fd < 20) {
                   13864:                                SREG(ES) = current_psp;
                   13865:                                i386_load_segment_descriptor(ES);
                   13866:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   13867:                        } else {
                   13868:                                REG16(AX) = 0x06;
                   13869:                                m_CF = 1;
                   13870:                        }
                   13871:                }
                   13872:                break;
1.1.1.29  root     13873:        case 0x21:
                   13874:                msdos_int_21h_60h(0);
                   13875:                break;
1.1.1.49  root     13876:        case 0x22:
                   13877:                {
                   13878:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13879:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00) != 0xff) {
                   13880:                                sda->extended_error_code = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00);
                   13881:                        }
                   13882:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01) != 0xff) {
                   13883:                                sda->error_class         = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01);
                   13884:                        }
                   13885:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02) != 0xff) {
                   13886:                                sda->suggested_action    = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02);
                   13887:                        }
                   13888:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03) != 0xff) {
                   13889:                                sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03);
                   13890:                        }
                   13891:                }
                   13892:                break;
1.1.1.29  root     13893: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   13894: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   13895:        case 0x25:
                   13896:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13897:                break;
                   13898:        case 0x26:
                   13899:                REG8(AL) = REG8(CL);
                   13900:                msdos_int_21h_3dh();
                   13901:                break;
                   13902:        case 0x27:
                   13903:                msdos_int_21h_3eh();
                   13904:                break;
                   13905:        case 0x28:
                   13906:                REG16(AX) = REG16(BP);
                   13907:                msdos_int_21h_42h();
                   13908:                break;
                   13909:        case 0x29:
                   13910:                msdos_int_21h_3fh();
                   13911:                break;
                   13912: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   13913:        case 0x2b:
                   13914:                REG16(AX) = REG16(BP);
                   13915:                msdos_int_21h_44h();
                   13916:                break;
                   13917:        case 0x2c:
                   13918:                REG16(BX) = DEVICE_TOP >> 4;
                   13919:                REG16(AX) = 22;
                   13920:                break;
                   13921:        case 0x2d:
                   13922:                {
                   13923:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13924:                        REG16(AX) = sda->extended_error_code;
                   13925:                }
                   13926:                break;
                   13927:        case 0x2e:
                   13928:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     13929:                        SREG(ES) = 0x0001;
                   13930:                        i386_load_segment_descriptor(ES);
                   13931:                        REG16(DI) = 0x00;
                   13932:                } else if(REG8(DL) == 0x08) {
1.1.1.49  root     13933:                        // dummy parameter error message read routine is at fffc:0010
                   13934:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.22  root     13935:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     13936:                        REG16(DI) = 0x0010;
1.1.1.22  root     13937:                }
                   13938:                break;
1.1.1.29  root     13939:        case 0x2f:
                   13940:                if(REG16(DX) != 0) {
1.1.1.30  root     13941:                        dos_major_version = REG8(DL);
                   13942:                        dos_minor_version = REG8(DH);
1.1.1.29  root     13943:                } else {
                   13944:                        REG8(DL) = 7;
                   13945:                        REG8(DH) = 10;
                   13946:                }
                   13947:                break;
                   13948: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   13949: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     13950:        default:
                   13951:                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));
                   13952:                REG16(AX) = 0x01;
                   13953:                m_CF = 1;
                   13954:                break;
                   13955:        }
                   13956: }
                   13957: 
1.1.1.30  root     13958: inline void msdos_int_2fh_13h()
                   13959: {
                   13960:        static UINT16 prevDS = 0, prevDX = 0;
                   13961:        static UINT16 prevES = 0, prevBX = 0;
                   13962:        UINT16 tmp;
                   13963:        
                   13964:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   13965:        i386_load_segment_descriptor(DS);
                   13966:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   13967:        
                   13968:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   13969:        i386_load_segment_descriptor(ES);
                   13970:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   13971: }
                   13972: 
1.1.1.22  root     13973: inline void msdos_int_2fh_14h()
                   13974: {
                   13975:        switch(REG8(AL)) {
                   13976:        case 0x00:
1.1.1.29  root     13977:                // NLSFUNC.COM is installed
                   13978:                REG8(AL) = 0xff;
1.1.1.25  root     13979:                break;
                   13980:        case 0x01:
                   13981:        case 0x03:
                   13982:                REG8(AL) = 0x00;
                   13983:                active_code_page = REG16(BX);
                   13984:                msdos_nls_tables_update();
                   13985:                break;
                   13986:        case 0x02:
                   13987:                REG8(AL) = get_extended_country_info(REG16(BP));
                   13988:                break;
                   13989:        case 0x04:
1.1.1.42  root     13990:                for(int i = 0;; i++) {
                   13991:                        if(country_table[i].code == REG16(DX)) {
                   13992:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   13993:                                break;
                   13994:                        } else if(country_table[i].code == -1) {
                   13995:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13996:                                break;
                   13997:                        }
                   13998:                }
1.1.1.25  root     13999:                REG8(AL) = 0x00;
1.1.1.22  root     14000:                break;
                   14001:        default:
                   14002:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14003:                REG16(AX) = 0x01;
                   14004:                m_CF = 1;
                   14005:                break;
                   14006:        }
                   14007: }
                   14008: 
                   14009: inline void msdos_int_2fh_15h()
                   14010: {
                   14011:        switch(REG8(AL)) {
1.1.1.29  root     14012:        case 0x00: // CD-ROM - Installation Check
                   14013:                if(REG16(BX) == 0x0000) {
1.1.1.53  root     14014: #ifdef SUPPORT_MSCDEX
1.1.1.43  root     14015:                        // MSCDEX is installed
                   14016:                        REG16(BX) = 0;
                   14017:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     14018:                                if(msdos_is_cdrom_drive(i)) {
                   14019:                                        if(REG16(BX) == 0) {
                   14020:                                                REG16(CX) = i;
1.1.1.43  root     14021:                                        }
1.1.1.44  root     14022:                                        REG16(BX)++;
1.1.1.43  root     14023:                                }
                   14024:                        }
                   14025: #else
1.1.1.29  root     14026:                        // MSCDEX is not installed
                   14027: //                     REG8(AL) = 0x00;
1.1.1.43  root     14028: #endif
1.1.1.29  root     14029:                } else {
                   14030:                        // GRAPHICS.COM is not installed
                   14031: //                     REG8(AL) = 0x00;
                   14032:                }
1.1.1.22  root     14033:                break;
1.1.1.43  root     14034:        case 0x0b:
1.1.1.44  root     14035:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   14036:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   14037:                REG16(BX) = 0xadad;
1.1.1.43  root     14038:                break;
                   14039:        case 0x0d:
1.1.1.44  root     14040:                for(int i = 0, n = 0; i < 26; i++) {
                   14041:                        if(msdos_is_cdrom_drive(i)) {
                   14042:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     14043:                        }
                   14044:                }
                   14045:                break;
1.1.1.22  root     14046:        case 0xff:
1.1.1.29  root     14047:                if(REG16(BX) == 0x0000) {
                   14048:                        // CORELCDX is not installed
                   14049:                } else {
                   14050:                        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));
                   14051:                        REG16(AX) = 0x01;
                   14052:                        m_CF = 1;
                   14053:                }
1.1.1.22  root     14054:                break;
1.1.1.21  root     14055:        default:
1.1.1.22  root     14056:                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     14057:                REG16(AX) = 0x01;
                   14058:                m_CF = 1;
                   14059:                break;
                   14060:        }
                   14061: }
                   14062: 
1.1       root     14063: inline void msdos_int_2fh_16h()
                   14064: {
                   14065:        switch(REG8(AL)) {
                   14066:        case 0x00:
1.1.1.14  root     14067:                if(no_windows) {
1.1.1.29  root     14068:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   14069: //                     REG8(AL) = 0x00;
1.1.1.14  root     14070:                } else {
1.1.1.30  root     14071:                        REG8(AL) = win_major_version;
                   14072:                        REG8(AH) = win_minor_version;
1.1       root     14073:                }
                   14074:                break;
1.1.1.43  root     14075:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     14076:                // from DOSBox
                   14077:                i386_set_a20_line(1);
                   14078:                break;
1.1.1.49  root     14079:        case 0x06: // Windows Enhanced Mode & 286 DOSX Exit Broadcast
1.1.1.43  root     14080:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   14081:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   14082:                break;
                   14083:        case 0x07:
                   14084:                // Virtual Device Call API
                   14085:                break;
1.1.1.22  root     14086:        case 0x0a:
                   14087:                if(!no_windows) {
                   14088:                        REG16(AX) = 0x0000;
1.1.1.30  root     14089:                        REG8(BH) = win_major_version;
                   14090:                        REG8(BL) = win_minor_version;
1.1.1.49  root     14091: //                     REG16(CX) = 0x0002; // standard
1.1.1.22  root     14092:                        REG16(CX) = 0x0003; // enhanced
                   14093:                }
                   14094:                break;
1.1.1.30  root     14095:        case 0x0b:
                   14096:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     14097:        case 0x0e:
                   14098:        case 0x0f:
1.1.1.30  root     14099:        case 0x10:
1.1.1.22  root     14100:        case 0x11:
                   14101:        case 0x12:
                   14102:        case 0x13:
                   14103:        case 0x14:
1.1.1.30  root     14104:        case 0x15:
1.1.1.43  root     14105:        case 0x81:
                   14106:        case 0x82:
1.1.1.44  root     14107:        case 0x84:
1.1.1.49  root     14108:        case 0x85:
1.1.1.33  root     14109:        case 0x86:
1.1.1.22  root     14110:        case 0x87:
1.1.1.30  root     14111:        case 0x89:
1.1.1.33  root     14112:        case 0x8a:
1.1.1.22  root     14113:                // function not supported, do not clear AX
                   14114:                break;
1.1.1.14  root     14115:        case 0x80:
                   14116:                Sleep(10);
1.1.1.35  root     14117:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     14118:                REG8(AL) = 0x00;
1.1.1.14  root     14119:                break;
1.1.1.33  root     14120:        case 0x83:
                   14121:                REG16(BX) = 0x01; // system vm id
                   14122:                break;
1.1.1.22  root     14123:        case 0x8e:
                   14124:                REG16(AX) = 0x00; // failed
                   14125:                break;
1.1.1.20  root     14126:        case 0x8f:
                   14127:                switch(REG8(DH)) {
                   14128:                case 0x01:
1.1.1.49  root     14129: //                     REG16(AX) = 0x0000; // close command selected but not yet acknowledged
                   14130: //                     REG16(AX) = 0x0001; // close command issued and acknowledged
                   14131:                        REG16(AX) = 0x168f; // close command not selected -- application should continue
                   14132:                        break;
                   14133:                default:
                   14134:                        REG16(AX) = 0x0000; // successful
1.1.1.20  root     14135:                        break;
                   14136:                }
                   14137:                break;
1.1       root     14138:        default:
1.1.1.22  root     14139:                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));
                   14140:                REG16(AX) = 0x01;
                   14141:                m_CF = 1;
                   14142:                break;
                   14143:        }
                   14144: }
                   14145: 
                   14146: inline void msdos_int_2fh_19h()
                   14147: {
                   14148:        switch(REG8(AL)) {
                   14149:        case 0x00:
1.1.1.29  root     14150:                // SHELLB.COM is not installed
                   14151: //             REG8(AL) = 0x00;
1.1.1.22  root     14152:                break;
                   14153:        case 0x01:
                   14154:        case 0x02:
                   14155:        case 0x03:
                   14156:        case 0x04:
                   14157:                REG16(AX) = 0x01;
                   14158:                m_CF = 1;
                   14159:                break;
1.1.1.29  root     14160:        case 0x80:
                   14161:                // IBM ROM-DOS v4.0 is not installed
                   14162: //             REG8(AL) = 0x00;
                   14163:                break;
1.1.1.22  root     14164:        default:
                   14165:                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     14166:                REG16(AX) = 0x01;
1.1.1.3   root     14167:                m_CF = 1;
1.1       root     14168:                break;
                   14169:        }
                   14170: }
                   14171: 
                   14172: inline void msdos_int_2fh_1ah()
                   14173: {
                   14174:        switch(REG8(AL)) {
                   14175:        case 0x00:
1.1.1.29  root     14176:                // ANSI.SYS is installed
1.1       root     14177:                REG8(AL) = 0xff;
                   14178:                break;
1.1.1.49  root     14179:        case 0x01:
1.1.1.50  root     14180:                if(REG8(CL) == 0x5f) {
                   14181:                        // set display information
                   14182:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   14183:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   14184:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   14185:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   14186:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   14187:                                
                   14188:                                if(cur_width != new_width || cur_height != new_height) {
                   14189:                                        pcbios_set_console_size(new_width, new_height, true);
                   14190:                                }
                   14191:                        }
                   14192:                } else if(REG8(CL) == 0x7f) {
1.1.1.49  root     14193:                        // get display information
1.1.1.50  root     14194:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   14195:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   14196:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   14197:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   14198:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   14199:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   14200:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   14201:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   14202:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   14203:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   14204:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.49  root     14205:                } else {
                   14206:                        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));
                   14207:                        REG16(AX) = 0x01;
                   14208:                        m_CF = 1;
                   14209:                }
                   14210:                break;
1.1       root     14211:        default:
1.1.1.22  root     14212:                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));
                   14213:                REG16(AX) = 0x01;
                   14214:                m_CF = 1;
                   14215:                break;
                   14216:        }
                   14217: }
                   14218: 
1.1.1.30  root     14219: inline void msdos_int_2fh_40h()
1.1.1.22  root     14220: {
                   14221:        switch(REG8(AL)) {
                   14222:        case 0x00:
1.1.1.30  root     14223:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   14224:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     14225:                break;
1.1.1.43  root     14226:        case 0x10:
                   14227:                // OS/2 v2.0+ - Installation Check
                   14228:                REG16(AX) = 0x01;
                   14229:                m_CF = 1;
                   14230:                break;
1.1.1.22  root     14231:        default:
                   14232:                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     14233:                REG16(AX) = 0x01;
1.1.1.3   root     14234:                m_CF = 1;
1.1       root     14235:                break;
                   14236:        }
                   14237: }
                   14238: 
                   14239: inline void msdos_int_2fh_43h()
                   14240: {
                   14241:        switch(REG8(AL)) {
                   14242:        case 0x00:
1.1.1.29  root     14243:                // XMS is installed ?
1.1.1.19  root     14244: #ifdef SUPPORT_XMS
                   14245:                if(support_xms) {
                   14246:                        REG8(AL) = 0x80;
1.1.1.44  root     14247:                }
                   14248: #endif
                   14249:                break;
                   14250:        case 0x08:
                   14251: #ifdef SUPPORT_XMS
                   14252:                if(support_xms) {
                   14253:                        REG8(AL) = 0x43;
                   14254:                        REG8(BL) = 0x01; // IBM PC/AT
                   14255:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   14256:                }
1.1.1.19  root     14257: #endif
                   14258:                break;
                   14259:        case 0x10:
                   14260:                SREG(ES) = XMS_TOP >> 4;
                   14261:                i386_load_segment_descriptor(ES);
1.1.1.26  root     14262:                REG16(BX) = 0x15;
1.1       root     14263:                break;
1.1.1.44  root     14264:        case 0xe0:
                   14265:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   14266:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   14267:                        break;
                   14268:                }
1.1       root     14269:        default:
1.1.1.22  root     14270:                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));
                   14271:                REG16(AX) = 0x01;
                   14272:                m_CF = 1;
                   14273:                break;
                   14274:        }
                   14275: }
                   14276: 
                   14277: inline void msdos_int_2fh_46h()
                   14278: {
                   14279:        switch(REG8(AL)) {
                   14280:        case 0x80:
1.1.1.29  root     14281:                // Windows v3.0 is not installed
                   14282: //             REG8(AL) = 0x00;
1.1.1.22  root     14283:                break;
                   14284:        default:
                   14285:                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));
                   14286:                REG16(AX) = 0x01;
                   14287:                m_CF = 1;
                   14288:                break;
                   14289:        }
                   14290: }
                   14291: 
                   14292: inline void msdos_int_2fh_48h()
                   14293: {
                   14294:        switch(REG8(AL)) {
                   14295:        case 0x00:
1.1.1.29  root     14296:                // DOSKEY is not installed
                   14297: //             REG8(AL) = 0x00;
1.1.1.22  root     14298:                break;
                   14299:        case 0x10:
                   14300:                msdos_int_21h_0ah();
                   14301:                REG16(AX) = 0x00;
                   14302:                break;
                   14303:        default:
                   14304:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     14305:                REG16(AX) = 0x01;
1.1.1.3   root     14306:                m_CF = 1;
1.1       root     14307:                break;
                   14308:        }
                   14309: }
                   14310: 
                   14311: inline void msdos_int_2fh_4ah()
                   14312: {
                   14313:        switch(REG8(AL)) {
1.1.1.29  root     14314: #ifdef SUPPORT_HMA
                   14315:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   14316:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14317:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14318:                                // restore first free mcb in high memory area
                   14319:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14320:                        }
                   14321:                        int offset = 0xffff;
                   14322:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   14323:                                REG16(DI) = offset + 0x10;
                   14324:                        } else {
                   14325:                                REG16(DI) = 0xffff;
                   14326:                        }
                   14327:                } else {
                   14328:                        // HMA is already used
                   14329:                        REG16(BX) = 0;
                   14330:                        REG16(DI) = 0xffff;
                   14331:                }
                   14332:                SREG(ES) = 0xffff;
                   14333:                i386_load_segment_descriptor(ES);
                   14334:                break;
                   14335:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   14336:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14337:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14338:                                // restore first free mcb in high memory area
                   14339:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14340:                        }
                   14341:                        int size = REG16(BX), offset;
                   14342:                        if((size % 16) != 0) {
                   14343:                                size &= ~15;
                   14344:                                size += 16;
                   14345:                        }
                   14346:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   14347:                                REG16(BX) = size;
                   14348:                                REG16(DI) = offset + 0x10;
                   14349:                                is_hma_used_by_int_2fh = true;
                   14350:                        } else {
                   14351:                                REG16(BX) = 0;
                   14352:                                REG16(DI) = 0xffff;
                   14353:                        }
                   14354:                } else {
                   14355:                        // HMA is already used
                   14356:                        REG16(BX) = 0;
                   14357:                        REG16(DI) = 0xffff;
                   14358:                }
                   14359:                SREG(ES) = 0xffff;
                   14360:                i386_load_segment_descriptor(ES);
                   14361:                break;
                   14362:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   14363:                if(REG8(DL) == 0x00) {
                   14364:                        if(!is_hma_used_by_xms) {
                   14365:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14366:                                        // restore first free mcb in high memory area
                   14367:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14368:                                        is_hma_used_by_int_2fh = false;
                   14369:                                }
                   14370:                                int size = REG16(BX), offset;
                   14371:                                if((size % 16) != 0) {
                   14372:                                        size &= ~15;
                   14373:                                        size += 16;
                   14374:                                }
                   14375:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   14376: //                                     REG16(BX) = size;
                   14377:                                        SREG(ES) = 0xffff;
                   14378:                                        i386_load_segment_descriptor(ES);
                   14379:                                        REG16(DI) = offset + 0x10;
                   14380:                                        is_hma_used_by_int_2fh = true;
                   14381:                                } else {
                   14382:                                        REG16(DI) = 0xffff;
                   14383:                                }
                   14384:                        } else {
                   14385:                                REG16(DI) = 0xffff;
                   14386:                        }
                   14387:                } else if(REG8(DL) == 0x01) {
                   14388:                        if(!is_hma_used_by_xms) {
                   14389:                                int size = REG16(BX);
                   14390:                                if((size % 16) != 0) {
                   14391:                                        size &= ~15;
                   14392:                                        size += 16;
                   14393:                                }
                   14394:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   14395:                                        // memory block address is not changed
                   14396:                                } else {
                   14397:                                        REG16(DI) = 0xffff;
                   14398:                                }
                   14399:                        } else {
                   14400:                                REG16(DI) = 0xffff;
                   14401:                        }
                   14402:                } else if(REG8(DL) == 0x02) {
                   14403:                        if(!is_hma_used_by_xms) {
                   14404:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14405:                                        // restore first free mcb in high memory area
                   14406:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14407:                                        is_hma_used_by_int_2fh = false;
                   14408:                                } else {
                   14409:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   14410:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   14411:                                                is_hma_used_by_int_2fh = false;
                   14412:                                        }
                   14413:                                }
                   14414:                        }
                   14415:                } else {
                   14416:                        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));
                   14417:                        REG16(AX) = 0x01;
                   14418:                        m_CF = 1;
                   14419:                }
                   14420:                break;
                   14421:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   14422:                if(!is_hma_used_by_xms) {
                   14423:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14424:                                // restore first free mcb in high memory area
                   14425:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14426:                                is_hma_used_by_int_2fh = false;
                   14427:                        }
                   14428:                        REG16(AX) = 0x0000;
                   14429:                        SREG(ES) = 0xffff;
                   14430:                        i386_load_segment_descriptor(ES);
                   14431:                        REG16(DI) = 0x10;
                   14432:                }
                   14433:                break;
                   14434: #else
1.1       root     14435:        case 0x01:
                   14436:        case 0x02:
1.1.1.29  root     14437:                // HMA is already used
1.1.1.27  root     14438:                REG16(BX) = 0x0000;
1.1.1.3   root     14439:                SREG(ES) = 0xffff;
                   14440:                i386_load_segment_descriptor(ES);
1.1       root     14441:                REG16(DI) = 0xffff;
                   14442:                break;
1.1.1.19  root     14443:        case 0x03:
                   14444:                // unable to allocate
                   14445:                REG16(DI) = 0xffff;
                   14446:                break;
                   14447:        case 0x04:
                   14448:                // function not supported, do not clear AX
                   14449:                break;
1.1.1.29  root     14450: #endif
                   14451:        case 0x10:
1.1.1.42  root     14452:                switch(REG16(BX)) {
                   14453:                case 0x0000:
                   14454:                case 0x0001:
                   14455:                case 0x0002:
                   14456:                case 0x0003:
                   14457:                case 0x0004:
                   14458:                case 0x0005:
                   14459:                case 0x0006:
                   14460:                case 0x0007:
                   14461:                case 0x0008:
                   14462:                case 0x000a:
                   14463:                case 0x1234:
                   14464:                        // SMARTDRV v4.00+ is not installed
                   14465:                        break;
                   14466:                default:
1.1.1.29  root     14467:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14468:                        REG16(AX) = 0x01;
                   14469:                        m_CF = 1;
1.1.1.42  root     14470:                        break;
1.1.1.29  root     14471:                }
                   14472:                break;
                   14473:        case 0x11:
1.1.1.42  root     14474:                switch(REG16(BX)) {
                   14475:                case 0x0000:
                   14476:                case 0x0001:
                   14477:                case 0x0002:
                   14478:                case 0x0003:
                   14479:                case 0x0004:
                   14480:                case 0x0005:
                   14481:                case 0x0006:
                   14482:                case 0x0007:
                   14483:                case 0x0008:
                   14484:                case 0x0009:
                   14485:                case 0x000a:
                   14486:                case 0x000b:
                   14487:                case 0xfffe:
                   14488:                case 0xffff:
1.1.1.29  root     14489:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     14490:                        break;
                   14491:                default:
                   14492:                        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));
                   14493:                        REG16(AX) = 0x01;
                   14494:                        m_CF = 1;
                   14495:                        break;
                   14496:                }
                   14497:                break;
                   14498:        case 0x12:
                   14499:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   14500:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   14501:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   14502:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     14503:                } else {
                   14504:                        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));
                   14505:                        REG16(AX) = 0x01;
                   14506:                        m_CF = 1;
                   14507:                }
1.1.1.22  root     14508:                break;
1.1.1.42  root     14509:        case 0x13:
                   14510:                // DBLSPACE.BIN is not installed
                   14511:                break;
1.1.1.22  root     14512:        default:
                   14513:                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));
                   14514:                REG16(AX) = 0x01;
                   14515:                m_CF = 1;
                   14516:                break;
                   14517:        }
                   14518: }
                   14519: 
                   14520: inline void msdos_int_2fh_4bh()
                   14521: {
                   14522:        switch(REG8(AL)) {
1.1.1.24  root     14523:        case 0x01:
1.1.1.22  root     14524:        case 0x02:
1.1.1.29  root     14525:                // Task Switcher is not installed
1.1.1.24  root     14526:                break;
                   14527:        case 0x03:
                   14528:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   14529:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     14530:                break;
1.1.1.30  root     14531:        case 0x04:
                   14532:                REG16(BX) = 0x0000; // free switcher id successfully
                   14533:                break;
1.1.1.43  root     14534:        case 0x05:
                   14535:                REG16(BX) = 0x0000; // no instance data chain
                   14536:                SREG(ES) = 0x0000;
                   14537:                i386_load_segment_descriptor(ES);
                   14538:                break;
1.1       root     14539:        default:
1.1.1.22  root     14540:                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     14541:                REG16(AX) = 0x01;
1.1.1.3   root     14542:                m_CF = 1;
1.1       root     14543:                break;
                   14544:        }
                   14545: }
                   14546: 
1.1.1.44  root     14547: inline void msdos_int_2fh_4dh()
                   14548: {
                   14549:        switch(REG8(AL)) {
                   14550:        case 0x00:
                   14551:                // KKCFUNC is not installed ???
                   14552:                break;
                   14553:        default:
                   14554: //             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));
                   14555:                REG16(AX) = 0x01; // invalid function
                   14556:                m_CF = 1;
                   14557:                break;
                   14558:        }
                   14559: }
                   14560: 
1.1       root     14561: inline void msdos_int_2fh_4fh()
                   14562: {
                   14563:        switch(REG8(AL)) {
                   14564:        case 0x00:
1.1.1.29  root     14565:                // BILING is installed
1.1.1.27  root     14566:                REG16(AX) = 0x0000;
                   14567:                REG8(DL) = 0x01;        // major version
                   14568:                REG8(DH) = 0x00;        // minor version
1.1       root     14569:                break;
                   14570:        case 0x01:
1.1.1.27  root     14571:                REG16(AX) = 0x0000;
1.1       root     14572:                REG16(BX) = active_code_page;
                   14573:                break;
                   14574:        default:
1.1.1.22  root     14575:                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));
                   14576:                REG16(AX) = 0x01;
                   14577:                m_CF = 1;
                   14578:                break;
                   14579:        }
                   14580: }
                   14581: 
                   14582: inline void msdos_int_2fh_55h()
                   14583: {
                   14584:        switch(REG8(AL)) {
                   14585:        case 0x00:
                   14586:        case 0x01:
                   14587: //             unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14588:                break;
                   14589:        default:
                   14590:                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     14591:                REG16(AX) = 0x01;
1.1.1.3   root     14592:                m_CF = 1;
1.1       root     14593:                break;
                   14594:        }
                   14595: }
                   14596: 
1.1.1.44  root     14597: inline void msdos_int_2fh_56h()
                   14598: {
                   14599:        switch(REG8(AL)) {
                   14600:        case 0x00:
                   14601:                // INTERLNK is not installed
                   14602:                break;
                   14603:        case 0x01:
                   14604:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   14605: //             if(msdos_is_remote_drive(REG8(BH))) {
                   14606: //                     REG8(AL) = 0x00;
                   14607: //             }
                   14608:                break;
                   14609:        default:
                   14610:                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));
                   14611:                REG16(AX) = 0x01;
                   14612:                m_CF = 1;
                   14613:                break;
                   14614:        }
                   14615: }
                   14616: 
1.1.1.24  root     14617: inline void msdos_int_2fh_adh()
                   14618: {
                   14619:        switch(REG8(AL)) {
                   14620:        case 0x00:
1.1.1.29  root     14621:                // DISPLAY.SYS is installed
1.1.1.24  root     14622:                REG8(AL) = 0xff;
                   14623:                REG16(BX) = 0x100; // ???
                   14624:                break;
                   14625:        case 0x01:
                   14626:                active_code_page = REG16(BX);
                   14627:                msdos_nls_tables_update();
                   14628:                REG16(AX) = 0x01;
                   14629:                break;
                   14630:        case 0x02:
                   14631:                REG16(BX) = active_code_page;
                   14632:                break;
                   14633:        case 0x03:
                   14634:                // FIXME
                   14635:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   14636:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   14637:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   14638:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   14639:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   14640:                break;
                   14641:        case 0x80:
1.1.1.49  root     14642:                // KEYB.COM is not installed
                   14643:                break;
1.1.1.24  root     14644:        default:
                   14645:                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));
                   14646:                REG16(AX) = 0x01;
                   14647:                m_CF = 1;
                   14648:                break;
                   14649:        }
                   14650: }
                   14651: 
1.1       root     14652: inline void msdos_int_2fh_aeh()
                   14653: {
                   14654:        switch(REG8(AL)) {
                   14655:        case 0x00:
1.1.1.28  root     14656:                // FIXME: we need to check the given command line
                   14657:                REG8(AL) = 0x00; // the command should be executed as usual
                   14658: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     14659:                break;
                   14660:        case 0x01:
                   14661:                {
                   14662:                        char command[MAX_PATH];
                   14663:                        memset(command, 0, sizeof(command));
1.1.1.3   root     14664:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     14665:                        
                   14666:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   14667:                        param->env_seg = 0;
                   14668:                        param->cmd_line.w.l = 44;
                   14669:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   14670:                        param->fcb1.w.l = 24;
                   14671:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   14672:                        param->fcb2.w.l = 24;
                   14673:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   14674:                        
                   14675:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   14676:                        
                   14677:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     14678:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   14679:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     14680:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   14681:                        
1.1.1.28  root     14682:                        try {
                   14683:                                msdos_process_exec(command, param, 0);
                   14684:                        } catch(...) {
                   14685:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     14686:                        }
                   14687:                }
                   14688:                break;
                   14689:        default:
1.1.1.22  root     14690:                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     14691:                REG16(AX) = 0x01;
1.1.1.3   root     14692:                m_CF = 1;
1.1       root     14693:                break;
                   14694:        }
                   14695: }
                   14696: 
1.1.1.34  root     14697: inline void msdos_int_2fh_b7h()
                   14698: {
                   14699:        switch(REG8(AL)) {
                   14700:        case 0x00:
                   14701:                // APPEND is not installed
                   14702: //             REG8(AL) = 0x00;
                   14703:                break;
1.1.1.44  root     14704:        case 0x06:
                   14705:                REG16(BX) = 0x0000;
                   14706:                break;
1.1.1.34  root     14707:        case 0x07:
1.1.1.43  root     14708:        case 0x11:
1.1.1.34  root     14709:                // COMMAND.COM calls this service without checking APPEND is installed
                   14710:                break;
                   14711:        default:
                   14712:                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));
                   14713:                REG16(AX) = 0x01;
                   14714:                m_CF = 1;
                   14715:                break;
                   14716:        }
                   14717: }
                   14718: 
1.1.1.24  root     14719: inline void msdos_int_33h_0000h()
                   14720: {
                   14721:        REG16(AX) = 0xffff; // hardware/driver installed
                   14722:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14723: }
                   14724: 
                   14725: inline void msdos_int_33h_0001h()
                   14726: {
1.1.1.34  root     14727:        if(mouse.hidden > 0) {
                   14728:                mouse.hidden--;
                   14729:        }
                   14730:        if(mouse.hidden == 0) {
1.1.1.24  root     14731:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   14732:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   14733:                }
                   14734:                pic[1].imr &= ~0x10;    // enable irq12
                   14735:        }
                   14736: }
                   14737: 
                   14738: inline void msdos_int_33h_0002h()
                   14739: {
1.1.1.34  root     14740:        mouse.hidden++;
                   14741: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
                   14742:        pic[1].imr |= 0x10;     // enable irq12
1.1.1.24  root     14743: }
                   14744: 
                   14745: inline void msdos_int_33h_0003h()
                   14746: {
1.1.1.34  root     14747: //     if(mouse.hidden > 0) {
                   14748:                update_console_input();
                   14749: //     }
1.1.1.24  root     14750:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     14751:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   14752:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   14753: }
                   14754: 
                   14755: inline void msdos_int_33h_0004h()
                   14756: {
                   14757:        mouse.position.x = REG16(CX);
                   14758:        mouse.position.x = REG16(DX);
1.1.1.24  root     14759: }
                   14760: 
                   14761: inline void msdos_int_33h_0005h()
                   14762: {
1.1.1.34  root     14763: //     if(mouse.hidden > 0) {
                   14764:                update_console_input();
                   14765: //     }
1.1.1.24  root     14766:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14767:                int idx = REG16(BX);
1.1.1.34  root     14768:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   14769:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   14770:                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     14771:                mouse.buttons[idx].pressed_times = 0;
                   14772:        } else {
                   14773:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14774:        }
                   14775:        REG16(AX) = mouse.get_buttons();
                   14776: }
                   14777: 
                   14778: inline void msdos_int_33h_0006h()
                   14779: {
1.1.1.34  root     14780: //     if(mouse.hidden > 0) {
                   14781:                update_console_input();
                   14782: //     }
1.1.1.24  root     14783:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14784:                int idx = REG16(BX);
1.1.1.34  root     14785:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   14786:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   14787:                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     14788:                mouse.buttons[idx].released_times = 0;
                   14789:        } else {
                   14790:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14791:        }
                   14792:        REG16(AX) = mouse.get_buttons();
                   14793: }
                   14794: 
                   14795: inline void msdos_int_33h_0007h()
                   14796: {
                   14797:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   14798:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   14799: }
                   14800: 
                   14801: inline void msdos_int_33h_0008h()
                   14802: {
                   14803:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   14804:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   14805: }
                   14806: 
                   14807: inline void msdos_int_33h_0009h()
                   14808: {
                   14809:        mouse.hot_spot[0] = REG16(BX);
                   14810:        mouse.hot_spot[1] = REG16(CX);
                   14811: }
                   14812: 
1.1.1.49  root     14813: inline void msdos_int_33h_000ah()
                   14814: {
                   14815:        mouse.screen_mask = REG16(CX);
                   14816:        mouse.cursor_mask = REG16(DX);
                   14817: }
                   14818: 
1.1.1.24  root     14819: inline void msdos_int_33h_000bh()
                   14820: {
1.1.1.34  root     14821: //     if(mouse.hidden > 0) {
                   14822:                update_console_input();
                   14823: //     }
1.1.1.24  root     14824:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14825:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   14826:        mouse.prev_position.x = mouse.position.x;
                   14827:        mouse.prev_position.y = mouse.position.y;
                   14828:        REG16(CX) = dx;
                   14829:        REG16(DX) = dy;
                   14830: }
                   14831: 
                   14832: inline void msdos_int_33h_000ch()
                   14833: {
                   14834:        mouse.call_mask = REG16(CX);
                   14835:        mouse.call_addr.w.l = REG16(DX);
                   14836:        mouse.call_addr.w.h = SREG(ES);
                   14837: }
                   14838: 
                   14839: inline void msdos_int_33h_000fh()
                   14840: {
                   14841:        mouse.mickey.x = REG16(CX);
                   14842:        mouse.mickey.y = REG16(DX);
                   14843: }
                   14844: 
                   14845: inline void msdos_int_33h_0011h()
                   14846: {
                   14847:        REG16(AX) = 0xffff;
                   14848:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14849: }
                   14850: 
                   14851: inline void msdos_int_33h_0014h()
                   14852: {
                   14853:        UINT16 old_mask = mouse.call_mask;
                   14854:        UINT16 old_ofs = mouse.call_addr.w.l;
                   14855:        UINT16 old_seg = mouse.call_addr.w.h;
                   14856:        
                   14857:        mouse.call_mask = REG16(CX);
                   14858:        mouse.call_addr.w.l = REG16(DX);
                   14859:        mouse.call_addr.w.h = SREG(ES);
                   14860:        
                   14861:        REG16(CX) = old_mask;
                   14862:        REG16(DX) = old_ofs;
                   14863:        SREG(ES) = old_seg;
                   14864:        i386_load_segment_descriptor(ES);
                   14865: }
                   14866: 
                   14867: inline void msdos_int_33h_0015h()
                   14868: {
                   14869:        REG16(BX) = sizeof(mouse);
                   14870: }
                   14871: 
                   14872: inline void msdos_int_33h_0016h()
                   14873: {
                   14874:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   14875: }
                   14876: 
                   14877: inline void msdos_int_33h_0017h()
                   14878: {
                   14879:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   14880: }
                   14881: 
1.1.1.43  root     14882: inline void msdos_int_33h_0018h()
                   14883: {
                   14884:        for(int i = 0; i < 8; i++) {
                   14885:                if(REG16(CX) & (1 << i)) {
                   14886:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   14887:                                // event handler already exists
                   14888:                                REG16(AX) = 0xffff;
                   14889:                                break;
                   14890:                        }
                   14891:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   14892:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   14893:                }
                   14894:        }
                   14895: }
                   14896: 
                   14897: inline void msdos_int_33h_0019h()
                   14898: {
                   14899:        UINT16 call_mask = REG16(CX);
                   14900:        
                   14901:        REG16(CX) = 0;
                   14902:        
                   14903:        for(int i = 0; i < 8; i++) {
                   14904:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   14905:                        for(int j = 0; j < 8; j++) {
                   14906:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   14907:                                        REG16(CX) |= (1 << j);
                   14908:                                }
                   14909:                        }
                   14910:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   14911:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   14912:                        break;
                   14913:                }
                   14914:        }
                   14915: }
                   14916: 
1.1.1.24  root     14917: inline void msdos_int_33h_001ah()
                   14918: {
                   14919:        mouse.sensitivity[0] = REG16(BX);
                   14920:        mouse.sensitivity[1] = REG16(CX);
                   14921:        mouse.sensitivity[2] = REG16(DX);
                   14922: }
                   14923: 
                   14924: inline void msdos_int_33h_001bh()
                   14925: {
                   14926:        REG16(BX) = mouse.sensitivity[0];
                   14927:        REG16(CX) = mouse.sensitivity[1];
                   14928:        REG16(DX) = mouse.sensitivity[2];
                   14929: }
                   14930: 
                   14931: inline void msdos_int_33h_001dh()
                   14932: {
                   14933:        mouse.display_page = REG16(BX);
                   14934: }
                   14935: 
                   14936: inline void msdos_int_33h_001eh()
                   14937: {
                   14938:        REG16(BX) = mouse.display_page;
                   14939: }
                   14940: 
1.1.1.34  root     14941: inline void msdos_int_33h_001fh()
                   14942: {
                   14943:        // from DOSBox
                   14944:        REG16(BX) = 0x0000;
                   14945:        SREG(ES) = 0x0000;
                   14946:        i386_load_segment_descriptor(ES);
                   14947:        mouse.enabled = false;
                   14948:        mouse.old_hidden = mouse.hidden;
                   14949:        mouse.hidden = 1;
                   14950: }
                   14951: 
                   14952: inline void msdos_int_33h_0020h()
                   14953: {
                   14954:        // from DOSBox
                   14955:        mouse.enabled = true;
                   14956:        mouse.hidden = mouse.old_hidden;
                   14957: }
                   14958: 
1.1.1.24  root     14959: inline void msdos_int_33h_0021h()
                   14960: {
                   14961:        REG16(AX) = 0xffff;
                   14962:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14963: }
                   14964: 
                   14965: inline void msdos_int_33h_0022h()
                   14966: {
                   14967:        mouse.language = REG16(BX);
                   14968: }
                   14969: 
                   14970: inline void msdos_int_33h_0023h()
                   14971: {
                   14972:        REG16(BX) = mouse.language;
                   14973: }
                   14974: 
                   14975: inline void msdos_int_33h_0024h()
                   14976: {
                   14977:        REG16(BX) = 0x0805; // V8.05
                   14978:        REG16(CX) = 0x0400; // PS/2
                   14979: }
                   14980: 
1.1.1.49  root     14981: inline void msdos_int_33h_0025h()
                   14982: {
                   14983:        REG16(AX) = 0x8000; // driver (not TSR), software text cursor
                   14984: }
                   14985: 
1.1.1.24  root     14986: inline void msdos_int_33h_0026h()
                   14987: {
                   14988:        REG16(BX) = 0x0000;
                   14989:        REG16(CX) = mouse.max_position.x;
                   14990:        REG16(DX) = mouse.max_position.y;
                   14991: }
                   14992: 
1.1.1.49  root     14993: inline void msdos_int_33h_0027h()
                   14994: {
                   14995: //     if(mouse.hidden > 0) {
                   14996:                update_console_input();
                   14997: //     }
                   14998:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14999:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   15000:        mouse.prev_position.x = mouse.position.x;
                   15001:        mouse.prev_position.y = mouse.position.y;
                   15002:        REG16(AX) = mouse.screen_mask;
                   15003:        REG16(BX) = mouse.cursor_mask;
                   15004:        REG16(CX) = dx;
                   15005:        REG16(DX) = dy;
                   15006: }
                   15007: 
                   15008: inline void msdos_int_33h_0028h()
                   15009: {
                   15010:        if(REG16(CX) != 0) {
                   15011:                UINT8 tmp = REG8(AL);
                   15012:                REG8(AL) = REG8(CL);
                   15013:                pcbios_int_10h_00h();
                   15014:                REG8(AL) = tmp;
                   15015:        }
                   15016:        REG8(CL) = 0x00; // successful
                   15017: }
                   15018: 
                   15019: inline void msdos_int_33h_0029h()
                   15020: {
                   15021:        switch(REG16(CX)) {
                   15022:        case 0x0000:
                   15023:                REG16(CX) = 0x0003;
                   15024:                sprintf((char *)(mem + WORK_TOP), "TEXT Mode (80x25)$");
                   15025:                break;
                   15026:        case 0x0003:
                   15027:                REG16(CX) = 0x0070;
                   15028:                sprintf((char *)(mem + WORK_TOP), "V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15029:                break;
                   15030:        case 0x0070:
                   15031:                REG16(CX) = 0x0071;
                   15032:                sprintf((char *)(mem + WORK_TOP), "Extended CGA V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15033:                break;
                   15034:        case 0x0071:
                   15035:                REG16(CX) = 0x0073;
                   15036:                sprintf((char *)(mem + WORK_TOP), "Extended CGA TEXT Mode (80x25)$");
                   15037:                break;
                   15038:        default:
                   15039:                REG16(CX) = 0x0000;
                   15040:                break;
                   15041:        }
                   15042:        if(REG16(CX) != 0) {
                   15043:                SREG(DS) = (WORK_TOP >> 4);
                   15044:        } else {
                   15045:                SREG(DS) = 0x0000;
                   15046:        }
                   15047:        i386_load_segment_descriptor(DS);
                   15048:        REG16(DX) = 0x0000;
                   15049: }
                   15050: 
1.1.1.24  root     15051: inline void msdos_int_33h_002ah()
                   15052: {
1.1.1.34  root     15053:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     15054:        REG16(BX) = mouse.hot_spot[0];
                   15055:        REG16(CX) = mouse.hot_spot[1];
                   15056:        REG16(DX) = 4; // PS/2
                   15057: }
                   15058: 
                   15059: inline void msdos_int_33h_0031h()
                   15060: {
                   15061:        REG16(AX) = mouse.min_position.x;
                   15062:        REG16(BX) = mouse.min_position.y;
                   15063:        REG16(CX) = mouse.max_position.x;
                   15064:        REG16(DX) = mouse.max_position.y;
                   15065: }
                   15066: 
                   15067: inline void msdos_int_33h_0032h()
                   15068: {
                   15069:        REG16(AX) = 0;
1.1.1.49  root     15070:        REG16(AX) |= 0x8000; // 0025h
1.1.1.24  root     15071:        REG16(AX) |= 0x4000; // 0026h
1.1.1.49  root     15072:        REG16(AX) |= 0x2000; // 0027h
1.1.1.24  root     15073: //     REG16(AX) |= 0x1000; // 0028h
                   15074: //     REG16(AX) |= 0x0800; // 0029h
                   15075:        REG16(AX) |= 0x0400; // 002ah
                   15076: //     REG16(AX) |= 0x0200; // 002bh
                   15077: //     REG16(AX) |= 0x0100; // 002ch
                   15078: //     REG16(AX) |= 0x0080; // 002dh
                   15079: //     REG16(AX) |= 0x0040; // 002eh
                   15080:        REG16(AX) |= 0x0020; // 002fh
                   15081: //     REG16(AX) |= 0x0010; // 0030h
                   15082:        REG16(AX) |= 0x0008; // 0031h
                   15083:        REG16(AX) |= 0x0004; // 0032h
                   15084: //     REG16(AX) |= 0x0002; // 0033h
                   15085: //     REG16(AX) |= 0x0001; // 0034h
                   15086: }
                   15087: 
1.1.1.49  root     15088: inline void msdos_int_33h_004dh()
                   15089: {
                   15090:        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), "Copyright 2017 MS-DOS Player");
                   15091: }
                   15092: 
                   15093: inline void msdos_int_33h_006dh()
                   15094: {
                   15095:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x08; // V8.05
                   15096:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = 0x05;
                   15097: }
                   15098: 
1.1.1.19  root     15099: inline void msdos_int_67h_40h()
                   15100: {
                   15101:        if(!support_ems) {
                   15102:                REG8(AH) = 0x84;
                   15103:        } else {
                   15104:                REG8(AH) = 0x00;
                   15105:        }
                   15106: }
                   15107: 
                   15108: inline void msdos_int_67h_41h()
                   15109: {
                   15110:        if(!support_ems) {
                   15111:                REG8(AH) = 0x84;
                   15112:        } else {
                   15113:                REG8(AH) = 0x00;
                   15114:                REG16(BX) = EMS_TOP >> 4;
                   15115:        }
                   15116: }
                   15117: 
                   15118: inline void msdos_int_67h_42h()
                   15119: {
                   15120:        if(!support_ems) {
                   15121:                REG8(AH) = 0x84;
                   15122:        } else {
                   15123:                REG8(AH) = 0x00;
                   15124:                REG16(BX) = free_ems_pages;
                   15125:                REG16(DX) = MAX_EMS_PAGES;
                   15126:        }
                   15127: }
                   15128: 
                   15129: inline void msdos_int_67h_43h()
                   15130: {
                   15131:        if(!support_ems) {
                   15132:                REG8(AH) = 0x84;
                   15133:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15134:                REG8(AH) = 0x87;
                   15135:        } else if(REG16(BX) > free_ems_pages) {
                   15136:                REG8(AH) = 0x88;
                   15137:        } else if(REG16(BX) == 0) {
                   15138:                REG8(AH) = 0x89;
                   15139:        } else {
1.1.1.31  root     15140:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15141:                        if(!ems_handles[i].allocated) {
                   15142:                                ems_allocate_pages(i, REG16(BX));
                   15143:                                REG8(AH) = 0x00;
                   15144:                                REG16(DX) = i;
                   15145:                                return;
                   15146:                        }
                   15147:                }
                   15148:                REG8(AH) = 0x85;
                   15149:        }
                   15150: }
                   15151: 
                   15152: inline void msdos_int_67h_44h()
                   15153: {
                   15154:        if(!support_ems) {
                   15155:                REG8(AH) = 0x84;
1.1.1.31  root     15156:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15157:                REG8(AH) = 0x83;
                   15158:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   15159:                REG8(AH) = 0x8a;
                   15160: //     } else if(!(REG8(AL) < 4)) {
                   15161: //             REG8(AH) = 0x8b;
                   15162:        } else if(REG16(BX) == 0xffff) {
                   15163:                ems_unmap_page(REG8(AL) & 3);
                   15164:                REG8(AH) = 0x00;
                   15165:        } else {
                   15166:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   15167:                REG8(AH) = 0x00;
                   15168:        }
                   15169: }
                   15170: 
                   15171: inline void msdos_int_67h_45h()
                   15172: {
                   15173:        if(!support_ems) {
                   15174:                REG8(AH) = 0x84;
1.1.1.31  root     15175:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15176:                REG8(AH) = 0x83;
                   15177:        } else {
                   15178:                ems_release_pages(REG16(DX));
                   15179:                REG8(AH) = 0x00;
                   15180:        }
                   15181: }
                   15182: 
                   15183: inline void msdos_int_67h_46h()
                   15184: {
                   15185:        if(!support_ems) {
                   15186:                REG8(AH) = 0x84;
                   15187:        } else {
1.1.1.29  root     15188: //             REG16(AX) = 0x0032; // EMS 3.2
                   15189:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     15190:        }
                   15191: }
                   15192: 
                   15193: inline void msdos_int_67h_47h()
                   15194: {
                   15195:        // NOTE: the map data should be stored in the specified ems page, not process data
                   15196:        process_t *process = msdos_process_info_get(current_psp);
                   15197:        
                   15198:        if(!support_ems) {
                   15199:                REG8(AH) = 0x84;
1.1.1.31  root     15200: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15201: //             REG8(AH) = 0x83;
                   15202:        } else if(process->ems_pages_stored) {
                   15203:                REG8(AH) = 0x8d;
                   15204:        } else {
                   15205:                for(int i = 0; i < 4; i++) {
                   15206:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   15207:                        process->ems_pages[i].page   = ems_pages[i].page;
                   15208:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   15209:                }
                   15210:                process->ems_pages_stored = true;
                   15211:                REG8(AH) = 0x00;
                   15212:        }
                   15213: }
                   15214: 
                   15215: inline void msdos_int_67h_48h()
                   15216: {
                   15217:        // NOTE: the map data should be restored from the specified ems page, not process data
                   15218:        process_t *process = msdos_process_info_get(current_psp);
                   15219:        
                   15220:        if(!support_ems) {
                   15221:                REG8(AH) = 0x84;
1.1.1.31  root     15222: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15223: //             REG8(AH) = 0x83;
                   15224:        } else if(!process->ems_pages_stored) {
                   15225:                REG8(AH) = 0x8e;
                   15226:        } else {
                   15227:                for(int i = 0; i < 4; i++) {
                   15228:                        if(process->ems_pages[i].mapped) {
                   15229:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   15230:                        } else {
                   15231:                                ems_unmap_page(i);
                   15232:                        }
                   15233:                }
                   15234:                process->ems_pages_stored = false;
                   15235:                REG8(AH) = 0x00;
                   15236:        }
                   15237: }
                   15238: 
                   15239: inline void msdos_int_67h_4bh()
                   15240: {
                   15241:        if(!support_ems) {
                   15242:                REG8(AH) = 0x84;
                   15243:        } else {
                   15244:                REG8(AH) = 0x00;
                   15245:                REG16(BX) = 0;
1.1.1.31  root     15246:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15247:                        if(ems_handles[i].allocated) {
                   15248:                                REG16(BX)++;
                   15249:                        }
                   15250:                }
                   15251:        }
                   15252: }
                   15253: 
                   15254: inline void msdos_int_67h_4ch()
                   15255: {
                   15256:        if(!support_ems) {
                   15257:                REG8(AH) = 0x84;
1.1.1.31  root     15258:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15259:                REG8(AH) = 0x83;
                   15260:        } else {
                   15261:                REG8(AH) = 0x00;
                   15262:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   15263:        }
                   15264: }
                   15265: 
                   15266: inline void msdos_int_67h_4dh()
                   15267: {
                   15268:        if(!support_ems) {
                   15269:                REG8(AH) = 0x84;
                   15270:        } else {
                   15271:                REG8(AH) = 0x00;
                   15272:                REG16(BX) = 0;
1.1.1.31  root     15273:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15274:                        if(ems_handles[i].allocated) {
                   15275:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   15276:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   15277:                                REG16(BX)++;
                   15278:                        }
                   15279:                }
                   15280:        }
                   15281: }
                   15282: 
1.1.1.20  root     15283: inline void msdos_int_67h_4eh()
                   15284: {
                   15285:        if(!support_ems) {
                   15286:                REG8(AH) = 0x84;
                   15287:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15288:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   15289:                        // save page map
                   15290:                        for(int i = 0; i < 4; i++) {
                   15291:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15292:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15293:                        }
                   15294:                }
                   15295:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15296:                        // restore page map
                   15297:                        for(int i = 0; i < 4; i++) {
                   15298:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15299:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15300:                                
1.1.1.31  root     15301:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     15302:                                        ems_map_page(i, handle, page);
                   15303:                                } else {
                   15304:                                        ems_unmap_page(i);
                   15305:                                }
                   15306:                        }
                   15307:                }
                   15308:                REG8(AH) = 0x00;
                   15309:        } else if(REG8(AL) == 0x03) {
                   15310:                REG8(AH) = 0x00;
1.1.1.21  root     15311:                REG8(AL) = 4 * 4;
                   15312:        } else {
1.1.1.22  root     15313:                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     15314:                REG8(AH) = 0x8f;
                   15315:        }
                   15316: }
                   15317: 
                   15318: inline void msdos_int_67h_4fh()
                   15319: {
                   15320:        if(!support_ems) {
                   15321:                REG8(AH) = 0x84;
                   15322:        } else if(REG8(AL) == 0x00) {
                   15323:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15324:                
                   15325:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   15326:                for(int i = 0; i < count; i++) {
                   15327:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   15328:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15329:                        
                   15330: //                     if(!(physical < 4)) {
                   15331: //                             REG8(AH) = 0x8b;
                   15332: //                             return;
                   15333: //                     }
                   15334:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     15335:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   15336:                        *(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     15337:                }
                   15338:                REG8(AH) = 0x00;
                   15339:        } else if(REG8(AL) == 0x01) {
                   15340:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15341:                
                   15342:                for(int i = 0; i < count; i++) {
                   15343:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   15344:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15345:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   15346:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   15347:                        
                   15348: //                     if(!(physical < 4)) {
                   15349: //                             REG8(AH) = 0x8b;
                   15350: //                             return;
                   15351: //                     } else
1.1.1.41  root     15352:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     15353:                                ems_map_page(physical & 3, handle, logical);
                   15354:                        } else {
1.1.1.41  root     15355:                                ems_unmap_page(physical & 3);
1.1.1.21  root     15356:                        }
                   15357:                }
                   15358:                REG8(AH) = 0x00;
                   15359:        } else if(REG8(AL) == 0x02) {
                   15360:                REG8(AH) = 0x00;
                   15361:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     15362:        } else {
1.1.1.22  root     15363:                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     15364:                REG8(AH) = 0x8f;
                   15365:        }
                   15366: }
                   15367: 
                   15368: inline void msdos_int_67h_50h()
                   15369: {
                   15370:        if(!support_ems) {
                   15371:                REG8(AH) = 0x84;
1.1.1.31  root     15372:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     15373:                REG8(AH) = 0x83;
                   15374:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15375:                for(int i = 0; i < REG16(CX); i++) {
                   15376:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15377:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15378:                        
                   15379:                        if(REG8(AL) == 0x01) {
                   15380:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15381:                        }
                   15382: //                     if(!(physical < 4)) {
                   15383: //                             REG8(AH) = 0x8b;
                   15384: //                             return;
                   15385: //                     } else
                   15386:                        if(logical == 0xffff) {
                   15387:                                ems_unmap_page(physical & 3);
                   15388:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15389:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15390:                        } else {
                   15391:                                REG8(AH) = 0x8a;
                   15392:                                return;
                   15393:                        }
                   15394:                }
                   15395:                REG8(AH) = 0x00;
                   15396:        } else {
1.1.1.22  root     15397:                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     15398:                REG8(AH) = 0x8f;
                   15399:        }
                   15400: }
                   15401: 
1.1.1.19  root     15402: inline void msdos_int_67h_51h()
                   15403: {
                   15404:        if(!support_ems) {
                   15405:                REG8(AH) = 0x84;
1.1.1.31  root     15406:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15407:                REG8(AH) = 0x83;
                   15408:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15409:                REG8(AH) = 0x87;
                   15410:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   15411:                REG8(AH) = 0x88;
                   15412:        } else {
                   15413:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   15414:                REG8(AH) = 0x00;
                   15415:        }
                   15416: }
                   15417: 
1.1.1.20  root     15418: inline void msdos_int_67h_52h()
                   15419: {
                   15420:        if(!support_ems) {
                   15421:                REG8(AH) = 0x84;
1.1.1.31  root     15422: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15423: //             REG8(AH) = 0x83;
1.1.1.20  root     15424:        } else if(REG8(AL) == 0x00) {
                   15425:                REG8(AL) = 0x00; // handle is volatile
                   15426:                REG8(AH) = 0x00;
                   15427:        } else if(REG8(AL) == 0x01) {
                   15428:                if(REG8(BL) == 0x00) {
                   15429:                        REG8(AH) = 0x00;
                   15430:                } else {
                   15431:                        REG8(AH) = 0x90; // undefined attribute type
                   15432:                }
                   15433:        } else if(REG8(AL) == 0x02) {
                   15434:                REG8(AL) = 0x00; // only volatile handles supported
                   15435:                REG8(AH) = 0x00;
                   15436:        } else {
1.1.1.22  root     15437:                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     15438:                REG8(AH) = 0x8f;
                   15439:        }
                   15440: }
                   15441: 
1.1.1.19  root     15442: inline void msdos_int_67h_53h()
                   15443: {
                   15444:        if(!support_ems) {
                   15445:                REG8(AH) = 0x84;
1.1.1.31  root     15446:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15447:                REG8(AH) = 0x83;
                   15448:        } else if(REG8(AL) == 0x00) {
                   15449:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   15450:                REG8(AH) = 0x00;
                   15451:        } else if(REG8(AL) == 0x01) {
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) = 0xa1;
                   15455:                                return;
                   15456:                        }
                   15457:                }
                   15458:                REG8(AH) = 0x00;
                   15459:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   15460:        } else {
1.1.1.22  root     15461:                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     15462:                REG8(AH) = 0x8f;
1.1.1.19  root     15463:        }
                   15464: }
                   15465: 
                   15466: inline void msdos_int_67h_54h()
                   15467: {
                   15468:        if(!support_ems) {
                   15469:                REG8(AH) = 0x84;
                   15470:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     15471:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15472:                        if(ems_handles[i].allocated) {
                   15473:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   15474:                        } else {
                   15475:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   15476:                        }
                   15477:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   15478:                }
                   15479:                REG8(AH) = 0x00;
                   15480:                REG8(AL) = MAX_EMS_HANDLES;
                   15481:        } else if(REG8(AL) == 0x01) {
                   15482:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     15483:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15484:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15485:                                REG8(AH) = 0x00;
                   15486:                                REG16(DX) = i;
                   15487:                                break;
                   15488:                        }
                   15489:                }
                   15490:        } else if(REG8(AL) == 0x02) {
                   15491:                REG8(AH) = 0x00;
                   15492:                REG16(BX) = MAX_EMS_HANDLES;
                   15493:        } else {
1.1.1.22  root     15494:                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     15495:                REG8(AH) = 0x8f;
                   15496:        }
                   15497: }
                   15498: 
1.1.1.49  root     15499: inline void msdos_int_67h_55h()
                   15500: {
                   15501:        if(!support_ems) {
                   15502:                REG8(AH) = 0x84;
                   15503:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15504:                REG8(AH) = 0x83;
                   15505:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15506:                UINT16 jump_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
                   15507:                UINT16 jump_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
                   15508:                UINT8  entries  = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
                   15509:                UINT16 map_ofs  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   15510:                UINT16 map_seg  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
                   15511:                
                   15512:                for(int i = 0; i < (int)entries; i++) {
                   15513:                        int logical  = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 0);
                   15514:                        int physical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 2);
                   15515:                        
                   15516:                        if(REG8(AL) == 0x01) {
                   15517:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15518:                        }
                   15519: //                     if(!(physical < 4)) {
                   15520: //                             REG8(AH) = 0x8b;
                   15521: //                             return;
                   15522: //                     } else
                   15523:                        if(logical == 0xffff) {
                   15524:                                ems_unmap_page(physical & 3);
                   15525:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15526:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15527:                        } else {
                   15528:                                REG8(AH) = 0x8a;
                   15529:                                return;
                   15530:                        }
                   15531:                }
                   15532:                i386_jmp_far(jump_seg, jump_ofs);
                   15533:                REG8(AH) = 0x00;
                   15534:        } else {
                   15535:                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));
                   15536:                REG8(AH) = 0x8f;
                   15537:        }
                   15538: }
                   15539: 
                   15540: inline void msdos_int_67h_56h()
                   15541: {
                   15542:        if(!support_ems) {
                   15543:                REG8(AH) = 0x84;
                   15544:        } else if(REG8(AL) == 0x02) {
                   15545:                REG16(BX) = (2 + 2) * 4;
                   15546:                REG8(AH) = 0x00;
                   15547:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15548:                REG8(AH) = 0x83;
                   15549:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15550:                UINT16 call_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  0);
                   15551:                UINT16 call_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  2);
                   15552:                UINT8  new_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  4);
                   15553:                UINT16 new_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  5);
                   15554:                UINT16 new_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  7);
                   15555: #if 0
                   15556:                UINT8  old_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  9);
                   15557:                UINT16 old_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 10);
                   15558:                UINT16 old_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 12);
                   15559: #endif
                   15560:                UINT16 handles[4], pages[4];
                   15561:                
                   15562:                // alter page map and call routine is at fffc:001f
                   15563:                if(!(call_seg == 0 && call_ofs == 0)) {
                   15564:                        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   15565:                        mem[DUMMY_TOP + 0x20] = (call_ofs >> 0) & 0xff;
                   15566:                        mem[DUMMY_TOP + 0x21] = (call_ofs >> 8) & 0xff;
                   15567:                        mem[DUMMY_TOP + 0x22] = (call_seg >> 0) & 0xff;
                   15568:                        mem[DUMMY_TOP + 0x23] = (call_seg >> 8) & 0xff;
                   15569:                } else {
                   15570:                        // invalid call addr :-(
                   15571:                        mem[DUMMY_TOP + 0x1f] = 0x90;   // nop
                   15572:                        mem[DUMMY_TOP + 0x20] = 0x90;   // nop
                   15573:                        mem[DUMMY_TOP + 0x21] = 0x90;   // nop
                   15574:                        mem[DUMMY_TOP + 0x22] = 0x90;   // nop
                   15575:                        mem[DUMMY_TOP + 0x23] = 0x90;   // nop
                   15576:                }
                   15577:                // do call far (push cs/ip) in old mapping
                   15578:                i386_call_far(DUMMY_TOP >> 4, 0x001f);
                   15579:                
                   15580:                // get old mapping data
                   15581: #if 0
                   15582:                for(int i = 0; i < (int)old_entries; i++) {
                   15583:                        int logical  = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 0);
                   15584:                        int physical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 2);
                   15585:                        
                   15586:                        if(REG8(AL) == 0x01) {
                   15587:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15588:                        }
                   15589: //                     if(!(physical < 4)) {
                   15590: //                             REG8(AH) = 0x8b;
                   15591: //                             return;
                   15592: //                     } else
                   15593:                        if(logical == 0xffff) {
                   15594:                                ems_unmap_page(physical & 3);
                   15595:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15596:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15597:                        } else {
                   15598:                                REG8(AH) = 0x8a;
                   15599:                                return;
                   15600:                        }
                   15601:                }
                   15602: #endif
                   15603:                for(int i = 0; i < 4; i++) {
                   15604:                        handles[i] = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15605:                        pages  [i] = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15606:                }
                   15607:                
                   15608:                // set new mapping
                   15609:                for(int i = 0; i < (int)new_entries; i++) {
                   15610:                        int logical  = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 0);
                   15611:                        int physical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 2);
                   15612:                        
                   15613:                        if(REG8(AL) == 0x01) {
                   15614:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15615:                        }
                   15616: //                     if(!(physical < 4)) {
                   15617: //                             REG8(AH) = 0x8b;
                   15618: //                             return;
                   15619: //                     } else
                   15620:                        if(logical == 0xffff) {
                   15621:                                ems_unmap_page(physical & 3);
                   15622:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15623:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15624:                        } else {
                   15625:                                REG8(AH) = 0x8a;
                   15626:                                return;
                   15627:                        }
                   15628:                }
                   15629:                
                   15630:                // push old mapping data in new mapping
                   15631:                for(int i = 0; i < 4; i++) {
                   15632:                        i386_push16(handles[i]);
                   15633:                        i386_push16(pages  [i]);
                   15634:                }
                   15635:                REG8(AH) = 0x00;
                   15636:        } else {
                   15637:                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));
                   15638:                REG8(AH) = 0x8f;
                   15639:        }
                   15640: }
                   15641: 
1.1.1.20  root     15642: inline void msdos_int_67h_57h_tmp()
                   15643: {
                   15644:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15645:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15646:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   15647:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   15648:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   15649:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   15650:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15651:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   15652:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   15653:        
1.1.1.32  root     15654:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     15655:        UINT32 src_addr, dest_addr;
                   15656:        UINT32 src_addr_max, dest_addr_max;
                   15657:        
                   15658:        if(src_type == 0) {
                   15659:                src_buffer = mem;
                   15660:                src_addr = (src_seg << 4) + src_ofs;
                   15661:                src_addr_max = MAX_MEM;
                   15662:        } else {
1.1.1.31  root     15663:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     15664:                        REG8(AH) = 0x83;
                   15665:                        return;
                   15666:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   15667:                        REG8(AH) = 0x8a;
                   15668:                        return;
                   15669:                }
1.1.1.32  root     15670:                if(ems_handles[src_handle].buffer != NULL) {
                   15671:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   15672:                }
1.1.1.20  root     15673:                src_addr = src_ofs;
1.1.1.32  root     15674:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     15675:        }
                   15676:        if(dest_type == 0) {
                   15677:                dest_buffer = mem;
                   15678:                dest_addr = (dest_seg << 4) + dest_ofs;
                   15679:                dest_addr_max = MAX_MEM;
                   15680:        } else {
1.1.1.31  root     15681:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     15682:                        REG8(AH) = 0x83;
                   15683:                        return;
                   15684:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   15685:                        REG8(AH) = 0x8a;
                   15686:                        return;
                   15687:                }
1.1.1.32  root     15688:                if(ems_handles[dest_handle].buffer != NULL) {
                   15689:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   15690:                }
1.1.1.20  root     15691:                dest_addr = dest_ofs;
1.1.1.32  root     15692:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     15693:        }
1.1.1.32  root     15694:        if(src_buffer != NULL && dest_buffer != NULL) {
                   15695:                for(int i = 0; i < copy_length; i++) {
                   15696:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   15697:                                if(REG8(AL) == 0x00) {
                   15698:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   15699:                                } else if(REG8(AL) == 0x01) {
                   15700:                                        UINT8 tmp = dest_buffer[dest_addr];
                   15701:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   15702:                                        src_buffer[src_addr++] = tmp;
                   15703:                                }
                   15704:                        } else {
                   15705:                                REG8(AH) = 0x93;
                   15706:                                return;
1.1.1.20  root     15707:                        }
                   15708:                }
1.1.1.32  root     15709:                REG8(AH) = 0x00;
                   15710:        } else {
                   15711:                REG8(AH) = 0x80;
1.1.1.20  root     15712:        }
                   15713: }
                   15714: 
                   15715: inline void msdos_int_67h_57h()
                   15716: {
                   15717:        if(!support_ems) {
                   15718:                REG8(AH) = 0x84;
                   15719:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15720:                struct {
                   15721:                        UINT16 handle;
                   15722:                        UINT16 page;
                   15723:                        bool mapped;
                   15724:                } tmp_pages[4];
                   15725:                
                   15726:                // unmap pages to copy memory data to ems buffer
                   15727:                for(int i = 0; i < 4; i++) {
                   15728:                        tmp_pages[i].handle = ems_pages[i].handle;
                   15729:                        tmp_pages[i].page   = ems_pages[i].page;
                   15730:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   15731:                        ems_unmap_page(i);
                   15732:                }
                   15733:                
                   15734:                // run move/exchange operation
                   15735:                msdos_int_67h_57h_tmp();
                   15736:                
                   15737:                // restore unmapped pages
                   15738:                for(int i = 0; i < 4; i++) {
                   15739:                        if(tmp_pages[i].mapped) {
                   15740:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   15741:                        }
                   15742:                }
                   15743:        } else {
1.1.1.22  root     15744:                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     15745:                REG8(AH) = 0x8f;
                   15746:        }
                   15747: }
                   15748: 
                   15749: inline void msdos_int_67h_58h()
                   15750: {
                   15751:        if(!support_ems) {
                   15752:                REG8(AH) = 0x84;
                   15753:        } else if(REG8(AL) == 0x00) {
                   15754:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     15755:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   15756:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     15757:                }
                   15758:                REG8(AH) = 0x00;
                   15759:                REG16(CX) = 4;
                   15760:        } else if(REG8(AL) == 0x01) {
                   15761:                REG8(AH) = 0x00;
                   15762:                REG16(CX) = 4;
                   15763:        } else {
1.1.1.22  root     15764:                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     15765:                REG8(AH) = 0x8f;
                   15766:        }
                   15767: }
                   15768: 
1.1.1.42  root     15769: inline void msdos_int_67h_59h()
                   15770: {
                   15771:        if(!support_ems) {
                   15772:                REG8(AH) = 0x84;
                   15773:        } else if(REG8(AL) == 0x00) {
1.1.1.49  root     15774:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1024;
                   15775:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 0;
                   15776:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 4 * 4;
                   15777:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = 0;
                   15778:                REG8(AH) = 0x00;
                   15779: //             REG8(AH) = 0xa4; // access denied by operating system
1.1.1.42  root     15780:        } else if(REG8(AL) == 0x01) {
                   15781:                REG8(AH) = 0x00;
                   15782:                REG16(BX) = free_ems_pages;
                   15783:                REG16(DX) = MAX_EMS_PAGES;
                   15784:        } else {
                   15785:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   15786:                REG8(AH) = 0x8f;
                   15787:        }
                   15788: }
                   15789: 
1.1.1.20  root     15790: inline void msdos_int_67h_5ah()
                   15791: {
                   15792:        if(!support_ems) {
1.1.1.19  root     15793:                REG8(AH) = 0x84;
1.1.1.20  root     15794:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15795:                REG8(AH) = 0x87;
                   15796:        } else if(REG16(BX) > free_ems_pages) {
                   15797:                REG8(AH) = 0x88;
                   15798: //     } else if(REG16(BX) == 0) {
                   15799: //             REG8(AH) = 0x89;
                   15800:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     15801:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     15802:                        if(!ems_handles[i].allocated) {
                   15803:                                ems_allocate_pages(i, REG16(BX));
                   15804:                                REG8(AH) = 0x00;
                   15805:                                REG16(DX) = i;
                   15806:                                return;
                   15807:                        }
                   15808:                }
                   15809:                REG8(AH) = 0x85;
                   15810:        } else {
1.1.1.22  root     15811:                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     15812:                REG8(AH) = 0x8f;
1.1.1.19  root     15813:        }
                   15814: }
                   15815: 
1.1.1.49  root     15816: inline void msdos_int_67h_5bh()
                   15817: {
                   15818:        static UINT8  stored_bl = 0x00;
                   15819:        static UINT16 stored_es = 0x0000;
                   15820:        static UINT16 stored_di = 0x0000;
                   15821:        
                   15822:        if(!support_ems) {
                   15823:                REG8(AH) = 0x84;
                   15824:        } else if(REG8(AL) == 0x00) {
                   15825:                if(stored_bl == 0x00) {
                   15826:                        if(!(stored_es == 0 && stored_di == 0)) {
                   15827:                                for(int i = 0; i < 4; i++) {
                   15828:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15829:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15830:                                }
                   15831:                        }
                   15832:                        SREG(ES) = stored_es;
                   15833:                        i386_load_segment_descriptor(ES);
                   15834:                        REG16(DI) = stored_di;
                   15835:                } else {
                   15836:                        REG8(BL) = stored_bl;
                   15837:                }
                   15838:                REG8(AH) = 0x00;
                   15839:        } else if(REG8(AL) == 0x01) {
                   15840:                if(REG8(BL) == 0x00) {
                   15841:                        if(!(SREG(ES) == 0 && REG16(DI) == 0)) {
                   15842:                                for(int i = 0; i < 4; i++) {
                   15843:                                        UINT16 handle = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0);
                   15844:                                        UINT16 page   = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2);
                   15845:                                        
                   15846:                                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   15847:                                                ems_map_page(i, handle, page);
                   15848:                                        } else {
                   15849:                                                ems_unmap_page(i);
                   15850:                                        }
                   15851:                                }
                   15852:                        }
                   15853:                }
                   15854:                stored_bl = REG8(BL);
                   15855:                stored_es = SREG(ES);
                   15856:                stored_di = REG16(DI);
                   15857:                REG8(AH) = 0x00;
                   15858:        } else if(REG8(AL) == 0x02) {
                   15859:                REG16(DX) = 4 * 4;
                   15860:                REG8(AH) = 0x00;
                   15861:        } else if(REG8(AL) == 0x03) {
                   15862:                REG8(BL) = 0x00; // not supported
                   15863:                REG8(AH) = 0x00;
                   15864:        } else if(REG8(AL) == 0x04) {
                   15865:                REG8(AH) = 0x00;
                   15866:        } else if(REG8(AL) == 0x05) {
                   15867:                REG8(BL) = 0x00; // not supported
                   15868:                REG8(AH) = 0x00;
                   15869:        } else {
                   15870:                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));
                   15871:                REG8(AH) = 0x8f;
                   15872:        }
                   15873: }
                   15874: 
1.1.1.43  root     15875: inline void msdos_int_67h_5dh()
                   15876: {
                   15877:        if(!support_ems) {
                   15878:                REG8(AH) = 0x84;
                   15879:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15880:                REG8(AH) = 0xa4; // operating system denied access
                   15881:        } else {
                   15882:                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));
                   15883:                REG8(AH) = 0x8f;
                   15884:        }
                   15885: }
                   15886: 
1.1.1.49  root     15887: inline void msdos_int_67h_70h()
                   15888: {
                   15889:        if(!support_ems) {
                   15890:                REG8(AH) = 0x84;
                   15891:        } else if(REG8(AL) == 0x00) {
                   15892:                REG8(AL) = 0x00;
                   15893:                REG8(AH) = 0x00;
                   15894:        } else if(REG8(AL) == 0x01) {
                   15895:                REG8(AL) = 0x00;
                   15896: //             REG8(AH) = (REG8(BL) == 0x00) ? 0x00 : 0x80;
                   15897:                REG8(AH) = 0x00;
                   15898:        } else {
                   15899:                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));
                   15900:                REG8(AH) = 0x8f;
                   15901:        }
                   15902: }
                   15903: 
1.1.1.30  root     15904: inline void msdos_int_67h_deh()
                   15905: {
                   15906:        REG8(AH) = 0x84;
                   15907: }
                   15908: 
1.1.1.19  root     15909: #ifdef SUPPORT_XMS
                   15910: 
1.1.1.32  root     15911: void msdos_xms_init()
1.1.1.26  root     15912: {
1.1.1.30  root     15913:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   15914:        emb_handle_top->address = EMB_TOP;
                   15915:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     15916:        xms_a20_local_enb_count = 0;
                   15917: }
                   15918: 
1.1.1.32  root     15919: void msdos_xms_finish()
                   15920: {
                   15921:        msdos_xms_release();
                   15922: }
                   15923: 
                   15924: void msdos_xms_release()
1.1.1.30  root     15925: {
                   15926:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   15927:                emb_handle_t *next_handle = emb_handle->next;
                   15928:                free(emb_handle);
                   15929:                emb_handle = next_handle;
                   15930:        }
                   15931: }
                   15932: 
                   15933: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   15934: {
                   15935:        if(handle != 0) {
                   15936:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15937:                        if(emb_handle->handle == handle) {
                   15938:                                return(emb_handle);
                   15939:                        }
                   15940:                }
                   15941:        }
                   15942:        return(NULL);
                   15943: }
                   15944: 
                   15945: int msdos_xms_get_unused_emb_handle_id()
                   15946: {
                   15947:        for(int handle = 1;; handle++) {
                   15948:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   15949:                        return(handle);
                   15950:                }
                   15951:        }
                   15952:        return(0);
                   15953: }
                   15954: 
                   15955: int msdos_xms_get_unused_emb_handle_count()
                   15956: {
                   15957:        int count = 64; //255;
                   15958:        
                   15959:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15960:                if(emb_handle->handle != 0) {
                   15961:                        if(--count == 1) {
                   15962:                                break;
                   15963:                        }
                   15964:                }
                   15965:        }
                   15966:        return(count);
                   15967: }
                   15968: 
                   15969: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   15970: {
                   15971:        if(emb_handle->size_kb > size_kb) {
                   15972:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   15973:                
                   15974:                new_handle->address = emb_handle->address + size_kb * 1024;
                   15975:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   15976:                emb_handle->size_kb = size_kb;
                   15977:                
                   15978:                new_handle->prev = emb_handle;
                   15979:                new_handle->next = emb_handle->next;
                   15980:                if(emb_handle->next != NULL) {
                   15981:                        emb_handle->next->prev = new_handle;
                   15982:                }
                   15983:                emb_handle->next = new_handle;
                   15984:        }
                   15985: }
                   15986: 
                   15987: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   15988: {
                   15989:        emb_handle_t *next_handle = emb_handle->next;
                   15990:        
                   15991:        if(next_handle != NULL) {
                   15992:                emb_handle->size_kb += next_handle->size_kb;
                   15993:                
                   15994:                if(next_handle->next != NULL) {
                   15995:                        next_handle->next->prev = emb_handle;
                   15996:                }
                   15997:                emb_handle->next = next_handle->next;
                   15998:                free(next_handle);
                   15999:        }
                   16000: }
                   16001: 
                   16002: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   16003: {
                   16004:        emb_handle_t *target_handle = NULL;
                   16005:        
                   16006:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16007:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   16008:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   16009:                                target_handle = emb_handle;
                   16010:                        }
                   16011:                }
                   16012:        }
                   16013:        if(target_handle != NULL) {
                   16014:                if(target_handle->size_kb > size_kb) {
                   16015:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   16016:                }
                   16017: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16018:                return(target_handle);
                   16019:        }
                   16020:        return(NULL);
                   16021: }
                   16022: 
                   16023: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   16024: {
                   16025:        emb_handle_t *prev_handle = emb_handle->prev;
                   16026:        emb_handle_t *next_handle = emb_handle->next;
                   16027:        
                   16028:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   16029:                msdos_xms_combine_emb_handles(prev_handle);
                   16030:                emb_handle = prev_handle;
                   16031:        }
                   16032:        if(next_handle != NULL && next_handle->handle == 0) {
                   16033:                msdos_xms_combine_emb_handles(emb_handle);
                   16034:        }
                   16035:        emb_handle->handle = 0;
                   16036: }
                   16037: 
1.1.1.19  root     16038: inline void msdos_call_xms_00h()
                   16039: {
1.1.1.29  root     16040: #if defined(HAS_I386)
                   16041:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.45  root     16042: //     REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
1.1.1.29  root     16043:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   16044: #else
                   16045:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   16046:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   16047: #endif
                   16048: //     REG16(DX) = 0x0000; // HMA does not exist
                   16049:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     16050: }
                   16051: 
                   16052: inline void msdos_call_xms_01h()
                   16053: {
1.1.1.29  root     16054:        if(REG8(AL) == 0x40) {
                   16055:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   16056:                // DX=KB free extended memory returned by last call of function 08h
                   16057:                REG16(AX) = 0x0000;
                   16058:                REG8(BL) = 0x91;
                   16059:                REG16(DX) = xms_dx_after_call_08h;
                   16060:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16061:                REG16(AX) = 0x0000;
                   16062:                REG8(BL) = 0x81; // Vdisk was detected
                   16063: #ifdef SUPPORT_HMA
                   16064:        } else if(is_hma_used_by_int_2fh) {
                   16065:                REG16(AX) = 0x0000;
                   16066:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16067:        } else if(is_hma_used_by_xms) {
                   16068:                REG16(AX) = 0x0000;
                   16069:                REG8(BL) = 0x91; // HMA is already in use
                   16070:        } else {
                   16071:                REG16(AX) = 0x0001;
                   16072:                is_hma_used_by_xms = true;
                   16073: #else
                   16074:        } else {
                   16075:                REG16(AX) = 0x0000;
                   16076:                REG8(BL) = 0x91; // HMA is already in use
                   16077: #endif
                   16078:        }
1.1.1.19  root     16079: }
                   16080: 
                   16081: inline void msdos_call_xms_02h()
                   16082: {
1.1.1.29  root     16083:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16084:                REG16(AX) = 0x0000;
                   16085:                REG8(BL) = 0x81; // Vdisk was detected
                   16086: #ifdef SUPPORT_HMA
                   16087:        } else if(is_hma_used_by_int_2fh) {
                   16088:                REG16(AX) = 0x0000;
                   16089:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16090:        } else if(!is_hma_used_by_xms) {
                   16091:                REG16(AX) = 0x0000;
                   16092:                REG8(BL) = 0x93; // HMA is not allocated
                   16093:        } else {
                   16094:                REG16(AX) = 0x0001;
                   16095:                is_hma_used_by_xms = false;
                   16096:                // restore first free mcb in high memory area
                   16097:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   16098: #else
                   16099:        } else {
                   16100:                REG16(AX) = 0x0000;
                   16101:                REG8(BL) = 0x91; // HMA is already in use
                   16102: #endif
                   16103:        }
1.1.1.19  root     16104: }
                   16105: 
                   16106: inline void msdos_call_xms_03h()
                   16107: {
                   16108:        i386_set_a20_line(1);
                   16109:        REG16(AX) = 0x0001;
                   16110:        REG8(BL) = 0x00;
                   16111: }
                   16112: 
                   16113: inline void msdos_call_xms_04h()
                   16114: {
1.1.1.21  root     16115:        i386_set_a20_line(0);
                   16116:        REG16(AX) = 0x0001;
                   16117:        REG8(BL) = 0x00;
1.1.1.19  root     16118: }
                   16119: 
                   16120: inline void msdos_call_xms_05h()
                   16121: {
                   16122:        i386_set_a20_line(1);
                   16123:        REG16(AX) = 0x0001;
                   16124:        REG8(BL) = 0x00;
1.1.1.21  root     16125:        xms_a20_local_enb_count++;
1.1.1.19  root     16126: }
                   16127: 
                   16128: void msdos_call_xms_06h()
                   16129: {
1.1.1.21  root     16130:        if(xms_a20_local_enb_count > 0) {
1.1.1.45  root     16131:                if(--xms_a20_local_enb_count == 0) {
                   16132:                        i386_set_a20_line(0);
                   16133:                        REG16(AX) = 0x0001;
                   16134:                        REG8(BL) = 0x00;
                   16135:                } else {
                   16136:                        REG16(AX) = 0x0000;
                   16137:                        REG8(BL) = 0x94;
                   16138:                }
1.1.1.21  root     16139:        } else {
1.1.1.45  root     16140:                i386_set_a20_line(0);
1.1.1.21  root     16141:                REG16(AX) = 0x0001;
                   16142:                REG8(BL) = 0x00;
1.1.1.19  root     16143:        }
                   16144: }
                   16145: 
                   16146: inline void msdos_call_xms_07h()
                   16147: {
                   16148:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   16149:        REG8(BL) = 0x00;
                   16150: }
                   16151: 
                   16152: inline void msdos_call_xms_08h()
                   16153: {
1.1.1.45  root     16154:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     16155:        
1.1.1.30  root     16156:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16157:                if(emb_handle->handle == 0) {
1.1.1.45  root     16158:                        if(eax < emb_handle->size_kb) {
                   16159:                                eax = emb_handle->size_kb;
1.1.1.19  root     16160:                        }
1.1.1.45  root     16161:                        edx += emb_handle->size_kb;
1.1.1.19  root     16162:                }
                   16163:        }
1.1.1.45  root     16164:        if(eax > 65535) {
                   16165:                eax = 65535;
                   16166:        }
                   16167:        if(edx > 65535) {
                   16168:                edx = 65535;
                   16169:        }
                   16170:        if(eax == 0 && edx == 0) {
1.1.1.19  root     16171:                REG8(BL) = 0xa0;
                   16172:        } else {
                   16173:                REG8(BL) = 0x00;
                   16174:        }
1.1.1.45  root     16175: #if defined(HAS_I386)
                   16176:        REG32(EAX) = eax;
                   16177:        REG32(EDX) = edx;
                   16178: #else
                   16179:        REG16(AX) = (UINT16)eax;
                   16180:        REG16(DX) = (UINT16)edx;
                   16181: #endif
1.1.1.29  root     16182:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     16183: }
                   16184: 
1.1.1.30  root     16185: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     16186: {
1.1.1.30  root     16187:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   16188:        
                   16189:        if(emb_handle != NULL) {
                   16190:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16191:                
                   16192:                REG16(AX) = 0x0001;
                   16193:                REG16(DX) = emb_handle->handle;
                   16194:                REG8(BL) = 0x00;
                   16195:        } else {
                   16196:                REG16(AX) = REG16(DX) = 0x0000;
                   16197:                REG8(BL) = 0xa0;
1.1.1.19  root     16198:        }
1.1.1.30  root     16199: }
                   16200: 
                   16201: inline void msdos_call_xms_09h()
                   16202: {
                   16203:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     16204: }
                   16205: 
                   16206: inline void msdos_call_xms_0ah()
                   16207: {
1.1.1.30  root     16208:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16209:        
                   16210:        if(emb_handle == NULL) {
1.1.1.19  root     16211:                REG16(AX) = 0x0000;
                   16212:                REG8(BL) = 0xa2;
1.1.1.45  root     16213: //     } else if(emb_handle->lock > 0) {
                   16214: //             REG16(AX) = 0x0000;
                   16215: //             REG8(BL) = 0xab;
1.1.1.19  root     16216:        } else {
1.1.1.30  root     16217:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     16218:                
                   16219:                REG16(AX) = 0x0001;
                   16220:                REG8(BL) = 0x00;
                   16221:        }
                   16222: }
                   16223: 
                   16224: inline void msdos_call_xms_0bh()
                   16225: {
                   16226:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   16227:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   16228:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   16229:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   16230:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   16231:        
                   16232:        UINT8 *src_buffer, *dest_buffer;
                   16233:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     16234:        emb_handle_t *emb_handle;
1.1.1.19  root     16235:        
                   16236:        if(src_handle == 0) {
                   16237:                src_buffer = mem;
                   16238:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   16239:                src_addr_max = MAX_MEM;
                   16240:        } else {
1.1.1.30  root     16241:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     16242:                        REG16(AX) = 0x0000;
                   16243:                        REG8(BL) = 0xa3;
                   16244:                        return;
                   16245:                }
1.1.1.30  root     16246:                src_buffer = mem + emb_handle->address;
                   16247:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16248:        }
                   16249:        if(dest_handle == 0) {
                   16250:                dest_buffer = mem;
                   16251:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   16252:                dest_addr_max = MAX_MEM;
                   16253:        } else {
1.1.1.30  root     16254:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     16255:                        REG16(AX) = 0x0000;
                   16256:                        REG8(BL) = 0xa5;
                   16257:                        return;
                   16258:                }
1.1.1.30  root     16259:                dest_buffer = mem + emb_handle->address;
                   16260:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16261:        }
                   16262:        for(int i = 0; i < copy_length; i++) {
                   16263:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   16264:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   16265:                } else {
                   16266:                        break;
                   16267:                }
                   16268:        }
                   16269:        REG16(AX) = 0x0001;
                   16270:        REG8(BL) = 0x00;
                   16271: }
                   16272: 
                   16273: inline void msdos_call_xms_0ch()
                   16274: {
1.1.1.30  root     16275:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16276:        
                   16277:        if(emb_handle == NULL) {
1.1.1.19  root     16278:                REG16(AX) = 0x0000;
                   16279:                REG8(BL) = 0xa2;
                   16280:        } else {
1.1.1.45  root     16281:                if(emb_handle->lock < 255) {
                   16282:                        emb_handle->lock++;
                   16283:                }
1.1.1.19  root     16284:                REG16(AX) = 0x0001;
1.1.1.30  root     16285:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   16286:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     16287:        }
                   16288: }
                   16289: 
                   16290: inline void msdos_call_xms_0dh()
                   16291: {
1.1.1.30  root     16292:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16293:        
                   16294:        if(emb_handle == NULL) {
1.1.1.19  root     16295:                REG16(AX) = 0x0000;
                   16296:                REG8(BL) = 0xa2;
1.1.1.30  root     16297:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     16298:                REG16(AX) = 0x0000;
                   16299:                REG8(BL) = 0xaa;
                   16300:        } else {
1.1.1.30  root     16301:                emb_handle->lock--;
1.1.1.19  root     16302:                REG16(AX) = 0x0001;
                   16303:                REG8(BL) = 0x00;
                   16304:        }
                   16305: }
                   16306: 
                   16307: inline void msdos_call_xms_0eh()
                   16308: {
1.1.1.30  root     16309:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16310:        
                   16311:        if(emb_handle == NULL) {
1.1.1.19  root     16312:                REG16(AX) = 0x0000;
                   16313:                REG8(BL) = 0xa2;
                   16314:        } else {
                   16315:                REG16(AX) = 0x0001;
1.1.1.30  root     16316:                REG8(BH) = emb_handle->lock;
                   16317:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   16318:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     16319:        }
                   16320: }
                   16321: 
1.1.1.30  root     16322: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     16323: {
1.1.1.30  root     16324:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16325:        
                   16326:        if(emb_handle == NULL) {
1.1.1.19  root     16327:                REG16(AX) = 0x0000;
                   16328:                REG8(BL) = 0xa2;
1.1.1.30  root     16329:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     16330:                REG16(AX) = 0x0000;
                   16331:                REG8(BL) = 0xab;
                   16332:        } else {
1.1.1.30  root     16333:                if(emb_handle->size_kb < size_kb) {
                   16334:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   16335:                                msdos_xms_combine_emb_handles(emb_handle);
                   16336:                                if(emb_handle->size_kb > size_kb) {
                   16337:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16338:                                }
                   16339:                        } else {
                   16340:                                int old_handle = emb_handle->handle;
                   16341:                                int old_size_kb = emb_handle->size_kb;
                   16342:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   16343:                                
                   16344:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   16345:                                msdos_xms_free_emb_handle(emb_handle);
                   16346:                                
                   16347:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   16348:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   16349:                                }
                   16350:                                emb_handle->handle = old_handle;
                   16351:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   16352:                                free(buffer);
                   16353:                        }
                   16354:                } else if(emb_handle->size_kb > size_kb) {
                   16355:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16356:                }
                   16357:                if(emb_handle->size_kb != size_kb) {
                   16358:                        REG16(AX) = 0x0000;
                   16359:                        REG8(BL) = 0xa0;
                   16360:                } else {
                   16361:                        REG16(AX) = 0x0001;
                   16362:                        REG8(BL) = 0x00;
                   16363:                }
1.1.1.19  root     16364:        }
                   16365: }
                   16366: 
1.1.1.30  root     16367: inline void msdos_call_xms_0fh()
                   16368: {
                   16369:        msdos_call_xms_0fh(REG16(BX));
                   16370: }
                   16371: 
1.1.1.19  root     16372: inline void msdos_call_xms_10h()
                   16373: {
                   16374:        int seg;
                   16375:        
                   16376:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   16377:                REG16(AX) = 0x0001;
                   16378:                REG16(BX) = seg;
                   16379:        } else {
                   16380:                REG16(AX) = 0x0000;
                   16381:                REG8(BL) = 0xb0;
                   16382:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   16383:        }
                   16384: }
                   16385: 
                   16386: inline void msdos_call_xms_11h()
                   16387: {
                   16388:        int mcb_seg = REG16(DX) - 1;
                   16389:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16390:        
                   16391:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16392:                msdos_mem_free(REG16(DX));
                   16393:                REG16(AX) = 0x0001;
                   16394:                REG8(BL) = 0x00;
                   16395:        } else {
                   16396:                REG16(AX) = 0x0000;
                   16397:                REG8(BL) = 0xb2;
                   16398:        }
                   16399: }
                   16400: 
                   16401: inline void msdos_call_xms_12h()
                   16402: {
                   16403:        int mcb_seg = REG16(DX) - 1;
                   16404:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16405:        int max_paragraphs;
                   16406:        
                   16407:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16408:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   16409:                        REG16(AX) = 0x0001;
                   16410:                        REG8(BL) = 0x00;
                   16411:                } else {
                   16412:                        REG16(AX) = 0x0000;
                   16413:                        REG8(BL) = 0xb0;
                   16414:                        REG16(DX) = max_paragraphs;
                   16415:                }
                   16416:        } else {
                   16417:                REG16(AX) = 0x0000;
                   16418:                REG8(BL) = 0xb2;
                   16419:        }
                   16420: }
                   16421: 
1.1.1.29  root     16422: #if defined(HAS_I386)
                   16423: 
                   16424: inline void msdos_call_xms_88h()
                   16425: {
                   16426:        REG32(EAX) = REG32(EDX) = 0x0000;
                   16427:        
1.1.1.30  root     16428:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16429:                if(emb_handle->handle == 0) {
                   16430:                        if(REG32(EAX) < emb_handle->size_kb) {
                   16431:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     16432:                        }
1.1.1.30  root     16433:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     16434:                }
                   16435:        }
                   16436:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   16437:                REG8(BL) = 0xa0;
                   16438:        } else {
                   16439:                REG8(BL) = 0x00;
                   16440:        }
                   16441:        REG32(ECX) = EMB_END - 1;
                   16442: }
                   16443: 
                   16444: inline void msdos_call_xms_89h()
                   16445: {
1.1.1.30  root     16446:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     16447: }
                   16448: 
                   16449: inline void msdos_call_xms_8eh()
                   16450: {
1.1.1.30  root     16451:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16452:        
                   16453:        if(emb_handle == NULL) {
1.1.1.29  root     16454:                REG16(AX) = 0x0000;
                   16455:                REG8(BL) = 0xa2;
                   16456:        } else {
                   16457:                REG16(AX) = 0x0001;
1.1.1.30  root     16458:                REG8(BH) = emb_handle->lock;
                   16459:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   16460:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     16461:        }
                   16462: }
                   16463: 
                   16464: inline void msdos_call_xms_8fh()
                   16465: {
1.1.1.30  root     16466:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     16467: }
                   16468: 
                   16469: #endif
1.1.1.19  root     16470: #endif
                   16471: 
1.1.1.26  root     16472: UINT16 msdos_get_equipment()
                   16473: {
                   16474:        static UINT16 equip = 0;
                   16475:        
                   16476:        if(equip == 0) {
                   16477: #ifdef SUPPORT_FPU
                   16478:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   16479: #endif
                   16480:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   16481:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   16482: //             equip |= (1 << 8);      // 0 if DMA installed
                   16483:                equip |= (2 << 9);      // number of serial ports
                   16484:                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     16485:                
                   16486:                // check only A: and B: if it is floppy drive
                   16487:                int n = 0;
                   16488:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     16489:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   16490:                                n++;
1.1.1.28  root     16491:                        }
                   16492:                }
                   16493:                if(n != 0) {
                   16494:                        equip |= (1 << 0);      // floppy disk(s) installed
                   16495:                        n--;
                   16496:                        equip |= (n << 6);      // number of floppies installed less 1
                   16497:                }
                   16498: //             if(joyGetNumDevs() != 0) {
                   16499: //                     equip |= (1 << 12);     // game port installed
                   16500: //             }
1.1.1.26  root     16501:        }
                   16502:        return(equip);
                   16503: }
                   16504: 
1.1       root     16505: void msdos_syscall(unsigned num)
                   16506: {
1.1.1.22  root     16507: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     16508:        if(num == 0x08 || num == 0x1c) {
                   16509:                // don't log the timer interrupts
1.1.1.45  root     16510: //             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     16511:        } else if(num == 0x30) {
                   16512:                // dummy interrupt for call 0005h (call near)
                   16513:                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     16514:        } else if(num == 0x68) {
1.1.1.22  root     16515:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     16516:                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     16517:        } else if(num == 0x69) {
                   16518:                // dummy interrupt for XMS (call far)
1.1.1.33  root     16519:                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     16520:        } else if(num >= 0x6a && num <= 0x6f) {
1.1.1.45  root     16521:                // dummy interrupt
1.1.1.22  root     16522:        } else {
1.1.1.33  root     16523:                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     16524:        }
                   16525: #endif
1.1.1.36  root     16526:        // update cursor position
                   16527:        if(cursor_moved) {
                   16528:                pcbios_update_cursor_position();
                   16529:                cursor_moved = false;
                   16530:        }
1.1.1.50  root     16531: #ifdef USE_SERVICE_THREAD
                   16532:        // this is called from dummy loop to wait until a serive that waits input is done
                   16533:        if(!in_service)
                   16534: #endif
1.1.1.33  root     16535:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     16536:        
1.1       root     16537:        switch(num) {
                   16538:        case 0x00:
1.1.1.28  root     16539:                try {
                   16540:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16541:                        error("division by zero\n");
                   16542:                } catch(...) {
                   16543:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   16544:                }
1.1       root     16545:                break;
                   16546:        case 0x04:
1.1.1.28  root     16547:                try {
                   16548:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16549:                        error("overflow\n");
                   16550:                } catch(...) {
                   16551:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   16552:                }
1.1       root     16553:                break;
                   16554:        case 0x06:
                   16555:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     16556:                if(!ignore_illegal_insn) {
1.1.1.28  root     16557:                        try {
                   16558:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16559:                                error("illegal instruction\n");
                   16560:                        } catch(...) {
                   16561:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   16562:                        }
1.1.1.14  root     16563:                } else {
                   16564: #if defined(HAS_I386)
1.1.1.39  root     16565:                        m_eip = m_int6h_skip_eip;
                   16566: #elif defined(HAS_I286)
                   16567:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     16568: #else
1.1.1.39  root     16569:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     16570: #endif
                   16571:                }
1.1       root     16572:                break;
1.1.1.33  root     16573:        case 0x09:
                   16574:                // ctrl-break is pressed
                   16575:                if(raise_int_1bh) {
                   16576: #if defined(HAS_I386)
                   16577:                        m_ext = 0; // not an external interrupt
                   16578:                        i386_trap(0x1b, 1, 0);
                   16579:                        m_ext = 1;
                   16580: #else
                   16581:                        PREFIX86(_interrupt)(0x1b);
                   16582: #endif
                   16583:                        raise_int_1bh = false;
                   16584:                }
1.1.1.8   root     16585:        case 0x08:
1.1.1.14  root     16586: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     16587:        case 0x0b:
                   16588:        case 0x0c:
                   16589:        case 0x0d:
                   16590:        case 0x0e:
                   16591:        case 0x0f:
                   16592:                // EOI
                   16593:                pic[0].isr &= ~(1 << (num - 0x08));
                   16594:                pic_update();
                   16595:                break;
1.1       root     16596:        case 0x10:
                   16597:                // PC BIOS - Video
1.1.1.14  root     16598:                if(!restore_console_on_exit) {
1.1.1.15  root     16599:                        change_console_size(scr_width, scr_height);
1.1       root     16600:                }
1.1.1.3   root     16601:                m_CF = 0;
1.1       root     16602:                switch(REG8(AH)) {
1.1.1.16  root     16603:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     16604:                case 0x01: pcbios_int_10h_01h(); break;
                   16605:                case 0x02: pcbios_int_10h_02h(); break;
                   16606:                case 0x03: pcbios_int_10h_03h(); break;
                   16607:                case 0x05: pcbios_int_10h_05h(); break;
                   16608:                case 0x06: pcbios_int_10h_06h(); break;
                   16609:                case 0x07: pcbios_int_10h_07h(); break;
                   16610:                case 0x08: pcbios_int_10h_08h(); break;
                   16611:                case 0x09: pcbios_int_10h_09h(); break;
                   16612:                case 0x0a: pcbios_int_10h_0ah(); break;
                   16613:                case 0x0b: break;
1.1.1.40  root     16614:                case 0x0c: pcbios_int_10h_0ch(); break;
                   16615:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     16616:                case 0x0e: pcbios_int_10h_0eh(); break;
                   16617:                case 0x0f: pcbios_int_10h_0fh(); break;
                   16618:                case 0x10: break;
1.1.1.14  root     16619:                case 0x11: pcbios_int_10h_11h(); break;
                   16620:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     16621:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     16622:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     16623:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     16624:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   16625:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     16626:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     16627:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   16628:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     16629:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     16630:                case 0x6f: break;
1.1.1.22  root     16631:                case 0x80: m_CF = 1; break; // unknown
                   16632:                case 0x81: m_CF = 1; break; // unknown
1.1       root     16633:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     16634:                case 0x83: pcbios_int_10h_83h(); break;
                   16635:                case 0x8b: break;
                   16636:                case 0x8c: m_CF = 1; break; // unknown
                   16637:                case 0x8d: m_CF = 1; break; // unknown
                   16638:                case 0x8e: m_CF = 1; break; // unknown
                   16639:                case 0x90: pcbios_int_10h_90h(); break;
                   16640:                case 0x91: pcbios_int_10h_91h(); break;
                   16641:                case 0x92: break;
                   16642:                case 0x93: break;
                   16643:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     16644:                case 0xfa: break; // ega register interface library is not installed
1.1       root     16645:                case 0xfe: pcbios_int_10h_feh(); break;
                   16646:                case 0xff: pcbios_int_10h_ffh(); break;
                   16647:                default:
1.1.1.22  root     16648:                        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));
                   16649:                        m_CF = 1;
1.1       root     16650:                        break;
                   16651:                }
                   16652:                break;
                   16653:        case 0x11:
                   16654:                // PC BIOS - Get Equipment List
1.1.1.26  root     16655:                REG16(AX) = msdos_get_equipment();
1.1       root     16656:                break;
                   16657:        case 0x12:
                   16658:                // PC BIOS - Get Memory Size
1.1.1.33  root     16659:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     16660:                break;
                   16661:        case 0x13:
1.1.1.42  root     16662:                // PC BIOS - Disk I/O
                   16663:                {
                   16664:                        static UINT8 last = 0x00;
                   16665:                        switch(REG8(AH)) {
                   16666:                        case 0x00: pcbios_int_13h_00h(); break;
                   16667:                        case 0x01: // get last status
                   16668:                                REG8(AH) = last;
                   16669:                                break;
                   16670:                        case 0x02: pcbios_int_13h_02h(); break;
                   16671:                        case 0x03: pcbios_int_13h_03h(); break;
                   16672:                        case 0x04: pcbios_int_13h_04h(); break;
                   16673:                        case 0x08: pcbios_int_13h_08h(); break;
                   16674:                        case 0x0a: pcbios_int_13h_02h(); break;
                   16675:                        case 0x0b: pcbios_int_13h_03h(); break;
                   16676:                        case 0x0d: pcbios_int_13h_00h(); break;
                   16677:                        case 0x10: pcbios_int_13h_10h(); break;
                   16678:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     16679:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     16680:                        case 0x05: // format
                   16681:                        case 0x06:
                   16682:                        case 0x07:
                   16683:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   16684:                                m_CF = 1;
                   16685:                                break;
                   16686:                        case 0x09:
                   16687:                        case 0x0c: // seek
                   16688:                        case 0x11: // recalib
                   16689:                        case 0x14:
                   16690:                        case 0x17:
                   16691:                                REG8(AH) = 0x00; // successful completion
                   16692:                                break;
1.1.1.43  root     16693:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   16694:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   16695:                                REG8(AH) = 0x01; // invalid function
                   16696:                                m_CF = 1;
                   16697:                                break;
1.1.1.42  root     16698:                        default:
                   16699:                                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));
                   16700:                                REG8(AH) = 0x01; // invalid function
                   16701:                                m_CF = 1;
                   16702:                                break;
                   16703:                        }
                   16704:                        last = REG8(AH);
                   16705:                }
1.1       root     16706:                break;
                   16707:        case 0x14:
                   16708:                // PC BIOS - Serial I/O
1.1.1.25  root     16709:                switch(REG8(AH)) {
                   16710:                case 0x00: pcbios_int_14h_00h(); break;
                   16711:                case 0x01: pcbios_int_14h_01h(); break;
                   16712:                case 0x02: pcbios_int_14h_02h(); break;
                   16713:                case 0x03: pcbios_int_14h_03h(); break;
                   16714:                case 0x04: pcbios_int_14h_04h(); break;
                   16715:                case 0x05: pcbios_int_14h_05h(); break;
                   16716:                default:
                   16717:                        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));
                   16718:                        break;
                   16719:                }
1.1       root     16720:                break;
                   16721:        case 0x15:
                   16722:                // PC BIOS
1.1.1.3   root     16723:                m_CF = 0;
1.1       root     16724:                switch(REG8(AH)) {
1.1.1.14  root     16725:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     16726:                case 0x23: pcbios_int_15h_23h(); break;
                   16727:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     16728:                case 0x41: break;
1.1       root     16729:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     16730:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     16731:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     16732:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     16733:                case 0x86: pcbios_int_15h_86h(); break;
                   16734:                case 0x87: pcbios_int_15h_87h(); break;
                   16735:                case 0x88: pcbios_int_15h_88h(); break;
                   16736:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     16737:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     16738:                case 0xc0: // PS/2 ???
1.1.1.54  root     16739: #ifndef EXT_BIOS_TOP
1.1.1.22  root     16740:                case 0xc1:
1.1.1.54  root     16741: #endif
1.1.1.30  root     16742:                case 0xc3: // PS50+ ???
                   16743:                case 0xc4:
1.1.1.22  root     16744:                        REG8(AH) = 0x86;
                   16745:                        m_CF = 1;
                   16746:                        break;
1.1.1.54  root     16747: #ifdef EXT_BIOS_TOP
                   16748:                case 0xc1: pcbios_int_15h_c1h(); break;
                   16749: #endif
                   16750:                case 0xc2: pcbios_int_15h_c2h(); break;
1.1.1.3   root     16751: #if defined(HAS_I386)
1.1       root     16752:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     16753: #endif
1.1       root     16754:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     16755:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     16756:                default:
1.1.1.22  root     16757:                        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));
                   16758:                        REG8(AH) = 0x86;
1.1.1.3   root     16759:                        m_CF = 1;
1.1       root     16760:                        break;
                   16761:                }
                   16762:                break;
                   16763:        case 0x16:
                   16764:                // PC BIOS - Keyboard
1.1.1.3   root     16765:                m_CF = 0;
1.1       root     16766:                switch(REG8(AH)) {
                   16767:                case 0x00: pcbios_int_16h_00h(); break;
                   16768:                case 0x01: pcbios_int_16h_01h(); break;
                   16769:                case 0x02: pcbios_int_16h_02h(); break;
                   16770:                case 0x03: pcbios_int_16h_03h(); break;
                   16771:                case 0x05: pcbios_int_16h_05h(); break;
                   16772:                case 0x10: pcbios_int_16h_00h(); break;
                   16773:                case 0x11: pcbios_int_16h_01h(); break;
                   16774:                case 0x12: pcbios_int_16h_12h(); break;
                   16775:                case 0x13: pcbios_int_16h_13h(); break;
                   16776:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     16777:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     16778:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     16779:                case 0xda: break; // unknown
1.1.1.43  root     16780:                case 0xdb: break; // unknown
1.1.1.22  root     16781:                case 0xff: break; // unknown
1.1       root     16782:                default:
1.1.1.22  root     16783:                        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     16784:                        break;
                   16785:                }
                   16786:                break;
                   16787:        case 0x17:
                   16788:                // PC BIOS - Printer
1.1.1.37  root     16789:                m_CF = 0;
                   16790:                switch(REG8(AH)) {
                   16791:                case 0x00: pcbios_int_17h_00h(); break;
                   16792:                case 0x01: pcbios_int_17h_01h(); break;
                   16793:                case 0x02: pcbios_int_17h_02h(); break;
                   16794:                case 0x03: pcbios_int_17h_03h(); break;
                   16795:                case 0x50: pcbios_int_17h_50h(); break;
                   16796:                case 0x51: pcbios_int_17h_51h(); break;
                   16797:                case 0x52: pcbios_int_17h_52h(); break;
                   16798:                case 0x84: pcbios_int_17h_84h(); break;
                   16799:                case 0x85: pcbios_int_17h_85h(); break;
                   16800:                default:
                   16801:                        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));
                   16802:                        break;
                   16803:                }
1.1       root     16804:                break;
                   16805:        case 0x1a:
                   16806:                // PC BIOS - Timer
1.1.1.3   root     16807:                m_CF = 0;
1.1       root     16808:                switch(REG8(AH)) {
                   16809:                case 0x00: pcbios_int_1ah_00h(); break;
                   16810:                case 0x01: break;
                   16811:                case 0x02: pcbios_int_1ah_02h(); break;
                   16812:                case 0x03: break;
                   16813:                case 0x04: pcbios_int_1ah_04h(); break;
                   16814:                case 0x05: break;
                   16815:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   16816:                case 0x0b: break;
1.1.1.14  root     16817:                case 0x35: break; // Word Perfect Third Party Interface?
                   16818:                case 0x36: break; // Word Perfect Third Party Interface
                   16819:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     16820:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     16821:                case 0xb1: break; // PCI BIOS v2.0c+
                   16822:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     16823:                default:
1.1.1.22  root     16824:                        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     16825:                        break;
                   16826:                }
                   16827:                break;
1.1.1.33  root     16828:        case 0x1b:
                   16829:                mem[0x471] = 0x00;
                   16830:                break;
1.1       root     16831:        case 0x20:
1.1.1.28  root     16832:                try {
                   16833:                        msdos_process_terminate(SREG(CS), retval, 1);
                   16834:                } catch(...) {
                   16835:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   16836:                }
1.1       root     16837:                break;
1.1.1.49  root     16838:        case 0x30:
1.1.1.46  root     16839:                // dummy interrupt for case map routine pointed in the country info
                   16840: //             if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
                   16841: //                     REG8(AL) = 0x00;
                   16842: //                     break;
                   16843: //             }
1.1       root     16844:        case 0x21:
                   16845:                // MS-DOS System Call
1.1.1.3   root     16846:                m_CF = 0;
1.1.1.28  root     16847:                try {
1.1.1.46  root     16848:                        switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28  root     16849:                        case 0x00: msdos_int_21h_00h(); break;
                   16850:                        case 0x01: msdos_int_21h_01h(); break;
                   16851:                        case 0x02: msdos_int_21h_02h(); break;
                   16852:                        case 0x03: msdos_int_21h_03h(); break;
                   16853:                        case 0x04: msdos_int_21h_04h(); break;
                   16854:                        case 0x05: msdos_int_21h_05h(); break;
                   16855:                        case 0x06: msdos_int_21h_06h(); break;
                   16856:                        case 0x07: msdos_int_21h_07h(); break;
                   16857:                        case 0x08: msdos_int_21h_08h(); break;
                   16858:                        case 0x09: msdos_int_21h_09h(); break;
                   16859:                        case 0x0a: msdos_int_21h_0ah(); break;
                   16860:                        case 0x0b: msdos_int_21h_0bh(); break;
                   16861:                        case 0x0c: msdos_int_21h_0ch(); break;
                   16862:                        case 0x0d: msdos_int_21h_0dh(); break;
                   16863:                        case 0x0e: msdos_int_21h_0eh(); break;
                   16864:                        case 0x0f: msdos_int_21h_0fh(); break;
                   16865:                        case 0x10: msdos_int_21h_10h(); break;
                   16866:                        case 0x11: msdos_int_21h_11h(); break;
                   16867:                        case 0x12: msdos_int_21h_12h(); break;
                   16868:                        case 0x13: msdos_int_21h_13h(); break;
                   16869:                        case 0x14: msdos_int_21h_14h(); break;
                   16870:                        case 0x15: msdos_int_21h_15h(); break;
                   16871:                        case 0x16: msdos_int_21h_16h(); break;
                   16872:                        case 0x17: msdos_int_21h_17h(); break;
                   16873:                        case 0x18: msdos_int_21h_18h(); break;
                   16874:                        case 0x19: msdos_int_21h_19h(); break;
                   16875:                        case 0x1a: msdos_int_21h_1ah(); break;
                   16876:                        case 0x1b: msdos_int_21h_1bh(); break;
                   16877:                        case 0x1c: msdos_int_21h_1ch(); break;
                   16878:                        case 0x1d: msdos_int_21h_1dh(); break;
                   16879:                        case 0x1e: msdos_int_21h_1eh(); break;
                   16880:                        case 0x1f: msdos_int_21h_1fh(); break;
                   16881:                        case 0x20: msdos_int_21h_20h(); break;
                   16882:                        case 0x21: msdos_int_21h_21h(); break;
                   16883:                        case 0x22: msdos_int_21h_22h(); break;
                   16884:                        case 0x23: msdos_int_21h_23h(); break;
                   16885:                        case 0x24: msdos_int_21h_24h(); break;
                   16886:                        case 0x25: msdos_int_21h_25h(); break;
                   16887:                        case 0x26: msdos_int_21h_26h(); break;
                   16888:                        case 0x27: msdos_int_21h_27h(); break;
                   16889:                        case 0x28: msdos_int_21h_28h(); break;
                   16890:                        case 0x29: msdos_int_21h_29h(); break;
                   16891:                        case 0x2a: msdos_int_21h_2ah(); break;
                   16892:                        case 0x2b: msdos_int_21h_2bh(); break;
                   16893:                        case 0x2c: msdos_int_21h_2ch(); break;
                   16894:                        case 0x2d: msdos_int_21h_2dh(); break;
                   16895:                        case 0x2e: msdos_int_21h_2eh(); break;
                   16896:                        case 0x2f: msdos_int_21h_2fh(); break;
                   16897:                        case 0x30: msdos_int_21h_30h(); break;
                   16898:                        case 0x31: msdos_int_21h_31h(); break;
                   16899:                        case 0x32: msdos_int_21h_32h(); break;
                   16900:                        case 0x33: msdos_int_21h_33h(); break;
                   16901:                        case 0x34: msdos_int_21h_34h(); break;
                   16902:                        case 0x35: msdos_int_21h_35h(); break;
                   16903:                        case 0x36: msdos_int_21h_36h(); break;
                   16904:                        case 0x37: msdos_int_21h_37h(); break;
                   16905:                        case 0x38: msdos_int_21h_38h(); break;
                   16906:                        case 0x39: msdos_int_21h_39h(0); break;
                   16907:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   16908:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   16909:                        case 0x3c: msdos_int_21h_3ch(); break;
                   16910:                        case 0x3d: msdos_int_21h_3dh(); break;
                   16911:                        case 0x3e: msdos_int_21h_3eh(); break;
                   16912:                        case 0x3f: msdos_int_21h_3fh(); break;
                   16913:                        case 0x40: msdos_int_21h_40h(); break;
                   16914:                        case 0x41: msdos_int_21h_41h(0); break;
                   16915:                        case 0x42: msdos_int_21h_42h(); break;
                   16916:                        case 0x43: msdos_int_21h_43h(0); break;
                   16917:                        case 0x44: msdos_int_21h_44h(); break;
                   16918:                        case 0x45: msdos_int_21h_45h(); break;
                   16919:                        case 0x46: msdos_int_21h_46h(); break;
                   16920:                        case 0x47: msdos_int_21h_47h(0); break;
                   16921:                        case 0x48: msdos_int_21h_48h(); break;
                   16922:                        case 0x49: msdos_int_21h_49h(); break;
                   16923:                        case 0x4a: msdos_int_21h_4ah(); break;
                   16924:                        case 0x4b: msdos_int_21h_4bh(); break;
                   16925:                        case 0x4c: msdos_int_21h_4ch(); break;
                   16926:                        case 0x4d: msdos_int_21h_4dh(); break;
                   16927:                        case 0x4e: msdos_int_21h_4eh(); break;
                   16928:                        case 0x4f: msdos_int_21h_4fh(); break;
                   16929:                        case 0x50: msdos_int_21h_50h(); break;
                   16930:                        case 0x51: msdos_int_21h_51h(); break;
                   16931:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     16932:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     16933:                        case 0x54: msdos_int_21h_54h(); break;
                   16934:                        case 0x55: msdos_int_21h_55h(); break;
                   16935:                        case 0x56: msdos_int_21h_56h(0); break;
                   16936:                        case 0x57: msdos_int_21h_57h(); break;
                   16937:                        case 0x58: msdos_int_21h_58h(); break;
                   16938:                        case 0x59: msdos_int_21h_59h(); break;
                   16939:                        case 0x5a: msdos_int_21h_5ah(); break;
                   16940:                        case 0x5b: msdos_int_21h_5bh(); break;
                   16941:                        case 0x5c: msdos_int_21h_5ch(); break;
                   16942:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     16943:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     16944:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     16945:                        case 0x60: msdos_int_21h_60h(0); break;
                   16946:                        case 0x61: msdos_int_21h_61h(); break;
                   16947:                        case 0x62: msdos_int_21h_62h(); break;
                   16948:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     16949:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     16950:                        case 0x65: msdos_int_21h_65h(); break;
                   16951:                        case 0x66: msdos_int_21h_66h(); break;
                   16952:                        case 0x67: msdos_int_21h_67h(); break;
                   16953:                        case 0x68: msdos_int_21h_68h(); break;
                   16954:                        case 0x69: msdos_int_21h_69h(); break;
                   16955:                        case 0x6a: msdos_int_21h_6ah(); break;
                   16956:                        case 0x6b: msdos_int_21h_6bh(); break;
                   16957:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.48  root     16958:                        case 0x6d: // Find First ROM Program
                   16959:                        case 0x6e: // Find Next ROM Program
                   16960:                        case 0x6f: // Get/Set ROM Scan Start Address
                   16961:                                REG8(AL) = 0x00; // if not supported (DOS <5, MS-DOS 5+ non-ROM versions)
                   16962:                                break;
1.1.1.43  root     16963:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.48  root     16964:                        case 0x71: // Windows95 - Long Filename Functions
1.1.1.28  root     16965:                                switch(REG8(AL)) {
                   16966:                                case 0x0d: msdos_int_21h_710dh(); break;
                   16967:                                case 0x39: msdos_int_21h_39h(1); break;
                   16968:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   16969:                                case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.48  root     16970:                                case 0x41: msdos_int_21h_7141h(); break;
1.1.1.28  root     16971:                                case 0x43: msdos_int_21h_43h(1); break;
                   16972:                                case 0x47: msdos_int_21h_47h(1); break;
                   16973:                                case 0x4e: msdos_int_21h_714eh(); break;
                   16974:                                case 0x4f: msdos_int_21h_714fh(); break;
                   16975:                                case 0x56: msdos_int_21h_56h(1); break;
                   16976:                                case 0x60: msdos_int_21h_60h(1); break;
                   16977:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   16978:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   16979:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   16980:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   16981:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   16982:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45  root     16983:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     16984:                                case 0xaa: msdos_int_21h_71aah(); break;
                   16985:                                default:
                   16986:                                        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));
                   16987:                                        REG16(AX) = 0x7100;
                   16988:                                        m_CF = 1;
                   16989:                                        break;
                   16990:                                }
                   16991:                                break;
1.1.1.48  root     16992:                        case 0x72: // Windows95 beta - LFN FindClose
                   16993: //                             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));
                   16994:                                REG16(AX) = 0x7200;
                   16995:                                m_CF = 1;
                   16996:                                break;
                   16997:                        case 0x73: // Windows95 - FAT32 Functions
1.1.1.28  root     16998:                                switch(REG8(AL)) {
                   16999:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     17000:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     17001:                                case 0x02: msdos_int_21h_7302h(); break;
                   17002:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     17003:                                // 0x04: Set DPB to Use for Formatting
                   17004:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     17005:                                default:
                   17006:                                        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));
                   17007:                                        REG16(AX) = 0x7300;
                   17008:                                        m_CF = 1;
                   17009:                                        break;
                   17010:                                }
1.1       root     17011:                                break;
1.1.1.30  root     17012:                        case 0xdb: msdos_int_21h_dbh(); break;
                   17013:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     17014:                        default:
1.1.1.22  root     17015:                                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     17016:                                REG16(AX) = 0x01;
1.1.1.3   root     17017:                                m_CF = 1;
1.1       root     17018:                                break;
                   17019:                        }
1.1.1.28  root     17020:                } catch(int error) {
                   17021:                        REG16(AX) = error;
                   17022:                        m_CF = 1;
                   17023:                } catch(...) {
                   17024:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     17025:                        m_CF = 1;
1.1       root     17026:                }
1.1.1.3   root     17027:                if(m_CF) {
1.1.1.23  root     17028:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     17029:                        sda->int21h_5d0ah_called = 0;
1.1.1.23  root     17030:                        sda->extended_error_code = REG16(AX);
                   17031:                        switch(sda->extended_error_code) {
                   17032:                        case  4: // Too many open files
                   17033:                        case  8: // Insufficient memory
                   17034:                                sda->error_class = 1; // Out of resource
                   17035:                                break;
                   17036:                        case  5: // Access denied
                   17037:                                sda->error_class = 3; // Authorization
                   17038:                                break;
                   17039:                        case  7: // Memory control block destroyed
                   17040:                                sda->error_class = 4; // Internal
                   17041:                                break;
                   17042:                        case  2: // File not found
                   17043:                        case  3: // Path not found
                   17044:                        case 15: // Invaid drive specified
                   17045:                        case 18: // No more files
                   17046:                                sda->error_class = 8; // Not found
                   17047:                                break;
                   17048:                        case 32: // Sharing violation
                   17049:                        case 33: // Lock violation
                   17050:                                sda->error_class = 10; // Locked
                   17051:                                break;
                   17052: //                     case 16: // Removal of current directory attempted
                   17053:                        case 19: // Attempted write on protected disk
                   17054:                        case 21: // Drive not ready
                   17055: //                     case 29: // Write failure
                   17056: //                     case 30: // Read failure
                   17057: //                     case 82: // Cannot create subdirectory
                   17058:                                sda->error_class = 11; // Media
                   17059:                                break;
                   17060:                        case 80: // File already exists
                   17061:                                sda->error_class = 12; // Already exist
                   17062:                                break;
                   17063:                        default:
                   17064:                                sda->error_class = 13; // Unknown
                   17065:                                break;
                   17066:                        }
                   17067:                        sda->suggested_action = 1; // Retry
                   17068:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     17069:                }
1.1.1.33  root     17070:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     17071:                        // raise int 23h
                   17072: #if defined(HAS_I386)
                   17073:                        m_ext = 0; // not an external interrupt
                   17074:                        i386_trap(0x23, 1, 0);
                   17075:                        m_ext = 1;
                   17076: #else
                   17077:                        PREFIX86(_interrupt)(0x23);
                   17078: #endif
                   17079:                }
1.1       root     17080:                break;
                   17081:        case 0x22:
                   17082:                fatalerror("int 22h (terminate address)\n");
                   17083:        case 0x23:
1.1.1.28  root     17084:                try {
                   17085:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   17086:                } catch(...) {
                   17087:                        fatalerror("failed to terminate the current process by int 23h\n");
                   17088:                }
1.1       root     17089:                break;
                   17090:        case 0x24:
1.1.1.32  root     17091: /*
1.1.1.28  root     17092:                try {
                   17093:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17094:                } catch(...) {
                   17095:                        fatalerror("failed to terminate the current process by int 24h\n");
                   17096:                }
1.1.1.32  root     17097: */
                   17098:                msdos_int_24h();
1.1       root     17099:                break;
                   17100:        case 0x25:
                   17101:                msdos_int_25h();
                   17102:                break;
                   17103:        case 0x26:
                   17104:                msdos_int_26h();
                   17105:                break;
                   17106:        case 0x27:
1.1.1.28  root     17107:                try {
                   17108:                        msdos_int_27h();
                   17109:                } catch(...) {
                   17110:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   17111:                }
1.1       root     17112:                break;
                   17113:        case 0x28:
                   17114:                Sleep(10);
1.1.1.35  root     17115:                REQUEST_HARDWRE_UPDATE();
1.1       root     17116:                break;
                   17117:        case 0x29:
                   17118:                msdos_int_29h();
                   17119:                break;
                   17120:        case 0x2e:
                   17121:                msdos_int_2eh();
                   17122:                break;
                   17123:        case 0x2f:
                   17124:                // multiplex interrupt
                   17125:                switch(REG8(AH)) {
1.1.1.22  root     17126:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     17127:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     17128:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     17129:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     17130:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     17131:                case 0x14: msdos_int_2fh_14h(); break;
                   17132:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     17133:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     17134:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     17135:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     17136:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     17137:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     17138:                case 0x46: msdos_int_2fh_46h(); break;
                   17139:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     17140:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     17141:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     17142:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     17143:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     17144:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     17145:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     17146:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     17147:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     17148:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     17149:                default:
1.1.1.30  root     17150:                        switch(REG8(AL)) {
                   17151:                        case 0x00:
                   17152:                                // This is not installed
                   17153: //                             REG8(AL) = 0x00;
                   17154:                                break;
1.1.1.33  root     17155:                        case 0x01:
1.1.1.42  root     17156:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   17157:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   17158:                                        break;
                   17159:                                }
1.1.1.33  root     17160:                                // Banyan VINES v4+ is not installed
                   17161:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   17162:                                        break;
                   17163:                                }
1.1.1.42  root     17164:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   17165:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   17166:                                        break;
                   17167:                                }
1.1.1.30  root     17168:                        default:
1.1.1.42  root     17169:                                // NORTON UTILITIES 5.0+
                   17170:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   17171:                                        break;
                   17172:                                }
1.1.1.30  root     17173:                                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     17174:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     17175:                                m_CF = 1;
                   17176:                                break;
                   17177:                        }
                   17178:                        break;
1.1       root     17179:                }
                   17180:                break;
1.1.1.24  root     17181:        case 0x33:
                   17182:                switch(REG8(AH)) {
                   17183:                case 0x00:
                   17184:                        // Mouse
1.1.1.49  root     17185:                        switch(REG16(AX)) {
                   17186:                        case 0x0000: msdos_int_33h_0000h(); break;
                   17187:                        case 0x0001: msdos_int_33h_0001h(); break;
                   17188:                        case 0x0002: msdos_int_33h_0002h(); break;
                   17189:                        case 0x0003: msdos_int_33h_0003h(); break;
                   17190:                        case 0x0004: msdos_int_33h_0004h(); break;
                   17191:                        case 0x0005: msdos_int_33h_0005h(); break;
                   17192:                        case 0x0006: msdos_int_33h_0006h(); break;
                   17193:                        case 0x0007: msdos_int_33h_0007h(); break;
                   17194:                        case 0x0008: msdos_int_33h_0008h(); break;
                   17195:                        case 0x0009: msdos_int_33h_0009h(); break;
                   17196:                        case 0x000a: msdos_int_33h_000ah(); break;
                   17197:                        case 0x000b: msdos_int_33h_000bh(); break;
                   17198:                        case 0x000c: msdos_int_33h_000ch(); break;
                   17199:                        case 0x000d: break; // MS MOUSE v1.0+ - Light Pen Emulation On
                   17200:                        case 0x000e: break; // MS MOUSE v1.0+ - Light Pen Emulation Off
                   17201:                        case 0x000f: msdos_int_33h_000fh(); break;
                   17202:                        case 0x0010: break; // MS MOUSE v1.0+ - Define Screen Region for Updating
                   17203:                        case 0x0011: msdos_int_33h_0011h(); break;
                   17204:                        case 0x0012: REG16(AX) = 0xffff; break; // MS MOUSE - Set Large Graphics Cursor Block
                   17205:                        case 0x0013: break; // MS MOUSE v5.0+ - Define Double-Speed Threshold
                   17206:                        case 0x0014: msdos_int_33h_0014h(); break;
                   17207:                        case 0x0015: msdos_int_33h_0015h(); break;
                   17208:                        case 0x0016: msdos_int_33h_0016h(); break;
                   17209:                        case 0x0017: msdos_int_33h_0017h(); break;
                   17210:                        case 0x0018: msdos_int_33h_0018h(); break;
                   17211:                        case 0x0019: msdos_int_33h_0019h(); break;
                   17212:                        case 0x001a: msdos_int_33h_001ah(); break;
                   17213:                        case 0x001b: msdos_int_33h_001bh(); break;
                   17214:                        case 0x001c: break; // MS MOUSE v6.0+ - Set Interrupt Rate
                   17215:                        case 0x001d: msdos_int_33h_001dh(); break;
                   17216:                        case 0x001e: msdos_int_33h_001eh(); break;
                   17217:                        case 0x001f: msdos_int_33h_001fh(); break;
                   17218:                        case 0x0020: msdos_int_33h_0020h(); break;
                   17219:                        case 0x0021: msdos_int_33h_0021h(); break;
                   17220:                        case 0x0022: msdos_int_33h_0022h(); break;
                   17221:                        case 0x0023: msdos_int_33h_0023h(); break;
                   17222:                        case 0x0024: msdos_int_33h_0024h(); break;
                   17223:                        case 0x0025: msdos_int_33h_0025h(); break;
                   17224:                        case 0x0026: msdos_int_33h_0026h(); break;
                   17225:                        case 0x0027: msdos_int_33h_0027h(); break;
                   17226:                        case 0x0028: msdos_int_33h_0028h(); break;
                   17227:                        case 0x0029: msdos_int_33h_0029h(); break;
                   17228:                        case 0x002a: msdos_int_33h_002ah(); break;
                   17229:                        // 0x002b: MS MOUSE v7.0+ - Load Acceleration Profiles
                   17230:                        // 0x002c: MS MOUSE v7.0+ - Get Acceleration Profiles
                   17231:                        // 0x002d: MS MOUSE v7.0+ - Select Acceleration Profile
                   17232:                        // 0x002e: MS MOUSE v8.10+ - Set Acceleration Profile Names
                   17233:                        case 0x002f: break; // Mouse Hardware Reset
                   17234:                        // 0x0030: MS MOUSE v7.04+ - Get/Set BallPoint Information
                   17235:                        case 0x0031: msdos_int_33h_0031h(); break;
                   17236:                        case 0x0032: msdos_int_33h_0032h(); break;
                   17237:                        // 0x0033: MS MOUSE v7.05+ - Get Switch Settings And Acceleration Profile Data
                   17238:                        // 0x0034: MS MOUSE v8.0+ - Get Initialization File
                   17239:                        // 0x0035: MS MOUSE v8.10+ - LCD Screen Large Pointer Support
                   17240:                        case 0x004d: msdos_int_33h_004dh(); break;
                   17241:                        case 0x006d: msdos_int_33h_006dh(); break;
1.1.1.24  root     17242:                        default:
                   17243:                                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));
                   17244:                                break;
                   17245:                        }
                   17246:                        break;
                   17247:                default:
                   17248:                        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));
                   17249:                        break;
                   17250:                }
                   17251:                break;
1.1.1.50  root     17252: /*
                   17253:        case 0x67:
                   17254:                // int 67h handler is in EMS device driver (EMMXXXX0) and it calls int 68h
                   17255:                // NOTE: some softwares get address of int 67h handler and recognize the address is in EMS device driver
                   17256:                break;
                   17257: */
1.1.1.19  root     17258:        case 0x68:
                   17259:                // dummy interrupt for EMS (int 67h)
                   17260:                switch(REG8(AH)) {
                   17261:                case 0x40: msdos_int_67h_40h(); break;
                   17262:                case 0x41: msdos_int_67h_41h(); break;
                   17263:                case 0x42: msdos_int_67h_42h(); break;
                   17264:                case 0x43: msdos_int_67h_43h(); break;
                   17265:                case 0x44: msdos_int_67h_44h(); break;
                   17266:                case 0x45: msdos_int_67h_45h(); break;
                   17267:                case 0x46: msdos_int_67h_46h(); break;
                   17268:                case 0x47: msdos_int_67h_47h(); break;
                   17269:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     17270:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   17271:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     17272:                case 0x4b: msdos_int_67h_4bh(); break;
                   17273:                case 0x4c: msdos_int_67h_4ch(); break;
                   17274:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     17275:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     17276:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     17277:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     17278:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     17279:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     17280:                case 0x53: msdos_int_67h_53h(); break;
                   17281:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.49  root     17282:                case 0x55: msdos_int_67h_55h(); break;
                   17283:                case 0x56: msdos_int_67h_56h(); break;
1.1.1.20  root     17284:                case 0x57: msdos_int_67h_57h(); break;
                   17285:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     17286:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     17287:                case 0x5a: msdos_int_67h_5ah(); break;
1.1.1.49  root     17288:                case 0x5b: msdos_int_67h_5bh(); break;
1.1.1.43  root     17289:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   17290:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.49  root     17291:                case 0x70: msdos_int_67h_70h(); break;
1.1.1.31  root     17292:                // 0xde: VCPI
1.1.1.30  root     17293:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     17294:                default:
1.1.1.22  root     17295:                        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     17296:                        REG8(AH) = 0x84;
                   17297:                        break;
                   17298:                }
                   17299:                break;
                   17300: #ifdef SUPPORT_XMS
                   17301:        case 0x69:
                   17302:                // dummy interrupt for XMS (call far)
1.1.1.28  root     17303:                try {
                   17304:                        switch(REG8(AH)) {
                   17305:                        case 0x00: msdos_call_xms_00h(); break;
                   17306:                        case 0x01: msdos_call_xms_01h(); break;
                   17307:                        case 0x02: msdos_call_xms_02h(); break;
                   17308:                        case 0x03: msdos_call_xms_03h(); break;
                   17309:                        case 0x04: msdos_call_xms_04h(); break;
                   17310:                        case 0x05: msdos_call_xms_05h(); break;
                   17311:                        case 0x06: msdos_call_xms_06h(); break;
                   17312:                        case 0x07: msdos_call_xms_07h(); break;
                   17313:                        case 0x08: msdos_call_xms_08h(); break;
                   17314:                        case 0x09: msdos_call_xms_09h(); break;
                   17315:                        case 0x0a: msdos_call_xms_0ah(); break;
                   17316:                        case 0x0b: msdos_call_xms_0bh(); break;
                   17317:                        case 0x0c: msdos_call_xms_0ch(); break;
                   17318:                        case 0x0d: msdos_call_xms_0dh(); break;
                   17319:                        case 0x0e: msdos_call_xms_0eh(); break;
                   17320:                        case 0x0f: msdos_call_xms_0fh(); break;
                   17321:                        case 0x10: msdos_call_xms_10h(); break;
                   17322:                        case 0x11: msdos_call_xms_11h(); break;
                   17323:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     17324: #if defined(HAS_I386)
                   17325:                        case 0x88: msdos_call_xms_88h(); break;
                   17326:                        case 0x89: msdos_call_xms_89h(); break;
                   17327:                        case 0x8e: msdos_call_xms_8eh(); break;
                   17328:                        case 0x8f: msdos_call_xms_8fh(); break;
                   17329: #endif
1.1.1.28  root     17330:                        default:
                   17331:                                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));
                   17332:                                REG16(AX) = 0x0000;
                   17333:                                REG8(BL) = 0x80; // function not implemented
                   17334:                                break;
                   17335:                        }
                   17336:                } catch(...) {
1.1.1.19  root     17337:                        REG16(AX) = 0x0000;
1.1.1.28  root     17338:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     17339:                }
                   17340:                break;
                   17341: #endif
                   17342:        case 0x6a:
1.1.1.24  root     17343:                // irq12 (mouse)
                   17344:                mouse_push_ax = REG16(AX);
                   17345:                mouse_push_bx = REG16(BX);
                   17346:                mouse_push_cx = REG16(CX);
                   17347:                mouse_push_dx = REG16(DX);
                   17348:                mouse_push_si = REG16(SI);
                   17349:                mouse_push_di = REG16(DI);
                   17350:                
1.1.1.43  root     17351:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     17352:                        REG16(AX) = mouse.status_irq;
                   17353:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     17354:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17355:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     17356:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17357:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17358:                        
1.1.1.49  root     17359:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17360:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr.w.l & 0xff;
                   17361:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr.w.l >> 8;
                   17362:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr.w.h & 0xff;
                   17363:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.43  root     17364:                        break;
1.1.1.24  root     17365:                }
1.1.1.43  root     17366:                for(int i = 0; i < 8; i++) {
                   17367:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17368:                                REG16(AX) = mouse.status_irq_alt;
                   17369:                                REG16(BX) = mouse.get_buttons();
                   17370:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17371:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   17372:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17373:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17374:                                
1.1.1.49  root     17375:                                mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17376:                                mem[DUMMY_TOP + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   17377:                                mem[DUMMY_TOP + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   17378:                                mem[DUMMY_TOP + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   17379:                                mem[DUMMY_TOP + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
1.1.1.43  root     17380:                                break;
                   17381:                        }
                   17382:                }
1.1.1.54  root     17383:                if(mouse.status_irq_ps2 && mouse.call_addr_ps2.dw) {
                   17384:                        UINT16 data_1st, data_2nd, data_3rd;
                   17385:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   17386:                        i386_push16(data_1st);
                   17387:                        i386_push16(data_2nd);
                   17388:                        i386_push16(data_3rd);
                   17389:                        i386_push16(0x0000);
                   17390:                        
                   17391:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17392:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr_ps2.w.l & 0xff;
                   17393:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr_ps2.w.l >> 8;
                   17394:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr_ps2.w.h & 0xff;
                   17395:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr_ps2.w.h >> 8;
                   17396:                        break;
                   17397:                }
1.1.1.43  root     17398:                // invalid call addr :-(
1.1.1.49  root     17399:                mem[DUMMY_TOP + 0x02] = 0x90;   // nop
                   17400:                mem[DUMMY_TOP + 0x03] = 0x90;   // nop
                   17401:                mem[DUMMY_TOP + 0x04] = 0x90;   // nop
                   17402:                mem[DUMMY_TOP + 0x05] = 0x90;   // nop
                   17403:                mem[DUMMY_TOP + 0x06] = 0x90;   // nop
1.1.1.24  root     17404:                break;
                   17405:        case 0x6b:
                   17406:                // end of irq12 (mouse)
                   17407:                REG16(AX) = mouse_push_ax;
                   17408:                REG16(BX) = mouse_push_bx;
                   17409:                REG16(CX) = mouse_push_cx;
                   17410:                REG16(DX) = mouse_push_dx;
                   17411:                REG16(SI) = mouse_push_si;
                   17412:                REG16(DI) = mouse_push_di;
                   17413:                
                   17414:                // EOI
                   17415:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   17416:                        pic[0].isr &= ~(1 << 2); // master
                   17417:                }
                   17418:                pic_update();
                   17419:                break;
                   17420:        case 0x6c:
1.1.1.19  root     17421:                // dummy interrupt for case map routine pointed in the country info
                   17422:                if(REG8(AL) >= 0x80) {
                   17423:                        char tmp[2] = {0};
                   17424:                        tmp[0] = REG8(AL);
                   17425:                        my_strupr(tmp);
                   17426:                        REG8(AL) = tmp[0];
                   17427:                }
                   17428:                break;
1.1.1.27  root     17429:        case 0x6d:
                   17430:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   17431:                REG8(AL) = 0x86; // not supported
                   17432:                m_CF = 1;
                   17433:                break;
1.1.1.32  root     17434:        case 0x6e:
                   17435:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   17436:                {
                   17437:                        UINT16 code = REG16(AX);
                   17438:                        if(code & 0xf0) {
                   17439:                                code = (code & 7) | ((code & 0x10) >> 1);
                   17440:                        }
                   17441:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   17442:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   17443:                                        const char *message = NULL;
                   17444:                                        if(active_code_page == 932) {
                   17445:                                                message = param_error_table[i].message_japanese;
                   17446:                                        }
                   17447:                                        if(message == NULL) {
                   17448:                                                message = param_error_table[i].message_english;
                   17449:                                        }
                   17450:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   17451:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   17452:                                        
                   17453:                                        SREG(ES) = WORK_TOP >> 4;
                   17454:                                        i386_load_segment_descriptor(ES);
                   17455:                                        REG16(DI) = 0x0000;
                   17456:                                        break;
                   17457:                                }
                   17458:                        }
                   17459:                }
                   17460:                break;
1.1.1.49  root     17461:        case 0x6f:
                   17462:                // dummy interrupt for end of alter page map and call
                   17463:                {
                   17464:                        UINT16 handles[4], pages[4];
                   17465:                        
                   17466:                        // pop old mapping data in new mapping
                   17467:                        for(int i = 0; i < 4; i++) {
                   17468:                                pages  [3 - i] = i386_pop16();
                   17469:                                handles[3 - i] = i386_pop16();
                   17470:                        }
                   17471:                        
                   17472:                        // restore old mapping
                   17473:                        for(int i = 0; i < 4; i++) {
                   17474:                                UINT16 handle = handles[i];
                   17475:                                UINT16 page   = pages  [i];
                   17476:                                
                   17477:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   17478:                                        ems_map_page(i, handle, page);
                   17479:                                } else {
                   17480:                                        ems_unmap_page(i);
                   17481:                                }
                   17482:                        }
                   17483:                        // do ret_far (pop cs/ip) in old mapping
                   17484:                }
                   17485:                break;
1.1.1.8   root     17486:        case 0x70:
                   17487:        case 0x71:
                   17488:        case 0x72:
                   17489:        case 0x73:
                   17490:        case 0x74:
                   17491:        case 0x75:
                   17492:        case 0x76:
                   17493:        case 0x77:
                   17494:                // EOI
                   17495:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   17496:                        pic[0].isr &= ~(1 << 2); // master
                   17497:                }
                   17498:                pic_update();
                   17499:                break;
1.1       root     17500:        default:
1.1.1.22  root     17501: //             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     17502:                break;
                   17503:        }
                   17504:        
                   17505:        // update cursor position
                   17506:        if(cursor_moved) {
1.1.1.36  root     17507:                pcbios_update_cursor_position();
1.1       root     17508:                cursor_moved = false;
                   17509:        }
                   17510: }
                   17511: 
                   17512: // init
                   17513: 
                   17514: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   17515: {
                   17516:        // init file handler
                   17517:        memset(file_handler, 0, sizeof(file_handler));
                   17518:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   17519:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   17520:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     17521: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     17522:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     17523: #else
                   17524:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   17525: #endif
                   17526:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     17527:        }
1.1.1.21  root     17528:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45  root     17529: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   17530:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     17531:        }
1.1       root     17532:        _dup2(0, DUP_STDIN);
                   17533:        _dup2(1, DUP_STDOUT);
                   17534:        _dup2(2, DUP_STDERR);
1.1.1.21  root     17535:        _dup2(3, DUP_STDAUX);
                   17536:        _dup2(4, DUP_STDPRN);
1.1       root     17537:        
1.1.1.24  root     17538:        // init mouse
                   17539:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     17540:        mouse.enabled = true;   // from DOSBox
                   17541:        mouse.hidden = 1;       // hidden in default ???
                   17542:        mouse.old_hidden = 1;   // from DOSBox
                   17543:        mouse.max_position.x = 8 * (scr_width  - 1);
                   17544:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     17545:        mouse.mickey.x = 8;
                   17546:        mouse.mickey.y = 16;
                   17547:        
1.1.1.26  root     17548: #ifdef SUPPORT_XMS
                   17549:        // init xms
                   17550:        msdos_xms_init();
                   17551: #endif
                   17552:        
1.1       root     17553:        // init process
                   17554:        memset(process, 0, sizeof(process));
                   17555:        
1.1.1.13  root     17556:        // init dtainfo
                   17557:        msdos_dta_info_init();
                   17558:        
1.1       root     17559:        // init memory
                   17560:        memset(mem, 0, sizeof(mem));
                   17561:        
                   17562:        // bios data area
1.1.1.23  root     17563:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     17564:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   17565:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     17566:        CONSOLE_FONT_INFO cfi;
1.1.1.57! root     17567: //     GetCurrentConsoleFont(hStdout, FALSE, &cfi);
        !          17568:        cfi.dwFontSize.Y = 18;
        !          17569:        
        !          17570:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
        !          17571:        if(hLibrary) {
        !          17572:                typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
        !          17573:                GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
        !          17574:                if(lpfnGetCurrentConsoleFont) {
        !          17575:                        lpfnGetCurrentConsoleFont(hStdout, FALSE, &cfi);
        !          17576:                }
        !          17577:                FreeLibrary(hLibrary);
        !          17578:        }
1.1.1.14  root     17579:        
                   17580:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   17581:        text_vram_top_address = TEXT_VRAM_TOP;
                   17582:        text_vram_end_address = text_vram_top_address + regen;
                   17583:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   17584:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     17585:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1.1.14  root     17586:        
                   17587:        if(regen > 0x4000) {
                   17588:                regen = 0x8000;
                   17589:                vram_pages = 1;
                   17590:        } else if(regen > 0x2000) {
                   17591:                regen = 0x4000;
                   17592:                vram_pages = 2;
                   17593:        } else if(regen > 0x1000) {
                   17594:                regen = 0x2000;
                   17595:                vram_pages = 4;
                   17596:        } else {
                   17597:                regen = 0x1000;
                   17598:                vram_pages = 8;
                   17599:        }
1.1       root     17600:        
1.1.1.25  root     17601:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   17602:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     17603:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   17604:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     17605:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     17606:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   17607:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     17608: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17609:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     17610: #endif
1.1.1.26  root     17611:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     17612:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     17613:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   17614:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     17615:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     17616:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   17617:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     17618:        *(UINT16 *)(mem + 0x44e) = 0;
                   17619:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     17620:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     17621:        *(UINT8  *)(mem + 0x460) = 7;
                   17622:        *(UINT8  *)(mem + 0x461) = 7;
                   17623:        *(UINT8  *)(mem + 0x462) = 0;
                   17624:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     17625:        *(UINT8  *)(mem + 0x465) = 0x09;
                   17626:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     17627:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     17628:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   17629:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     17630:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
1.1.1.57! root     17631:        *(UINT16 *)(mem + 0x485) = cfi.dwFontSize.Y;
1.1.1.14  root     17632:        *(UINT8  *)(mem + 0x487) = 0x60;
                   17633:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     17634: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17635:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     17636: #endif
1.1.1.14  root     17637:        
                   17638:        // initial screen
                   17639:        SMALL_RECT rect;
                   17640:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   17641:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   17642:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   17643:                for(int x = 0; x < scr_width; x++) {
                   17644:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   17645:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   17646:                }
                   17647:        }
1.1       root     17648:        
1.1.1.19  root     17649:        // init mcb
1.1       root     17650:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     17651:        
                   17652:        // iret table
                   17653:        // note: int 2eh vector should address the routine in command.com,
                   17654:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   17655:        // so move iret table into allocated memory block
                   17656:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33  root     17657:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19  root     17658:        IRET_TOP = seg << 4;
                   17659:        seg += IRET_SIZE >> 4;
1.1.1.25  root     17660:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     17661:        
                   17662:        // dummy xms/ems device
1.1.1.33  root     17663:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     17664:        XMS_TOP = seg << 4;
                   17665:        seg += XMS_SIZE >> 4;
                   17666:        
                   17667:        // environment
1.1.1.33  root     17668:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     17669:        int env_seg = seg;
                   17670:        int ofs = 0;
1.1.1.32  root     17671:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   17672:        char comspec_added = 0;
1.1.1.33  root     17673:        char lastdrive_added = 0;
1.1.1.32  root     17674:        char env_msdos_path[ENV_SIZE] = {0};
                   17675:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     17676:        char prompt_added = 0;
1.1.1.32  root     17677:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     17678:        char tz_added = 0;
1.1.1.45  root     17679:        const char *path, *short_path;
1.1.1.32  root     17680:        
                   17681:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   17682:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17683:                        strcpy(env_append, short_path);
                   17684:                }
                   17685:        }
                   17686:        if((path = getenv("APPEND")) != NULL) {
                   17687:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17688:                        if(env_append[0] != '\0') {
                   17689:                                strcat(env_append, ";");
                   17690:                        }
                   17691:                        strcat(env_append, short_path);
                   17692:                }
                   17693:        }
                   17694:        
                   17695:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   17696:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17697:                        strcpy(comspec_path, short_path);
                   17698:                }
                   17699:        }
                   17700:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   17701:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17702:                        strcpy(comspec_path, short_path);
                   17703:                }
                   17704:        }
1.1       root     17705:        
1.1.1.28  root     17706:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     17707:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17708:                        strcpy(env_msdos_path, short_path);
                   17709:                        strcpy(env_path, short_path);
1.1.1.14  root     17710:                }
                   17711:        }
1.1.1.28  root     17712:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     17713:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17714:                        if(env_path[0] != '\0') {
                   17715:                                strcat(env_path, ";");
                   17716:                        }
                   17717:                        strcat(env_path, short_path);
1.1.1.9   root     17718:                }
                   17719:        }
1.1.1.32  root     17720:        
                   17721:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   17722:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     17723:        }
1.1.1.32  root     17724:        for(int i = 0; i < 4; i++) {
                   17725:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   17726:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   17727:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17728:                                strcpy(env_temp, short_path);
                   17729:                                break;
                   17730:                        }
                   17731:                }
1.1.1.24  root     17732:        }
1.1.1.32  root     17733:        
1.1.1.9   root     17734:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     17735:                // lower to upper
1.1.1.28  root     17736:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     17737:                strcpy(tmp, *p);
                   17738:                for(int i = 0;; i++) {
                   17739:                        if(tmp[i] == '=') {
                   17740:                                tmp[i] = '\0';
                   17741:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     17742:                                my_strupr(name);
1.1       root     17743:                                tmp[i] = '=';
                   17744:                                break;
                   17745:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     17746:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     17747:                        }
                   17748:                }
1.1.1.33  root     17749:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   17750:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   17751:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     17752:                        // ignore non standard environments
                   17753:                } else {
1.1.1.33  root     17754:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     17755:                                if(env_append[0] != '\0') {
                   17756:                                        sprintf(tmp, "APPEND=%s", env_append);
                   17757:                                } else {
                   17758:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   17759:                                }
                   17760:                                append_added = 1;
                   17761:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     17762:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     17763:                                comspec_added = 1;
1.1.1.33  root     17764:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   17765:                                char *env = getenv("MSDOS_LASTDRIVE");
                   17766:                                if(env != NULL) {
                   17767:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   17768:                                }
                   17769:                                lastdrive_added = 1;
                   17770:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     17771:                                if(env_msdos_path[0] != '\0') {
                   17772:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   17773:                                } else {
                   17774:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   17775:                                }
1.1.1.33  root     17776:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     17777:                                if(env_path[0] != '\0') {
                   17778:                                        sprintf(tmp, "PATH=%s", env_path);
                   17779:                                } else {
                   17780:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   17781:                                }
1.1.1.32  root     17782:                                path_added = 1;
1.1.1.33  root     17783:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   17784:                                prompt_added = 1;
1.1.1.28  root     17785:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   17786:                                if(env_temp[0] != '\0') {
                   17787:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   17788:                                } else {
                   17789:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   17790:                                }
1.1.1.32  root     17791:                                temp_added = 1;
1.1.1.33  root     17792:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     17793:                                if(env_temp[0] != '\0') {
                   17794:                                        sprintf(tmp, "TMP=%s", env_temp);
                   17795:                                } else {
                   17796:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     17797:                                }
1.1.1.32  root     17798:                                tmp_added = 1;
1.1.1.33  root     17799:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   17800:                                char *env = getenv("MSDOS_TZ");
                   17801:                                if(env != NULL) {
                   17802:                                        sprintf(tmp, "TZ=%s", env);
                   17803:                                }
                   17804:                                tz_added = 1;
1.1       root     17805:                        }
                   17806:                        int len = strlen(tmp);
1.1.1.14  root     17807:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     17808:                                fatalerror("too many environments\n");
                   17809:                        }
                   17810:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   17811:                        ofs += len + 1;
                   17812:                }
                   17813:        }
1.1.1.32  root     17814:        if(!append_added && env_append[0] != '\0') {
                   17815:                #define SET_ENV(name, value) { \
                   17816:                        char tmp[ENV_SIZE]; \
                   17817:                        sprintf(tmp, "%s=%s", name, value); \
                   17818:                        int len = strlen(tmp); \
                   17819:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   17820:                                fatalerror("too many environments\n"); \
                   17821:                        } \
                   17822:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   17823:                        ofs += len + 1; \
                   17824:                }
                   17825:                SET_ENV("APPEND", env_append);
                   17826:        }
                   17827:        if(!comspec_added) {
                   17828:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   17829:        }
1.1.1.33  root     17830:        if(!lastdrive_added) {
                   17831:                SET_ENV("LASTDRIVE", "Z");
                   17832:        }
1.1.1.32  root     17833:        if(!path_added) {
                   17834:                SET_ENV("PATH", env_path);
                   17835:        }
1.1.1.33  root     17836:        if(!prompt_added) {
                   17837:                SET_ENV("PROMPT", "$P$G");
                   17838:        }
1.1.1.32  root     17839:        if(!temp_added) {
                   17840:                SET_ENV("TEMP", env_temp);
                   17841:        }
                   17842:        if(!tmp_added) {
                   17843:                SET_ENV("TMP", env_temp);
                   17844:        }
1.1.1.33  root     17845:        if(!tz_added) {
                   17846:                TIME_ZONE_INFORMATION tzi;
                   17847:                HKEY hKey, hSubKey;
                   17848:                char tzi_std_name[64];
                   17849:                char tz_std[8] = "GMT";
                   17850:                char tz_dlt[8] = "GST";
                   17851:                char tz_value[32];
                   17852:                
                   17853:                // timezone name from GetTimeZoneInformation may not be english
                   17854:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   17855:                setlocale(LC_CTYPE, "");
                   17856:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   17857:                
                   17858:                // get english timezone name from registry
                   17859:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   17860:                        for(DWORD i = 0; !tz_added; i++) {
                   17861:                                char reg_name[256], sub_key[1024], std_name[256];
                   17862:                                DWORD size;
                   17863:                                FILETIME ftTime;
                   17864:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   17865:                                
                   17866:                                if(result == ERROR_SUCCESS) {
                   17867:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   17868:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   17869:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   17870:                                                        // search english timezone name from table
1.1.1.37  root     17871:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     17872:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   17873:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   17874:                                                                                if(tz_table[j].std != NULL) {
                   17875:                                                                                        strcpy(tz_std, tz_table[j].std);
                   17876:                                                                                }
                   17877:                                                                                if(tz_table[j].dlt != NULL) {
                   17878:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   17879:                                                                                }
                   17880:                                                                                tz_added = 1;
                   17881:                                                                                break;
                   17882:                                                                        }
                   17883:                                                                }
                   17884:                                                        }
                   17885:                                                }
                   17886:                                                RegCloseKey(hSubKey);
                   17887:                                        }
                   17888:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   17889:                                        break;
                   17890:                                }
                   17891:                        }
                   17892:                        RegCloseKey(hKey);
                   17893:                }
                   17894:                if((tzi.Bias % 60) != 0) {
                   17895:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   17896:                } else {
                   17897:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   17898:                }
                   17899:                if(daylight) {
                   17900:                        strcat(tz_value, tz_dlt);
                   17901:                }
                   17902:                SET_ENV("TZ", tz_value);
                   17903:        }
1.1       root     17904:        seg += (ENV_SIZE >> 4);
                   17905:        
                   17906:        // psp
1.1.1.33  root     17907:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     17908:        current_psp = seg;
1.1.1.35  root     17909:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   17910:        psp->parent_psp = current_psp;
1.1       root     17911:        seg += (PSP_SIZE >> 4);
                   17912:        
1.1.1.19  root     17913:        // first free mcb in conventional memory
1.1.1.33  root     17914:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     17915:        first_mcb = seg;
                   17916:        
1.1.1.19  root     17917:        // dummy mcb to link to umb
1.1.1.33  root     17918: #if 0
1.1.1.39  root     17919:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     17920: #else
1.1.1.39  root     17921:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     17922: #endif
1.1.1.19  root     17923:        
                   17924:        // first free mcb in upper memory block
1.1.1.8   root     17925:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     17926:        
1.1.1.29  root     17927: #ifdef SUPPORT_HMA
                   17928:        // first free mcb in high memory area
                   17929:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   17930: #endif
                   17931:        
1.1.1.26  root     17932:        // interrupt vector
                   17933:        for(int i = 0; i < 0x80; i++) {
                   17934:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   17935:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   17936:        }
1.1.1.49  root     17937:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffc:0018 irq0 (system timer)
                   17938:        *(UINT16 *)(mem + 4 * 0x08 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     17939:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   17940:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   17941:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   17942:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
1.1.1.49  root     17943:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffc:0000 irq12 (mouse)
                   17944:        *(UINT16 *)(mem + 4 * 0x74 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     17945:        
1.1.1.29  root     17946:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     17947:        static const struct {
                   17948:                UINT16 attributes;
                   17949:                char *dev_name;
                   17950:        } dummy_devices[] = {
                   17951:                {0x8013, "CON     "},
                   17952:                {0x8000, "AUX     "},
                   17953:                {0xa0c0, "PRN     "},
                   17954:                {0x8008, "CLOCK$  "},
                   17955:                {0x8000, "COM1    "},
                   17956:                {0xa0c0, "LPT1    "},
                   17957:                {0xa0c0, "LPT2    "},
                   17958:                {0xa0c0, "LPT3    "},
                   17959:                {0x8000, "COM2    "},
                   17960:                {0x8000, "COM3    "},
                   17961:                {0x8000, "COM4    "},
1.1.1.30  root     17962: //             {0xc000, "CONFIG$ "},
                   17963:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     17964:        };
                   17965:        static const UINT8 dummy_device_routine[] = {
                   17966:                // from NUL device of Windows 98 SE
                   17967:                // or word ptr ES:[BX+03],0100
                   17968:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   17969:                // retf
                   17970:                0xcb,
                   17971:        };
1.1.1.29  root     17972:        device_t *last = NULL;
1.1.1.32  root     17973:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     17974:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     17975:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   17976:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     17977:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     17978:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   17979:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     17980:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     17981:                last = device;
                   17982:        }
                   17983:        if(last != NULL) {
                   17984:                last->next_driver.w.l = 0;
                   17985:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     17986:        }
1.1.1.29  root     17987:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     17988:        
1.1.1.25  root     17989:        // dos info
                   17990:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   17991:        dos_info->magic_word = 1;
                   17992:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   17993:        dos_info->first_dpb.w.l = 0;
                   17994:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   17995:        dos_info->first_sft.w.l = 0;
                   17996:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     17997:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     17998:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     17999:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     18000:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   18001:        dos_info->max_sector_len = 512;
                   18002:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   18003:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   18004:        dos_info->cds.w.l = 0;
                   18005:        dos_info->cds.w.h = CDS_TOP >> 4;
                   18006:        dos_info->fcb_table.w.l = 0;
                   18007:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   18008:        dos_info->last_drive = 'Z' - 'A' + 1;
                   18009:        dos_info->buffers_x = 20;
                   18010:        dos_info->buffers_y = 0;
                   18011:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     18012:        dos_info->nul_device.next_driver.w.l = 22;
                   18013:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     18014:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     18015:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   18016:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18017:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   18018:        dos_info->disk_buf_heads.w.l = 0;
                   18019:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     18020:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     18021:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   18022:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     18023:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     18024:        
                   18025:        char *env;
                   18026:        if((env = getenv("LASTDRIVE")) != NULL) {
                   18027:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18028:                        dos_info->last_drive = env[0] - 'A' + 1;
                   18029:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18030:                        dos_info->last_drive = env[0] - 'a' + 1;
                   18031:                }
                   18032:        }
                   18033:        if((env = getenv("windir")) != NULL) {
                   18034:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18035:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   18036:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18037:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   18038:                }
                   18039:        }
                   18040: #if defined(HAS_I386)
                   18041:        dos_info->i386_or_later = 1;
                   18042: #else
                   18043:        dos_info->i386_or_later = 0;
                   18044: #endif
                   18045:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   18046:        
1.1.1.27  root     18047:        // ems (int 67h) and xms
1.1.1.25  root     18048:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   18049:        xms_device->next_driver.w.l = 0xffff;
                   18050:        xms_device->next_driver.w.h = 0xffff;
                   18051:        xms_device->attributes = 0xc000;
1.1.1.29  root     18052:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   18053:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18054:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   18055:        
1.1.1.26  root     18056:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   18057:        mem[XMS_TOP + 0x13] = 0x68;
                   18058:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     18059: #ifdef SUPPORT_XMS
                   18060:        if(support_xms) {
1.1.1.26  root     18061:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   18062:                mem[XMS_TOP + 0x16] = 0x69;
                   18063:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     18064:        } else
                   18065: #endif
1.1.1.26  root     18066:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     18067:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     18068:        
1.1.1.26  root     18069:        // irq12 routine (mouse)
1.1.1.49  root     18070:        mem[DUMMY_TOP + 0x00] = 0xcd;   // int 6ah (dummy)
                   18071:        mem[DUMMY_TOP + 0x01] = 0x6a;
                   18072:        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far mouse
                   18073:        mem[DUMMY_TOP + 0x03] = 0xff;
                   18074:        mem[DUMMY_TOP + 0x04] = 0xff;
                   18075:        mem[DUMMY_TOP + 0x05] = 0xff;
                   18076:        mem[DUMMY_TOP + 0x06] = 0xff;
                   18077:        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   18078:        mem[DUMMY_TOP + 0x08] = 0x6b;
                   18079:        mem[DUMMY_TOP + 0x09] = 0xcf;   // iret
1.1.1.24  root     18080:        
1.1.1.27  root     18081:        // case map routine
1.1.1.49  root     18082:        mem[DUMMY_TOP + 0x0a] = 0xcd;   // int 6ch (dummy)
                   18083:        mem[DUMMY_TOP + 0x0b] = 0x6c;
                   18084:        mem[DUMMY_TOP + 0x0c] = 0xcb;   // retf
1.1.1.27  root     18085:        
                   18086:        // font read routine
1.1.1.49  root     18087:        mem[DUMMY_TOP + 0x0d] = 0xcd;   // int 6dh (dummy)
                   18088:        mem[DUMMY_TOP + 0x0e] = 0x6d;
                   18089:        mem[DUMMY_TOP + 0x0f] = 0xcb;   // retf
1.1.1.19  root     18090:        
1.1.1.32  root     18091:        // error message read routine
1.1.1.49  root     18092:        mem[DUMMY_TOP + 0x10] = 0xcd;   // int 6eh (dummy)
                   18093:        mem[DUMMY_TOP + 0x11] = 0x6e;
                   18094:        mem[DUMMY_TOP + 0x12] = 0xcb;   // retf
1.1.1.32  root     18095:        
1.1.1.35  root     18096:        // dummy loop to wait BIOS/DOS service is done
1.1.1.49  root     18097:        mem[DUMMY_TOP + 0x13] = 0xe6;   // out f7h, al
                   18098:        mem[DUMMY_TOP + 0x14] = 0xf7;
                   18099:        mem[DUMMY_TOP + 0x15] = 0x78;   // js/jns -4
                   18100:        mem[DUMMY_TOP + 0x16] = 0xfc;
                   18101:        mem[DUMMY_TOP + 0x17] = 0xcb;   // retf
1.1.1.35  root     18102:        
1.1.1.50  root     18103:        // irq0 routine (system timer)
1.1.1.49  root     18104:        mem[DUMMY_TOP + 0x18] = 0xcd;   // int 1ch
                   18105:        mem[DUMMY_TOP + 0x19] = 0x1c;
                   18106:        mem[DUMMY_TOP + 0x1a] = 0xea;   // jmp far (IRET_TOP >> 4):0008
                   18107:        mem[DUMMY_TOP + 0x1b] = 0x08;
                   18108:        mem[DUMMY_TOP + 0x1c] = 0x00;
                   18109:        mem[DUMMY_TOP + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   18110:        mem[DUMMY_TOP + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
                   18111:        
                   18112:        // alter page map and call routine
                   18113:        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   18114:        mem[DUMMY_TOP + 0x20] = 0xff;
                   18115:        mem[DUMMY_TOP + 0x21] = 0xff;
                   18116:        mem[DUMMY_TOP + 0x22] = 0xff;
                   18117:        mem[DUMMY_TOP + 0x23] = 0xff;
                   18118:        mem[DUMMY_TOP + 0x24] = 0xcd;   // int 6fh (dummy)
                   18119:        mem[DUMMY_TOP + 0x25] = 0x6f;
                   18120:        mem[DUMMY_TOP + 0x26] = 0xcb;   // retf
1.1.1.14  root     18121:        
1.1.1.50  root     18122:        // call int 29h routine
                   18123:        mem[DUMMY_TOP + 0x27] = 0xcd;   // int 29h
                   18124:        mem[DUMMY_TOP + 0x28] = 0x29;
                   18125:        mem[DUMMY_TOP + 0x29] = 0xcb;   // retf
                   18126:        
1.1.1.26  root     18127:        // boot routine
1.1.1.49  root     18128:        mem[0xffff0 + 0x00] = 0xf4;     // halt
                   18129:        mem[0xffff0 + 0x05] = '0';      // rom date
                   18130:        mem[0xffff0 + 0x06] = '2';
                   18131:        mem[0xffff0 + 0x07] = '/';
                   18132:        mem[0xffff0 + 0x08] = '2';
                   18133:        mem[0xffff0 + 0x09] = '2';
                   18134:        mem[0xffff0 + 0x0a] = '/';
                   18135:        mem[0xffff0 + 0x0b] = '0';
                   18136:        mem[0xffff0 + 0x0c] = '6';
                   18137:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id
                   18138:        mem[0xffff0 + 0x0f] = 0x00;
1.1.1.24  root     18139:        
1.1       root     18140:        // param block
                   18141:        // + 0: param block (22bytes)
                   18142:        // +24: fcb1/2 (20bytes)
                   18143:        // +44: command tail (128bytes)
                   18144:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   18145:        param->env_seg = 0;
                   18146:        param->cmd_line.w.l = 44;
                   18147:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   18148:        param->fcb1.w.l = 24;
                   18149:        param->fcb1.w.h = (WORK_TOP >> 4);
                   18150:        param->fcb2.w.l = 24;
                   18151:        param->fcb2.w.h = (WORK_TOP >> 4);
                   18152:        
                   18153:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   18154:        
                   18155:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   18156:        if(argc > 1) {
                   18157:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   18158:                for(int i = 2; i < argc; i++) {
                   18159:                        char tmp[128];
                   18160:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   18161:                        strcpy(cmd_line->cmd, tmp);
                   18162:                }
                   18163:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   18164:        } else {
                   18165:                cmd_line->len = 0;
                   18166:        }
                   18167:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   18168:        
                   18169:        // system file table
1.1.1.21  root     18170:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   18171:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     18172:        
1.1.1.19  root     18173:        // disk buffer header (from DOSBox)
                   18174:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   18175:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   18176:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   18177:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   18178:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   18179:        
1.1       root     18180:        // fcb table
                   18181:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     18182:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     18183:        
1.1.1.41  root     18184:        // drive parameter block
1.1.1.42  root     18185:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     18186:                // may be a floppy drive
1.1.1.44  root     18187:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   18188:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   18189:                cds->drive_attrib = 0x4000;     // physical drive
                   18190:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   18191:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   18192:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   18193:                cds->bs_offset = 2;
                   18194:                
1.1.1.41  root     18195:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   18196:                dpb->drive_num = i;
                   18197:                dpb->unit_num = i;
1.1.1.43  root     18198:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   18199:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     18200:        }
                   18201:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     18202:                msdos_cds_update(i);
1.1.1.42  root     18203:                UINT16 seg, ofs;
                   18204:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     18205:        }
                   18206:        
1.1.1.17  root     18207:        // nls stuff
                   18208:        msdos_nls_tables_init();
1.1       root     18209:        
                   18210:        // execute command
1.1.1.28  root     18211:        try {
1.1.1.52  root     18212:                if(msdos_process_exec(argv[0], param, 0, true)) {
1.1.1.28  root     18213:                        fatalerror("'%s' not found\n", argv[0]);
                   18214:                }
                   18215:        } catch(...) {
                   18216:                // we should not reach here :-(
                   18217:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     18218:        }
                   18219:        retval = 0;
                   18220:        return(0);
                   18221: }
                   18222: 
                   18223: #define remove_std_file(path) { \
                   18224:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   18225:        if(fd != -1) { \
                   18226:                _lseek(fd, 0, SEEK_END); \
                   18227:                int size = _tell(fd); \
                   18228:                _close(fd); \
                   18229:                if(size == 0) { \
                   18230:                        remove(path); \
                   18231:                } \
                   18232:        } \
                   18233: }
                   18234: 
                   18235: void msdos_finish()
                   18236: {
                   18237:        for(int i = 0; i < MAX_FILES; i++) {
                   18238:                if(file_handler[i].valid) {
                   18239:                        _close(i);
                   18240:                }
                   18241:        }
1.1.1.21  root     18242: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     18243:        remove_std_file("stdaux.txt");
1.1.1.21  root     18244: #endif
1.1.1.30  root     18245: #ifdef SUPPORT_XMS
                   18246:        msdos_xms_finish();
                   18247: #endif
1.1       root     18248:        msdos_dbcs_table_finish();
                   18249: }
                   18250: 
                   18251: /* ----------------------------------------------------------------------------
                   18252:        PC/AT hardware emulation
                   18253: ---------------------------------------------------------------------------- */
                   18254: 
                   18255: void hardware_init()
                   18256: {
1.1.1.3   root     18257:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     18258:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     18259:        m_IF = 1;
1.1.1.3   root     18260: #if defined(HAS_I386)
1.1       root     18261:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   18262:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     18263: #endif
                   18264:        i386_set_a20_line(0);
1.1.1.14  root     18265:        
1.1.1.19  root     18266:        ems_init();
1.1.1.25  root     18267:        dma_init();
1.1       root     18268:        pic_init();
1.1.1.25  root     18269:        pio_init();
1.1.1.8   root     18270: #ifdef PIT_ALWAYS_RUNNING
                   18271:        pit_init();
                   18272: #else
1.1       root     18273:        pit_active = 0;
                   18274: #endif
1.1.1.25  root     18275:        sio_init();
1.1.1.8   root     18276:        cmos_init();
                   18277:        kbd_init();
1.1       root     18278: }
                   18279: 
1.1.1.10  root     18280: void hardware_finish()
                   18281: {
                   18282: #if defined(HAS_I386)
                   18283:        vtlb_free(m_vtlb);
                   18284: #endif
1.1.1.19  root     18285:        ems_finish();
1.1.1.37  root     18286:        pio_finish();
1.1.1.25  root     18287:        sio_finish();
1.1.1.10  root     18288: }
                   18289: 
1.1.1.28  root     18290: void hardware_release()
                   18291: {
                   18292:        // release hardware resources when this program will be terminated abnormally
                   18293: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18294:        if(fp_debug_log != NULL) {
                   18295:                fclose(fp_debug_log);
                   18296:                fp_debug_log = NULL;
1.1.1.28  root     18297:        }
                   18298: #endif
                   18299: #if defined(HAS_I386)
                   18300:        vtlb_free(m_vtlb);
                   18301: #endif
                   18302:        ems_release();
1.1.1.37  root     18303:        pio_release();
1.1.1.28  root     18304:        sio_release();
                   18305: }
                   18306: 
1.1       root     18307: void hardware_run()
                   18308: {
1.1.1.22  root     18309: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     18310:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     18311:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     18312: #endif
1.1.1.51  root     18313: #ifdef USE_DEBUGGER
                   18314:        m_int_num = -1;
                   18315: #endif
1.1.1.54  root     18316:        while(!m_exit) {
1.1.1.50  root     18317:                hardware_run_cpu();
1.1       root     18318:        }
1.1.1.22  root     18319: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18320:        if(fp_debug_log != NULL) {
                   18321:                fclose(fp_debug_log);
                   18322:                fp_debug_log = NULL;
1.1.1.28  root     18323:        }
1.1.1.22  root     18324: #endif
1.1       root     18325: }
                   18326: 
1.1.1.50  root     18327: inline void hardware_run_cpu()
                   18328: {
                   18329: #if defined(HAS_I386)
                   18330:        CPU_EXECUTE_CALL(i386);
                   18331:        if(m_eip != m_prev_eip) {
                   18332:                idle_ops++;
                   18333:        }
                   18334: #else
                   18335:        CPU_EXECUTE_CALL(CPU_MODEL);
                   18336:        if(m_pc != m_prevpc) {
                   18337:                idle_ops++;
                   18338:        }
                   18339: #endif
                   18340: #ifdef USE_DEBUGGER
                   18341:        // Disallow reentering CPU_EXECUTE() in msdos_syscall()
                   18342:        if(m_int_num >= 0) {
                   18343:                unsigned num = (unsigned)m_int_num;
                   18344:                m_int_num = -1;
                   18345:                msdos_syscall(num);
                   18346:        }
                   18347: #endif
                   18348:        if(++update_ops == UPDATE_OPS) {
                   18349:                update_ops = 0;
                   18350:                hardware_update();
                   18351:        }
                   18352: }
                   18353: 
1.1       root     18354: void hardware_update()
                   18355: {
1.1.1.8   root     18356:        static UINT32 prev_time = 0;
                   18357:        UINT32 cur_time = timeGetTime();
                   18358:        
                   18359:        if(prev_time != cur_time) {
                   18360:                // update pit and raise irq0
                   18361: #ifndef PIT_ALWAYS_RUNNING
                   18362:                if(pit_active)
                   18363: #endif
                   18364:                {
                   18365:                        if(pit_run(0, cur_time)) {
                   18366:                                pic_req(0, 0, 1);
                   18367:                        }
                   18368:                        pit_run(1, cur_time);
                   18369:                        pit_run(2, cur_time);
                   18370:                }
1.1.1.24  root     18371:                
1.1.1.25  root     18372:                // update sio and raise irq4/3
1.1.1.29  root     18373:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     18374:                        sio_update(c);
                   18375:                }
                   18376:                
1.1.1.24  root     18377:                // update keyboard and mouse
1.1.1.14  root     18378:                static UINT32 prev_tick = 0;
                   18379:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     18380:                
1.1.1.14  root     18381:                if(prev_tick != cur_tick) {
                   18382:                        // update keyboard flags
                   18383:                        UINT8 state;
1.1.1.24  root     18384:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   18385:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   18386:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   18387:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   18388:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   18389:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   18390:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   18391:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     18392:                        mem[0x417] = state;
                   18393:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   18394:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   18395:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   18396:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     18397: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   18398: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     18399:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   18400:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   18401:                        mem[0x418] = state;
                   18402:                        
1.1.1.24  root     18403:                        // update console input if needed
1.1.1.34  root     18404:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     18405:                                update_console_input();
                   18406:                        }
1.1.1.57! root     18407:                        if(!(kbd_status & 1)) {
        !          18408:                                if(key_buf_data != NULL) {
        !          18409: #ifdef USE_SERVICE_THREAD
        !          18410:                                        EnterCriticalSection(&key_buf_crit_sect);
        !          18411: #endif
        !          18412:                                        if(!key_buf_data->empty()) {
        !          18413:                                                kbd_data = key_buf_data->read();
        !          18414:                                                kbd_status |= 1;
        !          18415:                                                key_changed = true;
        !          18416:                                        }
        !          18417: #ifdef USE_SERVICE_THREAD
        !          18418:                                        LeaveCriticalSection(&key_buf_crit_sect);
        !          18419: #endif
        !          18420:                                }
        !          18421:                        }
1.1.1.24  root     18422:                        
1.1.1.57! root     18423:                        // raise irq1 if key is pressed/released or key buffer is not empty
1.1.1.56  root     18424:                        if(!key_changed) {
1.1.1.55  root     18425: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     18426:                                EnterCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     18427: #endif
1.1.1.57! root     18428:                                if(!pcbios_is_key_buffer_empty()) {
        !          18429: /*
        !          18430:                                        if(!(kbd_status & 1)) {
        !          18431:                                                UINT16 head = *(UINT16 *)(mem + 0x41a);
        !          18432:                                                UINT16 tail = *(UINT16 *)(mem + 0x41c);
        !          18433:                                                if(head != tail) {
        !          18434:                                                        int key_char = mem[0x400 + (head++)];
        !          18435:                                                        int key_scan = mem[0x400 + (head++)];
        !          18436:                                                        kbd_data = key_char ? key_char : key_scan;
        !          18437:                                                        kbd_status |= 1;
        !          18438:                                                }
        !          18439:                                        }
        !          18440: */
        !          18441:                                        key_changed = true;
        !          18442:                                }
1.1.1.55  root     18443: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     18444:                                LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     18445: #endif
1.1.1.56  root     18446:                        }
                   18447:                        if(key_changed) {
1.1.1.8   root     18448:                                pic_req(0, 1, 1);
1.1.1.56  root     18449:                                key_changed = false;
1.1.1.24  root     18450:                        }
                   18451:                        
                   18452:                        // raise irq12 if mouse status is changed
1.1.1.54  root     18453:                        if((mouse.status & 0x1f) && mouse.call_addr_ps2.dw) {
                   18454:                                mouse.status_irq = 0; // ???
                   18455:                                mouse.status_irq_alt = 0; // ???
                   18456:                                mouse.status_irq_ps2 = mouse.status & 0x1f;
                   18457:                                mouse.status = 0;
                   18458:                                pic_req(1, 4, 1);
                   18459:                        } else if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
1.1.1.43  root     18460:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   18461:                                mouse.status_irq_alt = 0; // ???
1.1.1.54  root     18462:                                mouse.status_irq_ps2 = 0; // ???
1.1.1.24  root     18463:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     18464:                                pic_req(1, 4, 1);
                   18465:                        } else {
                   18466:                                for(int i = 0; i < 8; i++) {
                   18467:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   18468:                                                mouse.status_irq = 0; // ???
                   18469:                                                mouse.status_irq_alt = 0;
1.1.1.54  root     18470:                                                mouse.status_irq_ps2 = 0; // ???
1.1.1.43  root     18471:                                                for(int j = 0; j < 8; j++) {
                   18472:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   18473:                                                                mouse.status_irq_alt |=  (1 << j);
                   18474:                                                                mouse.status_alt     &= ~(1 << j);
                   18475:                                                        }
                   18476:                                                }
                   18477:                                                pic_req(1, 4, 1);
                   18478:                                                break;
                   18479:                                        }
                   18480:                                }
1.1.1.8   root     18481:                        }
1.1.1.24  root     18482:                        
1.1.1.14  root     18483:                        prev_tick = cur_tick;
1.1.1.8   root     18484:                }
1.1.1.24  root     18485:                
1.1.1.19  root     18486:                // update daily timer counter
                   18487:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     18488:                
1.1.1.8   root     18489:                prev_time = cur_time;
1.1       root     18490:        }
                   18491: }
                   18492: 
1.1.1.19  root     18493: // ems
                   18494: 
                   18495: void ems_init()
                   18496: {
                   18497:        memset(ems_handles, 0, sizeof(ems_handles));
                   18498:        memset(ems_pages, 0, sizeof(ems_pages));
                   18499:        free_ems_pages = MAX_EMS_PAGES;
                   18500: }
                   18501: 
                   18502: void ems_finish()
                   18503: {
1.1.1.28  root     18504:        ems_release();
                   18505: }
                   18506: 
                   18507: void ems_release()
                   18508: {
1.1.1.31  root     18509:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     18510:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     18511:                        free(ems_handles[i].buffer);
                   18512:                        ems_handles[i].buffer = NULL;
                   18513:                }
                   18514:        }
                   18515: }
                   18516: 
                   18517: void ems_allocate_pages(int handle, int pages)
                   18518: {
                   18519:        if(pages > 0) {
                   18520:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18521:        } else {
                   18522:                ems_handles[handle].buffer = NULL;
                   18523:        }
                   18524:        ems_handles[handle].pages = pages;
                   18525:        ems_handles[handle].allocated = true;
                   18526:        free_ems_pages -= pages;
                   18527: }
                   18528: 
                   18529: void ems_reallocate_pages(int handle, int pages)
                   18530: {
                   18531:        if(ems_handles[handle].allocated) {
                   18532:                if(ems_handles[handle].pages != pages) {
                   18533:                        UINT8 *new_buffer = NULL;
                   18534:                        
                   18535:                        if(pages > 0) {
                   18536:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18537:                        }
1.1.1.32  root     18538:                        if(ems_handles[handle].buffer != NULL) {
                   18539:                                if(new_buffer != NULL) {
1.1.1.19  root     18540:                                        if(pages > ems_handles[handle].pages) {
                   18541:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   18542:                                        } else {
                   18543:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   18544:                                        }
                   18545:                                }
                   18546:                                free(ems_handles[handle].buffer);
                   18547:                                ems_handles[handle].buffer = NULL;
                   18548:                        }
                   18549:                        free_ems_pages += ems_handles[handle].pages;
                   18550:                        
                   18551:                        ems_handles[handle].buffer = new_buffer;
                   18552:                        ems_handles[handle].pages = pages;
                   18553:                        free_ems_pages -= pages;
                   18554:                }
                   18555:        } else {
                   18556:                ems_allocate_pages(handle, pages);
                   18557:        }
                   18558: }
                   18559: 
                   18560: void ems_release_pages(int handle)
                   18561: {
                   18562:        if(ems_handles[handle].allocated) {
1.1.1.32  root     18563:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     18564:                        free(ems_handles[handle].buffer);
                   18565:                        ems_handles[handle].buffer = NULL;
                   18566:                }
                   18567:                free_ems_pages += ems_handles[handle].pages;
                   18568:                ems_handles[handle].allocated = false;
                   18569:        }
                   18570: }
                   18571: 
                   18572: void ems_map_page(int physical, int handle, int logical)
                   18573: {
                   18574:        if(ems_pages[physical].mapped) {
                   18575:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   18576:                        return;
                   18577:                }
                   18578:                ems_unmap_page(physical);
                   18579:        }
1.1.1.32  root     18580:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18581:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   18582:        }
                   18583:        ems_pages[physical].handle = handle;
                   18584:        ems_pages[physical].page = logical;
                   18585:        ems_pages[physical].mapped = true;
                   18586: }
                   18587: 
                   18588: void ems_unmap_page(int physical)
                   18589: {
                   18590:        if(ems_pages[physical].mapped) {
                   18591:                int handle = ems_pages[physical].handle;
                   18592:                int logical = ems_pages[physical].page;
                   18593:                
1.1.1.32  root     18594:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18595:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   18596:                }
                   18597:                ems_pages[physical].mapped = false;
                   18598:        }
                   18599: }
                   18600: 
1.1.1.25  root     18601: // dma
1.1       root     18602: 
1.1.1.25  root     18603: void dma_init()
1.1       root     18604: {
1.1.1.26  root     18605:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     18606:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     18607: //             for(int ch = 0; ch < 4; ch++) {
                   18608: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   18609: //             }
1.1.1.25  root     18610:                dma_reset(c);
                   18611:        }
1.1       root     18612: }
                   18613: 
1.1.1.25  root     18614: void dma_reset(int c)
1.1       root     18615: {
1.1.1.25  root     18616:        dma[c].low_high = false;
                   18617:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   18618:        dma[c].mask = 0xff;
                   18619: }
                   18620: 
                   18621: void dma_write(int c, UINT32 addr, UINT8 data)
                   18622: {
                   18623:        int ch = (addr >> 1) & 3;
                   18624:        UINT8 bit = 1 << (data & 3);
                   18625:        
                   18626:        switch(addr & 0x0f) {
                   18627:        case 0x00: case 0x02: case 0x04: case 0x06:
                   18628:                if(dma[c].low_high) {
                   18629:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     18630:                } else {
1.1.1.25  root     18631:                        dma[c].ch[ch].bareg.b.l = data;
                   18632:                }
                   18633:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   18634:                dma[c].low_high = !dma[c].low_high;
                   18635:                break;
                   18636:        case 0x01: case 0x03: case 0x05: case 0x07:
                   18637:                if(dma[c].low_high) {
                   18638:                        dma[c].ch[ch].bcreg.b.h = data;
                   18639:                } else {
                   18640:                        dma[c].ch[ch].bcreg.b.l = data;
                   18641:                }
                   18642:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   18643:                dma[c].low_high = !dma[c].low_high;
                   18644:                break;
                   18645:        case 0x08:
                   18646:                // command register
                   18647:                dma[c].cmd = data;
                   18648:                break;
                   18649:        case 0x09:
                   18650:                // dma[c].request register
                   18651:                if(data & 4) {
                   18652:                        if(!(dma[c].req & bit)) {
                   18653:                                dma[c].req |= bit;
                   18654: //                             dma_run(c, ch);
                   18655:                        }
                   18656:                } else {
                   18657:                        dma[c].req &= ~bit;
                   18658:                }
                   18659:                break;
                   18660:        case 0x0a:
                   18661:                // single mask register
                   18662:                if(data & 4) {
                   18663:                        dma[c].mask |= bit;
                   18664:                } else {
                   18665:                        dma[c].mask &= ~bit;
                   18666:                }
                   18667:                break;
                   18668:        case 0x0b:
                   18669:                // mode register
                   18670:                dma[c].ch[data & 3].mode = data;
                   18671:                break;
                   18672:        case 0x0c:
                   18673:                dma[c].low_high = false;
                   18674:                break;
                   18675:        case 0x0d:
                   18676:                // clear master
                   18677:                dma_reset(c);
                   18678:                break;
                   18679:        case 0x0e:
                   18680:                // clear mask register
                   18681:                dma[c].mask = 0;
                   18682:                break;
                   18683:        case 0x0f:
                   18684:                // all mask register
                   18685:                dma[c].mask = data & 0x0f;
                   18686:                break;
                   18687:        }
                   18688: }
                   18689: 
                   18690: UINT8 dma_read(int c, UINT32 addr)
                   18691: {
                   18692:        int ch = (addr >> 1) & 3;
                   18693:        UINT8 val = 0xff;
                   18694:        
                   18695:        switch(addr & 0x0f) {
                   18696:        case 0x00: case 0x02: case 0x04: case 0x06:
                   18697:                if(dma[c].low_high) {
                   18698:                        val = dma[c].ch[ch].areg.b.h;
                   18699:                } else {
                   18700:                        val = dma[c].ch[ch].areg.b.l;
                   18701:                }
                   18702:                dma[c].low_high = !dma[c].low_high;
                   18703:                return(val);
                   18704:        case 0x01: case 0x03: case 0x05: case 0x07:
                   18705:                if(dma[c].low_high) {
                   18706:                        val = dma[c].ch[ch].creg.b.h;
                   18707:                } else {
                   18708:                        val = dma[c].ch[ch].creg.b.l;
                   18709:                }
                   18710:                dma[c].low_high = !dma[c].low_high;
                   18711:                return(val);
                   18712:        case 0x08:
                   18713:                // status register
                   18714:                val = (dma[c].req << 4) | dma[c].tc;
                   18715:                dma[c].tc = 0;
                   18716:                return(val);
                   18717:        case 0x0d:
1.1.1.26  root     18718:                // temporary register (intel 82374 does not support)
1.1.1.25  root     18719:                return(dma[c].tmp & 0xff);
1.1.1.26  root     18720:        case 0x0f:
                   18721:                // mask register (intel 82374 does support)
                   18722:                return(dma[c].mask);
1.1.1.25  root     18723:        }
                   18724:        return(0xff);
                   18725: }
                   18726: 
                   18727: void dma_page_write(int c, int ch, UINT8 data)
                   18728: {
                   18729:        dma[c].ch[ch].pagereg = data;
                   18730: }
                   18731: 
                   18732: UINT8 dma_page_read(int c, int ch)
                   18733: {
                   18734:        return(dma[c].ch[ch].pagereg);
                   18735: }
                   18736: 
                   18737: void dma_run(int c, int ch)
                   18738: {
                   18739:        UINT8 bit = 1 << ch;
                   18740:        
                   18741:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   18742:                // execute dma
                   18743:                while(dma[c].req & bit) {
                   18744:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   18745:                                // memory -> memory
                   18746:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   18747:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   18748:                                
                   18749:                                if(c == 0) {
                   18750:                                        dma[c].tmp = read_byte(saddr);
                   18751:                                        write_byte(daddr, dma[c].tmp);
                   18752:                                } else {
                   18753:                                        dma[c].tmp = read_word(saddr << 1);
                   18754:                                        write_word(daddr << 1, dma[c].tmp);
                   18755:                                }
                   18756:                                if(!(dma[c].cmd & 0x02)) {
                   18757:                                        if(dma[c].ch[0].mode & 0x20) {
                   18758:                                                dma[c].ch[0].areg.w--;
                   18759:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   18760:                                                        dma[c].ch[0].pagereg--;
                   18761:                                                }
                   18762:                                        } else {
                   18763:                                                dma[c].ch[0].areg.w++;
                   18764:                                                if(dma[c].ch[0].areg.w == 0) {
                   18765:                                                        dma[c].ch[0].pagereg++;
                   18766:                                                }
                   18767:                                        }
                   18768:                                }
                   18769:                                if(dma[c].ch[1].mode & 0x20) {
                   18770:                                        dma[c].ch[1].areg.w--;
                   18771:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   18772:                                                dma[c].ch[1].pagereg--;
                   18773:                                        }
                   18774:                                } else {
                   18775:                                        dma[c].ch[1].areg.w++;
                   18776:                                        if(dma[c].ch[1].areg.w == 0) {
                   18777:                                                dma[c].ch[1].pagereg++;
                   18778:                                        }
                   18779:                                }
                   18780:                                
                   18781:                                // check dma condition
                   18782:                                if(dma[c].ch[0].creg.w-- == 0) {
                   18783:                                        if(dma[c].ch[0].mode & 0x10) {
                   18784:                                                // self initialize
                   18785:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   18786:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   18787:                                        } else {
                   18788: //                                             dma[c].mask |= bit;
                   18789:                                        }
                   18790:                                }
                   18791:                                if(dma[c].ch[1].creg.w-- == 0) {
                   18792:                                        // terminal count
                   18793:                                        if(dma[c].ch[1].mode & 0x10) {
                   18794:                                                // self initialize
                   18795:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   18796:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   18797:                                        } else {
                   18798:                                                dma[c].mask |= bit;
                   18799:                                        }
                   18800:                                        dma[c].req &= ~bit;
                   18801:                                        dma[c].tc |= bit;
                   18802:                                }
                   18803:                        } else {
                   18804:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   18805:                                
                   18806:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   18807:                                        // verify
                   18808:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   18809:                                        // io -> memory
                   18810:                                        if(c == 0) {
                   18811:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   18812:                                                write_byte(addr, dma[c].tmp);
                   18813:                                        } else {
                   18814:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   18815:                                                write_word(addr << 1, dma[c].tmp);
                   18816:                                        }
                   18817:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   18818:                                        // memory -> io
                   18819:                                        if(c == 0) {
                   18820:                                                dma[c].tmp = read_byte(addr);
                   18821:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   18822:                                        } else {
                   18823:                                                dma[c].tmp = read_word(addr << 1);
                   18824:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   18825:                                        }
                   18826:                                }
                   18827:                                if(dma[c].ch[ch].mode & 0x20) {
                   18828:                                        dma[c].ch[ch].areg.w--;
                   18829:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   18830:                                                dma[c].ch[ch].pagereg--;
                   18831:                                        }
                   18832:                                } else {
                   18833:                                        dma[c].ch[ch].areg.w++;
                   18834:                                        if(dma[c].ch[ch].areg.w == 0) {
                   18835:                                                dma[c].ch[ch].pagereg++;
                   18836:                                        }
                   18837:                                }
                   18838:                                
                   18839:                                // check dma condition
                   18840:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   18841:                                        // terminal count
                   18842:                                        if(dma[c].ch[ch].mode & 0x10) {
                   18843:                                                // self initialize
                   18844:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   18845:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   18846:                                        } else {
                   18847:                                                dma[c].mask |= bit;
                   18848:                                        }
                   18849:                                        dma[c].req &= ~bit;
                   18850:                                        dma[c].tc |= bit;
                   18851:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   18852:                                        // single mode
                   18853:                                        break;
                   18854:                                }
                   18855:                        }
                   18856:                }
                   18857:        }
                   18858: }
                   18859: 
                   18860: // pic
                   18861: 
                   18862: void pic_init()
                   18863: {
                   18864:        memset(pic, 0, sizeof(pic));
                   18865:        pic[0].imr = pic[1].imr = 0xff;
                   18866:        
                   18867:        // from bochs bios
                   18868:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   18869:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   18870:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   18871:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   18872:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   18873:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   18874:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   18875:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   18876:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   18877: }
                   18878: 
                   18879: void pic_write(int c, UINT32 addr, UINT8 data)
                   18880: {
                   18881:        if(addr & 1) {
                   18882:                if(pic[c].icw2_r) {
                   18883:                        // icw2
                   18884:                        pic[c].icw2 = data;
                   18885:                        pic[c].icw2_r = 0;
                   18886:                } else if(pic[c].icw3_r) {
                   18887:                        // icw3
                   18888:                        pic[c].icw3 = data;
                   18889:                        pic[c].icw3_r = 0;
                   18890:                } else if(pic[c].icw4_r) {
                   18891:                        // icw4
                   18892:                        pic[c].icw4 = data;
                   18893:                        pic[c].icw4_r = 0;
                   18894:                } else {
                   18895:                        // ocw1
1.1       root     18896:                        pic[c].imr = data;
                   18897:                }
                   18898:        } else {
                   18899:                if(data & 0x10) {
                   18900:                        // icw1
                   18901:                        pic[c].icw1 = data;
                   18902:                        pic[c].icw2_r = 1;
                   18903:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   18904:                        pic[c].icw4_r = data & 1;
                   18905:                        pic[c].irr = 0;
                   18906:                        pic[c].isr = 0;
                   18907:                        pic[c].imr = 0;
                   18908:                        pic[c].prio = 0;
                   18909:                        if(!(pic[c].icw1 & 1)) {
                   18910:                                pic[c].icw4 = 0;
                   18911:                        }
                   18912:                        pic[c].ocw3 = 0;
                   18913:                } else if(data & 8) {
                   18914:                        // ocw3
                   18915:                        if(!(data & 2)) {
                   18916:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   18917:                        }
                   18918:                        if(!(data & 0x40)) {
                   18919:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   18920:                        }
                   18921:                        pic[c].ocw3 = data;
                   18922:                } else {
                   18923:                        // ocw2
                   18924:                        int level = 0;
                   18925:                        if(data & 0x40) {
                   18926:                                level = data & 7;
                   18927:                        } else {
                   18928:                                if(!pic[c].isr) {
                   18929:                                        return;
                   18930:                                }
                   18931:                                level = pic[c].prio;
                   18932:                                while(!(pic[c].isr & (1 << level))) {
                   18933:                                        level = (level + 1) & 7;
                   18934:                                }
                   18935:                        }
                   18936:                        if(data & 0x80) {
                   18937:                                pic[c].prio = (level + 1) & 7;
                   18938:                        }
                   18939:                        if(data & 0x20) {
                   18940:                                pic[c].isr &= ~(1 << level);
                   18941:                        }
                   18942:                }
                   18943:        }
                   18944:        pic_update();
                   18945: }
                   18946: 
                   18947: UINT8 pic_read(int c, UINT32 addr)
                   18948: {
                   18949:        if(addr & 1) {
                   18950:                return(pic[c].imr);
                   18951:        } else {
                   18952:                // polling mode is not supported...
                   18953:                //if(pic[c].ocw3 & 4) {
                   18954:                //      return ???;
                   18955:                //}
                   18956:                if(pic[c].ocw3 & 1) {
                   18957:                        return(pic[c].isr);
                   18958:                } else {
                   18959:                        return(pic[c].irr);
                   18960:                }
                   18961:        }
                   18962: }
                   18963: 
                   18964: void pic_req(int c, int level, int signal)
                   18965: {
                   18966:        if(signal) {
                   18967:                pic[c].irr |= (1 << level);
                   18968:        } else {
                   18969:                pic[c].irr &= ~(1 << level);
                   18970:        }
                   18971:        pic_update();
                   18972: }
                   18973: 
                   18974: int pic_ack()
                   18975: {
                   18976:        // ack (INTA=L)
                   18977:        pic[pic_req_chip].isr |= pic_req_bit;
                   18978:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   18979:        if(pic_req_chip > 0) {
                   18980:                // update isr and irr of master
                   18981:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   18982:                pic[pic_req_chip - 1].isr |= slave;
                   18983:                pic[pic_req_chip - 1].irr &= ~slave;
                   18984:        }
                   18985:        //if(pic[pic_req_chip].icw4 & 1) {
                   18986:                // 8086 mode
                   18987:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   18988:        //} else {
                   18989:        //      // 8080 mode
                   18990:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   18991:        //      if(pic[pic_req_chip].icw1 & 4) {
                   18992:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   18993:        //      } else {
                   18994:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   18995:        //      }
                   18996:        //      vector = 0xcd | (addr << 8);
                   18997:        //}
                   18998:        if(pic[pic_req_chip].icw4 & 2) {
                   18999:                // auto eoi
                   19000:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   19001:        }
                   19002:        return(vector);
                   19003: }
                   19004: 
                   19005: void pic_update()
                   19006: {
                   19007:        for(int c = 0; c < 2; c++) {
                   19008:                UINT8 irr = pic[c].irr;
                   19009:                if(c + 1 < 2) {
                   19010:                        // this is master
                   19011:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   19012:                                // request from slave
                   19013:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   19014:                        }
                   19015:                }
                   19016:                irr &= (~pic[c].imr);
                   19017:                if(!irr) {
                   19018:                        break;
                   19019:                }
                   19020:                if(!(pic[c].ocw3 & 0x20)) {
                   19021:                        irr |= pic[c].isr;
                   19022:                }
                   19023:                int level = pic[c].prio;
                   19024:                UINT8 bit = 1 << level;
                   19025:                while(!(irr & bit)) {
                   19026:                        level = (level + 1) & 7;
                   19027:                        bit = 1 << level;
                   19028:                }
                   19029:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   19030:                        // check slave
                   19031:                        continue;
                   19032:                }
                   19033:                if(pic[c].isr & bit) {
                   19034:                        break;
                   19035:                }
                   19036:                // interrupt request
                   19037:                pic_req_chip = c;
                   19038:                pic_req_level = level;
                   19039:                pic_req_bit = bit;
1.1.1.3   root     19040:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     19041:                return;
                   19042:        }
1.1.1.3   root     19043:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     19044: }
1.1       root     19045: 
1.1.1.25  root     19046: // pio
                   19047: 
                   19048: void pio_init()
                   19049: {
1.1.1.38  root     19050: //     bool conv_mode = (GetConsoleCP() == 932);
                   19051:        
1.1.1.26  root     19052:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     19053:        
1.1.1.25  root     19054:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     19055:                pio[c].stat = 0xdf;
1.1.1.25  root     19056:                pio[c].ctrl = 0x0c;
1.1.1.38  root     19057: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     19058:        }
                   19059: }
                   19060: 
1.1.1.37  root     19061: void pio_finish()
                   19062: {
                   19063:        pio_release();
                   19064: }
                   19065: 
                   19066: void pio_release()
                   19067: {
                   19068:        for(int c = 0; c < 2; c++) {
                   19069:                if(pio[c].fp != NULL) {
1.1.1.38  root     19070:                        if(pio[c].jis_mode) {
                   19071:                                fputc(0x1c, pio[c].fp);
                   19072:                                fputc(0x2e, pio[c].fp);
                   19073:                        }
1.1.1.37  root     19074:                        fclose(pio[c].fp);
                   19075:                        pio[c].fp = NULL;
                   19076:                }
                   19077:        }
                   19078: }
                   19079: 
1.1.1.25  root     19080: void pio_write(int c, UINT32 addr, UINT8 data)
                   19081: {
                   19082:        switch(addr & 3) {
                   19083:        case 0:
                   19084:                pio[c].data = data;
                   19085:                break;
                   19086:        case 2:
1.1.1.37  root     19087:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   19088:                        // strobe H -> L
                   19089:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   19090:                                // auto feed
                   19091:                                printer_out(c, 0x0d);
                   19092:                                printer_out(c, 0x0a);
                   19093:                        } else {
                   19094:                                printer_out(c, pio[c].data);
                   19095:                        }
                   19096:                        pio[c].stat &= ~0x40; // set ack
                   19097:                }
1.1.1.25  root     19098:                pio[c].ctrl = data;
                   19099:                break;
                   19100:        }
                   19101: }
                   19102: 
                   19103: UINT8 pio_read(int c, UINT32 addr)
                   19104: {
                   19105:        switch(addr & 3) {
                   19106:        case 0:
1.1.1.37  root     19107:                if(pio[c].ctrl & 0x20) {
                   19108:                        // input mode
                   19109:                        return(0xff);
                   19110:                }
1.1.1.25  root     19111:                return(pio[c].data);
                   19112:        case 1:
1.1.1.37  root     19113:                {
                   19114:                        UINT8 stat = pio[c].stat;
                   19115:                        pio[c].stat |= 0x40; // clear ack
                   19116:                        return(stat);
                   19117:                }
1.1.1.25  root     19118:        case 2:
                   19119:                return(pio[c].ctrl);
                   19120:        }
                   19121:        return(0xff);
                   19122: }
                   19123: 
1.1.1.37  root     19124: void printer_out(int c, UINT8 data)
                   19125: {
                   19126:        SYSTEMTIME time;
1.1.1.38  root     19127:        bool jis_mode = false;
1.1.1.37  root     19128:        
                   19129:        GetLocalTime(&time);
                   19130:        
                   19131:        if(pio[c].fp != NULL) {
                   19132:                // if at least 1000ms passed from last written, close the current file
                   19133:                FILETIME ftime1;
                   19134:                FILETIME ftime2;
                   19135:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   19136:                SystemTimeToFileTime(&time, &ftime2);
                   19137:                INT64 *time1 = (INT64 *)&ftime1;
                   19138:                INT64 *time2 = (INT64 *)&ftime2;
                   19139:                INT64 msec = (*time2 - *time1) / 10000;
                   19140:                
                   19141:                if(msec >= 1000) {
1.1.1.38  root     19142:                        if(pio[c].jis_mode) {
                   19143:                                fputc(0x1c, pio[c].fp);
                   19144:                                fputc(0x2e, pio[c].fp);
                   19145:                                jis_mode = true;
                   19146:                        }
1.1.1.37  root     19147:                        fclose(pio[c].fp);
                   19148:                        pio[c].fp = NULL;
                   19149:                }
                   19150:        }
                   19151:        if(pio[c].fp == NULL) {
                   19152:                // create a new file in the temp folder
                   19153:                char file_name[MAX_PATH];
                   19154:                
                   19155:                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);
                   19156:                if(GetTempPath(MAX_PATH, pio[c].path)) {
                   19157:                        strcat(pio[c].path, file_name);
                   19158:                } else {
                   19159:                        strcpy(pio[c].path, file_name);
                   19160:                }
1.1.1.38  root     19161:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     19162:        }
                   19163:        if(pio[c].fp != NULL) {
1.1.1.38  root     19164:                if(jis_mode) {
                   19165:                        fputc(0x1c, pio[c].fp);
                   19166:                        fputc(0x26, pio[c].fp);
                   19167:                }
1.1.1.37  root     19168:                fputc(data, pio[c].fp);
1.1.1.38  root     19169:                
                   19170:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   19171:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   19172:                        UINT8 buffer[4];
                   19173:                        fseek(pio[c].fp, 0, SEEK_SET);
                   19174:                        fread(buffer, 4, 1, pio[c].fp);
                   19175:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   19176:                                fclose(pio[c].fp);
                   19177:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   19178:                        }
                   19179:                }
1.1.1.37  root     19180:                pio[c].time = time;
                   19181:        }
                   19182: }
                   19183: 
1.1       root     19184: // pit
                   19185: 
1.1.1.22  root     19186: #define PIT_FREQ 1193182ULL
1.1       root     19187: #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)
                   19188: 
                   19189: void pit_init()
                   19190: {
1.1.1.8   root     19191:        memset(pit, 0, sizeof(pit));
1.1       root     19192:        for(int ch = 0; ch < 3; ch++) {
                   19193:                pit[ch].count = 0x10000;
                   19194:                pit[ch].ctrl_reg = 0x34;
                   19195:                pit[ch].mode = 3;
                   19196:        }
                   19197:        
                   19198:        // from bochs bios
                   19199:        pit_write(3, 0x34);
                   19200:        pit_write(0, 0x00);
                   19201:        pit_write(0, 0x00);
                   19202: }
                   19203: 
                   19204: void pit_write(int ch, UINT8 val)
                   19205: {
1.1.1.8   root     19206: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19207:        if(!pit_active) {
                   19208:                pit_active = 1;
                   19209:                pit_init();
                   19210:        }
1.1.1.8   root     19211: #endif
1.1       root     19212:        switch(ch) {
                   19213:        case 0:
                   19214:        case 1:
                   19215:        case 2:
                   19216:                // write count register
                   19217:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19218:                        if(pit[ch].ctrl_reg & 0x10) {
                   19219:                                pit[ch].low_write = 1;
                   19220:                        }
                   19221:                        if(pit[ch].ctrl_reg & 0x20) {
                   19222:                                pit[ch].high_write = 1;
                   19223:                        }
                   19224:                }
                   19225:                if(pit[ch].low_write) {
                   19226:                        pit[ch].count_reg = val;
                   19227:                        pit[ch].low_write = 0;
                   19228:                } else if(pit[ch].high_write) {
                   19229:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19230:                                pit[ch].count_reg = val << 8;
                   19231:                        } else {
                   19232:                                pit[ch].count_reg |= val << 8;
                   19233:                        }
                   19234:                        pit[ch].high_write = 0;
                   19235:                }
                   19236:                // start count
1.1.1.8   root     19237:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19238:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   19239:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   19240:                                pit[ch].prev_time = timeGetTime();
                   19241:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19242:                        }
                   19243:                }
                   19244:                break;
                   19245:        case 3: // ctrl reg
                   19246:                if((val & 0xc0) == 0xc0) {
                   19247:                        // i8254 read-back command
                   19248:                        for(ch = 0; ch < 3; ch++) {
                   19249:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   19250:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   19251:                                        pit[ch].status_latched = 1;
                   19252:                                }
                   19253:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   19254:                                        pit_latch_count(ch);
                   19255:                                }
                   19256:                        }
                   19257:                        break;
                   19258:                }
                   19259:                ch = (val >> 6) & 3;
                   19260:                if(val & 0x30) {
1.1.1.35  root     19261:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     19262:                        pit[ch].mode = modes[(val >> 1) & 7];
                   19263:                        pit[ch].count_latched = 0;
                   19264:                        pit[ch].low_read = pit[ch].high_read = 0;
                   19265:                        pit[ch].low_write = pit[ch].high_write = 0;
                   19266:                        pit[ch].ctrl_reg = val;
                   19267:                        // stop count
1.1.1.8   root     19268:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     19269:                        pit[ch].count_reg = 0;
                   19270:                } else if(!pit[ch].count_latched) {
                   19271:                        pit_latch_count(ch);
                   19272:                }
                   19273:                break;
                   19274:        }
                   19275: }
                   19276: 
                   19277: UINT8 pit_read(int ch)
                   19278: {
1.1.1.8   root     19279: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19280:        if(!pit_active) {
                   19281:                pit_active = 1;
                   19282:                pit_init();
                   19283:        }
1.1.1.8   root     19284: #endif
1.1       root     19285:        switch(ch) {
                   19286:        case 0:
                   19287:        case 1:
                   19288:        case 2:
                   19289:                if(pit[ch].status_latched) {
                   19290:                        pit[ch].status_latched = 0;
                   19291:                        return(pit[ch].status);
                   19292:                }
                   19293:                // if not latched, through current count
                   19294:                if(!pit[ch].count_latched) {
                   19295:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   19296:                                pit_latch_count(ch);
                   19297:                        }
                   19298:                }
                   19299:                // return latched count
                   19300:                if(pit[ch].low_read) {
                   19301:                        pit[ch].low_read = 0;
                   19302:                        if(!pit[ch].high_read) {
                   19303:                                pit[ch].count_latched = 0;
                   19304:                        }
                   19305:                        return(pit[ch].latch & 0xff);
                   19306:                } else if(pit[ch].high_read) {
                   19307:                        pit[ch].high_read = 0;
                   19308:                        pit[ch].count_latched = 0;
                   19309:                        return((pit[ch].latch >> 8) & 0xff);
                   19310:                }
                   19311:        }
                   19312:        return(0xff);
                   19313: }
                   19314: 
1.1.1.8   root     19315: int pit_run(int ch, UINT32 cur_time)
1.1       root     19316: {
1.1.1.8   root     19317:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     19318:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     19319:                pit[ch].prev_time = pit[ch].expired_time;
                   19320:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   19321:                if(cur_time >= pit[ch].expired_time) {
                   19322:                        pit[ch].prev_time = cur_time;
                   19323:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19324:                }
1.1.1.8   root     19325:                return(1);
1.1       root     19326:        }
1.1.1.8   root     19327:        return(0);
1.1       root     19328: }
                   19329: 
                   19330: void pit_latch_count(int ch)
                   19331: {
1.1.1.8   root     19332:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     19333:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     19334:                pit_run(ch, cur_time);
                   19335:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     19336:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19337:                
                   19338:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   19339:                        // decrement counter in 1msec period
                   19340:                        if(pit[ch].next_latch == 0) {
                   19341:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   19342:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19343:                        }
                   19344:                        if(pit[ch].latch > pit[ch].next_latch) {
                   19345:                                pit[ch].latch--;
                   19346:                        }
                   19347:                } else {
                   19348:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   19349:                        pit[ch].next_latch = 0;
                   19350:                }
1.1.1.8   root     19351:        } else {
                   19352:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     19353:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     19354:        }
                   19355:        pit[ch].count_latched = 1;
                   19356:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   19357:                // lower byte
                   19358:                pit[ch].low_read = 1;
                   19359:                pit[ch].high_read = 0;
                   19360:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19361:                // upper byte
                   19362:                pit[ch].low_read = 0;
                   19363:                pit[ch].high_read = 1;
                   19364:        } else {
                   19365:                // lower -> upper
1.1.1.14  root     19366:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     19367:        }
                   19368: }
                   19369: 
1.1.1.8   root     19370: int pit_get_expired_time(int ch)
1.1       root     19371: {
1.1.1.22  root     19372:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   19373:        UINT64 val = pit[ch].accum >> 10;
                   19374:        pit[ch].accum -= val << 10;
                   19375:        return((val != 0) ? val : 1);
1.1.1.8   root     19376: }
                   19377: 
1.1.1.25  root     19378: // sio
                   19379: 
                   19380: void sio_init()
                   19381: {
1.1.1.26  root     19382:        memset(sio, 0, sizeof(sio));
                   19383:        memset(sio_mt, 0, sizeof(sio_mt));
                   19384:        
1.1.1.29  root     19385:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19386:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19387:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19388:                
                   19389:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   19390:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     19391:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   19392:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     19393:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   19394:                sio[c].irq_identify = 0x01;     // no pending irq
                   19395:                
                   19396:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   19397:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   19398:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   19399:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   19400:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   19401:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   19402:                
1.1.1.26  root     19403:                if(sio_port_number[c] != 0) {
1.1.1.25  root     19404:                        sio[c].channel = c;
                   19405:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   19406:                }
                   19407:        }
                   19408: }
                   19409: 
                   19410: void sio_finish()
                   19411: {
1.1.1.29  root     19412:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19413:                if(sio_mt[c].hThread != NULL) {
                   19414:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   19415:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     19416:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     19417:                }
                   19418:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   19419:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   19420:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   19421:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   19422:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   19423:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     19424:        }
                   19425:        sio_release();
                   19426: }
                   19427: 
                   19428: void sio_release()
                   19429: {
1.1.1.29  root     19430:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     19431:                // sio_thread() may access the resources :-(
1.1.1.32  root     19432:                bool running = (sio_mt[c].hThread != NULL);
                   19433:                
                   19434:                if(running) {
                   19435:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   19436:                }
                   19437:                if(sio[c].send_buffer != NULL) {
                   19438:                        sio[c].send_buffer->release();
                   19439:                        delete sio[c].send_buffer;
                   19440:                        sio[c].send_buffer = NULL;
                   19441:                }
                   19442:                if(running) {
                   19443:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19444:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   19445:                }
                   19446:                if(sio[c].recv_buffer != NULL) {
                   19447:                        sio[c].recv_buffer->release();
                   19448:                        delete sio[c].recv_buffer;
                   19449:                        sio[c].recv_buffer = NULL;
                   19450:                }
                   19451:                if(running) {
                   19452:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     19453:                }
1.1.1.25  root     19454:        }
                   19455: }
                   19456: 
                   19457: void sio_write(int c, UINT32 addr, UINT8 data)
                   19458: {
                   19459:        switch(addr & 7) {
                   19460:        case 0:
                   19461:                if(sio[c].selector & 0x80) {
                   19462:                        if(sio[c].divisor.b.l != data) {
                   19463:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19464:                                sio[c].divisor.b.l = data;
                   19465:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19466:                        }
                   19467:                } else {
                   19468:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19469:                        if(sio[c].send_buffer != NULL) {
                   19470:                                sio[c].send_buffer->write(data);
                   19471:                        }
1.1.1.25  root     19472:                        // transmitter holding/shift registers are not empty
                   19473:                        sio[c].line_stat_buf &= ~0x60;
                   19474:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19475:                        
                   19476:                        if(sio[c].irq_enable & 0x02) {
                   19477:                                sio_update_irq(c);
                   19478:                        }
                   19479:                }
                   19480:                break;
                   19481:        case 1:
                   19482:                if(sio[c].selector & 0x80) {
                   19483:                        if(sio[c].divisor.b.h != data) {
                   19484:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19485:                                sio[c].divisor.b.h = data;
                   19486:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19487:                        }
                   19488:                } else {
                   19489:                        if(sio[c].irq_enable != data) {
                   19490:                                sio[c].irq_enable = data;
                   19491:                                sio_update_irq(c);
                   19492:                        }
                   19493:                }
                   19494:                break;
                   19495:        case 3:
                   19496:                {
                   19497:                        UINT8 line_ctrl = data & 0x3f;
                   19498:                        bool set_brk = ((data & 0x40) != 0);
                   19499:                        
                   19500:                        if(sio[c].line_ctrl != line_ctrl) {
                   19501:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19502:                                sio[c].line_ctrl = line_ctrl;
                   19503:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19504:                        }
                   19505:                        if(sio[c].set_brk != set_brk) {
                   19506:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   19507:                                sio[c].set_brk = set_brk;
                   19508:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19509:                        }
                   19510:                }
                   19511:                sio[c].selector = data;
                   19512:                break;
                   19513:        case 4:
                   19514:                {
                   19515:                        bool set_dtr = ((data & 0x01) != 0);
                   19516:                        bool set_rts = ((data & 0x02) != 0);
                   19517:                        
                   19518:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     19519: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     19520:                                sio[c].set_dtr = set_dtr;
                   19521:                                sio[c].set_rts = set_rts;
1.1.1.26  root     19522: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19523:                                
                   19524:                                bool state_changed = false;
                   19525:                                
                   19526:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19527:                                if(set_dtr) {
                   19528:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   19529:                                } else {
                   19530:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   19531:                                }
                   19532:                                if(set_rts) {
                   19533:                                        sio[c].modem_stat |= 0x10;      // cts on
                   19534:                                } else {
                   19535:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   19536:                                }
                   19537:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   19538:                                        if(!(sio[c].modem_stat & 0x02)) {
                   19539:                                                if(sio[c].irq_enable & 0x08) {
                   19540:                                                        state_changed = true;
                   19541:                                                }
                   19542:                                                sio[c].modem_stat |= 0x02;
                   19543:                                        }
                   19544:                                }
                   19545:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   19546:                                        if(!(sio[c].modem_stat & 0x01)) {
                   19547:                                                if(sio[c].irq_enable & 0x08) {
                   19548:                                                        state_changed = true;
                   19549:                                                }
                   19550:                                                sio[c].modem_stat |= 0x01;
                   19551:                                        }
                   19552:                                }
                   19553:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19554:                                
                   19555:                                if(state_changed) {
                   19556:                                        sio_update_irq(c);
                   19557:                                }
1.1.1.25  root     19558:                        }
                   19559:                }
                   19560:                sio[c].modem_ctrl = data;
                   19561:                break;
                   19562:        case 7:
                   19563:                sio[c].scratch = data;
                   19564:                break;
                   19565:        }
                   19566: }
                   19567: 
                   19568: UINT8 sio_read(int c, UINT32 addr)
                   19569: {
                   19570:        switch(addr & 7) {
                   19571:        case 0:
                   19572:                if(sio[c].selector & 0x80) {
                   19573:                        return(sio[c].divisor.b.l);
                   19574:                } else {
                   19575:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19576:                        UINT8 data = 0;
                   19577:                        if(sio[c].recv_buffer != NULL) {
                   19578:                                data = sio[c].recv_buffer->read();
                   19579:                        }
1.1.1.25  root     19580:                        // data is not ready
                   19581:                        sio[c].line_stat_buf &= ~0x01;
                   19582:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19583:                        
                   19584:                        if(sio[c].irq_enable & 0x01) {
                   19585:                                sio_update_irq(c);
                   19586:                        }
                   19587:                        return(data);
                   19588:                }
                   19589:        case 1:
                   19590:                if(sio[c].selector & 0x80) {
                   19591:                        return(sio[c].divisor.b.h);
                   19592:                } else {
                   19593:                        return(sio[c].irq_enable);
                   19594:                }
                   19595:        case 2:
                   19596:                return(sio[c].irq_identify);
                   19597:        case 3:
                   19598:                return(sio[c].selector);
                   19599:        case 4:
                   19600:                return(sio[c].modem_ctrl);
                   19601:        case 5:
                   19602:                {
                   19603:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   19604:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   19605:                        sio[c].line_stat_err = 0x00;
                   19606:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   19607:                        
                   19608:                        bool state_changed = false;
                   19609:                        
                   19610:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   19611:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19612:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     19613:                                        // transmitter holding register will be empty first
                   19614:                                        if(sio[c].irq_enable & 0x02) {
                   19615:                                                state_changed = true;
                   19616:                                        }
                   19617:                                        sio[c].line_stat_buf |= 0x20;
                   19618:                                }
                   19619:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   19620:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   19621:                                // transmitter shift register will be empty later
                   19622:                                sio[c].line_stat_buf |= 0x40;
                   19623:                        }
                   19624:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   19625:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19626:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     19627:                                        // data is ready
                   19628:                                        if(sio[c].irq_enable & 0x01) {
                   19629:                                                state_changed = true;
                   19630:                                        }
                   19631:                                        sio[c].line_stat_buf |= 0x01;
                   19632:                                }
                   19633:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19634:                        }
                   19635:                        if(state_changed) {
                   19636:                                sio_update_irq(c);
                   19637:                        }
                   19638:                        return(val);
                   19639:                }
                   19640:        case 6:
                   19641:                {
                   19642:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   19643:                        UINT8 val = sio[c].modem_stat;
                   19644:                        sio[c].modem_stat &= 0xf0;
                   19645:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   19646:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19647:                        
                   19648:                        if(sio[c].modem_ctrl & 0x10) {
                   19649:                                // loop-back
                   19650:                                val &= 0x0f;
                   19651:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   19652:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   19653:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   19654:                        }
                   19655:                        return(val);
                   19656:                }
                   19657:        case 7:
                   19658:                return(sio[c].scratch);
                   19659:        }
                   19660:        return(0xff);
                   19661: }
                   19662: 
                   19663: void sio_update(int c)
                   19664: {
                   19665:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   19666:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19667:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     19668:                        // transmitter holding/shift registers will be empty
                   19669:                        sio[c].line_stat_buf |= 0x60;
                   19670:                }
                   19671:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   19672:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   19673:                // transmitter shift register will be empty
                   19674:                sio[c].line_stat_buf |= 0x40;
                   19675:        }
                   19676:        if(!(sio[c].line_stat_buf & 0x01)) {
                   19677:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19678:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     19679:                        // data is ready
                   19680:                        sio[c].line_stat_buf |= 0x01;
                   19681:                }
                   19682:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19683:        }
                   19684:        sio_update_irq(c);
                   19685: }
                   19686: 
                   19687: void sio_update_irq(int c)
                   19688: {
                   19689:        int level = -1;
                   19690:        
                   19691:        if(sio[c].irq_enable & 0x08) {
                   19692:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19693:                if((sio[c].modem_stat & 0x0f) != 0) {
                   19694:                        level = 0;
                   19695:                }
                   19696:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19697:        }
                   19698:        if(sio[c].irq_enable & 0x02) {
                   19699:                if(sio[c].line_stat_buf & 0x20) {
                   19700:                        level = 1;
                   19701:                }
                   19702:        }
                   19703:        if(sio[c].irq_enable & 0x01) {
                   19704:                if(sio[c].line_stat_buf & 0x01) {
                   19705:                        level = 2;
                   19706:                }
                   19707:        }
                   19708:        if(sio[c].irq_enable & 0x04) {
                   19709:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   19710:                if(sio[c].line_stat_err != 0) {
                   19711:                        level = 3;
                   19712:                }
                   19713:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   19714:        }
1.1.1.29  root     19715:        
                   19716:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     19717:        if(level != -1) {
                   19718:                sio[c].irq_identify = level << 1;
1.1.1.29  root     19719:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     19720:        } else {
                   19721:                sio[c].irq_identify = 1;
1.1.1.29  root     19722:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     19723:        }
                   19724: }
                   19725: 
                   19726: DWORD WINAPI sio_thread(void *lpx)
                   19727: {
                   19728:        volatile sio_t *p = (sio_t *)lpx;
                   19729:        sio_mt_t *q = &sio_mt[p->channel];
                   19730:        
                   19731:        char name[] = "COM1";
1.1.1.26  root     19732:        name[3] = '0' + sio_port_number[p->channel];
                   19733:        HANDLE hComm = NULL;
                   19734:        COMMPROP commProp;
                   19735:        DCB dcb;
                   19736:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   19737:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   19738:        
                   19739:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   19740:                if(GetCommProperties(hComm, &commProp)) {
                   19741:                        dwSettableBaud = commProp.dwSettableBaud;
                   19742:                }
1.1.1.25  root     19743:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     19744: //             EscapeCommFunction(hComm, SETRTS);
                   19745: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     19746:                
1.1.1.54  root     19747:                while(!m_exit) {
1.1.1.25  root     19748:                        // setup comm port
                   19749:                        bool comm_state_changed = false;
                   19750:                        
                   19751:                        EnterCriticalSection(&q->csLineCtrl);
                   19752:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   19753:                                p->prev_divisor = p->divisor.w;
                   19754:                                p->prev_line_ctrl = p->line_ctrl;
                   19755:                                comm_state_changed = true;
                   19756:                        }
                   19757:                        LeaveCriticalSection(&q->csLineCtrl);
                   19758:                        
                   19759:                        if(comm_state_changed) {
1.1.1.26  root     19760:                                if(GetCommState(hComm, &dcb)) {
                   19761: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   19762:                                        DWORD baud = 115200 / p->prev_divisor;
                   19763:                                        dcb.BaudRate = 9600; // default
                   19764:                                        
                   19765:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   19766:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   19767:                                        // 134.5bps is not supported ???
                   19768: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   19769:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   19770:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   19771:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   19772:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   19773:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   19774:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   19775:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   19776:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   19777:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   19778: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   19779: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   19780: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   19781:                                        
                   19782:                                        switch(p->prev_line_ctrl & 0x03) {
                   19783:                                        case 0x00: dcb.ByteSize = 5; break;
                   19784:                                        case 0x01: dcb.ByteSize = 6; break;
                   19785:                                        case 0x02: dcb.ByteSize = 7; break;
                   19786:                                        case 0x03: dcb.ByteSize = 8; break;
                   19787:                                        }
                   19788:                                        switch(p->prev_line_ctrl & 0x04) {
                   19789:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   19790:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   19791:                                        }
                   19792:                                        switch(p->prev_line_ctrl & 0x38) {
                   19793:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   19794:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   19795:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   19796:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   19797:                                        default:   dcb.Parity = NOPARITY;    break;
                   19798:                                        }
                   19799:                                        dcb.fBinary = TRUE;
                   19800:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   19801:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   19802:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   19803:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   19804:                                        dcb.fTXContinueOnXoff = TRUE;
                   19805:                                        dcb.fOutX = dcb.fInX = FALSE;
                   19806:                                        dcb.fErrorChar = FALSE;
                   19807:                                        dcb.fNull = FALSE;
                   19808:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   19809:                                        dcb.fAbortOnError = FALSE;
                   19810:                                        
                   19811:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     19812:                                }
                   19813:                                
                   19814:                                // check again to apply all comm state changes
                   19815:                                Sleep(10);
                   19816:                                continue;
                   19817:                        }
                   19818:                        
                   19819:                        // set comm pins
                   19820:                        bool change_brk = false;
1.1.1.26  root     19821: //                     bool change_rts = false;
                   19822: //                     bool change_dtr = false;
1.1.1.25  root     19823:                        
                   19824:                        EnterCriticalSection(&q->csModemCtrl);
                   19825:                        if(p->prev_set_brk != p->set_brk) {
                   19826:                                p->prev_set_brk = p->set_brk;
                   19827:                                change_brk = true;
                   19828:                        }
1.1.1.26  root     19829: //                     if(p->prev_set_rts != p->set_rts) {
                   19830: //                             p->prev_set_rts = p->set_rts;
                   19831: //                             change_rts = true;
                   19832: //                     }
                   19833: //                     if(p->prev_set_dtr != p->set_dtr) {
                   19834: //                             p->prev_set_dtr = p->set_dtr;
                   19835: //                             change_dtr = true;
                   19836: //                     }
1.1.1.25  root     19837:                        LeaveCriticalSection(&q->csModemCtrl);
                   19838:                        
                   19839:                        if(change_brk) {
1.1.1.26  root     19840:                                static UINT32 clear_time = 0;
                   19841:                                if(p->prev_set_brk) {
                   19842:                                        EscapeCommFunction(hComm, SETBREAK);
                   19843:                                        clear_time = timeGetTime() + 200;
                   19844:                                } else {
                   19845:                                        // keep break for at least 200msec
                   19846:                                        UINT32 cur_time = timeGetTime();
                   19847:                                        if(clear_time > cur_time) {
                   19848:                                                Sleep(clear_time - cur_time);
                   19849:                                        }
                   19850:                                        EscapeCommFunction(hComm, CLRBREAK);
                   19851:                                }
1.1.1.25  root     19852:                        }
1.1.1.26  root     19853: //                     if(change_rts) {
                   19854: //                             if(p->prev_set_rts) {
                   19855: //                                     EscapeCommFunction(hComm, SETRTS);
                   19856: //                             } else {
                   19857: //                                     EscapeCommFunction(hComm, CLRRTS);
                   19858: //                             }
                   19859: //                     }
                   19860: //                     if(change_dtr) {
                   19861: //                             if(p->prev_set_dtr) {
                   19862: //                                     EscapeCommFunction(hComm, SETDTR);
                   19863: //                             } else {
                   19864: //                                     EscapeCommFunction(hComm, CLRDTR);
                   19865: //                             }
                   19866: //                     }
1.1.1.25  root     19867:                        
                   19868:                        // get comm pins
                   19869:                        DWORD dwModemStat = 0;
                   19870:                        
                   19871:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   19872:                                EnterCriticalSection(&q->csModemStat);
                   19873:                                if(dwModemStat & MS_RLSD_ON) {
                   19874:                                        p->modem_stat |= 0x80;
                   19875:                                } else {
                   19876:                                        p->modem_stat &= ~0x80;
                   19877:                                }
                   19878:                                if(dwModemStat & MS_RING_ON) {
                   19879:                                        p->modem_stat |= 0x40;
                   19880:                                } else {
                   19881:                                        p->modem_stat &= ~0x40;
                   19882:                                }
1.1.1.26  root     19883: //                             if(dwModemStat & MS_DSR_ON) {
                   19884: //                                     p->modem_stat |= 0x20;
                   19885: //                             } else {
                   19886: //                                     p->modem_stat &= ~0x20;
                   19887: //                             }
                   19888: //                             if(dwModemStat & MS_CTS_ON) {
                   19889: //                                     p->modem_stat |= 0x10;
                   19890: //                             } else {
                   19891: //                                     p->modem_stat &= ~0x10;
                   19892: //                             }
1.1.1.25  root     19893:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   19894:                                        p->modem_stat |= 0x08;
                   19895:                                }
                   19896:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   19897:                                        p->modem_stat |= 0x04;
                   19898:                                }
1.1.1.26  root     19899: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   19900: //                                     p->modem_stat |= 0x02;
                   19901: //                             }
                   19902: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   19903: //                                     p->modem_stat |= 0x01;
                   19904: //                             }
1.1.1.25  root     19905:                                LeaveCriticalSection(&q->csModemStat);
                   19906:                        }
                   19907:                        
                   19908:                        // send data
                   19909:                        DWORD dwSend = 0;
                   19910:                        
                   19911:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     19912:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     19913:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   19914:                        }
                   19915:                        LeaveCriticalSection(&q->csSendData);
                   19916:                        
                   19917:                        if(dwSend != 0) {
                   19918:                                DWORD dwWritten = 0;
                   19919:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   19920:                        }
                   19921:                        
                   19922:                        // get line status and recv data
                   19923:                        DWORD dwLineStat = 0;
                   19924:                        COMSTAT comStat;
                   19925:                        
                   19926:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   19927:                                EnterCriticalSection(&q->csLineStat);
                   19928:                                if(dwLineStat & CE_BREAK) {
                   19929:                                        p->line_stat_err |= 0x10;
                   19930:                                }
                   19931:                                if(dwLineStat & CE_FRAME) {
                   19932:                                        p->line_stat_err |= 0x08;
                   19933:                                }
                   19934:                                if(dwLineStat & CE_RXPARITY) {
                   19935:                                        p->line_stat_err |= 0x04;
                   19936:                                }
                   19937:                                if(dwLineStat & CE_OVERRUN) {
                   19938:                                        p->line_stat_err |= 0x02;
                   19939:                                }
                   19940:                                LeaveCriticalSection(&q->csLineStat);
                   19941:                                
                   19942:                                if(comStat.cbInQue != 0) {
                   19943:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     19944:                                        DWORD dwRecv = 0;
                   19945:                                        if(p->recv_buffer != NULL) {
                   19946:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   19947:                                        }
1.1.1.25  root     19948:                                        LeaveCriticalSection(&q->csRecvData);
                   19949:                                        
                   19950:                                        if(dwRecv != 0) {
                   19951:                                                DWORD dwRead = 0;
                   19952:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   19953:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     19954:                                                        if(p->recv_buffer != NULL) {
                   19955:                                                                for(int i = 0; i < dwRead; i++) {
                   19956:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   19957:                                                                }
1.1.1.25  root     19958:                                                        }
                   19959:                                                        LeaveCriticalSection(&q->csRecvData);
                   19960:                                                }
                   19961:                                        }
                   19962:                                }
                   19963:                        }
                   19964:                        Sleep(10);
                   19965:                }
                   19966:                CloseHandle(hComm);
                   19967:        }
                   19968:        return 0;
                   19969: }
                   19970: 
1.1.1.8   root     19971: // cmos
                   19972: 
                   19973: void cmos_init()
                   19974: {
                   19975:        memset(cmos, 0, sizeof(cmos));
                   19976:        cmos_addr = 0;
1.1       root     19977:        
1.1.1.8   root     19978:        // from DOSBox
                   19979:        cmos_write(0x0a, 0x26);
                   19980:        cmos_write(0x0b, 0x02);
                   19981:        cmos_write(0x0d, 0x80);
1.1       root     19982: }
                   19983: 
1.1.1.8   root     19984: void cmos_write(int addr, UINT8 val)
1.1       root     19985: {
1.1.1.8   root     19986:        cmos[addr & 0x7f] = val;
                   19987: }
                   19988: 
                   19989: #define CMOS_GET_TIME() { \
                   19990:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   19991:        if(prev_sec != cur_sec) { \
                   19992:                GetLocalTime(&time); \
                   19993:                prev_sec = cur_sec; \
                   19994:        } \
1.1       root     19995: }
1.1.1.8   root     19996: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     19997: 
1.1.1.8   root     19998: UINT8 cmos_read(int addr)
1.1       root     19999: {
1.1.1.8   root     20000:        static SYSTEMTIME time;
                   20001:        static UINT32 prev_sec = 0;
1.1       root     20002:        
1.1.1.8   root     20003:        switch(addr & 0x7f) {
                   20004:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   20005:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   20006:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   20007:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   20008:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   20009:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   20010:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   20011: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   20012:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   20013:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   20014:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   20015:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20016:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20017:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20018:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20019:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     20020:        }
1.1.1.8   root     20021:        return(cmos[addr & 0x7f]);
1.1       root     20022: }
                   20023: 
1.1.1.7   root     20024: // kbd (a20)
                   20025: 
                   20026: void kbd_init()
                   20027: {
1.1.1.8   root     20028:        kbd_data = kbd_command = 0;
1.1.1.7   root     20029:        kbd_status = 0x18;
                   20030: }
                   20031: 
                   20032: UINT8 kbd_read_data()
                   20033: {
1.1.1.57! root     20034:        UINT8 data = kbd_data;
        !          20035:        kbd_data = 0;
1.1.1.8   root     20036:        kbd_status &= ~1;
1.1.1.57! root     20037:        return(data);
1.1.1.7   root     20038: }
                   20039: 
                   20040: void kbd_write_data(UINT8 val)
                   20041: {
                   20042:        switch(kbd_command) {
                   20043:        case 0xd1:
                   20044:                i386_set_a20_line((val >> 1) & 1);
                   20045:                break;
                   20046:        }
                   20047:        kbd_command = 0;
1.1.1.8   root     20048:        kbd_status &= ~8;
1.1.1.7   root     20049: }
                   20050: 
                   20051: UINT8 kbd_read_status()
                   20052: {
                   20053:        return(kbd_status);
                   20054: }
                   20055: 
                   20056: void kbd_write_command(UINT8 val)
                   20057: {
                   20058:        switch(val) {
                   20059:        case 0xd0:
                   20060:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     20061:                kbd_status |= 1;
1.1.1.7   root     20062:                break;
                   20063:        case 0xdd:
                   20064:                i386_set_a20_line(0);
                   20065:                break;
                   20066:        case 0xdf:
                   20067:                i386_set_a20_line(1);
                   20068:                break;
1.1.1.26  root     20069:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   20070:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     20071:                if(!(val & 1)) {
1.1.1.8   root     20072:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     20073:                                // reset pic
                   20074:                                pic_init();
                   20075:                                pic[0].irr = pic[1].irr = 0x00;
                   20076:                                pic[0].imr = pic[1].imr = 0xff;
                   20077:                        }
                   20078:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     20079:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   20080:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   20081:                        i386_jmp_far(selector, address);
1.1.1.7   root     20082:                }
                   20083:                i386_set_a20_line((val >> 1) & 1);
                   20084:                break;
                   20085:        }
                   20086:        kbd_command = val;
1.1.1.8   root     20087:        kbd_status |= 8;
1.1.1.7   root     20088: }
                   20089: 
1.1.1.9   root     20090: // vga
                   20091: 
                   20092: UINT8 vga_read_status()
                   20093: {
                   20094:        // 60hz
                   20095:        static const int period[3] = {16, 17, 17};
                   20096:        static int index = 0;
                   20097:        UINT32 time = timeGetTime() % period[index];
                   20098:        
                   20099:        index = (index + 1) % 3;
1.1.1.14  root     20100:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     20101: }
                   20102: 
1.1       root     20103: // i/o bus
                   20104: 
1.1.1.29  root     20105: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   20106: //#define SW1US_PATCH
                   20107: 
1.1.1.25  root     20108: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     20109: #ifdef USE_DEBUGGER
1.1.1.25  root     20110: {
1.1.1.33  root     20111:        if(now_debugging) {
                   20112:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20113:                        if(in_break_point.table[i].status == 1) {
                   20114:                                if(addr == in_break_point.table[i].addr) {
                   20115:                                        in_break_point.hit = i + 1;
                   20116:                                        now_suspended = true;
                   20117:                                        break;
                   20118:                                }
                   20119:                        }
                   20120:                }
1.1.1.25  root     20121:        }
1.1.1.33  root     20122:        return(debugger_read_io_byte(addr));
1.1.1.25  root     20123: }
1.1.1.33  root     20124: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     20125: #endif
1.1       root     20126: {
1.1.1.33  root     20127:        UINT8 val = 0xff;
                   20128:        
1.1       root     20129:        switch(addr) {
1.1.1.29  root     20130: #ifdef SW1US_PATCH
                   20131:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20132:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     20133:                val = sio_read(0, addr - 1);
                   20134:                break;
1.1.1.29  root     20135: #else
1.1.1.25  root     20136:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20137:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     20138:                val = dma_read(0, addr);
                   20139:                break;
1.1.1.29  root     20140: #endif
1.1.1.25  root     20141:        case 0x20: case 0x21:
1.1.1.33  root     20142:                val = pic_read(0, addr);
                   20143:                break;
1.1.1.25  root     20144:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     20145:                val = pit_read(addr & 0x03);
                   20146:                break;
1.1.1.7   root     20147:        case 0x60:
1.1.1.33  root     20148:                val = kbd_read_data();
                   20149:                break;
1.1.1.9   root     20150:        case 0x61:
1.1.1.33  root     20151:                val = system_port;
                   20152:                break;
1.1.1.7   root     20153:        case 0x64:
1.1.1.33  root     20154:                val = kbd_read_status();
                   20155:                break;
1.1       root     20156:        case 0x71:
1.1.1.33  root     20157:                val = cmos_read(cmos_addr);
                   20158:                break;
1.1.1.25  root     20159:        case 0x81:
1.1.1.33  root     20160:                val = dma_page_read(0, 2);
                   20161:                break;
1.1.1.25  root     20162:        case 0x82:
1.1.1.33  root     20163:                val = dma_page_read(0, 3);
                   20164:                break;
1.1.1.25  root     20165:        case 0x83:
1.1.1.33  root     20166:                val = dma_page_read(0, 1);
                   20167:                break;
1.1.1.25  root     20168:        case 0x87:
1.1.1.33  root     20169:                val = dma_page_read(0, 0);
                   20170:                break;
1.1.1.25  root     20171:        case 0x89:
1.1.1.33  root     20172:                val = dma_page_read(1, 2);
                   20173:                break;
1.1.1.25  root     20174:        case 0x8a:
1.1.1.33  root     20175:                val = dma_page_read(1, 3);
                   20176:                break;
1.1.1.25  root     20177:        case 0x8b:
1.1.1.33  root     20178:                val = dma_page_read(1, 1);
                   20179:                break;
1.1.1.25  root     20180:        case 0x8f:
1.1.1.33  root     20181:                val = dma_page_read(1, 0);
                   20182:                break;
1.1       root     20183:        case 0x92:
1.1.1.33  root     20184:                val = (m_a20_mask >> 19) & 2;
                   20185:                break;
1.1.1.25  root     20186:        case 0xa0: case 0xa1:
1.1.1.33  root     20187:                val = pic_read(1, addr);
                   20188:                break;
1.1.1.25  root     20189:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20190:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     20191:                val = dma_read(1, (addr - 0xc0) >> 1);
                   20192:                break;
1.1.1.37  root     20193:        case 0x278: case 0x279: case 0x27a:
                   20194:                val = pio_read(1, addr);
                   20195:                break;
1.1.1.29  root     20196:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     20197:                val = sio_read(3, addr);
                   20198:                break;
1.1.1.25  root     20199:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     20200:                val = sio_read(1, addr);
                   20201:                break;
1.1.1.25  root     20202:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     20203:                val = pio_read(0, addr);
                   20204:                break;
1.1.1.25  root     20205:        case 0x3ba: case 0x3da:
1.1.1.33  root     20206:                val = vga_read_status();
                   20207:                break;
1.1.1.37  root     20208:        case 0x3bc: case 0x3bd: case 0x3be:
                   20209:                val = pio_read(2, addr);
                   20210:                break;
1.1.1.29  root     20211:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     20212:                val = sio_read(2, addr);
                   20213:                break;
1.1.1.25  root     20214:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     20215:                val = sio_read(0, addr);
                   20216:                break;
1.1       root     20217:        default:
1.1.1.33  root     20218: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     20219:                break;
                   20220:        }
1.1.1.33  root     20221: #ifdef ENABLE_DEBUG_IOPORT
                   20222:        if(fp_debug_log != NULL) {
                   20223:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   20224:        }
                   20225: #endif
                   20226:        return(val);
1.1       root     20227: }
                   20228: 
                   20229: UINT16 read_io_word(offs_t addr)
                   20230: {
                   20231:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   20232: }
                   20233: 
1.1.1.33  root     20234: #ifdef USE_DEBUGGER
                   20235: UINT16 debugger_read_io_word(offs_t addr)
                   20236: {
                   20237:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   20238: }
                   20239: #endif
                   20240: 
1.1       root     20241: UINT32 read_io_dword(offs_t addr)
                   20242: {
                   20243:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   20244: }
                   20245: 
1.1.1.33  root     20246: #ifdef USE_DEBUGGER
                   20247: UINT32 debugger_read_io_dword(offs_t addr)
                   20248: {
                   20249:        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));
                   20250: }
                   20251: #endif
                   20252: 
1.1       root     20253: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     20254: #ifdef USE_DEBUGGER
                   20255: {
                   20256:        if(now_debugging) {
                   20257:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20258:                        if(out_break_point.table[i].status == 1) {
                   20259:                                if(addr == out_break_point.table[i].addr) {
                   20260:                                        out_break_point.hit = i + 1;
                   20261:                                        now_suspended = true;
                   20262:                                        break;
                   20263:                                }
                   20264:                        }
                   20265:                }
                   20266:        }
                   20267:        debugger_write_io_byte(addr, val);
                   20268: }
                   20269: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   20270: #endif
1.1       root     20271: {
1.1.1.25  root     20272: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     20273:        if(fp_debug_log != NULL) {
1.1.1.43  root     20274: #ifdef USE_SERVICE_THREAD
                   20275:                if(addr != 0xf7)
                   20276: #endif
1.1.1.33  root     20277:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     20278:        }
                   20279: #endif
1.1       root     20280:        switch(addr) {
1.1.1.29  root     20281: #ifdef SW1US_PATCH
                   20282:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20283:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   20284:                sio_write(0, addr - 1, val);
                   20285:                break;
                   20286: #else
1.1.1.25  root     20287:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20288:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   20289:                dma_write(0, addr, val);
                   20290:                break;
1.1.1.29  root     20291: #endif
1.1.1.25  root     20292:        case 0x20: case 0x21:
1.1       root     20293:                pic_write(0, addr, val);
                   20294:                break;
1.1.1.25  root     20295:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     20296:                pit_write(addr & 0x03, val);
                   20297:                break;
1.1.1.7   root     20298:        case 0x60:
                   20299:                kbd_write_data(val);
                   20300:                break;
1.1.1.9   root     20301:        case 0x61:
                   20302:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   20303:                        // beep on
                   20304: //                     MessageBeep(-1);
                   20305:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   20306:                        // beep off
                   20307:                }
                   20308:                system_port = val;
                   20309:                break;
1.1       root     20310:        case 0x64:
1.1.1.7   root     20311:                kbd_write_command(val);
1.1       root     20312:                break;
                   20313:        case 0x70:
                   20314:                cmos_addr = val;
                   20315:                break;
                   20316:        case 0x71:
1.1.1.8   root     20317:                cmos_write(cmos_addr, val);
1.1       root     20318:                break;
1.1.1.25  root     20319:        case 0x81:
                   20320:                dma_page_write(0, 2, val);
                   20321:        case 0x82:
                   20322:                dma_page_write(0, 3, val);
                   20323:        case 0x83:
                   20324:                dma_page_write(0, 1, val);
                   20325:        case 0x87:
                   20326:                dma_page_write(0, 0, val);
                   20327:        case 0x89:
                   20328:                dma_page_write(1, 2, val);
                   20329:        case 0x8a:
                   20330:                dma_page_write(1, 3, val);
                   20331:        case 0x8b:
                   20332:                dma_page_write(1, 1, val);
                   20333:        case 0x8f:
                   20334:                dma_page_write(1, 0, val);
1.1       root     20335:        case 0x92:
1.1.1.7   root     20336:                i386_set_a20_line((val >> 1) & 1);
1.1       root     20337:                break;
1.1.1.25  root     20338:        case 0xa0: case 0xa1:
1.1       root     20339:                pic_write(1, addr, val);
                   20340:                break;
1.1.1.25  root     20341:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20342:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     20343:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     20344:                break;
1.1.1.35  root     20345: #ifdef USE_SERVICE_THREAD
                   20346:        case 0xf7:
                   20347:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     20348:                if(in_service && cursor_moved) {
                   20349:                        // update cursor position before service is done
                   20350:                        pcbios_update_cursor_position();
                   20351:                        cursor_moved = false;
                   20352:                }
1.1.1.35  root     20353:                finish_service_loop();
                   20354:                break;
                   20355: #endif
1.1.1.37  root     20356:        case 0x278: case 0x279: case 0x27a:
                   20357:                pio_write(1, addr, val);
                   20358:                break;
1.1.1.29  root     20359:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   20360:                sio_write(3, addr, val);
                   20361:                break;
1.1.1.25  root     20362:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   20363:                sio_write(1, addr, val);
                   20364:                break;
                   20365:        case 0x378: case 0x379: case 0x37a:
                   20366:                pio_write(0, addr, val);
                   20367:                break;
1.1.1.37  root     20368:        case 0x3bc: case 0x3bd: case 0x3be:
                   20369:                pio_write(2, addr, val);
                   20370:                break;
1.1.1.29  root     20371:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   20372:                sio_write(2, addr, val);
                   20373:                break;
1.1.1.25  root     20374:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   20375:                sio_write(0, addr, val);
                   20376:                break;
1.1       root     20377:        default:
1.1.1.33  root     20378: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     20379:                break;
                   20380:        }
                   20381: }
                   20382: 
                   20383: void write_io_word(offs_t addr, UINT16 val)
                   20384: {
                   20385:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20386:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20387: }
                   20388: 
1.1.1.33  root     20389: #ifdef USE_DEBUGGER
                   20390: void debugger_write_io_word(offs_t addr, UINT16 val)
                   20391: {
                   20392:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20393:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20394: }
                   20395: #endif
                   20396: 
1.1       root     20397: void write_io_dword(offs_t addr, UINT32 val)
                   20398: {
                   20399:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20400:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20401:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20402:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20403: }
1.1.1.33  root     20404: 
                   20405: #ifdef USE_DEBUGGER
                   20406: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   20407: {
                   20408:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20409:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20410:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20411:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20412: }
                   20413: #endif

unix.superglobalmegacorp.com

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